private void readMetadata() { // must open a prototype in order to get the data variable TimedCollection.Dataset td = pointCollections.getPrototype(); if (td == null) throw new RuntimeException("No datasets in the collection"); Formatter errlog = new Formatter(); try (FeatureDatasetPoint openDataset = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.POINT, td.getLocation(), null, errlog)) { if (openDataset != null) { dataVariables = openDataset.getDataVariables(); globalAttributes = openDataset.getGlobalAttributes(); } } catch (IOException ioe) { throw new RuntimeException(ioe); } }
public List<VariableSimpleIF> getDataVariables() { if (dataVariables == null) { // must open a prototype in order to get the data variable TimedCollection.Dataset td = pointCollections.getPrototype(); if (td == null) throw new RuntimeException("No datasets in the collection"); Formatter errlog = new Formatter(); FeatureDatasetPoint openDataset = null; try { openDataset = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.POINT, td.getLocation(), null, errlog); if (openDataset != null) dataVariables = openDataset.getDataVariables(); } catch (IOException ioe) { throw new RuntimeException(ioe); } finally { try { if (openDataset != null) openDataset.close(); } catch (Throwable t) { } } } return dataVariables; }
private void readMetadata() { // must open a prototype in order to get the data variable TimedCollection.Dataset td = pointCollections.getPrototype(); if (td == null) throw new RuntimeException("No datasets in the collection"); Formatter errlog = new Formatter(); FeatureDatasetPoint openDataset = null; try { openDataset = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.POINT, td.getLocation(), null, errlog); if (openDataset != null) { dataVariables = openDataset.getDataVariables(); globalAttributes = openDataset.getGlobalAttributes(); } } catch (IOException ioe) { throw new RuntimeException(ioe); } finally { try { if (openDataset != null) openDataset.close(); } catch (Throwable t) { } } }
@Test public void testDataVars() throws Exception { String file = TestDir.cdmLocalTestDataDir + "point/stationSingle.ncml"; Formatter buf = new Formatter(); try (FeatureDatasetPoint pods = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(ucar.nc2.constants.FeatureType.STATION, file, null, buf)) { List<VariableSimpleIF> dataVars = pods.getDataVariables(); for (VariableSimpleIF dv : dataVars) System.out.printf(" %s%n", dv ); assert (dataVars.size() == 1) : "Should only be one data var"; VariableSimpleIF data = dataVars.get(0); assert data.getShortName().equalsIgnoreCase("data"); } }
public ThreddsMetadata.VariableGroup extractVariables(FeatureDatasetPoint fd) { List<ThreddsMetadata.Variable> vars = new ArrayList<>(); List<VariableSimpleIF> dataVars = fd.getDataVariables(); if (dataVars == null) return null; for (VariableSimpleIF v : dataVars) { String name = v.getShortName(); String desc = v.getDescription(); String units = v.getUnitsString(); String vname = null; String id = null; ucar.nc2.Attribute att = v.findAttributeIgnoreCase("standard_name"); if (att != null) vname = att.getStringValue(); vars.add(new ThreddsMetadata.Variable(name, desc, vname, units, id)); } Collections.sort(vars); // String vocab, String vocabHref, URI vocabUri, URI mapUri, List<Variable> variables return new ThreddsMetadata.VariableGroup("CF-1.0", null, null, vars); }
static void compare(FeatureDatasetPoint org, FeatureDatasetPoint copy) { FeatureType fcOrg = org.getFeatureType(); FeatureType fcCopy = copy.getFeatureType(); assert fcOrg == fcCopy; List<VariableSimpleIF> orgVars = org.getDataVariables(); List<VariableSimpleIF> copyVars = copy.getDataVariables(); Formatter f = new Formatter(); boolean ok = CompareNetcdf2.compareLists(getNames(orgVars, Lists.newArrayList("profileId")), getNames(copyVars, Lists.newArrayList("profileId")), f); if (ok) System.out.printf("Data Vars OK%n"); else { System.out.printf("Data Vars NOT OK%n %s%n", f); if (failOnDataVarsDifferent) assert false; } }
private static int writePointFeatureCollection(FeatureDatasetPoint fdpoint, PointFeatureCollection pfc, String fileOut, NetcdfFileWriter.Version version) throws IOException { if (debug) System.out.printf("write to file %s%n ", fileOut); WriterCFPointCollection writer = new WriterCFPointCollection(version, fileOut, fdpoint.getGlobalAttributes()); int count = 0; while (pfc.hasNext()) { PointFeature pf = pfc.next(); if (count == 0) { writer.writeHeader(fdpoint.getDataVariables(), pf.getTimeUnit(), null); } writer.writeRecord(pf, pf.getData()); count++; if (debug && count % 100 == 0) System.out.printf("%d ", count); if (debug && count % 1000 == 0) System.out.printf("%n "); } writer.finish(); return count; }
@Override protected StationHelper createStationHelper() throws IOException { TimedCollection.Dataset td = dataCollection.getPrototype(); if (td == null) throw new RuntimeException("No datasets in the collection"); Formatter errlog = new Formatter(); try (FeatureDatasetPoint openDataset = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.STATION, td.getLocation(), null, errlog)) { if (openDataset == null) throw new IllegalStateException("Cant open FeatureDatasetPoint " + td.getLocation()); StationHelper stationHelper = new StationHelper(); List<DsgFeatureCollection> fcList = openDataset.getPointFeatureCollectionList(); StationTimeSeriesCollectionImpl openCollection = (StationTimeSeriesCollectionImpl) fcList.get(0); List<StationFeature> stns = openCollection.getStationFeatures(); for (StationFeature stnFeature : stns) { stationHelper.addStation(new CompositeStationFeature(stnFeature, timeUnit, altUnits, stnFeature.getFeatureData(), this.dataCollection)); } dataVariables = openDataset.getDataVariables(); globalAttributes = openDataset.getGlobalAttributes(); return stationHelper; } }
@Override protected void initStationHelper() { TimedCollection.Dataset td = dataCollection.getPrototype(); if (td == null) throw new RuntimeException("No datasets in the collection"); Formatter errlog = new Formatter(); FeatureDatasetPoint openDataset = null; try { openDataset = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open(FeatureType.STATION, td.getLocation(), null, errlog); List<FeatureCollection> fcList = openDataset.getPointFeatureCollectionList(); StationTimeSeriesCollectionImpl openCollection = (StationTimeSeriesCollectionImpl) fcList.get(0); List<Station> stns = openCollection.getStations(); stationHelper = new StationHelper(); for (Station s : stns) stationHelper.addStation(new CompositeStationFeature(s, null, this.dataCollection)); dataVariables = openDataset.getDataVariables(); } catch (Exception ioe) { throw new RuntimeException(td.getLocation(), ioe); } finally { try { if (openDataset != null) openDataset.close(); } catch (Throwable t) { } } }
private static int writePointFeatureCollection(FeatureDatasetPoint fdpoint, PointFeatureCollection pfc, String fileOut, CFPointWriterConfig config) throws IOException { try (WriterCFPointCollection pointWriter = new WriterCFPointCollection(fileOut, fdpoint.getGlobalAttributes(), fdpoint.getDataVariables(), pfc.getTimeUnit(), pfc.getAltUnits(), config)) { pointWriter.setExtraVariables( pfc.getExtraVariables()); int count = 0; for (PointFeature pf : pfc) { if (count == 0) pointWriter.writeHeader(pf); pointWriter.writeRecord(pf, pf.getFeatureData()); count++; if (debug && count % 100 == 0) System.out.printf("%d ", count); if (debug && count % 1000 == 0) System.out.printf("%n "); } pointWriter.finish(); return count; } }
private static int writeProfileFeatureCollection(FeatureDatasetPoint fdpoint, ProfileFeatureCollection fc, String fileOut, CFPointWriterConfig config) throws IOException { try (WriterCFProfileCollection cfWriter = new WriterCFProfileCollection(fileOut, fdpoint.getGlobalAttributes(), fdpoint.getDataVariables(), fc.getTimeUnit(), fc.getAltUnits(), config)) { cfWriter.setExtraVariables( fc.getExtraVariables()); // LOOK not always needed int count = 0; int name_strlen = 0; int nprofiles = fc.size(); if (nprofiles < 0) { for (ProfileFeature pf : fc) { name_strlen = Math.max(name_strlen, pf.getName().length()); count++; } nprofiles = count; } cfWriter.setFeatureAuxInfo(nprofiles, name_strlen); count = 0; for (ProfileFeature profile : fc) { count += cfWriter.writeProfile(profile); if (debug && count % 10 == 0) System.out.printf("%d ", count); if (debug && count % 100 == 0) System.out.printf("%n "); } cfWriter.finish(); return count; } }
private static int writeStationFeatureCollection(FeatureDatasetPoint fdpoint, StationTimeSeriesFeatureCollection fds, String fileOut, NetcdfFileWriter.Version version) throws IOException { if (debug) System.out.printf("write to file %s%n ", fileOut); WriterCFStationCollection writer = new WriterCFStationCollection(version, fileOut, fdpoint.getGlobalAttributes()); ucar.nc2.ft.PointFeatureCollection pfc = fds.flatten(null, (CalendarDateRange) null); int count = 0; while (pfc.hasNext()) { PointFeature pf = pfc.next(); if (count == 0) { writer.writeHeader(fds.getStations(), fdpoint.getDataVariables(), pf.getTimeUnit(), ""); } StationPointFeature spf = (StationPointFeature) pf; writer.writeRecord(spf.getStation(), pf, pf.getData()); count++; if (debug && count % 100 == 0) System.out.printf("%d ", count); if (debug && count % 1000 == 0) System.out.printf("%n "); } writer.finish(); return count; }
@Override protected StationHelper createStationHelper() throws IOException { TimedCollection.Dataset td = dataCollection.getPrototype(); if (td == null) throw new RuntimeException("No datasets in the collection"); Formatter errlog = new Formatter(); try (FeatureDatasetPoint openDataset = (FeatureDatasetPoint) FeatureDatasetFactoryManager.open( FeatureType.STATION, td.getLocation(), null, errlog)) { StationHelper stationHelper = new StationHelper(); List<FeatureCollection> fcList = openDataset.getPointFeatureCollectionList(); StationTimeSeriesCollectionImpl openCollection = (StationTimeSeriesCollectionImpl) fcList.get(0); List<Station> stns = openCollection.getStations(); for (Station s : stns) { StationTimeSeriesFeature stnFeature = openCollection.getStationFeature(s); stationHelper.addStation(new CompositeStationFeature(s, timeUnit, altUnits, stnFeature.getFeatureData(), this.dataCollection)); } dataVariables = openDataset.getDataVariables(); globalAttributes = openDataset.getGlobalAttributes(); return stationHelper; } }
private static int writePointFeatureCollection(FeatureDatasetPoint fdpoint, PointFeatureCollection pfc, String fileOut, CFPointWriterConfig config) throws IOException { try (WriterCFPointCollection pointWriter = new WriterCFPointCollection(fileOut, fdpoint.getGlobalAttributes(), fdpoint.getDataVariables(), pfc.getExtraVariables(), pfc.getTimeUnit(), pfc.getAltUnits(), config)) { int count = 0; pfc.resetIteration(); while(pfc.hasNext()) { PointFeature pf = pfc.next(); if (count == 0) pointWriter.writeHeader(pf); pointWriter.writeRecord(pf, pf.getFeatureData()); count++; if (debug && count % 100 == 0) System.out.printf("%d ", count); if (debug && count % 1000 == 0) System.out.printf("%n "); } pointWriter.finish(); return count; } }
private static int writeTrajectoryFeatureCollection(FeatureDatasetPoint fdpoint, TrajectoryFeatureCollection pds, String fileOut, CFPointWriterConfig config) throws IOException { WriterCFTrajectoryCollection cfWriter = new WriterCFTrajectoryCollection(fileOut, fdpoint.getGlobalAttributes(), fdpoint.getDataVariables(), pds.getExtraVariables(), pds.getTimeUnit(), pds.getAltUnits(), config); // LOOK not always needed int count = 0; int name_strlen = 0; int ntrajs = pds.size(); if (ntrajs < 0) { pds.resetIteration(); while (pds.hasNext()) { TrajectoryFeature feature = pds.next(); name_strlen = Math.max(name_strlen, feature.getName().length()); count++; } ntrajs = count; } cfWriter.setFeatureAuxInfo(ntrajs, name_strlen); count = 0; pds.resetIteration(); while (pds.hasNext()) { TrajectoryFeature feature = pds.next(); count += cfWriter.writeTrajectory(feature); if (debug && count % 10 == 0) System.out.printf("%d ", count); if (debug && count % 100 == 0) System.out.printf("%n "); } cfWriter.finish(); return count; }
private static int writeStationFeatureCollection(FeatureDatasetPoint dataset, StationTimeSeriesFeatureCollection fc, String fileOut, CFPointWriterConfig config) throws IOException { try (WriterCFStationCollection cfWriter = new WriterCFStationCollection(fileOut, dataset.getGlobalAttributes(), dataset.getDataVariables(), fc.getTimeUnit(), fc.getAltUnits(), config)) { cfWriter.setExtraVariables( fc.getExtraVariables()); ucar.nc2.ft.PointFeatureCollection pfc = fc.flatten(null, null, null); // all data, but no need to sort by station int count = 0; for (PointFeature pf : pfc) { StationPointFeature spf = (StationPointFeature) pf; if (count == 0) cfWriter.writeHeader(fc.getStationFeatures(), spf); cfWriter.writeRecord(spf.getStation(), pf, pf.getFeatureData()); count++; if (debug && count % 100 == 0) System.out.printf("%d ", count); if (debug && count % 1000 == 0) System.out.printf("%n "); } cfWriter.finish(); return count; } }
private static int writeStationFeatureCollection(FeatureDatasetPoint dataset, StationTimeSeriesFeatureCollection fc, String fileOut, CFPointWriterConfig config) throws IOException { WriterCFStationCollection cfWriter = new WriterCFStationCollection(fileOut, dataset.getGlobalAttributes(), dataset.getDataVariables(), fc.getExtraVariables(), fc.getTimeUnit(), fc.getAltUnits(), config); ucar.nc2.ft.PointFeatureCollection pfc = fc.flatten(null, (CalendarDateRange) null); // LOOK int count = 0; while (pfc.hasNext()) { PointFeature pf = pfc.next(); StationPointFeature spf = (StationPointFeature) pf; if (count == 0) cfWriter.writeHeader(fc.getStationFeatures(), spf); cfWriter.writeRecord(spf.getStation(), pf, pf.getFeatureData()); count++; if (debug && count % 100 == 0) System.out.printf("%d ", count); if (debug && count % 1000 == 0) System.out.printf("%n "); } cfWriter.finish(); return count; }
static public ThreddsMetadata.Variables extractVariables(FeatureDatasetPoint fd) { ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.5"); List<VariableSimpleIF> dataVars = fd.getDataVariables(); if (dataVars == null) return vars; for (VariableSimpleIF v : dataVars) { ThreddsMetadata.Variable tv = new ThreddsMetadata.Variable(); vars.addVariable(tv); tv.setName(v.getShortName()); tv.setDescription(v.getDescription()); tv.setUnits(v.getUnitsString()); ucar.nc2.Attribute att = v.findAttributeIgnoreCase("standard_name"); if (att != null) tv.setVocabularyName(att.getStringValue()); } vars.sort(); return vars; }
static public ThreddsMetadata.Variables extractVariables(FeatureDatasetPoint fd) { ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.5"); List<VariableSimpleIF> dataVars = fd.getDataVariables(); if (dataVars == null) return vars; for (VariableSimpleIF v : dataVars) { ThreddsMetadata.Variable tv = new ThreddsMetadata.Variable(); vars.addVariable(tv); tv.setName(v.getShortName()); tv.setDescription(v.getDescription()); tv.setUnits(v.getUnitsString()); ucar.nc2.Attribute att = v.findAttributeIgnoreCase("standard_name"); if (att != null) tv.setVocabularyName(att.getStringValue()); } vars.sort(); return vars; }
static public ThreddsMetadata.Variables extractVariables(FeatureDatasetPoint fd) { ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.5"); List<VariableSimpleIF> dataVars = fd.getDataVariables(); if (dataVars == null) return vars; for (VariableSimpleIF v : dataVars) { ThreddsMetadata.Variable tv = new ThreddsMetadata.Variable(); vars.addVariable(tv); tv.setName(v.getShortName()); tv.setDescription(v.getDescription()); tv.setUnits(v.getUnitsString()); ucar.nc2.Attribute att = v.findAttributeIgnoreCase("standard_name"); if (att != null) tv.setVocabularyName(att.getStringValue()); } vars.sort(); return vars; }