janchytry
janchytry

Reputation: 350

Clip NetCDF files by non-associated coordinates

Look at this NetCDF EURO-CORDEX file:

<xarray.Dataset>
Dimensions:       (bnds: 2, rlat: 412, rlon: 424, time: 1800)
Coordinates:
    lat           (rlat, rlon) float64 ...
    lon           (rlat, rlon) float64 ...
  * rlat          (rlat) float64 -23.38 -23.26 -23.16 ... 21.61 21.73 21.83
  * rlon          (rlon) float64 -28.38 -28.26 -28.16 ... 17.93 18.05 18.16
  * time          (time) object 2011-01-01 12:00:00 ... 2015-12-30 12:00:00
Dimensions without coordinates: bnds
Data variables:
    pr            (time, rlat, rlon) float32 ...
    rotated_pole  |S1 ...
    time_bnds     (time, bnds) object ...

When I attempt to clip it using xarray (in Python script) or nco (in the command line) by lat and lon coordinates, it is presumably not working as lat and lon are not dimensions. I read these are rather coordinates not associated with any variable. How can I associate them or make them dimensions? Do the asterisks by the coordinates rlat and rlon suggest something I am missing?

Note I achieve clipping by xarray.Dataset.where, nevertheless I am rather interested with manipulating the dimensions/coordinates such that usual clipping operations would work. Thanks!

Upvotes: 1

Views: 500

Answers (1)

Robert Wilson
Robert Wilson

Reputation: 3397

You should be able to clip this using CDO:

sellonlatbox,lonmin,lonmax,latmin,latmax infile outfile

CDO will figure out the smallest possible rectangle in the original dataset that will contain the specified box. This seems to be a rotated polar grid, so you will not get some grid cells outside the box.

NCO will also work, but you will need to crop using rlon and rlat. So, you will need to select an appropriate rlon and rlat that will result in the desired lon and lat.

Upvotes: 2

Related Questions