示例#1
1
  public Map<Band, Variable> addBands(
      Product product, Variable idxVariable, List<Variable> l3ProdVars) {

    final Structure binListStruc = (Structure) idxVariable;

    final Map<Band, Variable> bandToVariableMap = new HashMap<Band, Variable>();

    //        bandToVariableMap.put(addBand(product, "bin_num", ProductData.TYPE_UINT32),
    // binListStruc.select("bin_num").findVariable("bin_num"));
    bandToVariableMap.put(
        addBand(product, "weights", ProductData.TYPE_FLOAT32),
        binListStruc.select("weights").findVariable("weights"));
    bandToVariableMap.put(
        addBand(product, "nobs", ProductData.TYPE_UINT16),
        binListStruc.select("nobs").findVariable("nobs"));
    bandToVariableMap.put(
        addBand(product, "nscenes", ProductData.TYPE_UINT16),
        binListStruc.select("nscenes").findVariable("nscenes"));
    //        ncFile.getRootGroup().findGroup("Level-3 Binned Data").findVariable("BinList");
    if (ncFile.getRootGroup().findGroup("Level-3_Binned_Data").findVariable("qual_l3") != null) {
      bandToVariableMap.put(
          addBand(product, "qual_l3", ProductData.TYPE_UINT8),
          ncFile.getRootGroup().findGroup("Level-3_Binned_Data").findVariable("qual_l3"));
    }
    String groupnames = "";
    for (Variable l3Var : l3ProdVars) {
      String varName = l3Var.getShortName();
      final int dataType = ProductData.TYPE_FLOAT32;

      if (!varName.contains("Bin")
          && (!varName.startsWith("qual"))
          && (!varName.equalsIgnoreCase("SEAGrid"))
          && (!varName.equalsIgnoreCase("Input_Files"))) {
        final Structure binStruc = (Structure) l3Var;
        if (groupnames.length() == 0) {
          groupnames = varName;
        } else {
          groupnames = groupnames + ":" + varName;
        }

        List<String> vnames = binStruc.getVariableNames();
        for (String bandvar : vnames) {
          bandToVariableMap.put(
              addBand(product, bandvar, dataType), binStruc.select(bandvar).findVariable(bandvar));
        }
        // Add virtual band for product mean
        StringBuilder prodname = new StringBuilder(varName);
        prodname.append("_mean");

        String calcmean = ComputeBinMeans(varName);
        Band varmean =
            new VirtualBand(
                prodname.toString(),
                ProductData.TYPE_FLOAT32,
                product.getSceneRasterWidth(),
                product.getSceneRasterHeight(),
                calcmean);
        varmean.setNoDataValue(Double.NaN);
        varmean.setNoDataValueUsed(true);
        product.addBand(varmean);

        // Add virtual band for product stdev
        int underscore = prodname.indexOf("_mean");
        prodname.delete(underscore, underscore + 5);
        prodname.append("_stdev");

        String calcstdev = ComputeBinVariances(varName);

        Band varstdev =
            new VirtualBand(
                prodname.toString(),
                ProductData.TYPE_FLOAT32,
                product.getSceneRasterWidth(),
                product.getSceneRasterHeight(),
                calcstdev);
        varstdev.setNoDataValue(Double.NaN);
        varstdev.setNoDataValueUsed(true);

        product.addBand(varstdev);
      }
    }
    product.setAutoGrouping(groupnames);
    return bandToVariableMap;
  }
