public void writeRecord(String stnName, Date obsDate, StructureData sdata) throws IOException {
    StationTracker stnTracker = stationMap.get(stnName);
    ProfileTracker proTracker = stnTracker.profileMap.get(obsDate);

    if (proTracker == null) {
      proTracker = new ProfileTracker(profileIndex);
      stnTracker.profileMap.put(obsDate, proTracker);

      stnTracker.link.add(profileIndex);
      stnTracker.lastChild = profileIndex;
      stnTracker.numChildren++;

      try {
        originTime[0] = profileIndex; // 2d index
        timeArray.set(0, dateFormatter.toDateTimeStringISO(obsDate));
        parentArray.set(0, stnTracker.parent_index);
        ncfile.writeStringData(timeName, originTime, timeArray);
        ncfile.write(parentStationIndex, originTime, parentArray);
      } catch (InvalidRangeException e) {
        e.printStackTrace();
        throw new IllegalStateException(e);
      }

      profileIndex++;
    }

    // needs to be wrapped as an ArrayStructure, even though we are only writing one at a time.
    ArrayStructureW sArray = new ArrayStructureW(sdata.getStructureMembers(), new int[] {1});
    sArray.setStructureData(sdata, 0);

    // track the min and max date
    if ((minDate == null) || minDate.after(obsDate)) minDate = obsDate;
    if ((maxDate == null) || maxDate.before(obsDate)) maxDate = obsDate;

    timeArray.set(0, dateFormatter.toDateTimeStringISO(obsDate));
    parentArray.set(0, proTracker.parent_index);

    proTracker.link.add(recno);
    proTracker.lastChild = recno; // not currently used
    proTracker.numChildren++;

    // write the recno record
    origin[0] = recno; // 1d index
    originTime[0] = recno; // 2d index
    try {
      ncfile.write("record", origin, sArray);
      ncfile.write(parentProfileIndex, originTime, parentArray);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }

    recno++;
  }
예제 #2
0
  private void writeCoordinateVariable(VariableEntry variable, NetcdfFileWriteable ncFile)
      throws IOException, InvalidRangeException {

    int[] dimensions = {variable.getSize()};
    Array array;

    // so far, everything is a float or an int
    // way too much code duplication but I'm done fightin java for now
    if (variable.getType() == DataType.INT) {
      array = new ArrayInt(dimensions);
      int tempInt = 0;
      IndexIterator iter = array.getIndexIterator();

      while (iter.hasNext()) {
        iter.getIntNext();
        iter.setIntCurrent(tempInt);
        tempInt++;
      }

      ncFile.write(variable.getVariableName(), array);
    } else if (variable.getType() == DataType.FLOAT) {
      array = new ArrayFloat(dimensions);
      float tempFloat = 0;
      IndexIterator iter = array.getIndexIterator();

      while (iter.hasNext()) {
        iter.getFloatNext();
        iter.setFloatCurrent(tempFloat);
        tempFloat++;
      }

      ncFile.write(variable.getVariableName(), array);
    } else if (variable.getType() == DataType.LONG) {
      array = new ArrayLong(dimensions);
      long tempLong = 0;
      IndexIterator iter = array.getIndexIterator();

      while (iter.hasNext()) {
        iter.getLongNext();
        iter.setLongCurrent(tempLong);
        tempLong++;
      }

      ncFile.write(variable.getVariableName(), array);
    }

    // ncFile.write(variable.getVariableName(),array);

  }
