public List<VariableBean> getStructureVariables(Structure s) { List<VariableBean> vlist = new ArrayList<VariableBean>(); for (Variable v : s.getVariables()) { vlist.add(new VariableBean(v)); } return vlist; }
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); }
// create from a dataset public ObsBean(Structure obs, StructureData sdata) { // first choice for (Variable v : obs.getVariables()) { Attribute att = v.findAttribute("BUFR:TableB_descriptor"); if (att == null) continue; String val = att.getStringValue(); if (val.equals("0-5-1") && Double.isNaN(lat)) { lat = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-6-1") && Double.isNaN(lon)) { lon = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-7-30") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-4-1") && (year < 0)) { year = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-2") && (month < 0)) { month = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-3") && (day < 0)) { day = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-4") && (hour < 0)) { hour = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-5") && (minute < 0)) { minute = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-4-6") && (sec < 0)) { sec = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-1-1") && (wmo_block < 0)) { wmo_block = sdata.convertScalarInt(v.getShortName()); } else if (val.equals("0-1-2") && (wmo_id < 0)) { wmo_id = sdata.convertScalarInt(v.getShortName()); } else if ((stn == null) && (val.equals("0-1-7") || val.equals("0-1-194") || val.equals("0-1-11") || val.equals("0-1-18"))) { if (v.getDataType().isString()) stn = sdata.getScalarString(v.getShortName()); else stn = Integer.toString(sdata.convertScalarInt(v.getShortName())); } } // second choice for (Variable v : obs.getVariables()) { Attribute att = v.findAttribute("BUFR:TableB_descriptor"); if (att == null) continue; String val = att.getStringValue(); if (val.equals("0-5-2") && Double.isNaN(lat)) { lat = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-6-2") && Double.isNaN(lon)) { lon = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-7-1") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } else if ((val.equals("0-4-7")) && (sec < 0)) { sec = sdata.convertScalarInt(v.getShortName()); } } // third choice for (Variable v : obs.getVariables()) { Attribute att = v.findAttribute("BUFR:TableB_descriptor"); if (att == null) continue; String val = att.getStringValue(); if (val.equals("0-7-10") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } else if (val.equals("0-7-2") && Double.isNaN(alt)) { alt = sdata.convertScalarDouble(v.getShortName()); } } }