示例#2
0
  private void doVariable(Variable v, opendap.dap.AttributeTable parentTable) {

    List dims = v.getDimensions();
    for (int i = 0; i < dims.size(); i++) {
      Dimension dim = (Dimension) dims.get(i);
      if (dim.isShared()) usedDims.put(dim.getName(), dim);
    }

    // if (v.getAttributes().size() == 0) return; // LOOK DAP 2 say must have empty

    String name = NcDDS.escapeName(v.getShortName());
    opendap.dap.AttributeTable table;

    if (parentTable == null) {
      table = new opendap.dap.AttributeTable(name);
      try {
        addAttributeTable(name, table);
      } catch (AttributeExistsException e) {
        log.error("Cant add " + name, e);
      }
    } else {
      table = parentTable.appendContainer(name);
    }

    addAttributes(table, v, v.getAttributes().iterator());

    if (v instanceof Structure) {
      Structure s = (Structure) v;
      List nested = s.getVariables();
      for (int i = 0; i < nested.size(); i++) {
        Variable nv = (Variable) nested.get(i);
        doVariable(nv, table);
      }
    }
  }
    public Component getTreeCellRendererComponent(
        JTree tree,
        Object value,
        boolean selected,
        boolean expanded,
        boolean leaf,
        int row,
        boolean hasFocus) {

      Component c =
          super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

      if (value instanceof VariableNode) {
        VariableNode node = (VariableNode) value;
        tooltipText = node.getToolTipText();

        if (node.var instanceof Structure) {
          Structure s = (Structure) node.var;
          setIcon(structIcon);
          tooltipText = s.getNameAndAttributes();
        } else tooltipText = node.getToolTipText();
      } else if (value instanceof DimensionNode) {
        DimensionNode node = (DimensionNode) value;
        tooltipText = node.getToolTipText();
        setIcon(dimIcon);
      } else if (value instanceof GroupNode) {
        GroupNode node = (GroupNode) value;
        tooltipText = node.getToolTipText();
      }

      return c;
    }
  /*
   Structure {
    int a_name;
    byte b_name(3);
    byte c_name(3);
    short d_name(3);
    int e_name(3);
    long f_name(3);
    int g_name(3);
    short h_name(3);
    int i_name(3);
    long j_name(3);
    float k_name(3);
    double l_name(3);
  } CompoundNative(15);
      type = Layout(8);  type= 1 (contiguous) storageSize = (15,144) dataSize=0 dataAddress=2048
  */
  @Test
  public void testReadH5StructureArrayMembers() throws java.io.IOException {
    try (NetcdfFile ncfile = TestH5.openH5("complex/compound_native.h5")) {

      Variable dset = ncfile.findVariable("CompoundNative");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 15);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 15);

      Structure s = (Structure) dset;

      // read all with the iterator
      StructureDataIterator iter = s.getStructureIterator();
      while (iter.hasNext()) {
        StructureData sd = iter.next();

        for (StructureMembers.Member m : sd.getMembers()) {
          Array data = sd.getArray(m);
          NCdumpW.printArray(data, m.getName(), out, null);
        }
      }
    }
    System.out.println("*** testReadH5StructureArrayMembers ok");
  }
  /* Structure {
      int LAT[0];
      ...
      int LAT[149];
  } IMAGE_LAT_ARRAY(3600);
     type = Layout(8);  type= 2 (chunked) storageSize = (1,600) dataSize=0 dataAddress=2548046
  */
  @Test
  public void testReadOneAtATime() throws java.io.IOException, InvalidRangeException {
    try (NetcdfFile ncfile = TestH5.openH5("IASI/IASI.h5")) {

      Variable dset = ncfile.findVariable("U-MARF/EPS/IASI_xxx_1C/DATA/IMAGE_LAT_ARRAY");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 3600);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 3600);

      Structure s = (Structure) dset;

      // read last one - chunked
      StructureData sd = s.readStructure(3599);
      assert sd.getScalarInt("LAT[0]") == 70862722;
      assert sd.getScalarInt("LAT[149]") == 85302263;

      // read one at a time
      for (int i = 3590; i < d.getLength(); i++) {
        s.readStructure(i);
        System.out.println(" read structure " + i);
      }
    }
    System.out.println("*** testReadIASI ok");
  }
示例#6
0
  public boolean compareVariables(
      Variable org, Variable copy, boolean compareData, boolean justOne) {
    boolean ok = true;

    if (showCompare)
      f.format("compare Variable %s to %s %n", org.getFullName(), copy.getFullName());
    if (!org.getFullName().equals(copy.getFullName())) {
      f.format(" ** names are different %s != %s %n", org.getFullName(), copy.getFullName());
      ok = false;
    }

    // dimensions
    ok &= checkAll(org.getDimensions(), copy.getDimensions(), null);

    // attributes
    ok &= checkAll(org.getAttributes(), copy.getAttributes(), null);

    // coord sys
    if ((org instanceof VariableEnhanced) && (copy instanceof VariableEnhanced)) {
      VariableEnhanced orge = (VariableEnhanced) org;
      VariableEnhanced copye = (VariableEnhanced) copy;
      ok &= checkAll(orge.getCoordinateSystems(), copye.getCoordinateSystems(), null);
    }

    // data !!
    if (compareData) {
      try {
        compareVariableData(org, copy, showCompare, justOne);

      } catch (IOException e) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(10000);
        e.printStackTrace(new PrintStream(bos));
        f.format("%s", bos.toString());
      }
    }

    // nested variables
    if (org instanceof Structure) {
      if (!(copy instanceof Structure)) {
        f.format("  ** %s not Structure%n", org);
        ok = false;

      } else {
        Structure orgS = (Structure) org;
        Structure ncmlS = (Structure) copy;

        List vars = new ArrayList();
        ok &= checkAll(orgS.getVariables(), ncmlS.getVariables(), vars);
        for (int i = 0; i < vars.size(); i += 2) {
          Variable orgV = (Variable) vars.get(i);
          Variable ncmlV = (Variable) vars.get(i + 1);
          ok &= compareVariables(orgV, ncmlV, false, true);
        }
      }
    }

    return ok;
  }
    void makeChildren() {
      children = new ArrayList<>();

      if (var instanceof Structure) {
        Structure s = (Structure) var;
        List vars = s.getVariables();
        for (int i = 0; i < vars.size(); i++)
          children.add(new VariableNode(this, (VariableIF) vars.get(i)));
      }

      if (debugTree) System.out.println("children=" + var.getShortName() + " ");
    }