예제 #3
0
  private void copySome(NetcdfFileWriteable ncfile, Variable oldVar, int nelems)
      throws IOException {
    String newName = N3iosp.makeValidNetcdfObjectName(oldVar.getShortName());

    int[] shape = oldVar.getShape();
    int[] origin = new int[oldVar.getRank()];
    int size = shape[0];

    for (int i = 0; i < size; i += nelems) {
      origin[0] = i;
      int left = size - i;
      shape[0] = Math.min(nelems, left);

      Array data;
      try {
        data = oldVar.read(origin, shape);
        if (oldVar.getDataType() == DataType.STRING) {
          data = convertToChar(ncfile.findVariable(newName), data);
        }
        if (data.getSize() > 0) { // zero when record dimension = 0
          ncfile.write(newName, origin, data);
          if (debug) System.out.println("write " + data.getSize() + " bytes");
        }

      } catch (InvalidRangeException e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
      }
    }
  }
  private void writeStationData(List<ucar.unidata.geoloc.Station> stnList) throws IOException {
    this.stnList = stnList;
    int nstns = stnList.size();
    stationMap = new HashMap<String, StationTracker>(2 * nstns);
    if (debug) System.out.println("stationMap created");

    // now write the station data
    ArrayDouble.D1 latArray = new ArrayDouble.D1(nstns);
    ArrayDouble.D1 lonArray = new ArrayDouble.D1(nstns);
    ArrayDouble.D1 altArray = new ArrayDouble.D1(nstns);
    ArrayObject.D1 idArray = new ArrayObject.D1(String.class, nstns);
    ArrayObject.D1 descArray = new ArrayObject.D1(String.class, nstns);
    ArrayObject.D1 wmoArray = new ArrayObject.D1(String.class, nstns);

    for (int i = 0; i < stnList.size(); i++) {
      ucar.unidata.geoloc.Station stn = stnList.get(i);
      stationMap.put(stn.getName(), new StationTracker(i));

      latArray.set(i, stn.getLatitude());
      lonArray.set(i, stn.getLongitude());
      if (useAlt) altArray.set(i, stn.getAltitude());

      idArray.set(i, stn.getName());
      descArray.set(i, stn.getDescription());
      if (useWmoId) wmoArray.set(i, stn.getWmoId());
    }

    try {
      ncfile.write(latName, latArray);
      ncfile.write(lonName, lonArray);
      if (useAlt) ncfile.write(altName, altArray);
      ncfile.writeStringData(idName, idArray);
      ncfile.writeStringData(descName, descArray);
      if (useWmoId) ncfile.writeStringData(wmoName, wmoArray);

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }
  }
예제 #5
0
  /**
   * Copies the contents of single NetCDF files into a block file
   *
   * @param map
   * @param ncf_out
   */
  private void copyContent(TreeMap<Long, NetcdfFile> map, NetcdfFileWriteable ncf_out) {
    try {

      // Write the static information for 1D axes.

      ncf_out.write(outTimeName, timeArr);
      ncf_out.write(outDepthName, depthArr);
      ncf_out.write(outLatName, latArr);
      ncf_out.write(outLonName, lonArr);

      Iterator<Long> it = map.keySet().iterator();
      int ct = 0;

      while (it.hasNext()) {
        long time = it.next();
        System.out.print("\t" + ct + "\tWorking on " + new Date(time));
        NetcdfFile copyfile = map.get(time);
        int timesteps = copyfile.findDimension(inTimeName).getLength();
        Variable invar = copyfile.findVariable(inVarName);

        int[] shape = invar.getShape();
        shape[0] = 1;

        for (int i = 0; i < timesteps; i++) {
          int[] origin_in = new int[] {i, 0, 0, 0};
          int[] origin_out = new int[] {ct, 0, 0, 0};
          ncf_out.write(outVarName, origin_out, invar.read(origin_in, shape));
          ct++;
        }
        System.out.println("\tDone.");
      }

    } catch (IOException e) {
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      e.printStackTrace();
    }
  }
예제 #6
0
  private void doWrite2(NetcdfFileWriteable ncfile, String varName) throws Exception {
    Variable v = ncfile.findVariable(varName);
    int[] w = getWeights(v);

    int[] shape = v.getShape();
    Array aa = Array.factory(v.getDataType().getPrimitiveClassType(), shape);
    Index ima = aa.getIndex();
    for (int i = 0; i < shape[0]; i++) {
      for (int j = 0; j < shape[1]; j++) {
        aa.setDouble(ima.set(i, j), (double) (i * w[0] + j * w[1]));
      }
    }

    ncfile.write(varName, aa);
  }
