Using Xray like NCO

Select a specific OPeNDAP variable to subset and stride, then write the result to a NetCDF file


In [1]:
import xray

In [2]:
xray.__version__


Out[2]:
'0.6.0'

specify the opendap data url endpoint


In [3]:
url ='http://ecowatch.ncddc.noaa.gov/thredds/dodsC/hycom_sfc/20151007/hycom_glb_sfc_2015100700_t000.nc'

create the xray dataset from an opendap endpoint, but drop the 'tau' variable because it's units are invalid:


In [4]:
ds = xray.open_dataset(url,drop_variables=['tau'])

In [5]:
ds['water_temp']


Out[5]:
<xray.DataArray 'water_temp' (time: 1, depth: 1, lat: 2001, lon: 4499)>
[9002499 values with dtype=float64]
Coordinates:
  * lat      (lat) float64 -80.0 -79.92 -79.84 -79.76 -79.68 -79.6 -79.52 ...
  * depth    (depth) float64 0.0
  * lon      (lon) float64 0.0 0.08 0.16 0.24 0.32 0.4 0.48 0.56 0.64 0.72 ...
  * time     (time) datetime64[ns] 2015-10-07
Attributes:
    long_name: Water Temperature
    standard_name: sea_water_temperature
    units: degC
    NAVO_code: 15

subsample the xray dataarray object and promote to a dataset


In [6]:
ds_wt = ds['water_temp'][0,0,::12,::12].to_dataset()

save subsetted data to netcdf, includes coordinate values as well


In [7]:
ds_wt.to_netcdf('water_temp.nc')