Operators | Long name | Functions |
---|---|---|
ncap/ncap2 | NetCDF Arithmetic Processor | Algebraic manipulation of data |
ncatted | NetCDF Attribute Editor | Modify metadata |
ncbo | NetCDF Binary Operator (ncadd, ncmultiply) | Math involving two files |
ncea | NetCDF Ensemble Averager | Avergage across multiple input files |
ncecat | NetCDF Ensemble Concatenator | Combine files into a single record |
ncflint | NetCDF File Interpolator | Combine inputs via weighted interpolation |
ncks | NetCDF Kitchen Sink | Copies data to ascii output file & many more |
ncpdq | NetCDF Permute Dimensions Quickly, Pack Data Quietly | Rearrange dimensions or pack data |
ncra | NetCDF Record Averager | Average across time (record dimension) |
ncrcat | NetCDF Record Concatenator | Combine sequential files |
ncrename | NetCDF Renamer | Rename dimensions, variables, or attributes |
ncwa | NetCDF Weighted Average | Weighted average over one file |
Extract variable from a file
ncks -v [var_name] in.nc out.nc
Delete variable from a file
ncks -C -O -x -v [var_name] in.nc out.nc
Delete dimension from a file
ncwa -a [dim_name] in.nc out.nc
Repack a file after averaging throughout a dimension
ncpdq in.nc out.nc
Masking data file (data.nc, var_name: tas) using another mask file (mask.nc, mask_var_name: mask) having same grid
ncks -A -v mask mask.nc data.nc
ncap2 -s 'where (mask == 0) tas=tas.get_miss()' data.nc masked_data.nc
Note: CDO could be a better solution.
Change variables name
ncrename -v longitude,lon file.nc
ncrename -d longitude,lon file.nc
Or
ncrename -v longitude,lon -d longitude,lon file.nc
Manage attributes
ncatted -a [att_name],[var_name],[mode],[att_type],[att_value] file.nc
Or
ncatted -a [att_name],[var_name],[mode],[att_type],[att_value] in.nc out.nc
[mode] includes:
* a = append
* c = create
* d = delete
* m = modify
* o = overwrite
[att_type] includes:
* f = float
* d = double
* l = long
* s = short
* c = character
* b = byte
* i = integer
Examples:
ncatted -O -a axis,lon,c,c,"X" file.nc
ncatted -O -h -a history,global,o,c,"Overwriten history" file.nc
ncatted -a coordinates,urb_2d,c,c,"xlon xlat" \
-a coordinates,xlon,c,c,"xlon xlat" \
-a coordinates,xlat,c,c,"xlon xlat" \
file.nc
Add data for a dimension
ncap2 -O -s 'numurbl=array(1,1,$numurbl)' in.nc out.nc
Re-order dimensions
ncpdq -a lon,lat,time in.nc out.nc
Extract PFT data from CLM4.5 used as input for RegCM4
#!/bin/bash
IFILE=download/mksrf_urban_0.05x0.05_simyr2000.c170724.nc
OFILE=conv-urbdist.nc
ncks -C -v LONGXY,LATIXY,PCT_URBAN $IFILE _tmp.1.nc
ncap2 -O -s 'lon=LONGXY(1,:)' _tmp.1.nc _tmp.2.nc
ncap2 -O -s 'lat=LATIXY(:,1)' _tmp.2.nc _tmp.3.nc
ncatted -O -a units,lon,o,c,degrees_east _tmp.3.nc
ncatted -O -a units,lat,o,c,degrees_north _tmp.3.nc
ncatted -O -a axis,lon,o,c,X _tmp.3.nc
ncatted -O -a axis,lat,o,c,Y _tmp.3.nc
ncrename -d density_class,lev _tmp.3.nc
ncap2 -O -s 'lev=array(1,1,$lev)' _tmp.3.nc $OFILE
ncatted -O -a axis,lev,o,c,Z $OFILE
rm -f _tmp*
Create a new windspeed variable from component wind variables, u and v:
ncap -O -s "windspeed=sqrt(u^2+v^2)" in.nc out.nc
Compute monthly temperature anomalies from 1985 mean:
ncdiff -v T 85_0112.nc 85.nc t_anm_85_0112.nc
Average five ensemble members (see documentation to average N ensemble members):
ncea 85_0[1-5].nc 85.nc
Concatenate five ensemble members into single file (see documentation to conatenate N ensemble members):
ncecat 85_0[1-5].nc 85.nc
Interpolate fields known at times 85 and 87 to time=86:
ncflint -i time,86 85.nc 87.nc 86.nc
Print value of variable near specified coordinates. For example to print the value of the variable “tos” nearest longitude 203 degrees E and latitude 19.5 degree N from the file sst.nc:
ncks -H -v tos -d lon,203.0 -d lat,19.5 sst.nc
Extract variables time and pressure from file in.nc and write to out.nc, including any needed dimensions, coordinate variables, and variable attributes:
ncks -v time,pressure in.nc out.nc
Pack all variables in file in.nc and store the results in out.nc, using scale_factor and add_offset attributes:
ncpack in.nc out.nc
Average timeseries across five files:
ncra 85_0[1-5].nc 85.nc
Concatenate timeseries across five files:
ncrcat 85_0[1-5].nc 85.nc
Globally average file, weighting variables by area:
ncwa -w area -a lat,lon in.nc out.nc