예제 #7
0
  private void copyAll(NetcdfFileWriteable ncfile, Variable oldVar) throws IOException {
    String newName = N3iosp.makeValidNetcdfObjectName(oldVar.getShortName());

    Array data = oldVar.read();
    try {
      if (oldVar.getDataType() == DataType.STRING) {
        data = convertToChar(ncfile.findVariable(newName), data);
      }
      if (data.getSize() > 0) // zero when record dimension = 0
      ncfile.write(newName, data);

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IOException(e.getMessage() + " for Variable " + oldVar.getFullName());
    }
  }
  private void writeDataFinish() throws IOException {
    // finish global variables
    ArrayInt.D0 totalArray = new ArrayInt.D0();
    totalArray.set(profileIndex);
    try {
      ncfile.write(numProfilesTotalName, totalArray);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }

    // finish the station data
    int nstns = stnList.size();
    ArrayInt.D1 firstProfileArray = new ArrayInt.D1(nstns);
    ArrayInt.D1 numProfileArray = new ArrayInt.D1(nstns);
    ArrayInt.D1 nextProfileArray = new ArrayInt.D1(nprofiles);

    for (int i = 0; i < stnList.size(); i++) {
      ucar.unidata.geoloc.Station stn = stnList.get(i);
      StationTracker tracker = stationMap.get(stn.getName());

      numProfileArray.set(i, tracker.numChildren);

      int first = (tracker.link.size() > 0) ? tracker.link.get(0) : -1;
      firstProfileArray.set(i, first);

      if (tracker.link.size() > 0) {
        // construct forward link
        List<Integer> nextList = tracker.link;
        for (int j = 0; j < nextList.size() - 1; j++) {
          Integer curr = nextList.get(j);
          Integer next = nextList.get(j + 1);
          nextProfileArray.set(curr, next);
        }
        Integer curr = nextList.get(nextList.size() - 1);
        nextProfileArray.set(curr, -1);
      }
    }

    try {
      ncfile.write(firstProfileName, firstProfileArray);
      ncfile.write(numProfilesName, numProfileArray);
      ncfile.write(nextProfileName, nextProfileArray);

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }

    // finish the profile data
    ArrayInt.D1 nextObsArray = new ArrayInt.D1(recno);
    ArrayInt.D1 firstObsArray = new ArrayInt.D1(nprofiles);
    ArrayInt.D1 numObsArray = new ArrayInt.D1(nprofiles);

    for (int i = 0; i < stnList.size(); i++) {
      ucar.unidata.geoloc.Station stn = stnList.get(i);
      StationTracker stnTracker = stationMap.get(stn.getName());

      Set<Date> dates = stnTracker.profileMap.keySet();
      for (Date date : dates) {
        ProfileTracker proTracker = stnTracker.profileMap.get(date);
        int trackerIndex = proTracker.parent_index;
        numObsArray.set(trackerIndex, proTracker.numChildren);

        int first = (proTracker.link.size() > 0) ? proTracker.link.get(0) : -1;
        firstObsArray.set(trackerIndex, first);

        if (proTracker.link.size() > 0) {
          // construct forward link
          List<Integer> nextList = proTracker.link;
          for (int j = 0; j < nextList.size() - 1; j++) {
            Integer curr = nextList.get(j);
            Integer next = nextList.get(j + 1);
            nextObsArray.set(curr, next);
          }
          Integer curr = nextList.get(nextList.size() - 1);
          nextObsArray.set(curr, -1);
        }
      }
    }

    try {
      ncfile.write(firstObsName, firstObsArray);
      ncfile.write(numObsName, numObsArray);
      ncfile.write(nextObsName, nextObsArray);

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }

    // finish the obs data

    ncfile.updateAttribute(
        null, new Attribute("time_coverage_start", dateFormatter.toDateTimeStringISO(minDate)));
    ncfile.updateAttribute(
        null, new Attribute("time_coverage_end", dateFormatter.toDateTimeStringISO(maxDate)));
  }
