Search code examples
pythonpython-xarraynetcdf4

Multiply xarray datasets with different dimensions


I have two NetCDF files, one covers the continental US (dataset2) and the other only the northeast (dataset1). I'm trying to multiply the two values together in order to create one dataset, however I get a ValueError after doing the multiplication.

import xarray

dataset1=xarray.open_dataset('../data/precip.nc')
print(dataset1)

Output:

<xarray.Dataset>
Dimensions:  (time: 24, x: 180, y: 235)
Coordinates:
  * time     (time) datetime64[ns] 2019-02-14 ... 2019-02-14T23:00:00
  * y        (y) float64 -4.791e+06 -4.786e+06 ... -3.681e+06 -3.677e+06
  * x        (x) float64 2.234e+06 2.238e+06 2.243e+06 ... 3.081e+06 3.086e+06
    lat      (y, x) float64 ...
    lon      (y, x) float64 ...
Data variables:
    z        (y, x) float64 ...
    crs      int32 ...
    PRECIP   (time, y, x) float32 ...
dataset2=xarray.open_dataset('../data/ratio.nc')
print(dataset2)

Output:

<xarray.Dataset>
Dimensions:     (lat: 272, lon: 480, nv: 2)
Coordinates:
  * lat         (lat) float64 21.06 21.19 21.31 21.44 ... 54.69 54.81 54.94
  * lon         (lon) float64 -125.9 -125.8 -125.7 ... -66.31 -66.19 -66.06
Dimensions without coordinates: nv
Data variables:
    lat_bounds  (lat, nv) float64 ...
    lon_bounds  (lon, nv) float64 ...
    crs         int16 ...
    Data        (lat, lon) float32 ...
# Merge datasets
data=xarray.merge([dataset1, dataset2], compat='override')
print(data)

Output:

<xarray.Dataset>
Dimensions:     (lat: 272, lon: 480, nv: 2, time: 24, x: 180, y: 235)
Coordinates:
  * time        (time) datetime64[ns] 2019-02-14 ... 2019-02-14T23:00:00
  * y           (y) float64 -4.791e+06 -4.786e+06 ... -3.681e+06 -3.677e+06
  * x           (x) float64 2.234e+06 2.238e+06 ... 3.081e+06 3.086e+06
  * lat         (lat) float64 21.06 21.19 21.31 21.44 ... 54.69 54.81 54.94
  * lon         (lon) float64 -125.9 -125.8 -125.7 ... -66.31 -66.19 -66.06
Dimensions without coordinates: nv
Data variables:
    z           (y, x) float64 ...
    crs         int32 ...
    PRECIP      (time, y, x) float32 ...
    lat_bounds  (lat, nv) float64 ...
    lon_bounds  (lon, nv) float64 ...
    Data        (lat, lon) float32 ...
# Get first hour of precip data
precip=data.PRECIP[0:, :, :]

# Get ratio data
slr=data.Data

# Multiply to get snowfall
snow=slr*precip

That last line then give me this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-b9ed8e05f451> in <module>
----> 1 snow=slr*precip

~/.local/lib/python3.7/site-packages/xarray/core/dataarray.py in func(self, other)
   2597             variable = (
   2598                 f(self.variable, other_variable)
-> 2599                 if not reflexive
   2600                 else f(other_variable, self.variable)
   2601             )

~/.local/lib/python3.7/site-packages/xarray/core/variable.py in func(self, other)
   2034                 new_data = (
   2035                     f(self_data, other_data)
-> 2036                     if not reflexive
   2037                     else f(other_data, self_data)
   2038                 )

ValueError: iterator is too large

Solution

  • Solved following https://gis.stackexchange.com/questions/339463/using-xarray-to-resample-and-merge-two-datasets

    slr_interpolate = slr.interp(lat=precip["lat"], lon=precip["lon"])
    
    mpe_snowfall=slr_interpolate.Data*precip.Data