1

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to edit the longitude of the hs DataArray as well as the hs file had longitude: -100 to 30, where as ws10 had longitude: 260 to 30. hs.coords['lon'] = lon # where lon is from ws10 Is there a built in function in xarray to convert longitude from -180:180 to 0:360 and vice versa? The code is fairly straight forward lon180 = ((lon360 + 180) % 360) - 180 lon360 = lon180 % 360
0

If two of your datasets are the same size, try

new_data = xr.where(hs > 0, ws10, hs)

That means, for the hs grids that are larger than 0, use ws10 grids to replace them, otherwise, keep the original hs grids.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.