/**
   * Clones the attributes of one NetCDF file into another.
   *
   * @param infile - The source NetCDF file
   * @param inVarName - The variable name to be copied
   * @param outfile - The destination NetCDF file
   * @param outVarName - The output variable name
   */
  private void cloneAttributes(
      NetcdfFile infile, String inVarName, NetcdfFileWriteable outfile, String outVarName) {

    // Find the variable

    Variable vi = infile.findVariable(inVarName);

    // Grab all of its attributes - unchecked, but should be OK.

    List<Attribute> l = vi.getAttributes();
    for (Attribute a : l) {
      if (a.getName().equalsIgnoreCase("units") && inVarName.equalsIgnoreCase("time")) {
        Attribute aa = new Attribute("units", "days since 1900-12-31 00:00:00");
        outfile.addVariableAttribute(outVarName, aa);
      } else if (a.getName().equalsIgnoreCase("time_origin")
          && inVarName.equalsIgnoreCase("time")) {
        Attribute aa = new Attribute("time_origin", "1900-12-31 00:00:00");
        outfile.addVariableAttribute(outVarName, aa);
      } else if (a.getName().equalsIgnoreCase("calendar")) {
        Attribute aa = new Attribute("calendar", "standard");
        outfile.addVariableAttribute(outVarName, aa);
      } else {
        outfile.addVariableAttribute(outVarName, a);
      }
    }
  }
  public void writeHeader(
      List<ucar.unidata.geoloc.Station> stns,
      List<VariableSimpleIF> vars,
      int nprofiles,
      String altVarName)
      throws IOException {
    createGlobalAttributes();
    createStations(stns);
    createProfiles(nprofiles);

    // dummys, update in finish()
    ncfile.addGlobalAttribute("zaxis_coordinate", altVarName);
    ncfile.addGlobalAttribute("time_coverage_start", dateFormatter.toDateTimeStringISO(new Date()));
    ncfile.addGlobalAttribute("time_coverage_end", dateFormatter.toDateTimeStringISO(new Date()));

    createDataVariables(vars);

    ncfile.create(); // done with define mode

    writeStationData(stns); // write out the station info

    // now write the observations
    if (!(Boolean) ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE))
      throw new IllegalStateException("can't add record variable");
  }
Example #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());
      }
    }
  }
 /**
  * Closes the output file
  *
  * @param ncf_out
  */
 private void close(NetcdfFileWriteable ncf_out) {
   if (ncf_out != null) {
     try {
       ncf_out.flush();
       ncf_out.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
 private void createGlobalAttributes() {
   ncfile.addGlobalAttribute("Conventions", "Unidata Observation Dataset v1.0");
   ncfile.addGlobalAttribute("cdm_datatype", "Profile");
   ncfile.addGlobalAttribute("title", title);
   ncfile.addGlobalAttribute("desc", "Extracted by THREDDS/Netcdf Subset Service");
   /* ncfile.addGlobalAttribute("observationDimension", recordDimName);
   ncfile.addGlobalAttribute("stationDimension", stationDimName);
   ncfile.addGlobalAttribute("latitude_coordinate", latName);
   ncfile.addGlobalAttribute("longitude_coordinate", lonName);
   ncfile.addGlobalAttribute("time_coordinate", timeName); */
 }
  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++;
  }
  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);

  }
Example #8
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);
  }
Example #9
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 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);
    }
  }
Example #11
0
  /**
   * Clones the attributes of the input file(s) to the output file
   *
   * @param infile
   * @param inVarName
   * @param outfile
   * @param outVarName
   */
  private void cloneAttributes(
      NetcdfDataset infile, String inVarName, NetcdfFileWriteable outfile, String outVarName) {

    // Find the variable

    Variable vi = infile.findVariable(inVarName);

    List<Attribute> l = vi.getAttributes();

    for (Attribute a : l) {
      outfile.addVariableAttribute(outVarName, a);
    }
  }
  /**
   * 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();
    }
  }
  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 void finish() throws IOException {
   writeDataFinish();
   ncfile.close();
 }
Example #15
0
 @Override
 public void addDimension(String name, int length) {
   netcdfFileWriteable.addDimension(name, length);
 }
  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)));
  }
Example #17
0
 @Override
 public String getDimensions() {
   return Dimension.makeDimensionList(netcdfFileWriteable.getRootGroup().getDimensions());
 }
  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"));
  }
Example #19
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");
  }
Example #20
0
 @Override
 public void addGlobalAttribute(String name, String value) {
   netcdfFileWriteable.addGlobalAttribute(name, value);
 }
Example #21
0
 @Override
 public NVariable addScalarVariable(String name, DataType dataType) throws IOException {
   Variable variable = netcdfFileWriteable.addVariable(name, dataType, "");
   return new N3Variable(variable, netcdfFileWriteable);
 }
 public void setLength(long size) {
   ncfile.setLength(size);
 }
Example #23
0
 @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);
 }
Example #24
0
 @Override
 public NVariable findVariable(String variableName) {
   Variable variable = netcdfFileWriteable.getRootGroup().findVariable(variableName);
   return variable != null ? new N3Variable(variable, netcdfFileWriteable) : null;
 }
Example #25
0
 public static NFileWriteable create(String filename) throws IOException {
   NetcdfFileWriteable writeable = NetcdfFileWriteable.createNew(filename);
   writeable.setLargeFile(true);
   return new N3FileWriteable(writeable);
 }
  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"));
  }
Example #27
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");
  }
Example #28
0
 @Override
 public void close() throws IOException {
   netcdfFileWriteable.close();
 }
  /**
   * 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 WriterProfileObsDataset(String fileOut, String title) throws IOException {
   ncfile = NetcdfFileWriteable.createNew(fileOut, false);
   ncfile.setFill(false);
   this.title = title;
 }