示例#8
0
  private RowInfo[] createRowInfos() throws IOException {
    final ISINGrid grid = this.grid;
    final RowInfo[] binLines = new RowInfo[sceneHeight];
    final Variable idxVariable =
        ncFile.getRootGroup().findGroup("Level-3_Binned_Data").findVariable("BinList");
    final Structure idxStructure = (Structure) idxVariable;
    final Variable idx = idxStructure.findVariable("bin_num");
    final int[] idxValues;
    synchronized (ncFile) {
      idxValues = (int[]) idx.read().getStorage();
    }
    if (bins == null) {
      bins = idxValues; // (int[]) idxVariable.read().copyTo1DJavaArray();
    }
    final Point gridPoint = new Point();
    int lastBinIndex = -1;
    int lastRowIndex = -1;
    int lineOffset = 0;
    int lineLength = 0;
    for (int i = 0; i < idxValues.length; i++) {

      final int binIndex = idxValues[i];
      if (binIndex < lastBinIndex) {
        throw new IOException(
            "Unrecognized level-3 format. Bins numbers expected to appear in ascending order.");
      }
      lastBinIndex = binIndex;

      grid.getGridPoint(binIndex, gridPoint);
      final int rowIndex = gridPoint.y;

      if (rowIndex != lastRowIndex) {
        if (lineLength > 0) {
          binLines[lastRowIndex] = new RowInfo(lineOffset, lineLength);
        }
        lineOffset = i;
        lineLength = 0;
      }

      lineLength++;
      lastRowIndex = rowIndex;
    }

    if (lineLength > 0) {
      binLines[lastRowIndex] = new RowInfo(lineOffset, lineLength);
    }

    return binLines;
  }
示例#9
0
 /**
  * Make a structure for the part
  *
  * @param partName partname
  * @param dimensions dimensions for the structure
  * @param includeMissing true to include the missing variable
  * @return a Structure
  */
 protected Structure makeStructure(String partName, List dimensions, boolean includeMissing) {
   List<GempakParameter> params = gemreader.getParameters(partName);
   if (params == null) {
     return null;
   }
   Structure sVar = new Structure(ncfile, null, null, partName);
   sVar.setDimensions(dimensions);
   for (GempakParameter param : params) {
     sVar.addMemberVariable(makeParamVariable(param, null));
   }
   if (includeMissing) {
     sVar.addMemberVariable(makeMissingVariable());
   }
   return sVar;
 }
示例#10
0
  private int countSeq(Structure recordStruct) throws IOException {
    int total = 0;
    int count = 0;
    int max = 0;

    StructureDataIterator iter = recordStruct.getStructureIterator();
    try {
      while (iter.hasNext()) {

        StructureData sdata = iter.next();
        ArraySequence seq1 = sdata.getArraySequence("seq1");
        int n = seq1.getStructureDataCount();
        total += n;
        count++;
        max = Math.max(max, n);
      }
    } finally {
      iter.finish();
    }
    double avg = total / count;
    int wasted = count * max - total;
    double wp = (double) wasted / (count * max);
    System.out.println(" Max = " + max + " avg = " + avg + " wasted = " + wasted + " %= " + wp);
    return max;
  }
