public void writeHeader( List<ucar.unidata.geoloc.Station> stns, List<VariableSimpleIF> vars, int nprofiles, String altVarName) throws IOException { createGlobalAttributes(); createStations(stns); createProfiles(nprofiles); // dummys, update in finish() ncfile.addGlobalAttribute("zaxis_coordinate", altVarName); ncfile.addGlobalAttribute("time_coverage_start", dateFormatter.toDateTimeStringISO(new Date())); ncfile.addGlobalAttribute("time_coverage_end", dateFormatter.toDateTimeStringISO(new Date())); createDataVariables(vars); ncfile.create(); // done with define mode writeStationData(stns); // write out the station info // now write the observations if (!(Boolean) ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE)) throw new IllegalStateException("can't add record variable"); }
public void writeRecord(String stnName, Date obsDate, StructureData sdata) throws IOException { StationTracker stnTracker = stationMap.get(stnName); ProfileTracker proTracker = stnTracker.profileMap.get(obsDate); if (proTracker == null) { proTracker = new ProfileTracker(profileIndex); stnTracker.profileMap.put(obsDate, proTracker); stnTracker.link.add(profileIndex); stnTracker.lastChild = profileIndex; stnTracker.numChildren++; try { originTime[0] = profileIndex; // 2d index timeArray.set(0, dateFormatter.toDateTimeStringISO(obsDate)); parentArray.set(0, stnTracker.parent_index); ncfile.writeStringData(timeName, originTime, timeArray); ncfile.write(parentStationIndex, originTime, parentArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } profileIndex++; } // needs to be wrapped as an ArrayStructure, even though we are only writing one at a time. ArrayStructureW sArray = new ArrayStructureW(sdata.getStructureMembers(), new int[] {1}); sArray.setStructureData(sdata, 0); // track the min and max date if ((minDate == null) || minDate.after(obsDate)) minDate = obsDate; if ((maxDate == null) || maxDate.before(obsDate)) maxDate = obsDate; timeArray.set(0, dateFormatter.toDateTimeStringISO(obsDate)); parentArray.set(0, proTracker.parent_index); proTracker.link.add(recno); proTracker.lastChild = recno; // not currently used proTracker.numChildren++; // write the recno record origin[0] = recno; // 1d index originTime[0] = recno; // 2d index try { ncfile.write("record", origin, sArray); ncfile.write(parentProfileIndex, originTime, parentArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } recno++; }
private void createGlobalAttributes() { ncfile.addGlobalAttribute("Conventions", "Unidata Observation Dataset v1.0"); ncfile.addGlobalAttribute("cdm_datatype", "Profile"); ncfile.addGlobalAttribute("title", title); ncfile.addGlobalAttribute("desc", "Extracted by THREDDS/Netcdf Subset Service"); /* ncfile.addGlobalAttribute("observationDimension", recordDimName); ncfile.addGlobalAttribute("stationDimension", stationDimName); ncfile.addGlobalAttribute("latitude_coordinate", latName); ncfile.addGlobalAttribute("longitude_coordinate", lonName); ncfile.addGlobalAttribute("time_coordinate", timeName); */ }
private void writeStationData(List<ucar.unidata.geoloc.Station> stnList) throws IOException { this.stnList = stnList; int nstns = stnList.size(); stationMap = new HashMap<String, StationTracker>(2 * nstns); if (debug) System.out.println("stationMap created"); // now write the station data ArrayDouble.D1 latArray = new ArrayDouble.D1(nstns); ArrayDouble.D1 lonArray = new ArrayDouble.D1(nstns); ArrayDouble.D1 altArray = new ArrayDouble.D1(nstns); ArrayObject.D1 idArray = new ArrayObject.D1(String.class, nstns); ArrayObject.D1 descArray = new ArrayObject.D1(String.class, nstns); ArrayObject.D1 wmoArray = new ArrayObject.D1(String.class, nstns); for (int i = 0; i < stnList.size(); i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); stationMap.put(stn.getName(), new StationTracker(i)); latArray.set(i, stn.getLatitude()); lonArray.set(i, stn.getLongitude()); if (useAlt) altArray.set(i, stn.getAltitude()); idArray.set(i, stn.getName()); descArray.set(i, stn.getDescription()); if (useWmoId) wmoArray.set(i, stn.getWmoId()); } try { ncfile.write(latName, latArray); ncfile.write(lonName, lonArray); if (useAlt) ncfile.write(altName, altArray); ncfile.writeStringData(idName, idArray); ncfile.writeStringData(descName, descArray); if (useWmoId) ncfile.writeStringData(wmoName, wmoArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } }
private void createDataVariables(List<VariableSimpleIF> dataVars) throws IOException { /* height variable Variable heightVar = ncfile.addStringVariable(altName, recordDims, 20); ncfile.addVariableAttribute(heightVar, new Attribute("long_name", "height of observation")); ncfile.addVariableAttribute(heightVar, new Attribute("units", altUnits)); */ Variable v = ncfile.addVariable(parentProfileIndex, DataType.INT, recordDimName); ncfile.addVariableAttribute(v, new Attribute("long_name", "index of parent profile")); v = ncfile.addVariable(nextObsName, DataType.INT, recordDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "record number of next obs in linked list for this profile")); // find all dimensions needed by the data variables for (VariableSimpleIF var : dataVars) { List<Dimension> dims = var.getDimensions(); dimSet.addAll(dims); } // add them for (Dimension d : dimSet) { if (!d.isUnlimited()) ncfile.addDimension(d.getName(), d.getLength(), d.isShared(), false, d.isVariableLength()); } // add the data variables all using the record dimension for (VariableSimpleIF oldVar : dataVars) { List<Dimension> dims = oldVar.getDimensions(); StringBuffer dimNames = new StringBuffer(recordDimName); for (Dimension d : dims) { if (!d.isUnlimited()) dimNames.append(" ").append(d.getName()); } Variable newVar = ncfile.addVariable(oldVar.getName(), oldVar.getDataType(), dimNames.toString()); List<Attribute> atts = oldVar.getAttributes(); for (Attribute att : atts) { ncfile.addVariableAttribute(newVar, att); } } }
public void finish() throws IOException { writeDataFinish(); ncfile.close(); }
private void writeDataFinish() throws IOException { // finish global variables ArrayInt.D0 totalArray = new ArrayInt.D0(); totalArray.set(profileIndex); try { ncfile.write(numProfilesTotalName, totalArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } // finish the station data int nstns = stnList.size(); ArrayInt.D1 firstProfileArray = new ArrayInt.D1(nstns); ArrayInt.D1 numProfileArray = new ArrayInt.D1(nstns); ArrayInt.D1 nextProfileArray = new ArrayInt.D1(nprofiles); for (int i = 0; i < stnList.size(); i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); StationTracker tracker = stationMap.get(stn.getName()); numProfileArray.set(i, tracker.numChildren); int first = (tracker.link.size() > 0) ? tracker.link.get(0) : -1; firstProfileArray.set(i, first); if (tracker.link.size() > 0) { // construct forward link List<Integer> nextList = tracker.link; for (int j = 0; j < nextList.size() - 1; j++) { Integer curr = nextList.get(j); Integer next = nextList.get(j + 1); nextProfileArray.set(curr, next); } Integer curr = nextList.get(nextList.size() - 1); nextProfileArray.set(curr, -1); } } try { ncfile.write(firstProfileName, firstProfileArray); ncfile.write(numProfilesName, numProfileArray); ncfile.write(nextProfileName, nextProfileArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } // finish the profile data ArrayInt.D1 nextObsArray = new ArrayInt.D1(recno); ArrayInt.D1 firstObsArray = new ArrayInt.D1(nprofiles); ArrayInt.D1 numObsArray = new ArrayInt.D1(nprofiles); for (int i = 0; i < stnList.size(); i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); StationTracker stnTracker = stationMap.get(stn.getName()); Set<Date> dates = stnTracker.profileMap.keySet(); for (Date date : dates) { ProfileTracker proTracker = stnTracker.profileMap.get(date); int trackerIndex = proTracker.parent_index; numObsArray.set(trackerIndex, proTracker.numChildren); int first = (proTracker.link.size() > 0) ? proTracker.link.get(0) : -1; firstObsArray.set(trackerIndex, first); if (proTracker.link.size() > 0) { // construct forward link List<Integer> nextList = proTracker.link; for (int j = 0; j < nextList.size() - 1; j++) { Integer curr = nextList.get(j); Integer next = nextList.get(j + 1); nextObsArray.set(curr, next); } Integer curr = nextList.get(nextList.size() - 1); nextObsArray.set(curr, -1); } } } try { ncfile.write(firstObsName, firstObsArray); ncfile.write(numObsName, numObsArray); ncfile.write(nextObsName, nextObsArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } // finish the obs data ncfile.updateAttribute( null, new Attribute("time_coverage_start", dateFormatter.toDateTimeStringISO(minDate))); ncfile.updateAttribute( null, new Attribute("time_coverage_end", dateFormatter.toDateTimeStringISO(maxDate))); }
private void createProfiles(int nprofiles) throws IOException { this.nprofiles = nprofiles; // add the dimensions Dimension profileDim = ncfile.addDimension(profileDimName, nprofiles); profileDims.add(profileDim); Variable v = ncfile.addVariable(numObsName, DataType.INT, profileDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "number of children in linked list for this profile")); v = ncfile.addVariable(numProfilesTotalName, DataType.INT, ""); ncfile.addVariableAttribute(v, new Attribute("long_name", "number of valid profiles")); v = ncfile.addVariable(firstObsName, DataType.INT, profileDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "record number of first obs in linked list for this profile")); // time variable Variable timeVar = ncfile.addStringVariable(timeName, profileDims, 20); ncfile.addVariableAttribute( timeVar, new Attribute("long_name", "ISO-8601 Date - time of observation")); v = ncfile.addVariable(parentStationIndex, DataType.INT, profileDimName); ncfile.addVariableAttribute(v, new Attribute("long_name", "index of parent station")); v = ncfile.addVariable(nextProfileName, DataType.INT, profileDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "index of next profile in linked list for this station")); }
private void createStations(List<ucar.unidata.geoloc.Station> stnList) throws IOException { int nstns = stnList.size(); // see if there's altitude, wmoId for any stations for (int i = 0; i < nstns; i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); // if (!Double.isNaN(stn.getAltitude())) // useAlt = true; if ((stn.getWmoId() != null) && (stn.getWmoId().trim().length() > 0)) useWmoId = true; } /* if (useAlt) ncfile.addGlobalAttribute("altitude_coordinate", altName); */ // find string lengths for (int i = 0; i < nstns; i++) { ucar.unidata.geoloc.Station station = stnList.get(i); name_strlen = Math.max(name_strlen, station.getName().length()); desc_strlen = Math.max(desc_strlen, station.getDescription().length()); if (useWmoId) wmo_strlen = Math.max(wmo_strlen, station.getName().length()); } LatLonRect llbb = getBoundingBox(stnList); ncfile.addGlobalAttribute( "geospatial_lat_min", Double.toString(llbb.getLowerLeftPoint().getLatitude())); ncfile.addGlobalAttribute( "geospatial_lat_max", Double.toString(llbb.getUpperRightPoint().getLatitude())); ncfile.addGlobalAttribute( "geospatial_lon_min", Double.toString(llbb.getLowerLeftPoint().getLongitude())); ncfile.addGlobalAttribute( "geospatial_lon_max", Double.toString(llbb.getUpperRightPoint().getLongitude())); // add the dimensions Dimension recordDim = ncfile.addUnlimitedDimension(recordDimName); recordDims.add(recordDim); Dimension stationDim = ncfile.addDimension(stationDimName, nstns); stationDims.add(stationDim); // add the station Variables using the station dimension Variable v = ncfile.addVariable(latName, DataType.DOUBLE, stationDimName); ncfile.addVariableAttribute(v, new Attribute("units", "degrees_north")); ncfile.addVariableAttribute(v, new Attribute("long_name", "station latitude")); v = ncfile.addVariable(lonName, DataType.DOUBLE, stationDimName); ncfile.addVariableAttribute(v, new Attribute("units", "degrees_east")); ncfile.addVariableAttribute(v, new Attribute("long_name", "station longitude")); if (useAlt) { v = ncfile.addVariable(altName, DataType.DOUBLE, stationDimName); ncfile.addVariableAttribute(v, new Attribute("units", "meters")); ncfile.addVariableAttribute(v, new Attribute("long_name", "station altitude")); } v = ncfile.addStringVariable(idName, stationDims, name_strlen); ncfile.addVariableAttribute(v, new Attribute("long_name", "station identifier")); v = ncfile.addStringVariable(descName, stationDims, desc_strlen); ncfile.addVariableAttribute(v, new Attribute("long_name", "station description")); if (useWmoId) { v = ncfile.addStringVariable(wmoName, stationDims, wmo_strlen); ncfile.addVariableAttribute(v, new Attribute("long_name", "station WMO id")); } v = ncfile.addVariable(numProfilesName, DataType.INT, stationDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "number of profiles in linked list for this station")); v = ncfile.addVariable(firstProfileName, DataType.INT, stationDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "index of first profile in linked list for this station")); }
public void setLength(long size) { ncfile.setLength(size); }
public WriterProfileObsDataset(String fileOut, String title) throws IOException { ncfile = NetcdfFileWriteable.createNew(fileOut, false); ncfile.setFill(false); this.title = title; }
public void testWriteStandardVar() throws Exception { NetcdfFileWriteable ncfile = new NetcdfFileWriteable(filename, false); // define dimensions Dimension latDim = ncfile.addDimension("lat", 2); Dimension lonDim = ncfile.addDimension("lon", 3); ArrayList dims = new ArrayList(); dims.add(latDim); dims.add(lonDim); // case 1 ncfile.addVariable("t1", DataType.DOUBLE, dims); ncfile.addVariableAttribute("t1", CDM.SCALE_FACTOR, new Double(2.0)); ncfile.addVariableAttribute("t1", "add_offset", new Double(77.0)); // case 2 ncfile.addVariable("t2", DataType.BYTE, dims); ncfile.addVariableAttribute("t2", CDM.SCALE_FACTOR, new Short((short) 2)); ncfile.addVariableAttribute("t2", "add_offset", new Short((short) 77)); // case 3 ncfile.addVariable("t3", DataType.BYTE, dims); ncfile.addVariableAttribute("t3", "_FillValue", new Byte((byte) 255)); // case 4 ncfile.addVariable("t4", DataType.SHORT, dims); ncfile.addVariableAttribute("t4", CDM.MISSING_VALUE, new Short((short) -9999)); // case 5 ncfile.addVariable("t5", DataType.SHORT, dims); ncfile.addVariableAttribute("t5", CDM.MISSING_VALUE, new Short((short) -9999)); ncfile.addVariableAttribute("t5", CDM.SCALE_FACTOR, new Short((short) 2)); ncfile.addVariableAttribute("t5", "add_offset", new Short((short) 77)); // case 1 ncfile.addVariable("m1", DataType.DOUBLE, dims); ncfile.addVariableAttribute("m1", CDM.MISSING_VALUE, -999.99); // create the file ncfile.create(); // write t1 ArrayDouble A = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength()); int i, j; Index ima = A.getIndex(); // write for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) A.setDouble(ima.set(i, j), (double) (i * 10.0 + j)); int[] origin = new int[2]; ncfile.write("t1", origin, A); // write t2 ArrayByte Ab = new ArrayByte.D2(latDim.getLength(), lonDim.getLength()); ima = Ab.getIndex(); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) Ab.setByte(ima.set(i, j), (byte) (i * 10 + j)); ncfile.write("t2", origin, Ab); // write t3 ncfile.write("t3", origin, Ab); // write t4 Array As = new ArrayShort.D2(latDim.getLength(), lonDim.getLength()); ima = As.getIndex(); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) As.setShort(ima.set(i, j), (short) (i * 10 + j)); ncfile.write("t4", origin, As); As.setShort(ima.set(0, 0), (short) -9999); ncfile.write("t5", origin, As); // write m1 ArrayDouble.D2 Ad = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength()); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) Ad.setDouble(ima.set(i, j), (double) (i * 10.0 + j)); Ad.set(1, 1, -999.99); ncfile.write("m1", new int[2], Ad); // all done ncfile.close(); System.out.println("**************TestStandardVar Write done"); }