WxJack
WxJack

Reputation: 31

Slice NetCdf Dataset

I am looking to take a subset of the netcdf data set bounded by lat/lon coordinates.

<xarray.Dataset>
Dimensions:              (ICcheckNameLen: 72, ICcheckNum: 55, QCcheckNameLen: 60, QCcheckNum: 10, maxAutoStaLen: 6, maxLocationLen: 24, maxMETARLen: 256, maxRepLen: 6, maxSkyCover: 6, maxSkyLen: 8, maxStaNamLen: 5, maxStaticIds: 10000, maxWeatherLen: 25, nInventoryBins: 24, recNum: 8329, totalIdLen: 6)
Dimensions without coordinates: ICcheckNameLen, ICcheckNum, QCcheckNameLen, QCcheckNum, maxAutoStaLen, maxLocationLen, maxMETARLen, maxRepLen, maxSkyCover, maxSkyLen, maxStaNamLen, maxStaticIds, maxWeatherLen, nInventoryBins, recNum, totalIdLen
Data variables:
    nStaticIds           int32 ...
    staticIds            (maxStaticIds, totalIdLen) |S1 ...
    lastRecord           (maxStaticIds) int32 ...
    invTime              (recNum) int32 ...
    prevRecord           (recNum) int32 ...
    inventory            (maxStaticIds) int32 ...
    globalInventory      int32 ...
    firstOverflow        int32 ...
    isOverflow           (recNum) int32 ...
    firstInBin           (nInventoryBins) int32 ...
    lastInBin            (nInventoryBins) int32 ...
    secondsStage1_2      (recNum) int32 ...
    secondsStage3        (recNum) int32 ...
    wmoId                (recNum) int32 ...
    stationName          (recNum, maxStaNamLen) |S1 ...
    locationName         (recNum, maxLocationLen) |S1 ...
    QCT                  (QCcheckNum, QCcheckNameLen) |S1 ...
    ICT                  (ICcheckNum, ICcheckNameLen) |S1 ...
    latitude             (recNum) float32 ...
    longitude            (recNum) float32 ...
    elevation            (recNum) float32 ...

I have tried multiple methods based on Help1 and Help2 to setup the boundaries which should be between latitude[20,53] and longitude[-131,-62]. The dataset can be accessed at NetCDF Data.

When I use the below, it says, "ValueError: dimensions or multi-index levels ['latitude', 'longitude'] do not exist"

import xarray as xr
ds = xr.open_dataset('/home/awips/python-awips/ups/20181110_1600.nc',
                     decode_cf=False)
print(ds)
lat_bnds, lon_bnds = [20, 53], [-131, -62]
ds.sel(latitude=slice(*lat_bnds), longitude=slice(*lon_bnds))
ds.to_netcdf(path='/home/awips/python-awips/ups/subset.nc')

When I try the below, it works through the data, but does not remove any data.

import xarray as xr
ds = xr.open_dataset('/home/awips/python-awips/ups/20181110_1600.nc', decode_cf=True)

ds.where((-131 < ds.longitude) & (ds.longitude < -62)
         & (20 < ds.latitude) & (ds.latitude < 53), drop=True)
ds.to_netcdf(path='/home/awips/python-awips/ups/subset.nc')

Any ideas?

Upvotes: 0

Views: 1232

Answers (1)

shoyer
shoyer

Reputation: 9603

Xarray operations usually return new objects instead of modifying objects inplace. So you need to assign the result of where to a new variable and save that instead, e.g.,

ds2 = ds.where((-131 < ds.longitude) & (ds.longitude < -62)
               & (20 < ds.latitude) & (ds.latitude < 53), drop=True)
ds2.to_netcdf(path='/home/awips/python-awips/ups/subset.nc')

Upvotes: 1

Related Questions