Combining data at the Antimeridian

Example provided by Dale Robinson, Coast Watch West Coast Node

This example shows how to merge two NetCDF files that span the antimeridian into one NetCDF file. Here we will demonstrate downloading ice concentration from ERDDAP in two sections and then piece them together into one file. This is accomplished with the easy to use NetCDF Operator Tools which are command line programs for working with NetCDF files. Learn more about NCO and download the package at: http://nco.sourceforge.net/nco.html#Summary

Download the two data files in NetCDF format

Western Pacific File Download
Eastern Pacific File Download

Change record dimension from "time" to "longitude"

Temporarily change the record dimension from "time" to "longitude" in the two files. The resulting files are named with a "_L" ending to make it easier to follow the work flow.

ncpdq -O -a longitude,time Wpacific.nc Wpacific_L.nc
ncks -O --mk_rec_dmn longitude Wpacific_L.nc Wpacific_L.nc
ncpdq -O -a longitude,time Epacific.nc Epacific_L.nc
ncks -O --mk_rec_dmn longitude Epacific_L.nc Epacific_L.nc

Remove references to Longitude Extent

Remove the valid_min and valid_max from the longitude variable in both files

ncatted -O -a valid_min,longitude,d,f, Wpacific_L.nc
ncatted -O -a valid_max,longitude,d,f, Wpacific_L.nc
ncatted -O -a valid_min,longitude,d,f, Epacific_L.nc
ncatted -O -a valid_max,longitude,d,f, Epacific_L.nc

Shift Longitude Range

Use ncap2 to shift the eastern pacific (western hemisphere) longitudes into the range 180 - 360

ncap2 -s "longitude= longitude+360.0f" Epacific_L.nc Epacific360.nc

Combine the Files

Concatenate Western and Eastern Pacific files along new (longitude) record dimension, naming the resulting file "combined_pacific360.nc"

ncrcat Wpacific_L.nc Epacific360.nc combined_pacific360.nc

Update Dimensions

Change record dimension back to "time" in the combined_pacific360.nc file

ncpdq -O -a time,longitude combined_pacific360.nc combined_pacific360.nc
ncks -O --mk_rec_dmn time combined_pacific360.nc combined_pacific360.nc

Set Max and Min Ranges

Restore the valid min and max attributes of the longitude variable, making them 0 and 360, respectively (optional) and delete the old "actual_range" (optional)

ncatted -O -a valid_min,longitude,c,f,0.0 combined_pacific360.nc
ncatted -O -a valid_max,longitude,c,f,360.0 combined_pacific360.nc
ncatted -O -a actual_range,longitude,d,, combined_pacific360.nc

Clean up (optional)

rm Epacific_L.nc rm Epacific.nc Epacific360.nc Wpacific_L.nc Wpacific.nc