使用 masked_array 时如何使我的降水数据保持相同的单位?

How to keep the same units from my precipitation data when using masked_array?

我正在尝试使用一个 nc 文件创建降水图,类似于我发现的 NWS 示例 here

不过,就我而言,我的降水数据已经在 mm 中了。我如何保持相同的单位?我确实阅读了以下内容,

Create a numpy.ma.MaskedArray with units attached. This is a thin wrapper around numpy.ma.masked_array() that ensures that units are properly attached to the result (otherwise units are silently lost). Units are taken from the data_units argument, or if this is None, the units on data are used.

我按照给定的参数 (masked_array(data, data_units=None, **kwargs)) 对于我的文件,但是

  1. kwargs 未定义,
  2. 当我不包含 kwargs 时,我得到

"AttributeError: 'MaskedArray' object has no attribute 'units'".

我是新手,请轻喷。感谢您的指导!这是我的代码...

from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
import numpy as np
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cf
import matplotlib.colors as mcolors
from metpy.units import masked_array, units

nc_data = NetCDFFile(r'C:\Users\Jocelyn\Desktop\TRMM_daily_prcp_dataB42_Daily.19980601.7.nc4', 'r')

print (nc_data)
print(nc_data.variables.keys()) 

prcp = nc_data.variables['precipitation']
data = masked_array(prcp[:], prcp_units=None, **kwargs)
lat = nc_data.variables['lat']
lon = nc_data.variables['lon']

如果您不尝试对降水数据使用 MetPy 计算并且不需要进行任何单位转换,那么您应该能够完全消除对 masked_array 的使用。所以试试这个:

prcp = nc_data.variables['precipitation']
data = prcp[:]
lat = nc_data.variables['lat']
lon = nc_data.variables['lon']