Skip to content

Instantly share code, notes, and snippets.

@lesserwhirls
Created January 5, 2017 22:29
Show Gist options
  • Save lesserwhirls/5eea2ffa0f1e66b50fb87b971fa111b5 to your computer and use it in GitHub Desktop.
Save lesserwhirls/5eea2ffa0f1e66b50fb87b971fa111b5 to your computer and use it in GitHub Desktop.
netCDF-Java code to aggregate grib2 files into a "best" collection
import org.jdom2.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import thredds.featurecollection.FeatureCollectionConfig;
import thredds.featurecollection.FeatureCollectionType;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.grib.collection.GribCdmIndex;
import ucar.nc2.grib.collection.GribCollectionImmutable;
/**
* Created by sarms on 1/5/17.
*/
public class AggGrib2 {
final static Logger exampleLogger = LoggerFactory.getLogger(AggGrib2.class);
public static void main(String[] args) {
// the directory where the grib2 files that you wish to aggregate live
String dataDir = "/Users/sarms/Desktop/gribAgg/grib2/";
// the is the name of the collection - it can be whatever you like, just don't use spaces
String name = "GribAgg";
String collectionName = "GribAgg_Collection";
// basically a regex used to locate your grib files
String spec = dataDir + ".*grib2$";
//
FeatureCollectionType fcType = FeatureCollectionType.GRIB2;
// don't worry about these for now
String timePartition = "file";
String dateFormatMark = null;
String olderThan = null;
Element innerNcml = null;
String path = "";
// aggregation magic
FeatureCollectionConfig fcc = new FeatureCollectionConfig(name, path, fcType, spec,
collectionName, dateFormatMark, olderThan, timePartition, innerNcml);
try (GribCollectionImmutable gc = GribCdmIndex.openGribCollection(fcc, null, exampleLogger)) {
GribCollectionImmutable.Dataset ds = gc.getDatasetByTypeName("Best");
String fullCollectionIndexFilePath = dataDir + name + ".ncx3";
// now we open the collection index file, which catalogs all of the grib
// records in your collection
NetcdfDataset ncd = gc.getNetcdfDataset(ds, ds.getGroup(0), fullCollectionIndexFilePath,
fcc, null, exampleLogger);
System.out.println(ncd.getVariables());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment