public static DateRange extractDateRange(GridDataset gridDataset) { DateRange maxDateRange = null; for (GridDataset.Gridset gridset : gridDataset.getGridsets()) { GridCoordSystem gsys = gridset.getGeoCoordSystem(); DateRange dateRange; CoordinateAxis1DTime time1D = gsys.getTimeAxis1D(); if (time1D != null) { dateRange = time1D.getDateRange(); } else { CoordinateAxis time = gsys.getTimeAxis(); if (time == null) continue; try { DateUnit du = new DateUnit(time.getUnitsString()); Date minDate = du.makeDate(time.getMinValue()); Date maxDate = du.makeDate(time.getMaxValue()); dateRange = new DateRange(minDate, maxDate); } catch (Exception e) { logger.warn("Illegal Date Unit " + time.getUnitsString()); continue; } } if (maxDateRange == null) maxDateRange = dateRange; else maxDateRange.extend(dateRange); } return maxDateRange; }
/** * Extract a list of data variables (and their canonical names if possible) from the dataset. * * @param threddsDataset open this dataset * @return ThreddsMetadata.Variables, or null if unable. * @throws IOException on read error */ public static ThreddsMetadata.Variables extractVariables(InvDatasetImpl threddsDataset) throws IOException { ThreddsDataFactory.Result result = null; try { result = new ThreddsDataFactory().openFeatureDataset(threddsDataset, null); if (result.fatalError) { System.out.println(" openDatatype errs=" + result.errLog); return null; } if (result.featureType == FeatureType.GRID) { // System.out.println(" extractVariables GRID=" + result.location); GridDataset gridDataset = (GridDataset) result.featureDataset; return extractVariables(threddsDataset, gridDataset); } else if ((result.featureType == FeatureType.STATION) || (result.featureType == FeatureType.POINT)) { PointObsDataset pobsDataset = (PointObsDataset) result.featureDataset; ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.0"); for (VariableSimpleIF vs : pobsDataset.getDataVariables()) { ThreddsMetadata.Variable v = new ThreddsMetadata.Variable(); vars.addVariable(v); v.setName(vs.getName()); v.setDescription(vs.getDescription()); v.setUnits(vs.getUnitsString()); ucar.nc2.Attribute att = vs.findAttributeIgnoreCase("standard_name"); v.setVocabularyName((att != null) ? att.getStringValue() : "N/A"); } vars.sort(); return vars; } } finally { try { if ((result != null) && (result.featureDataset != null)) result.featureDataset.close(); } catch (IOException ioe) { logger.error("Closing dataset " + result.featureDataset, ioe); } } return null; }
/** * Extract the lat/lon/alt bounding boxes from the dataset. * * @param threddsDataset open this dataset * @return ThreddsMetadata.GeospatialCoverage, or null if unable. * @throws IOException on read error */ public static ThreddsMetadata.GeospatialCoverage extractGeospatial(InvDatasetImpl threddsDataset) throws IOException { ThreddsDataFactory.Result result = null; try { result = new ThreddsDataFactory().openFeatureDataset(threddsDataset, null); if (result.fatalError) { System.out.println(" openDatatype errs=" + result.errLog); return null; } if (result.featureType == FeatureType.GRID) { System.out.println(" GRID=" + result.location); GridDataset gridDataset = (GridDataset) result.featureDataset; return extractGeospatial(gridDataset); } else if (result.featureType == FeatureType.POINT) { PointObsDataset pobsDataset = (PointObsDataset) result.featureDataset; LatLonRect llbb = pobsDataset.getBoundingBox(); if (null != llbb) { ThreddsMetadata.GeospatialCoverage gc = new ThreddsMetadata.GeospatialCoverage(); gc.setBoundingBox(llbb); return gc; } } else if (result.featureType == FeatureType.STATION) { StationObsDataset sobsDataset = (StationObsDataset) result.featureDataset; LatLonRect llbb = sobsDataset.getBoundingBox(); if (null != llbb) { ThreddsMetadata.GeospatialCoverage gc = new ThreddsMetadata.GeospatialCoverage(); gc.setBoundingBox(llbb); return gc; } } } finally { try { if ((result != null) && (result.featureDataset != null)) result.featureDataset.close(); } catch (IOException ioe) { logger.error("Closing dataset " + result.featureDataset, ioe); } } return null; }