1

I have a dataset that looks like this:

enter image description here

As you can see, it only covers Latitudes between -55.75 and 83.25. I would like to expand that dataset so that it covers the whole globe (-89.75 to 89.75 in my case) and fill it with an arbitrary NA value.

Ideally I would want to do this with xarray. I have looked at .pad(), .expand_dims() and .assign_coords(), but did not really get a handle on the working ofeither of those.

If someone can provide an alternative solution with cdo, I would also be grateful for that.

LuleSa
  • 75
  • 7
  • what about [reindex](http://xarray.pydata.org/en/stable/generated/xarray.DataArray.reindex.html)? you can use this to define new latitude coordinates, and provide a `fill_value`. also, that's a map, not a dataset :) if you want more help, please post the code you have tried and what's not working, ideally as a [minimal reproducible example](/help/minimal-reproducible-example) or at the very least, `print(ds)`. – Michael Delgado Nov 11 '21 at 23:32

1 Answers1

1

You could do this with nctoolkit (https://nctoolkit.readthedocs.io/en/latest/), which uses CDO as a backend.

The example below shows how you could do it. Example starts by cropping a global temperature dataset to latitudes between -50 and 50. You would then need to regrid it to a global dataset, at whatever resolution you need. This uses CDO, which will extrapolate at the edges. So you probably want to set everything to NA outside the original dataset's values, so my code calls masklonlatbox from CDO.

import nctoolkit as nc
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.ltm.1981-2010.nc")
ds.subset(time = 0)
ds.crop(lat = [-50, 50])
ds.to_latlon(lon = [-179.5, 179.5], lat = [-89.5, 89.5], res = 1)
ds.mask_box(lon = [-179.5, 179.5], lat = [-50, 50])
ds.plot()
# convert to xarray dataset
ds_xr = ds.to_xarray()
Robert Wilson
  • 3,192
  • 11
  • 19