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 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); } } }
/** * Generates an empty template for a block * * @param files * @param outputFile * @return */ private NetcdfFileWriteable makeTemplate(TreeMap<Long, NetcdfFile> files, String outputFile) { NetcdfFileWriteable ncf_out = null; if (!(new File(outputDir).exists())) { throw new IllegalArgumentException("Directory " + outputDir + " does not exist."); } try { ncf_in = files.get((new ArrayList<Long>(files.keySet())).get(0)); Variable time = ncf_in.findVariable(inTimeName); if (time == null) { List<Variable> var = ncf_in.getVariables(); System.out.println( "WARNING: Variable " + inTimeName + " was not found. File variables are:\n" + Arrays.toString(var.toArray())); } Variable depth = ncf_in.findVariable(inDepthName); if (depth == null && ncf_in.getDimensions().size() > 3) { List<Variable> var = ncf_in.getVariables(); System.out.println( "WARNING: Depth variable " + inDepthName + " not found, and the number of dimensions is greater than 3." + " File variables are:\n" + Arrays.toString(var.toArray())); } Variable lat = ncf_in.findVariable(inLatName); if (lat == null) { List<Variable> var = ncf_in.getVariables(); System.out.println( "WARNING: Variable " + inLatName + " was not found. File variables are:\n" + Arrays.toString(var.toArray())); } Variable lon = ncf_in.findVariable(inLonName); if (lon == null) { List<Variable> var = ncf_in.getVariables(); System.out.println( "WARNING: Variable " + inLonName + " was not found. File variables are:\n" + Arrays.toString(var.toArray())); } int latidx = lat.findDimensionIndex(inVerticalDim); int lonidx = lon.findDimensionIndex(inHorizontalDim); int latlen = lat.getDimension(latidx).getLength(); int lonlen = lon.getDimension(lonidx).getLength(); Long[] la = files.keySet().toArray(new Long[files.size()]); timeArr = Array.factory(DataType.DOUBLE, new int[] {files.size()}); for (int i = 0; i < la.length; i++) { timeArr.setDouble(i, TimeConvert.millisToHYCOM(la[i])); } if (depth != null) { depthArr = depth.read(); if (reverseDepth) { for (int i = 0; i < depthArr.getShape()[0]; i++) { if (i != 0) { depthArr.setDouble(i, -depthArr.getDouble(i)); } else { depthArr.setDouble(i, 0); } } } } int[] latshape = ones(lat.getRank()); latshape[latidx] = latlen; latArr = (lat.read(new int[lat.getRank()], latshape)).reduce(); int[] lonshape = ones(lon.getRank()); lonshape[lonidx] = lonlen; lonArr = (lon.read(new int[lon.getRank()], lonshape)).reduce(); ncf_out = NetcdfFileWriteable.createNew(outputFile, false); // Add Dimensions Dimension timeDim = new Dimension(outTimeName, timeArr.getShape()[0]); Dimension depthDim = null; if (depthArr != null) { depthDim = new Dimension(outDepthName, depthArr.getShape()[0]); } Dimension latDim = new Dimension(outLatName, latArr.getShape()[0]); Dimension lonDim = new Dimension(outLonName, lonArr.getShape()[0]); ncf_out.addDimension(null, timeDim); if (depthDim != null) { ncf_out.addDimension(null, depthDim); } ncf_out.addDimension(null, latDim); ncf_out.addDimension(null, lonDim); // Add Variables ncf_out.addVariable(outTimeName, DataType.DOUBLE, new Dimension[] {timeDim}); if (depthDim != null) { ncf_out.addVariable(outDepthName, DataType.DOUBLE, new Dimension[] {depthDim}); } ncf_out.addVariable(outLatName, DataType.DOUBLE, new Dimension[] {latDim}); ncf_out.addVariable(outLonName, DataType.DOUBLE, new Dimension[] {lonDim}); Dimension[] dims = null; if (depthDim == null) { dims = new Dimension[] {timeDim, latDim, lonDim}; } else { dims = new Dimension[] {timeDim, depthDim, latDim, lonDim}; } ncf_out.addVariable(outVarName, DataType.FLOAT, dims); // Add attribute information (cloned from source) cloneAttributes(ncf_in, inTimeName, ncf_out, outTimeName); cloneAttributes(ncf_in, inDepthName, ncf_out, outDepthName); cloneAttributes(ncf_in, inLatName, ncf_out, outLatName); cloneAttributes(ncf_in, inLonName, ncf_out, outLonName); cloneAttributes(ncf_in, inVarName, ncf_out, outVarName); ncf_out.create(); } catch (IOException e) { e.printStackTrace(); } catch (InvalidRangeException e) { e.printStackTrace(); } return ncf_out; }
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"); }
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")); }
@Override public NVariable addVariable( String name, DataType dataType, java.awt.Dimension tileSize, String dims) throws IOException { Variable variable = netcdfFileWriteable.addVariable(name, dataType, dims); return new N3Variable(variable, netcdfFileWriteable); }
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"); }
@Override public NVariable addScalarVariable(String name, DataType dataType) throws IOException { Variable variable = netcdfFileWriteable.addVariable(name, dataType, ""); return new N3Variable(variable, netcdfFileWriteable); }
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(); }
public static void main2(String args[]) throws Exception { final int NLVL = 2; final int NLAT = 6; final int NLON = 12; final int NumberOfRecords = 2; final float SAMPLE_PRESSURE = 900.0f; final float SAMPLE_TEMP = 9.0f; final float START_LAT = 25.0f; final float START_LON = -125.0f; // Create the file. String filename = "pres_temp_4D.nc"; NetcdfFileWriteable dataFile = null; try { // Create new netcdf-3 file with the given filename dataFile = NetcdfFileWriteable.createNew(filename, false); // add dimensions where time dimension is unlimit Dimension lvlDim = dataFile.addDimension("level", NLVL); Dimension latDim = dataFile.addDimension("latitude", NLAT); Dimension lonDim = dataFile.addDimension("longitude", NLON); Dimension timeDim = dataFile.addDimension("time", 1000); // should // not be // need // second // argument ArrayList dims = null; // Define the coordinate variables. dataFile.addVariable("latitude", DataType.FLOAT, new Dimension[] {latDim}); dataFile.addVariable("longitude", DataType.FLOAT, new Dimension[] {lonDim}); // Define units attributes for data variables. dataFile.addVariableAttribute("latitude", "units", "degrees_north"); dataFile.addVariableAttribute("longitude", "units", "degrees_east"); // Define the netCDF variables for the pressure and temperature // data. dims = new ArrayList(); dims.add(timeDim); dims.add(lvlDim); dims.add(latDim); dims.add(lonDim); dataFile.addVariable("pressure", DataType.FLOAT, dims); dataFile.addVariable("temperature", DataType.FLOAT, dims); // Define units attributes for data variables. dataFile.addVariableAttribute("pressure", "units", "hPa"); dataFile.addVariableAttribute("temperature", "units", "celsius"); // Create some pretend data. If this wasn't an example program, we // would have some real data to write for example, model output. ArrayFloat.D1 lats = new ArrayFloat.D1(latDim.getLength()); ArrayFloat.D1 lons = new ArrayFloat.D1(lonDim.getLength()); int i, j; for (i = 0; i < latDim.getLength(); i++) { lats.set(i, START_LAT + 5.f * i); } for (j = 0; j < lonDim.getLength(); j++) { lons.set(j, START_LON + 5.f * j); } // Create the pretend data. This will write our surface pressure and // surface temperature data. ArrayFloat.D4 dataTemp = new ArrayFloat.D4( NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength()); ArrayFloat.D4 dataPres = new ArrayFloat.D4( NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength()); for (int record = 0; record < NumberOfRecords; record++) { i = 0; for (int lvl = 0; lvl < NLVL; lvl++) for (int lat = 0; lat < NLAT; lat++) for (int lon = 0; lon < NLON; lon++) { dataPres.set(record, lvl, lat, lon, SAMPLE_PRESSURE + i); dataTemp.set(record, lvl, lat, lon, SAMPLE_TEMP + i++); } } // Create the file. At this point the (empty) file will be written // to disk dataFile.create(); // A newly created Java integer array to be initialized to zeros. int[] origin = new int[4]; dataFile.write("latitude", lats); dataFile.write("longitude", lons); dataFile.write("pressure", origin, dataPres); dataFile.write("temperature", origin, dataTemp); dataFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } catch (InvalidRangeException e) { e.printStackTrace(System.err); } finally { // if (dataFile != null) { // try { // dataFile.close(); // } catch (IOException ioe) { // ioe.printStackTrace(); // } // } } System.out.println("*** SUCCESS writing example file " + filename); }
private NetcdfFileWriteable writeNetcdfMetadata( NetcdfFileWriteable ncFile, String variableName, ArrayList<ArrayList<VariableEntry>> variableListList) throws IOException { int suffixInt = 1; for (ArrayList<VariableEntry> variableList : variableListList) { ArrayList<Dimension> dims = new ArrayList<Dimension>(); // add coordinate variables for each dimension for (int i = 0; i < variableList.size(); i++) { VariableEntry temp = variableList.get(i); Dimension tempDim; System.out.println("writing metadata for dim " + temp.getVariableName()); if (temp.isUnlimited()) { // see if there is already an unlimited dimension in this filea, // if so, use it. If not, add it // if ( ncFile.hasUnlimitedDimension() ) { if (this._unlimitedDim == null) { tempDim = ncFile.addUnlimitedDimension(temp.getVariableName()); this._unlimitedDim = tempDim; } else { // tempDim = this._unlimitedDim; dims.add(this._unlimitedDim); continue; } } else { tempDim = ncFile.addDimension(temp.getVariableName(), temp.getSize()); } // add a coordinate variable Dimension[] tempDimArray = {tempDim}; ncFile.addVariable(temp.getVariableName(), temp.getType(), tempDimArray); ncFile.addVariableAttribute(temp.getVariableName(), "units", temp.getUnits()); ncFile.addVariableAttribute(temp.getVariableName(), "long_name", temp.getLongName()); dims.add(tempDim); } // add the main variable System.out.println("adding variable " + (variableName + Integer.toString(suffixInt))); ncFile.addVariable(variableName + Integer.toString(suffixInt), DataType.INT, dims); suffixInt++; } /* ArrayList<Dimension> dims = new ArrayList<Dimension>(); Dimension tempDim; String localName = "dim"; for( int i = 0; i < dimensions.length; i++){ if ( (0 == i) && variableDim) { tempDim = ncFile.addUnlimitedDimension("dim" + Integer.toString(i)); } else { tempDim = ncFile.addDimension("dim" + Integer.toString(i), dimensions[i]); } dims.add( tempDim); } ncFile.addVariable(varName, dataType, dims); */ return ncFile; }
/** * Creates the w file * * @param uFile - the input east-west velocity file * @param vFile - the input north-south velocity file * @throws InvalidRangeException * @throws IOException */ private void generate(NetcdfDataset uFile, NetcdfDataset vFile) throws InvalidRangeException, IOException { // Set up empty arrays for ranges and dimensions. dims = new ArrayList<Dimension>(); // Create the output file outfile = NetcdfFileWriteable.createNew(outputWFile, false); // Construct the data set dimensions - Time, Depth, Latitude and // Longitude (in order) int uTimeLength = uFile.findDimension(inTimeName).getLength(); int uLayerLength = uFile.findDimension(inLayerName).getLength(); int uXLength = uFile.findDimension(inXName).getLength(); int uYLength = uFile.findDimension(inYName).getLength(); int vTimeLength = vFile.findDimension(inTimeName).getLength(); int vLayerLength = vFile.findDimension(inLayerName).getLength(); int vXLength = vFile.findDimension(inXName).getLength(); int vYLength = vFile.findDimension(inYName).getLength(); Dimension timeDim = outfile.addDimension(outTimeName, Math.min(uTimeLength, vTimeLength)); Dimension layerDim = outfile.addDimension(outLayerName, Math.min(uLayerLength, vLayerLength)); Dimension yDim = outfile.addDimension(outYName, Math.min(uYLength, vYLength)); Dimension xDim = outfile.addDimension(outXName, Math.min(uXLength, vXLength)); // Add to a list - this becomes the coordinate system for the output // variable. The order is *very* important! dims.add(timeDim); dims.add(layerDim); dims.add(yDim); dims.add(xDim); // Create variables in the output file outfile.addVariable(outTimeName, DataType.DOUBLE, new Dimension[] {timeDim}); outfile.addVariable(outLayerName, DataType.DOUBLE, new Dimension[] {layerDim}); outfile.addVariable(outLatName, DataType.DOUBLE, new Dimension[] {yDim, xDim}); outfile.addVariable(outLonName, DataType.DOUBLE, new Dimension[] {yDim, xDim}); // outfile.setLargeFile(true); outfile.addVariable(outVarName, DataType.FLOAT, dims); // Add attribute information (cloned from source) cloneAttributes(uFile, inTimeName, outfile, outTimeName); cloneAttributes(uFile, inLayerName, outfile, outLayerName); cloneAttributes(uFile, inLatName, outfile, outLatName); cloneAttributes(uFile, inLonName, outfile, outLonName); // Finalizes the structure of the output file, making the changes real. outfile.create(); // Write the static information for 1D axes. outfile.write(outTimeName, uFile.findVariable(inTimeName).read()); outfile.write(outLayerName, uFile.findVariable(inLayerName).read()); outfile.write(outLatName, uFile.findVariable(inLatName).read()); outfile.write(outLonName, uFile.findVariable(inLonName).read()); Variable u = uFile.findVariable(inUName); Variable v = vFile.findVariable(inVName); Variable latdim = uFile.findVariable(inLatName); Variable londim = uFile.findVariable(inLonName); Variable zdim = uFile.findVariable(inLayerName); // Read the depth array Array depth_array = zdim.read(new int[] {0}, new int[] {uLayerLength}); double[] depths; if (depth_array.getClass().getName().contains("Float")) { float[] tmp = (float[]) depth_array.copyTo1DJavaArray(); depths = new double[tmp.length]; for (int i = 0; i < tmp.length; i++) { depths[i] = tmp[i]; } } else { depths = (double[]) depth_array.copyTo1DJavaArray(); } // Determine the size and midpoint of the depth bins double[] binz = new double[depths.length - 1]; float[] mpz = new float[depths.length - 1]; for (int i = 0; i < depths.length - 1; i++) { binz[i] = depths[i + 1] - depths[i]; mpz[i] = (float) (depths[i + 1] + depths[i]) / 2; } float[] nan = new float[uLayerLength]; for (int n = 0; n < uLayerLength; n++) { nan[n] = Float.NaN; } // Write the collar as NaNs. Array nana = Array.factory(java.lang.Float.class, new int[] {1, uLayerLength, 1, 1}, nan); for (int t = 0; t < u.getShape(0); t++) { for (int j = 0; j < u.getShape(3); j++) { outfile.write(outVarName, new int[] {t, 0, 0, j}, nana); outfile.write(outVarName, new int[] {t, 0, u.getShape(2) - 1, j}, nana); } for (int i = 1; i < u.getShape(2) - 1; i++) { outfile.write(outVarName, new int[] {t, 0, i, 0}, nana); outfile.write(outVarName, new int[] {t, 0, i, u.getShape(3) - 1}, nana); } } // Calculate w values for each time, depth, lat and lon // using 3x3 (horizontal) vertical pencils for (int t = 0; t < u.getShape(0); t++) { for (int i = 1; i < u.getShape(2) - 1; i++) { Array lta = latdim .read( new int[] {i - 1, 0}, // move for // speed new int[] {3, 1}) .reduce(); double currentLat = lta.getDouble(1); for (int j = 1; j < u.getShape(3) - 1; j++) { float[] w_arr = new float[uLayerLength]; if (i == 0 || j == 0 || i == u.getShape(2) - 1 || j == u.getShape(3) - 1) { for (int n = 0; n < w_arr.length; n++) { w_arr[n] = Float.NaN; } continue; } Array lna = londim.read(new int[] {0, j - 1}, new int[] {1, 3}).reduce(); double currentLon = lna.getDouble(1); float maxZ = bathy.value_at_lonlat((float) currentLon, (float) currentLat); boolean alwaysNaN = true; for (int k = u.getShape(1) - 1; k >= 0; k--) { // If the bathymetry says we're on land, then all values // will be NaN. This type of check is required because // the value does not equal java.Float.NaN, but // 1.2676506E30. if (maxZ > bathy_cutoff) { w_arr[k] = Float.NaN; continue; } if (k == 0) { if (!alwaysNaN) { w_arr[k] = 0; } else { w_arr[k] = Float.NaN; } continue; } if (maxZ < mpz[k - 1]) { w_arr[k] = Float.NaN; continue; } Array ua = u.read(new int[] {t, k, i - 1, j - 1}, new int[] {1, 1, 3, 3}).reduce(); Array va = v.read(new int[] {t, k, i - 1, j - 1}, new int[] {1, 1, 3, 3}).reduce(); float dz = calcdz(k, mpz, maxZ); if (Float.isNaN(dz)) { w_arr[k] = dz; continue; } float dwdz = calcdwdz(lta, lna, ua, va); if (Float.isNaN(dwdz)) { continue; } if (alwaysNaN) { w_arr[k] = dwdz; alwaysNaN = false; } else { w_arr[k] = w_arr[k + 1] + dwdz; } } Array warra = Array.factory(java.lang.Float.class, new int[] {1, uLayerLength, 1, 1}, w_arr); outfile.write(outVarName, new int[] {t, 0, i, j}, warra); } System.out.println("\tRow " + i + " complete."); } System.out.printf("Time %d of " + (u.getShape(0)) + " is complete.\n", t + 1); } }