/** * Write a ucar.nc2.ft.PointFeatureCollection in CF point format. * * @param pfDataset find the first PointFeatureCollection, and write all data from it * @param fileOut write to this netcdf-3 file * @return number of records written * @throws IOException on read/write error, or if no PointFeatureCollection in pfDataset */ public static int writePointFeatureCollection(FeatureDatasetPoint pfDataset, String fileOut) throws IOException { // extract the PointFeatureCollection PointFeatureCollection pointFeatureCollection = null; List<FeatureCollection> featureCollectionList = pfDataset.getPointFeatureCollectionList(); for (FeatureCollection featureCollection : featureCollectionList) { if (featureCollection instanceof PointFeatureCollection) pointFeatureCollection = (PointFeatureCollection) featureCollection; } if (null == pointFeatureCollection) throw new IOException("There is no PointFeatureCollection in " + pfDataset.getLocation()); long start = System.currentTimeMillis(); FileOutputStream fos = new FileOutputStream(fileOut); DataOutputStream out = new DataOutputStream(new BufferedOutputStream(fos, 10000)); WriterCFPointDataset writer = null; /* LOOK BAD List<VariableSimpleIF> dataVars = new ArrayList<VariableSimpleIF>(); ucar.nc2.NetcdfFile ncfile = pfDataset.getNetcdfFile(); if ((ncfile == null) || !(ncfile instanceof NetcdfDataset)) { dataVars.addAll(pfDataset.getDataVariables()); } else { NetcdfDataset ncd = (NetcdfDataset) ncfile; for (VariableSimpleIF vs : pfDataset.getDataVariables()) { if (ncd.findCoordinateAxis(vs.getName()) == null) dataVars.add(vs); } } */ int count = 0; pointFeatureCollection.resetIteration(); while (pointFeatureCollection.hasNext()) { PointFeature pointFeature = (PointFeature) pointFeatureCollection.next(); StructureData data = pointFeature.getData(); if (count == 0) { EarthLocation loc = pointFeature.getLocation(); // LOOK we dont know this until we see the obs String altUnits = Double.isNaN(loc.getAltitude()) ? null : "meters"; // LOOK units may be wrong writer = new WriterCFPointDataset(out, pfDataset.getGlobalAttributes(), altUnits); writer.writeHeader(pfDataset.getDataVariables(), -1); } writer.writeRecord(pointFeature, data); count++; } writer.finish(); out.flush(); out.close(); long took = System.currentTimeMillis() - start; System.out.printf( "Write %d records from %s to %s took %d msecs %n", count, pfDataset.getLocation(), fileOut, took); return count; }
/** * Constructor * * @param stream write to this stream * @param globalAtts optional list of global attributes (may be null) * @param altUnits optional altitude units (set to null if no altitude variable) * @param dataVars the set of data variables: first the double values, then the String values * @throws IOException if write error */ public CFPointObWriter( DataOutputStream stream, List<Attribute> globalAtts, String altUnits, List<PointObVar> dataVars, int numrec) throws IOException { ncWriter = new WriterCFPointDataset(stream, globalAtts, altUnits); List<VariableSimpleIF> vars = new ArrayList<VariableSimpleIF>(dataVars.size()); for (PointObVar pvar : dataVars) vars.add(new PointObVarAdapter(pvar)); ncWriter.writeHeader(vars, numrec); }
/** * Call this when all done, output is flushed * * @throws IOException if write error */ public void finish() throws IOException { ncWriter.finish(); }
/** * Add one data point to the file * * @param lat latitude value in units of degrees_north * @param lon longitude value in units of degrees_east * @param alt altitude value in units of altUnits (may be NaN) * @param time time value as a date * @param vals list of data values, matching dataVars in the constructor * @param svals list of String values, matching dataVars in the constructor * @throws IOException if write error */ public void addPoint(double lat, double lon, double alt, Date time, double[] vals, String[] svals) throws IOException { ncWriter.writeRecord(lat, lon, alt, time, vals, svals); }