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"); }
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 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 testWritePermute() throws Exception { NetcdfFileWriteable ncfile = new NetcdfFileWriteable(); ncfile.setName(TestLocal.cdmTestDataDir + "permuteTest.nc"); // define dimensions Dimension xDim = ncfile.addDimension("x", 3); Dimension yDim = ncfile.addDimension("y", 5); Dimension zDim = ncfile.addDimension("z", 4); Dimension tDim = ncfile.addDimension("time", 2); // define Variables ncfile.addVariable("time", double.class, new Dimension[] {tDim}); ncfile.addVariableAttribute("time", "units", "secs since 1-1-1 00:00"); ncfile.addVariable("z", double.class, new Dimension[] {zDim}); ncfile.addVariableAttribute("z", "units", "meters"); ncfile.addVariableAttribute("z", "positive", "up"); ncfile.addVariable("y", double.class, new Dimension[] {yDim}); ncfile.addVariableAttribute("y", "units", "degrees_north"); ncfile.addVariable("x", double.class, new Dimension[] {xDim}); ncfile.addVariableAttribute("x", "units", "degrees_east"); ncfile.addVariable("tzyx", double.class, new Dimension[] {tDim, zDim, yDim, xDim}); ncfile.addVariableAttribute("tzyx", "units", "K"); ncfile.addVariable("tzxy", double.class, new Dimension[] {tDim, zDim, xDim, yDim}); ncfile.addVariableAttribute("tzxy", "units", "K"); ncfile.addVariable("tyxz", double.class, new Dimension[] {tDim, yDim, xDim, zDim}); ncfile.addVariableAttribute("tyxz", "units", "K"); ncfile.addVariable("txyz", double.class, new Dimension[] {tDim, xDim, yDim, zDim}); ncfile.addVariableAttribute("txyz", "units", "K"); ncfile.addVariable("zyxt", double.class, new Dimension[] {zDim, yDim, xDim, tDim}); ncfile.addVariableAttribute("zyxt", "units", "K"); ncfile.addVariable("zxyt", double.class, new Dimension[] {zDim, xDim, yDim, tDim}); ncfile.addVariableAttribute("zxyt", "units", "K"); ncfile.addVariable("yxzt", double.class, new Dimension[] {yDim, xDim, zDim, tDim}); ncfile.addVariableAttribute("yxzt", "units", "K"); ncfile.addVariable("xyzt", double.class, new Dimension[] {xDim, yDim, zDim, tDim}); ncfile.addVariableAttribute("xyzt", "units", "K"); // missing one dimension ncfile.addVariable("zyx", double.class, new Dimension[] {zDim, yDim, xDim}); ncfile.addVariable("txy", double.class, new Dimension[] {tDim, xDim, yDim}); ncfile.addVariable("yxz", double.class, new Dimension[] {yDim, xDim, zDim}); ncfile.addVariable("xzy", double.class, new Dimension[] {xDim, zDim, yDim}); ncfile.addVariable("yxt", double.class, new Dimension[] {yDim, xDim, tDim}); ncfile.addVariable("xyt", double.class, new Dimension[] {xDim, yDim, tDim}); ncfile.addVariable("xyz", double.class, new Dimension[] {xDim, yDim, zDim}); // missing two dimension ncfile.addVariable("yx", double.class, new Dimension[] {yDim, xDim}); ncfile.addVariable("xy", double.class, new Dimension[] {xDim, yDim}); ncfile.addVariable("yz", double.class, new Dimension[] {yDim, zDim}); ncfile.addVariable("xz", double.class, new Dimension[] {xDim, zDim}); ncfile.addVariable("yt", double.class, new Dimension[] {yDim, tDim}); ncfile.addVariable("xt", double.class, new Dimension[] {xDim, tDim}); ncfile.addVariable("ty", double.class, new Dimension[] {tDim, yDim}); ncfile.addVariable("tx", double.class, new Dimension[] {tDim, xDim}); // add global attributes ncfile.addGlobalAttribute("Convention", "COARDS"); // create the file try { ncfile.create(); } catch (IOException e) { System.err.println("ERROR creating file"); assert (false); } // write time data int len = tDim.getLength(); ArrayDouble A = new ArrayDouble.D1(len); Index ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3600)); int[] origin = new int[1]; try { ncfile.write("time", origin, A); } catch (IOException e) { System.err.println("ERROR writing time"); assert (false); } // write z data len = zDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 10)); try { ncfile.write("z", origin, A); } catch (IOException e) { System.err.println("ERROR writing z"); assert (false); } // write y data len = yDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3)); try { ncfile.write("y", origin, A); } catch (IOException e) { System.err.println("ERROR writing y"); assert (false); } // write x data len = xDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 5)); try { ncfile.write("x", origin, A); } catch (IOException e) { System.err.println("ERROR writing x"); assert (false); } // write tzyx data doWrite4(ncfile, "tzyx"); doWrite4(ncfile, "tzxy"); doWrite4(ncfile, "txyz"); doWrite4(ncfile, "tyxz"); doWrite4(ncfile, "zyxt"); doWrite4(ncfile, "zxyt"); doWrite4(ncfile, "xyzt"); doWrite4(ncfile, "yxzt"); doWrite3(ncfile, "zyx"); doWrite3(ncfile, "txy"); doWrite3(ncfile, "yxz"); doWrite3(ncfile, "xzy"); doWrite3(ncfile, "yxt"); doWrite3(ncfile, "xyt"); doWrite3(ncfile, "yxt"); doWrite3(ncfile, "xyz"); doWrite2(ncfile, "yx"); doWrite2(ncfile, "xy"); doWrite2(ncfile, "yz"); doWrite2(ncfile, "xz"); doWrite2(ncfile, "yt"); doWrite2(ncfile, "xt"); doWrite2(ncfile, "ty"); doWrite2(ncfile, "tx"); if (show) System.out.println("ncfile = " + ncfile); // all done try { ncfile.close(); } catch (IOException e) { System.err.println("ERROR writing file"); assert (false); } System.out.println("*****************Test Write done"); }
Write2ncRect(NetcdfFile bufr, String fileOutName, boolean fill) throws IOException, InvalidRangeException { NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(fileOutName, fill); if (debug) { System.out.println("FileWriter write " + bufr.getLocation() + " to " + fileOutName); } // global attributes List<Attribute> glist = bufr.getGlobalAttributes(); for (Attribute att : glist) { String useName = N3iosp.makeValidNetcdfObjectName(att.getName()); Attribute useAtt; if (att.isArray()) useAtt = ncfile.addGlobalAttribute(useName, att.getValues()); else if (att.isString()) useAtt = ncfile.addGlobalAttribute(useName, att.getStringValue()); else useAtt = ncfile.addGlobalAttribute(useName, att.getNumericValue()); if (debug) System.out.println("add gatt= " + useAtt); } // global dimensions Dimension recordDim = null; Map<String, Dimension> dimHash = new HashMap<String, Dimension>(); for (Dimension oldD : bufr.getDimensions()) { String useName = N3iosp.makeValidNetcdfObjectName(oldD.getName()); boolean isRecord = useName.equals("record"); Dimension newD = ncfile.addDimension(useName, oldD.getLength(), true, false, false); dimHash.put(newD.getName(), newD); if (isRecord) recordDim = newD; if (debug) System.out.println("add dim= " + newD); } // Variables Structure recordStruct = (Structure) bufr.findVariable(BufrIosp.obsRecord); for (Variable oldVar : recordStruct.getVariables()) { if (oldVar.getDataType() == DataType.STRUCTURE) continue; String varName = N3iosp.makeValidNetcdfObjectName(oldVar.getShortName()); DataType newType = oldVar.getDataType(); List<Dimension> newDims = new ArrayList<Dimension>(); newDims.add(recordDim); for (Dimension dim : oldVar.getDimensions()) { newDims.add(ncfile.addDimension(oldVar.getShortName() + "_strlen", dim.getLength())); } Variable newVar = ncfile.addVariable(varName, newType, newDims); if (debug) System.out.println("add var= " + newVar); // attributes List<Attribute> attList = oldVar.getAttributes(); for (Attribute att : attList) { String useName = N3iosp.makeValidNetcdfObjectName(att.getName()); if (att.isArray()) ncfile.addVariableAttribute(varName, useName, att.getValues()); else if (att.isString()) ncfile.addVariableAttribute(varName, useName, att.getStringValue()); else ncfile.addVariableAttribute(varName, useName, att.getNumericValue()); } } // int max_seq = countSeq(recordStruct); // Dimension seqD = ncfile.addDimension("level", max_seq); for (Variable v : recordStruct.getVariables()) { if (v.getDataType() != DataType.STRUCTURE) continue; String structName = N3iosp.makeValidNetcdfObjectName(v.getShortName()); int shape[] = v.getShape(); Dimension structDim = ncfile.addDimension(structName, shape[0]); Structure struct = (Structure) v; for (Variable seqVar : struct.getVariables()) { String varName = N3iosp.makeValidNetcdfObjectName(seqVar.getShortName() + "-" + structName); DataType newType = seqVar.getDataType(); List<Dimension> newDims = new ArrayList<Dimension>(); newDims.add(recordDim); newDims.add(structDim); for (Dimension dim : seqVar.getDimensions()) { newDims.add(ncfile.addDimension(seqVar.getShortName() + "_strlen", dim.getLength())); } Variable newVar = ncfile.addVariable(varName, newType, newDims); if (debug) System.out.println("add var= " + newVar); // attributes List<Attribute> attList = seqVar.getAttributes(); for (Attribute att : attList) { String useName = N3iosp.makeValidNetcdfObjectName(att.getName()); if (att.isArray()) ncfile.addVariableAttribute(varName, useName, att.getValues()); else if (att.isString()) ncfile.addVariableAttribute(varName, useName, att.getStringValue()); else ncfile.addVariableAttribute(varName, useName, att.getNumericValue()); } } } // create the file ncfile.create(); if (debug) System.out.println("File Out= " + ncfile.toString()); // boolean ok = (Boolean) ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); double total = copyVarData(ncfile, recordStruct); ncfile.flush(); System.out.println("FileWriter done total bytes = " + total); ncfile.close(); }