예제 #9
0
  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");
  }
예제 #10
0
  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");
  }
예제 #11
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;
  }
예제 #12
0
  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 void writeCDFChromatogram(
      File file, IChromatogramMSD chromatogram, IProgressMonitor monitor) throws IOException {

    NetcdfFileWriteable cdfChromatogram = NetcdfFileWriteable.createNew(file.getAbsolutePath());
    DimensionSupport dimensionSupport = new DimensionSupport(cdfChromatogram, chromatogram);
    AttributeSupport.setAttributes(cdfChromatogram, chromatogram);
    Dimension errorNumber = dimensionSupport.getErrorNumber();
    Dimension byteString64 = dimensionSupport.getByteString64();
    Dimension byteString32 = dimensionSupport.getByteString32();
    Dimension numberOfScans = dimensionSupport.getNumberOfScans();
    Dimension instrumentNumber = dimensionSupport.getInstrumentNumber();
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_ERROR_LOG, errorNumber, byteString64, "");
    dimensionSupport.addVariableDoubleD1(
        CDFConstants.VARIABLE_A_D_SAMPLING_RATE, numberOfScans, dimensionSupport.NULL_VALUE_DOUBLE);
    dimensionSupport.addVariableShortD1(
        CDFConstants.VARIABLE_A_D_COADDITION_FACTOR,
        numberOfScans,
        dimensionSupport.NULL_VALUE_SHORT);
    dimensionSupport.addVariableScanAcquisitionTime();
    dimensionSupport.addVariableDoubleD1(
        CDFConstants.VARIABLE_SCAN_DURATION, numberOfScans, dimensionSupport.NULL_VALUE_DOUBLE);
    dimensionSupport.addVariableDoubleD1(
        CDFConstants.VARIABLE_INTER_SCAN_TIME, numberOfScans, dimensionSupport.NULL_VALUE_DOUBLE);
    dimensionSupport.addVariableDoubleD1(
        CDFConstants.VARIABLE_RESOLUTION, numberOfScans, dimensionSupport.NULL_VALUE_DOUBLE);
    dimensionSupport.addVariableActualScanNumber();
    dimensionSupport.addVariableTotalIntensity();
    dimensionSupport.addVariableDoubleD1(CDFConstants.VARIABLE_MASS_RANGE_MIN, numberOfScans, 0);
    // dimensionSupport.addVariableMassRangeMin();
    dimensionSupport.addVariableMassRangeMax();
    dimensionSupport.addVariableDoubleD1(
        CDFConstants.VARIABLE_TIME_RANGE_MIN, numberOfScans, dimensionSupport.NULL_VALUE_DOUBLE);
    dimensionSupport.addVariableDoubleD1(
        CDFConstants.VARIABLE_TIME_RANGE_MAX, numberOfScans, dimensionSupport.NULL_VALUE_DOUBLE);
    dimensionSupport.addVariableScanIndex();
    dimensionSupport.addVariablePointCount();
    dimensionSupport.addVariableIntD1(CDFConstants.VARIABLE_FLAG_COUNT, numberOfScans, 0);
    /*
     * mass values, time values, intensity values
     */
    dimensionSupport.addVariableScanValues();
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_NAME, instrumentNumber, byteString32, "Gas Chromatograph");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_ID, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_MFR, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_MODEL, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_SERIAL_NO, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_SW_VERSION, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_FW_VERSION, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_OS_VERSION, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_APP_VERSION, instrumentNumber, byteString32, "");
    dimensionSupport.addVariableCharD2(
        CDFConstants.VARIABLE_INSTRUMENT_COMMENTS, instrumentNumber, byteString32, "");
    try {
      monitor.subTask(IConstants.EXPORT_DATA_ENTRIES);
      cdfChromatogram.create();
      ArrayList<IDataEntry> dataEntries = dimensionSupport.getDataEntries();
      for (IDataEntry entry : dataEntries) {
        monitor.subTask(entry.getVarName());
        cdfChromatogram.write(entry.getVarName(), entry.getValues());
      }
      cdfChromatogram.close();
      cdfChromatogram = null;
    } catch (IOException e) {
      logger.warn(e);
    } catch (InvalidRangeException e) {
      logger.warn(e);
    } finally {
      if (cdfChromatogram != null) {
        try {
          cdfChromatogram.close();
        } catch (IOException e) {
          logger.warn(e);
        }
      }
    }
  }