示例#11
0
 public List<VariableBean> getStructureVariables(Structure s) {
   List<VariableBean> vlist = new ArrayList<VariableBean>();
   for (Variable v : s.getVariables()) {
     vlist.add(new VariableBean(v));
   }
   return vlist;
 }
  @Test
  public void testH5StructureDS() throws java.io.IOException {
    int a_name = 0;
    String[] b_name =
        new String[] {
          "A fight is a contract that takes two people to honor.",
          "A combative stance means that you've accepted the contract.",
          "In which case, you deserve what you get.",
          "  --  Professor Cheng Man-ch'ing"
        };
    String c_name = "Hello!";

    // H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header"));
    try (NetcdfDataset ncfile =
        NetcdfDataset.openDataset(TestH5.testDir + "complex/compound_complex.h5")) {

      Variable dset = ncfile.findVariable("CompoundComplex");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 6);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 6);

      Structure s = (Structure) dset;

      // read all with the iterator
      StructureDataIterator iter = s.getStructureIterator();
      while (iter.hasNext()) {
        StructureData sd = iter.next();
        assert sd.getScalarInt("a_name") == a_name;
        a_name++;
        assert sd.getScalarString("c_name").equals(c_name);
        String[] results = sd.getJavaArrayString(sd.findMember("b_name"));
        assert results.length == b_name.length;
        int count = 0;
        for (String r : results) assert r.equals(b_name[count++]);

        for (StructureMembers.Member m : sd.getMembers()) {
          Array data = sd.getArray(m);
          NCdumpW.printArray(data, m.getName(), out, null);
        }
      }
    }
    System.out.println("*** testH5StructureDS ok");
  }
  private void setObs(Message m) {

    java.util.List<ObsBean> beanList = new ArrayList<ObsBean>();
    try {
      NetcdfDataset ncd = getBufrMessageAsDataset(m);
      Variable v = ncd.findVariable(BufrIosp.obsRecord);
      if ((v != null) && (v instanceof Structure)) {
        Structure obs = (Structure) v;
        StructureDataIterator iter = obs.getStructureIterator();
        while (iter.hasNext()) {
          beanList.add(new ObsBean(obs, iter.next()));
        }
      }
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(BufrMessageViewer.this, ex.getMessage());
      ex.printStackTrace();
    }
    obsTable.setBeans(beanList);
  }
示例#14
0
  private double copyVarData(NetcdfFileWriteable ncfile, Structure recordStruct)
      throws IOException, InvalidRangeException {
    int nrecs = (int) recordStruct.getSize();
    int sdataSize = recordStruct.getElementSize();

    double total = 0;
    double totalRecordBytes = 0;
    for (int count = 0; count < nrecs; count++) {

      StructureData recordData = recordStruct.readStructure(count);
      for (StructureMembers.Member m : recordData.getMembers()) {

        if (m.getDataType() == DataType.STRUCTURE) {
          int countLevel = 0;
          ArrayStructure seq1 = recordData.getArrayStructure(m);
          StructureDataIterator iter = seq1.getStructureDataIterator();
          try {
            while (iter.hasNext()) {
              StructureData seqData = iter.next();
              for (StructureMembers.Member seqm : seqData.getMembers()) {
                Array data = seqData.getArray(seqm);
                int[] shape = data.getShape();
                int[] newShape = new int[data.getRank() + 2];
                newShape[0] = 1;
                newShape[1] = 1;
                for (int i = 0; i < data.getRank(); i++) newShape[i + 1] = shape[i];

                int[] origin = new int[data.getRank() + 2];
                origin[0] = count;
                origin[1] = countLevel;

                String mname = seqm.getName() + "-" + m.getName();
                if (debug && (count == 0) && (countLevel == 0))
                  System.out.println("write to = " + mname);
                ncfile.write(mname, origin, data.reshape(newShape));
              }
              countLevel++;
            }
          } finally {
            iter.finish();
          }
        } else {

          Array data = recordData.getArray(m);
          int[] shape = data.getShape();
          int[] newShape = new int[data.getRank() + 1];
          newShape[0] = 1;
          for (int i = 0; i < data.getRank(); i++) newShape[i + 1] = shape[i];

          int[] origin = new int[data.getRank() + 1];
          origin[0] = count;

          if (debug && (count == 0)) System.out.println("write to = " + m.getName());
          ncfile.write(m.getName(), origin, data.reshape(newShape));
        }
      }
      totalRecordBytes += sdataSize;
    }

    total += totalRecordBytes;
    totalRecordBytes /= 1000 * 1000;
    if (debug)
      System.out.println(
          "write record var; total = " + totalRecordBytes + " Mbytes # recs=" + nrecs);

    return total;
  }
    // 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());
        }
      }
    }
  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);
  }
示例#17
0
  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();
  }