Skip to content

Instantly share code, notes, and snippets.

@lesserwhirls
Created February 10, 2015 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesserwhirls/df3d4d4cd51ae0f6a208 to your computer and use it in GitHub Desktop.
Save lesserwhirls/df3d4d4cd51ae0f6a208 to your computer and use it in GitHub Desktop.
Read a THREDDS catalog and print dates from the first dataset found
import java.io.IOException;
import java.util.List;
import thredds.catalog.CrawlableCatalog;
import thredds.crawlabledataset.CrawlableDataset;
import ucar.nc2.dataset.CoordinateAxis1DTime;
import ucar.nc2.dataset.CoordinateSystem;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.dt.grid.GridCoordSys;
import ucar.nc2.time.CalendarDate;
public class DatesFromCatalog {
public static void main(String[] args) {
String location = "http://sflthredds.er.usgs.gov/thredds/catalog/TSdata/surfaces/catalog.xml";
try {
// read the xml catalog
CrawlableCatalog cat = new CrawlableCatalog(location, null);
List<CrawlableDataset> datasets = cat.listDatasets();
// get the first dataset (as an example)
CrawlableDataset ds = datasets.get(0);
// try to open the dataset
try (NetcdfDataset ncd = NetcdfDataset.openDataset("thredds:"+ds.getPath())) {
// print the title of the dataset
System.out.println(ncd.getTitle());
// get all coordinate systems in the dataset
List<CoordinateSystem> coordSystems = ncd.getCoordinateSystems();
// go through each coordinate system and look for a time axis
for (CoordinateSystem cs : coordSystems) {
GridCoordSys gcs = new GridCoordSys(cs, null);
if (gcs.hasTimeAxis1D()) {
CoordinateAxis1DTime time = gcs.getTimeAxis1D();
List<CalendarDate> calDates = time.getCalendarDates();
// loop through the CalendarDate object and print each one
for (CalendarDate calDate : calDates) {
System.out.println(calDate.toString());
}
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment