Last active
September 15, 2020 09:13
-
-
Save selimnairb/652a243fa3086a4ad844 to your computer and use it in GitHub Desktop.
Barebones tool to subset a netCDF grid dataset based on geographic (WGS84) bounding box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Barebones tool to subset a netCDF grid dataset based on geographic bounding box. | |
Requires NetCDF-Java version 4.3 (may work with 4.5, haven't tested it): | |
@see http://www.unidata.ucar.edu/software/thredds/current/netcdf-java/documentation.htm | |
@author [email protected] | |
To use: | |
export CLASSPATH=${CLASSPATH}:./lib/netcdfAll-4.3.jar | |
javac SpatialSubset.java | |
java SpatialSubset mynetCDF.nc -78.931940 36.013984 -78.921160 36.023954 mynetCDF_spsubset.nc | |
*/ | |
import ucar.nc2.dt.grid.GridDataset; | |
import ucar.nc2.dt.GridDatatype; | |
import ucar.nc2.dt.grid.NetcdfCFWriter; | |
import ucar.ma2.InvalidRangeException; | |
import ucar.unidata.geoloc.LatLonRect; | |
import ucar.unidata.geoloc.LatLonPoint; | |
import ucar.unidata.geoloc.LatLonPointImpl; | |
public class SpatialSubset { | |
public static void main(String[] args) throws java.io.IOException, InvalidRangeException { | |
GridDataset gridDs = GridDataset.open(args[0]); | |
// Grid list | |
java.util.List<GridDatatype> grids = gridDs.getGrids(); | |
java.util.List<java.lang.String> gridList = new java.util.ArrayList<java.lang.String>(); | |
for (GridDatatype grid : grids) { | |
gridList.add(grid.getName()); | |
} | |
// Bounding box | |
double xmin = Double.parseDouble(args[1]); | |
double ymin = Double.parseDouble(args[2]); | |
double xmax = Double.parseDouble(args[3]); | |
double ymax = Double.parseDouble(args[4]); | |
LatLonPoint rectLeft = new LatLonPointImpl(ymin, xmin); | |
LatLonPoint rectRight = new LatLonPointImpl(ymax, xmax); | |
LatLonRect rect = new LatLonRect(rectLeft, rectRight); | |
// Output file name | |
String outfile = args[5]; | |
NetcdfCFWriter.makeFile(outfile, gridDs, gridList, rect, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment