问题
I got this to work for a simple case:
arr2 = xr.DataArray((np.arange(16)-8).reshape(4, 4), dims=['x', 'y'])
arr3 = xr.DataArray(np.arange(16).reshape(4, 4), dims=['x', 'y'])
<xarray.DataArray (x: 4, y: 4)>
array([[ nan, nan, nan, nan],
[ nan, nan, nan, nan],
[ nan, 9., 10., 11.],
[ 12., 13., 14., 15.]])
Dimensions without coordinates: x, y
However, i'm having troubling applying to NetCDF files. I have two datasets: significant wave height (Hs) and wind speed (ws). I would like to use the mask of where Hs<0 and apply it to ws. The size of the datasets are [time=1,lat=81,lon=131]. There will be a time in the futures where my ws DataArray will be a slightly different size e.g. [time=1,ens=10,lat=81,lon=131].
If I try:
f = xr.open_dataset('CCSM4_ens1_19821201_19831130_ws10_0_NAtl_DJFmean.nc')
ws10 = f.ws10
f = xr.open_dataset('ww3.Hs.19820901_19830831_NAtl_DJFmean.nc')
hs = f.hs
ws10_masked = ws10.where(hs > 0)
ws10_masked looks like:
xarray.DataArray (time: 1, lat: 81, lon: 131, latitude: 81, longitude: 131)
array([[[[[ nan, ..., nan],
...,
[ nan, ..., nan]],
...,
[[ nan, ..., nan],
...,
[ nan, ..., nan]]],
...,
[[[ nan, ..., nan],
...,
[ nan, ..., nan]],
...,
[[ nan, ..., nan],
...,
[ nan, ..., nan]]]]])
Coordinates:
* lat (lat) float64 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 ...
* lon (lon) float64 260.0 261.0 262.0 263.0 264.0 265.0 266.0 267.0 ...
* time (time) datetime64[ns] 1983-01-15T12:00:00
* latitude (latitude) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude (longitude) float32 -100.0 -99.0 -98.0 -97.0 -96.0 -95.0
...
Attributes:
associated_files: baseURL: http://cmip-
pcmdi.llnl.gov/CMIP5/dataLocation...
cell_methods: time: mean
history: 2014-07-03T07:58:56Z altered by CMOR: Treated
scalar d...
long_name: Eastward Near-Surface Wind
standard_name: eastward_wind
units: m s-1
You can see because ws has dimension names lon and lat where as Hs has dimension names longitude and latitude it is creating a 5 dimension DataArray and not picking the mask up correctly.
Any way I can pick the mask regardless if the dimensions names are different or if the DataArrays are different sizes?
I previously did this with numpy.math (ma) as:
hs = f.variables['hs'][:]
hs_masked = ma.masked_values(hs, -65.534004)
tmp = np.zeros((len(lat), len(lon))
# Create masked array
data_cs = ma.masked_values(tmp, 0)
# Read new file
tmp = f.variables['cusp'][:]
data_cs[:,:] = ma.masked_array(tmp, hs_masked.mask)
But hoping to learn/use xarray.
Cheers, Ray
回答1:
You will need to explicitly rename dimension names to match, e.g., hs = hs.rename({'lat': 'latitude', 'longitude': 'longitude'})
. You might also need to reindex with nearest-neighbor indexing, if the coordinate labels don't match exactly, e.g., hs.reindex_like(ws10, method='nearest', tolerance=0.01)
.
Or, less safely, you could strip out the coordinate labels from the second argument, and just pass in a unlabeled array instead, e.g., ws10.where(hs.data > 0)
. But I don't recommend this option, because nothing guarantees the consistency of the metadata.
来源:https://stackoverflow.com/questions/43480389/python-xarray-copy-mask-from-one-dataarray-to-another