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 static void main(String args[]) throws Exception { long start = System.currentTimeMillis(); Map<String, ucar.unidata.geoloc.Station> staHash = new HashMap<String, ucar.unidata.geoloc.Station>(); String location = "R:/testdata/sounding/netcdf/Upperair_20070401_0000.nc"; NetcdfDataset ncfile = NetcdfDataset.openDataset(location); ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); // look through record varibles, for those that have "manLevel" dimension // make a StructureData object for those StructureMembers sm = new StructureMembers("manLevel"); Dimension manDim = ncfile.findDimension("manLevel"); Structure record = (Structure) ncfile.findVariable("record"); List<Variable> allList = record.getVariables(); List<VariableSimpleIF> varList = new ArrayList<VariableSimpleIF>(); for (Variable v : allList) { if ((v.getRank() == 1) && v.getDimension(0).equals(manDim)) { // public VariableDS(NetcdfDataset ds, Group group, Structure parentStructure, String // shortName, DataType dataType, // String dims, String units, String desc) { varList.add( new VariableDS( ncfile, null, null, v.getShortName(), v.getDataType(), "", v.getUnitsString(), v.getDescription())); // (String name, String desc, String units, DataType dtype, int []shape) sm.addMember( v.getShortName(), v.getDescription(), v.getUnitsString(), v.getDataType(), new int[0]); // scalar } } ArrayStructureMA manAS = new ArrayStructureMA(sm, new int[] {manDim.getLength()}); // need the date units Variable time = ncfile.findVariable("synTime"); String timeUnits = ncfile.findAttValueIgnoreCase(time, "units", null); timeUnits = StringUtil.remove(timeUnits, '('); // crappy fsl'ism timeUnits = StringUtil.remove(timeUnits, ')'); DateUnit timeUnit = new DateUnit(timeUnits); // extract stations int nrecs = 0; StructureDataIterator iter = record.getStructureIterator(); while (iter.hasNext()) { StructureData sdata = iter.next(); String name = sdata.getScalarString("staName"); ucar.unidata.geoloc.Station s = staHash.get(name); if (s == null) { float lat = sdata.convertScalarFloat("staLat"); float lon = sdata.convertScalarFloat("staLon"); float elev = sdata.convertScalarFloat("staElev"); s = new StationImpl(name, "", lat, lon, elev); staHash.put(name, s); } nrecs++; } List<ucar.unidata.geoloc.Station> stnList = Arrays.asList(staHash.values().toArray(new ucar.unidata.geoloc.Station[staHash.size()])); Collections.sort(stnList); // create the writer WriterProfileObsDataset writer = new WriterProfileObsDataset(location + ".out", "rewrite " + location); writer.writeHeader(stnList, varList, nrecs, "prMan"); // extract records iter = record.getStructureIterator(); while (iter.hasNext()) { StructureData sdata = iter.next(); String name = sdata.getScalarString("staName"); double timeValue = sdata.convertScalarDouble("synTime"); Date date = timeUnit.makeDate(timeValue); // transfer to the ArrayStructure List<String> names = sm.getMemberNames(); for (String mname : names) { manAS.setMemberArray(mname, sdata.getArray(mname)); } // each level is weritten as a seperate structure int numMand = sdata.getScalarInt("numMand"); if (numMand >= manDim.getLength()) continue; for (int i = 0; i < numMand; i++) { StructureData useData = manAS.getStructureData(i); writer.writeRecord(name, date, useData); } } writer.finish(); long took = System.currentTimeMillis() - start; System.out.println("That took = " + took); }
public static boolean isValidFile(NetcdfDataset ds) { // Check that has a time dimension and a trajectory dimension. List list = ds.getRootGroup().getDimensions(); if (list.size() != 2) return (false); Dimension d; for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } // Check that has a trajectory coordinate variable. Variable var = ds.getRootGroup().findVariable(trajVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 1) return (false); d = (Dimension) list.get(0); if (!d.getShortName().equals(trajDimNameDefault)) return (false); // Check that has a time coordinate variable with units that are udunits time var = ds.getRootGroup().findVariable(timeVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 1) return (false); d = (Dimension) list.get(0); if (!d.getShortName().equals(timeDimNameDefault)) return (false); String units = var.findAttribute("units").getStringValue(); Date date = DateUnit.getStandardDate("0 " + units); if (date == null) return (false); // Check for variable latitude(time) with units of "deg". var = ds.getRootGroup().findVariable(latVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 2) return (false); for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } // units = var.findAttribute( "units").getStringValue(); // if ( ! SimpleUnit.isCompatible( units, "degrees_north")) return( false); // Check for variable longitude(time) with units of "deg". var = ds.getRootGroup().findVariable(lonVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 2) return (false); for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } // units = var.findAttribute( "units").getStringValue(); // if ( ! SimpleUnit.isCompatible( units, "degrees_east")) return( false); // Check for variable altitude(time) with units of "m". var = ds.getRootGroup().findVariable(elevVarNameDefault); if (var == null) return (false); list = var.getDimensions(); if (list.size() != 2) return (false); for (int i = 0; i < 2; i++) { d = (Dimension) list.get(i); if (!d.getShortName().equals(timeDimNameDefault) && !d.getShortName().equals(trajDimNameDefault)) return (false); } units = var.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(units, "m")) return (false); return (true); }