예제 #14
0
  private int populateFile2(
      NetcdfFileWriteable ncFile,
      String varName,
      DataType dataType,
      int[] dimensions,
      int valueCounter,
      Random generator)
      throws IOException, InvalidRangeException {

    long singleWriteSize = maxMemory / dataType.getSize();
    int highestNonWriteDim = determineHighestNonWriteDimension(dimensions, singleWriteSize);
    int[] singleStep = createWriteStep(dimensions, singleWriteSize, highestNonWriteDim);
    int numStepWrites = determineNumberOfStepWrites(singleStep, singleWriteSize);

    System.out.println(
        "SingleWriteSize: "
            + singleWriteSize
            + " datatype size: "
            + dataType.getSize()
            + " singleStep: "
            + arrayToString(singleStep)
            + " numberStepsPerWrite: "
            + numStepWrites);

    int[] allOnes = new int[dimensions.length];
    int[] allZeros = new int[dimensions.length];

    for (int i = 0; i < allOnes.length; i++) {
      allOnes[i] = 1;
      allZeros[i] = 0;
    }

    Index origin = new Index(dimensions, allOnes);

    long totalSize = origin.getSize();
    long writtenSoFar = 0;

    boolean done = false;

    while (!done) {
      for (int i = 0; i < numStepWrites; i++) {
        // this is a crack at an optimization

        if ((highestNonWriteDim >= 0)
            && ((origin.getCurrentCounter()[highestNonWriteDim] + numStepWrites)
                <= dimensions[highestNonWriteDim])) {
          singleStep[highestNonWriteDim] = numStepWrites;
          System.out.println("JUST OPTIMIZED. New write step: " + arrayToString(singleStep));
          // keep 'i' right
          i += numStepWrites - 1;
        } else {
          singleStep[highestNonWriteDim] = 1;
        }

        Array array = new ArrayInt(singleStep);
        IndexIterator iter = array.getIndexIterator();

        while (iter.hasNext()) {
          iter.getIntNext();
          // uncomment the following line for a random distribution
          // iter.setIntCurrent((int)  (Math.abs(generator.nextGaussian()) * 40) );
          // uncomment this line for an incrementing value
          iter.setIntCurrent(valueCounter);
          valueCounter++;

          // book keeping
          writtenSoFar++;
          // origin.incr();
        }

        System.out.println(
            "Writing to file: "
                + ncFile.getLocation()
                + ". var_name: "
                + varName
                + " origin: "
                + arrayToString(origin.getCurrentCounter())
                + " writeSize: "
                + array.getSize()
                + " write shape: "
                + arrayToString(singleStep));

        // write to the actual file
        ncFile.write(varName, origin.getCurrentCounter(), array);

        // update origin accordingly

        for (int j = 0; j < Index.computeSize(singleStep); j++) {
          // writtenSoFar++;
          origin.incr();
        }

        System.out.println(
            "\tcurrentIndex: "
                + arrayToString(origin.getCurrentCounter())
                + " currentElement: "
                + origin.currentElement()
                + " totalsize: "
                + (totalSize)
                + " writtenSoFar: "
                + writtenSoFar);

        if (writtenSoFar >= totalSize) {
          done = true;
          break;
        }
      }
    }

    return valueCounter;
  }
예제 #15
0
파일: MakeW_Daily.java 프로젝트: jkool/NetC
  /**
   * 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);
    }
  }