Example #1
0
  /**
   * Read the data for each variable passed in
   *
   * @param v2
   * @param section
   * @return output data
   * @throws IOException
   * @throws ucar.ma2.InvalidRangeException
   */
  public Array readData(Variable v2, Section section) throws IOException, InvalidRangeException {

    // subset
    Object data;
    Array outputData;
    byte[] vdata = null;
    NOWRadheader.Vinfo vinfo;
    ByteBuffer bos;
    List<Range> ranges = section.getRanges();

    vinfo = (NOWRadheader.Vinfo) v2.getSPobject();

    try {
      vdata = headerParser.getData((int) vinfo.hoff);
    } catch (Exception e) {
    }

    bos = ByteBuffer.wrap(vdata);
    data = readOneScanData(bos, vinfo, v2.getShortName());
    outputData = Array.factory(v2.getDataType().getPrimitiveClassType(), v2.getShape(), data);
    outputData = outputData.flip(1);

    // outputData = outputData.flip(2);
    return (outputData.sectionNoReduce(ranges).copy());

    // return outputData;
  }
Example #2
0
  public void testAggCoordVar2(NetcdfFile ncfile) {

    Variable time = ncfile.findVariable("time");
    assert null != time;
    assert time.getShortName().equals("time");
    assert time.getRank() == 1 : time.getRank();
    assert time.getShape()[0] == 3;
    assert time.getDataType() == DataType.INT;

    assert time.getDimension(0) == ncfile.findDimension("time");

    try {
      Array data = time.read();

      assert (data instanceof ArrayInt);
      IndexIterator dataI = data.getIndexIterator();
      assert dataI.getIntNext() == 0 : dataI.getIntCurrent();
      assert dataI.getIntNext() == 1 : dataI.getIntCurrent();
      assert dataI.getIntNext() == 2 : dataI.getIntCurrent();

    } catch (IOException io) {
      io.printStackTrace();
      assert false;
    }
  }
Example #3
0
  public void testAggCoordVar(NetcdfFile ncfile) {

    Variable time = ncfile.findVariable("time");
    assert null != time;
    assert time.getShortName().equals("time");
    assert time.getRank() == 1 : time.getRank();
    assert time.getShape()[0] == 3;
    assert time.getDataType() == DataType.INT;

    assert time.getDimension(0) == ncfile.findDimension("time");

    try {
      Array data = time.read();

      assert (data instanceof ArrayInt.D1) : data.getClass().getName();
      ArrayInt.D1 dataI = (ArrayInt.D1) data;
      assert dataI.get(0) == 0;
      assert dataI.get(1) == 10;
      assert dataI.get(2) == 99;

    } catch (IOException io) {
      io.printStackTrace();
      assert false;
    }
  }
  public void testAggCoordVar(NetcdfFile ncfile) {
    Variable time = ncfile.findVariable("time");
    assert null != time;

    assert time.getShortName().equals("time");
    assert time.getRank() == 1;
    assert time.getSize() == 3;
    assert time.getShape()[0] == 3;
    assert time.getDataType() == DataType.DOUBLE;

    assert time.getDimension(0) == ncfile.findDimension("time");

    try {
      Array data = time.read();
      assert data.getRank() == 1;
      assert data.getSize() == 3;
      assert data.getShape()[0] == 3;
      assert data.getElementType() == double.class;

      int count = 0;
      IndexIterator dataI = data.getIndexIterator();
      while (dataI.hasNext()) {
        assert Misc.closeEnough(dataI.getDoubleNext(), result[count]);
        count++;
      }

    } catch (IOException io) {
      io.printStackTrace();
      assert false;
    }
  }
Example #5
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());
      }
    }
  }
Example #6
0
  private int addAttributes(opendap.dap.AttributeTable table, Variable v, Iterator iter) {
    int count = 0;

    // add attribute table for this variable
    while (iter.hasNext()) {
      Attribute att = (Attribute) iter.next();
      int dods_type = DODSNetcdfFile.convertToDODSType(att.getDataType(), false);

      try {
        String attName = NcDDS.escapeName(att.getName());
        if (att.isString()) {
          /* FIX String value = escapeAttributeStringValues(att.getStringValue());
          table.appendAttribute(attName, dods_type, "\""+value+"\"");
          */
          table.appendAttribute(attName, dods_type, att.getStringValue());
        } else {
          // cant send signed bytes
          if (att.getDataType() == DataType.BYTE) {
            boolean signed = false;
            for (int i = 0; i < att.getLength(); i++) {
              if (att.getNumericValue(i).byteValue() < 0) signed = true;
            }
            if (signed) // promote to signed short
            dods_type = opendap.dap.Attribute.INT16;
          }

          for (int i = 0; i < att.getLength(); i++)
            table.appendAttribute(attName, dods_type, att.getNumericValue(i).toString());
        }
        count++;

      } catch (Exception e) {
        log.error(
            "Error appending attribute " + att.getName() + " = " + att.getStringValue() + "\n" + e);
      }
    } // loop over variable attributes

    // kludgy thing to map char arrays to DODS Strings
    if ((v != null) && (v.getDataType().getPrimitiveClassType() == char.class)) {
      int rank = v.getRank();
      int strlen = (rank == 0) ? 0 : v.getShape(rank - 1);
      Dimension dim = (rank == 0) ? null : v.getDimension(rank - 1);
      try {
        opendap.dap.AttributeTable dodsTable = table.appendContainer("DODS");
        dodsTable.appendAttribute("strlen", opendap.dap.Attribute.INT32, Integer.toString(strlen));
        if ((dim != null) && dim.isShared())
          dodsTable.appendAttribute("dimName", opendap.dap.Attribute.STRING, dim.getName());
        count++;
      } catch (Exception e) {
        log.error("Error appending attribute strlen\n" + e);
      }
    }

    return count;
  }
Example #7
0
 private Array convertToChar(Variable newVar, Array oldData) {
   ArrayChar newData = (ArrayChar) Array.factory(DataType.CHAR, newVar.getShape());
   Index ima = newData.getIndex();
   IndexIterator ii = oldData.getIndexIterator();
   while (ii.hasNext()) {
     String s = (String) ii.getObjectNext();
     int[] c = ii.getCurrentCounter();
     for (int i = 0; i < c.length; i++) ima.setDim(i, c[i]);
     newData.setString(ima, s);
   }
   return newData;
 }
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);
  }
 /**
  * Read data from a top level Variable and return a memory resident Array.
  *
  * @param v2 Variable. It may have FLOAT/INTEGER data type.
  * @param section wanted section of data of Variable. The section list is a list of ucar.ma2.Range
  *     which define the requested data subset.
  * @return Array of data which will be read from Variable through this call.
  */
 public Array readData1(ucar.nc2.Variable v2, Section section)
     throws IOException, InvalidRangeException {
   // doData(raf, ncfile, varList);
   int[] sh = section.getShape();
   Array temp = Array.factory(v2.getDataType(), sh);
   long pos0 = 0;
   // Suppose that the data has LayoutRegular
   LayoutRegular index = new LayoutRegular(pos0, v2.getElementSize(), v2.getShape(), section);
   if (v2.getShortName().startsWith("time") | v2.getShortName().startsWith("numGates")) {
     temp = readIntData(index, v2);
   } else {
     temp = readFloatData(index, v2);
   }
   return temp;
 }
Example #10
0
  public void testReadData(NetcdfFile ncfile, String name) throws IOException {

    Variable v = ncfile.findVariable(name);
    assert null != v;
    assert v.getShortName().equals(name);
    assert v.getRank() == 3;
    assert v.getSize() == 36 : v.getSize();
    assert v.getShape()[0] == 3;
    assert v.getShape()[1] == 3;
    assert v.getShape()[2] == 4;
    assert v.getDataType() == DataType.DOUBLE;

    assert !v.isCoordinateVariable();

    assert v.getDimension(0) == ncfile.findDimension("time");
    assert v.getDimension(1) == ncfile.findDimension("lat");
    assert v.getDimension(2) == ncfile.findDimension("lon");

    Array data = v.read();
    assert data.getRank() == 3;
    assert data.getSize() == 36;
    assert data.getShape()[0] == 3;
    assert data.getShape()[1] == 3;
    assert data.getShape()[2] == 4;
    assert data.getElementType() == double.class;

    int[] shape = data.getShape();
    Index tIndex = data.getIndex();
    for (int i = 0; i < shape[0]; i++)
      for (int j = 0; j < shape[1]; j++)
        for (int k = 0; k < shape[2]; k++) {
          double val = data.getDouble(tIndex.set(i, j, k));
          // System.out.println(" "+val);
          assert TestUtils.close(val, 100 * i + 10 * j + k) : val;
        }
  }
 public Array getData(Range range, String parameterName)
     throws IOException, InvalidRangeException {
   Variable variable = ncfile.getRootGroup().findVariable(parameterName);
   int varRank = variable.getRank();
   int[] varShape = variable.getShape();
   List section = new ArrayList(varRank);
   section.add(range);
   for (int i = 1; i < varRank; i++) {
     section.add(new Range(0, varShape[i] - 1));
   }
   Array array = variable.read(section);
   if (array.getShape()[0] == 1) {
     return (array.reduce(0));
   } else {
     return (array);
   }
   // return( array.getShape()[0] == 1 ? array.reduce( 0 ) : array);
 }
Example #12
0
  protected Band addNewBand(Product product, Variable variable) {
    final int sceneRasterWidth = product.getSceneRasterWidth();
    final int sceneRasterHeight = product.getSceneRasterHeight();
    Band band = null;

    int variableRank = variable.getRank();
    if (variableRank == 2) {
      final int[] dimensions = variable.getShape();
      final int height = dimensions[0] - leadLineSkip - tailLineSkip;
      final int width = dimensions[1];
      if (height == sceneRasterHeight && width == sceneRasterWidth) {
        final String name = variable.getShortName();
        final int dataType = getProductDataType(variable);
        band = new Band(name, dataType, width, height);
        final String validExpression = bandInfoMap.get(name);
        if (validExpression != null && !validExpression.equals("")) {
          band.setValidPixelExpression(validExpression);
        }
        product.addBand(band);

        try {
          band.setNoDataValue(
              (double) variable.findAttribute("bad_value_scaled").getNumericValue().floatValue());
          band.setNoDataValueUsed(true);
        } catch (Exception ignored) {
        }

        final List<Attribute> list = variable.getAttributes();
        for (Attribute hdfAttribute : list) {
          final String attribName = hdfAttribute.getShortName();
          if ("units".equals(attribName)) {
            band.setUnit(hdfAttribute.getStringValue());
          } else if ("long_name".equals(attribName)) {
            band.setDescription(hdfAttribute.getStringValue());
          } else if ("slope".equals(attribName)) {
            band.setScalingFactor(hdfAttribute.getNumericValue(0).doubleValue());
          } else if ("intercept".equals(attribName)) {
            band.setScalingOffset(hdfAttribute.getNumericValue(0).doubleValue());
          }
        }
      }
    }
    return band;
  }
Example #13
0
  public void testAggCoordVar3(NetcdfFile ncfile) throws IOException {
    Variable time = ncfile.findVariable("time");
    assert null != time;
    assert time.getShortName().equals("time");
    assert time.getRank() == 1 : time.getRank();
    assert time.getShape()[0] == 3;
    assert time.getDataType() == DataType.DOUBLE : time.getDataType();

    assert time.getDimension(0) == ncfile.findDimension("time");

    Array data = time.read();

    assert (data instanceof ArrayDouble);
    IndexIterator dataI = data.getIndexIterator();
    double val = dataI.getDoubleNext();
    assert Misc.closeEnough(val, 0.0) : val;
    assert Misc.closeEnough(dataI.getDoubleNext(), 10.0) : dataI.getDoubleCurrent();
    assert Misc.closeEnough(dataI.getDoubleNext(), 99.0) : dataI.getDoubleCurrent();
  }
Example #14
0
  public void testAggCoordVarScan(NetcdfFile ncfile) throws IOException {
    Variable time = ncfile.findVariable("time");
    assert null != time;
    assert time.getShortName().equals("time");
    assert time.getRank() == 1 : time.getRank();
    assert time.getShape()[0] == 3;
    assert time.getDataType() == DataType.INT : time.getDataType();

    assert time.getDimension(0) == ncfile.findDimension("time");

    int count = 0;
    Array data = time.read();
    assert (data instanceof ArrayInt);
    while (data.hasNext()) {
      int val = data.nextInt();
      assert val == count * 10 : val + "!=" + count * 10;
      count++;
    }
  }
  public void testAggCoordVarSubsetDefeatLocalCache(NetcdfFile ncfile)
      throws InvalidRangeException, IOException {
    Variable time = ncfile.findVariable("time");
    assert null != time;

    assert time.getShortName().equals("time");
    assert time.getRank() == 1;
    assert time.getSize() == 3;
    assert time.getShape()[0] == 3;
    assert time.getDataType() == DataType.DOUBLE;

    assert time.getDimension(0) == ncfile.findDimension("time");

    time.setCachedData(null, false);
    Array data = time.read("1:2");
    assert data.getRank() == 1;
    assert data.getSize() == 2;
    assert data.getShape()[0] == 2;
    assert data.getElementType() == double.class;

    int count = 0;
    IndexIterator dataI = data.getIndexIterator();
    while (dataI.hasNext()) {
      assert Misc.closeEnough(dataI.getDoubleNext(), result[count + 1]);
      count++;
    }

    time.setCachedData(null, false);
    data = time.read("0:2:2");
    assert data.getRank() == 1;
    assert data.getSize() == 2;
    assert data.getShape()[0] == 2;
    assert data.getElementType() == double.class;

    count = 0;
    dataI = data.getIndexIterator();
    while (dataI.hasNext()) {
      assert Misc.closeEnough(dataI.getDoubleNext(), result[count * 2]);
      count++;
    }
  }
Example #16
0
  /** Converts a 3D variable to a {@link Metadata} representation */
  private static void convert3DVariable(GridDatatype g, Date date, Map<String, Metadata> metaMap)
      throws IOException {

    Variable v = g.getVariable();
    System.out.println("Reading: " + v.getFullName());
    Array values = v.read();

    int h = v.getShape(1);
    int w = v.getShape(2);

    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        LatLonPoint pt = g.getCoordinateSystem().getLatLon(j, i);
        String hash =
            GeoHash.encode((float) pt.getLatitude(), (float) pt.getLongitude(), 10).toLowerCase();

        Metadata meta = metaMap.get(hash);
        if (meta == null) {
          /* We need to create Metadata for this location */
          meta = new Metadata();

          UUID metaUUID = UUID.nameUUIDFromBytes(hash.getBytes());
          meta.setName(metaUUID.toString());

          SpatialProperties location =
              new SpatialProperties((float) pt.getLatitude(), (float) pt.getLongitude());
          meta.setSpatialProperties(location);

          TemporalProperties time = new TemporalProperties(date.getTime());
          meta.setTemporalProperties(time);

          metaMap.put(hash, meta);
        }

        String featureName = v.getFullName().toLowerCase();
        float featureValue = values.getFloat(i * w + j);
        Feature feature = new Feature(featureName, featureValue);
        meta.putAttribute(feature);
      }
    }
  }
Example #17
0
  public void testAggCoordVarNoCoordsDir(NetcdfFile ncfile) throws IOException {
    Variable time = ncfile.findVariable("time");
    assert null != time;
    assert time.getShortName().equals("time");
    assert time.getRank() == 1 : time.getRank();
    assert time.getShape()[0] == 3;
    assert time.getDataType() == DataType.STRING : time.getDataType();

    assert time.getDimension(0) == ncfile.findDimension("time");

    Array data = time.read();

    assert (data instanceof ArrayObject);
    IndexIterator dataI = data.getIndexIterator();
    String coordName = (String) dataI.getObjectNext();
    assert coordName.equals("time0Dir.nc") : coordName;
    coordName = (String) dataI.getObjectNext();
    assert coordName.equals("time1Dir.nc") : coordName;
    coordName = (String) dataI.getObjectNext();
    assert coordName.equals("time2Dir.nc") : coordName;
  }
Example #18
0
 protected synchronized void invalidateLines(SkipBadNav skipBadNav, Variable latitude)
     throws IOException {
   final int[] shape = latitude.getShape();
   try {
     int lineCount = shape[0];
     final int[] start = new int[] {0, 0};
     // just grab the first and last pixel for each scan line
     final int[] stride = new int[] {1, shape[1] - 1};
     final int[] count = new int[] {shape[0], shape[1]};
     Section section = new Section(start, count, stride);
     Array array;
     synchronized (ncFile) {
       array = latitude.read(section);
     }
     for (int i = 0; i < lineCount; i++) {
       int ix = i * 2;
       float valstart = array.getFloat(ix);
       float valend = array.getFloat(ix + 1);
       if (skipBadNav.isBadNav(valstart) || skipBadNav.isBadNav(valend)) {
         leadLineSkip++;
       } else {
         break;
       }
     }
     for (int i = lineCount; i-- > 0; ) {
       int ix = i * 2;
       float valstart = array.getFloat(ix);
       float valend = array.getFloat(ix + 1);
       if (skipBadNav.isBadNav(valstart) || skipBadNav.isBadNav(valend)) {
         tailLineSkip++;
       } else {
         break;
       }
     }
   } catch (InvalidRangeException e) {
     throw new IOException(e.getMessage());
   }
 }
  /**
   * 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();
    }
  }
Example #20
0
  public void testCoordVar(NetcdfFile ncfile) {

    Variable lat = ncfile.findVariable("lat");
    assert null != lat;
    assert lat.getShortName().equals("lat");
    assert lat.getRank() == 1;
    assert lat.getSize() == 3;
    assert lat.getShape()[0] == 3;
    assert lat.getDataType() == DataType.FLOAT;

    assert !lat.isUnlimited();
    assert lat.getDimension(0).equals(ncfile.findDimension("lat"));

    Attribute att = lat.findAttribute("units");
    assert null != att;
    assert !att.isArray();
    assert att.isString();
    assert att.getDataType() == DataType.STRING;
    assert att.getStringValue().equals("degrees_north");
    assert att.getNumericValue() == null;
    assert att.getNumericValue(3) == null;

    try {
      Array data = lat.read();
      assert data.getRank() == 1;
      assert data.getSize() == 3;
      assert data.getShape()[0] == 3;
      assert data.getElementType() == float.class;

      IndexIterator dataI = data.getIndexIterator();
      assert TestUtils.close(dataI.getDoubleNext(), 41.0);
      assert TestUtils.close(dataI.getDoubleNext(), 40.0);
      assert TestUtils.close(dataI.getDoubleNext(), 39.0);
    } catch (IOException io) {
    }
  }
 /** Retrieves the shapes of the velocity variables (u,v,w) */
 @Override
 public int[][] getShape() {
   return new int[][] {uVar.getShape(), vVar.getShape(), wVar.getShape()};
 }
  private void makeCoordinateDataWithMissing(
      int datatype,
      Variable time,
      Variable elev,
      Variable azi,
      Variable nradialsVar,
      Variable ngatesVar,
      List groups) {

    Array timeData = Array.factory(time.getDataType().getPrimitiveClassType(), time.getShape());
    Index timeIndex = timeData.getIndex();

    Array elevData = Array.factory(elev.getDataType().getPrimitiveClassType(), elev.getShape());
    Index elevIndex = elevData.getIndex();

    Array aziData = Array.factory(azi.getDataType().getPrimitiveClassType(), azi.getShape());
    Index aziIndex = aziData.getIndex();

    Array nradialsData =
        Array.factory(nradialsVar.getDataType().getPrimitiveClassType(), nradialsVar.getShape());
    IndexIterator nradialsIter = nradialsData.getIndexIterator();

    Array ngatesData =
        Array.factory(ngatesVar.getDataType().getPrimitiveClassType(), ngatesVar.getShape());
    IndexIterator ngatesIter = ngatesData.getIndexIterator();

    // first fill with missing data
    IndexIterator ii = timeData.getIndexIterator();
    while (ii.hasNext()) ii.setIntNext(MISSING_INT);

    ii = elevData.getIndexIterator();
    while (ii.hasNext()) ii.setFloatNext(MISSING_FLOAT);

    ii = aziData.getIndexIterator();
    while (ii.hasNext()) ii.setFloatNext(MISSING_FLOAT);

    // now set the  coordinate variables from the Cinrad2Record radial
    int last_msecs = Integer.MIN_VALUE;
    int nscans = groups.size();
    try {
      for (int scan = 0; scan < nscans; scan++) {
        List scanGroup = (List) groups.get(scan);
        int nradials = scanGroup.size();

        Cinrad2Record first = null;
        for (int j = 0; j < nradials; j++) {
          Cinrad2Record r = (Cinrad2Record) scanGroup.get(j);
          if (first == null) first = r;

          int radial = r.radial_num - 1;
          timeData.setInt(timeIndex.set(scan, radial), r.data_msecs);
          elevData.setFloat(elevIndex.set(scan, radial), r.getElevation());
          aziData.setFloat(aziIndex.set(scan, radial), r.getAzimuth());

          if (r.data_msecs < last_msecs)
            logger.warn("makeCoordinateData time out of order " + r.data_msecs);
          last_msecs = r.data_msecs;
        }

        nradialsIter.setIntNext(nradials);
        ngatesIter.setIntNext(first.getGateCount(datatype));
      }
    } catch (java.lang.ArrayIndexOutOfBoundsException ae) {
      logger.debug("Cinrad2IOSP.uncompress ", ae);
    }
    time.setCachedData(timeData, false);
    elev.setCachedData(elevData, false);
    azi.setCachedData(aziData, false);
    nradialsVar.setCachedData(nradialsData, false);
    ngatesVar.setCachedData(ngatesData, false);
  }
  /**
   * Fill all of the variables/attributes in the ncfile
   *
   * @param ncfile NetcdfFile object which will be filled.
   * @param bst number of seconds since midnight for start of sweep
   * @param yr year of start of each sweep
   * @param m month of start of each sweep
   * @param dda day of start of each sweep
   * @param varList ArrayList of Variables of ncfile
   * @param recHdr java.util.Map with values for Attributes
   */
  public void doNetcdfFileCoordinate(
      ucar.nc2.NetcdfFile ncfile,
      int[] bst,
      short[] yr,
      short[] m,
      short[] dda,
      ArrayList<Variable> varList,
      java.util.Map<String, Number> recHdr) {
    // prepare attribute values

    String[] unit = {" ", "dbZ", "dbZ", "m/sec", "m/sec", "dB"};
    String def_datafile = "SIGMET-IRIS";
    Short header_length = 80;
    Short ray_header_length = 6;
    int ngates = 0;

    float radar_lat =
        recHdr.get("radar_lat").floatValue(); // System.out.println("rad_lat="+radar_lat);
    float radar_lon =
        recHdr.get("radar_lon").floatValue(); // System.out.println("rad_lon="+radar_lon);
    short ground_height =
        recHdr.get("ground_height").shortValue(); // System.out.println("ground_H="+ground_height);
    short radar_height =
        recHdr.get("radar_height").shortValue(); // System.out.println("radar_H="+radar_height);
    int radar_alt =
        (recHdr.get("radar_alt").intValue()) / 100; // System.out.println("rad_alt="+radar_alt);
    short num_rays =
        recHdr.get("num_rays").shortValue(); // System.out.println("HERE!! num_rays="+num_rays);
    float range_first =
        (recHdr.get("range_first").intValue())
            * 0.01f; // System.out.println("range_1st="+range_first);
    float range_last =
        (recHdr.get("range_last").intValue()) * 0.01f; // System.out.println("step="+step);
    short number_sweeps = recHdr.get("number_sweeps").shortValue();
    int nparams = (recHdr.get("nparams").intValue()); // System.out.println("nparams="+nparams);
    // define date/time
    // int last_t=(int)(ray[nparams*number_sweeps-1][num_rays-1].getTime());
    int last_t = volScan.lastRay.getTime();
    String sss1 = Short.toString(m[0]);
    if (sss1.length() < 2) sss1 = "0" + sss1;
    String sss2 = Short.toString(dda[0]);
    if (sss2.length() < 2) sss2 = "0" + sss2;
    String base_date0 = String.valueOf(yr[0]) + "-" + sss1 + "-" + sss2;
    String sss11 = Short.toString(m[number_sweeps - 1]);
    if (sss11.length() < 2) sss11 = "0" + sss11;
    String sss22 = Short.toString(dda[number_sweeps - 1]);
    if (sss22.length() < 2) sss22 = "0" + sss22;
    String base_date1 = String.valueOf(yr[number_sweeps - 1]) + "-" + sss11 + "-" + sss22;
    String start_time = base_date0 + "T" + calcTime(bst[0], 0) + "Z";
    String end_time = base_date1 + "T" + calcTime(bst[number_sweeps - 1], last_t) + "Z";
    ncfile.addAttribute(null, new Attribute("time_coverage_start", start_time));
    ncfile.addAttribute(null, new Attribute("time_coverage_end", end_time));

    // set all of Variables
    try {
      int sz = varList.size();

      ArrayFloat.D2[] dataArr = new ArrayFloat.D2[nparams * number_sweeps];
      Index[] dataIndex = new Index[nparams * number_sweeps];

      Ray[] rtemp = new Ray[(int) num_rays];

      // NCdump.printArray(dataArr[0], "Total_Power", System.out, null);

      Variable[] distanceR = new Variable[number_sweeps];
      ArrayFloat.D1[] distArr = new ArrayFloat.D1[number_sweeps];
      Index[] distIndex = new Index[number_sweeps];
      String distName = "distanceR";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          distName = "distanceR_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(distName.trim())) {
            distanceR[i] = aVarList;
            break;
          }
        }
        distArr[i] = (ArrayFloat.D1) Array.factory(DataType.FLOAT, distanceR[i].getShape());
        distIndex[i] = distArr[i].getIndex();

        // for (int jj=0; jj<num_rays; jj++) { rtemp[jj]=ray[i][jj]; }
        ngates = sweep_bins[i];
        float stp = calcStep(range_first, range_last, (short) ngates);
        for (int ii = 0; ii < ngates; ii++) {
          distArr[i].setFloat(distIndex[i].set(ii), (range_first + ii * stp));
        }
      }
      // NCdump.printArray(distArr[0], "distanceR", System.out, null);
      List rgp = volScan.getTotalPowerGroups();
      if (rgp.size() == 0) rgp = volScan.getReflectivityGroups();
      List[] sgp = new ArrayList[number_sweeps];
      for (int i = 0; i < number_sweeps; i++) {
        sgp[i] = (List) rgp.get((short) i);
      }

      Variable[] time = new Variable[number_sweeps];
      ArrayInt.D1[] timeArr = new ArrayInt.D1[number_sweeps];
      Index[] timeIndex = new Index[number_sweeps];
      String t_n = "time";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          t_n = "time_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(t_n.trim())) {
            time[i] = aVarList;
            break;
          }
        }

        //                if (time[i].getShape().length == 0) {
        //                    continue;
        //                }
        timeArr[i] = (ArrayInt.D1) Array.factory(DataType.INT, time[i].getShape());
        timeIndex[i] = timeArr[i].getIndex();
        List rlist = sgp[i];

        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        for (int jj = 0; jj < num_rays; jj++) {
          timeArr[i].setInt(timeIndex[i].set(jj), rtemp[jj].getTime());
        }
      }

      // NCdump.printArray(timeArr[0], "time", System.out, null);

      Variable[] azimuthR = new Variable[number_sweeps];
      ArrayFloat.D1[] azimArr = new ArrayFloat.D1[number_sweeps];
      Index[] azimIndex = new Index[number_sweeps];
      String azimName = "azimuthR";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          azimName = "azimuthR_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(azimName.trim())) {
            azimuthR[i] = aVarList;
            break;
          }
        }
        azimArr[i] = (ArrayFloat.D1) Array.factory(DataType.FLOAT, azimuthR[i].getShape());
        azimIndex[i] = azimArr[i].getIndex();
        List rlist = sgp[i];

        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        for (int jj = 0; jj < num_rays; jj++) {
          azimArr[i].setFloat(azimIndex[i].set(jj), rtemp[jj].getAz());
        }
      }
      // NCdump.printArray(azimArr[0], "azimuthR", System.out, null);

      Variable[] elevationR = new Variable[number_sweeps];
      ArrayFloat.D1[] elevArr = new ArrayFloat.D1[number_sweeps];
      Index[] elevIndex = new Index[number_sweeps];
      String elevName = "elevationR";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          elevName = "elevationR_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(elevName.trim())) {
            elevationR[i] = aVarList;
            break;
          }
        }
        elevArr[i] = (ArrayFloat.D1) Array.factory(DataType.FLOAT, elevationR[i].getShape());
        elevIndex[i] = elevArr[i].getIndex();
        List rlist = sgp[i];

        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        for (int jj = 0; jj < num_rays; jj++) {
          elevArr[i].setFloat(elevIndex[i].set(jj), rtemp[jj].getElev());
        }
      }
      // NCdump.printArray(elevArr[0], "elevationR", System.out, null);

      Variable numGates = null;
      for (int i = 0; i < number_sweeps; i++) {
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals("numGates")) {
            numGates = aVarList;
            break;
          }
        }
      }
      ArrayInt.D1 gatesArr = (ArrayInt.D1) Array.factory(DataType.INT, numGates.getShape());
      Index gatesIndex = gatesArr.getIndex();

      for (int i = 0; i < number_sweeps; i++) {
        List rlist = sgp[i];
        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        ngates = rtemp[0].getBins();
        gatesArr.setInt(gatesIndex.set(i), ngates);
      }

      for (int i = 0; i < number_sweeps; i++) {
        distanceR[i].setCachedData(distArr[i], false);
        time[i].setCachedData(timeArr[i], false);
        azimuthR[i].setCachedData(azimArr[i], false);
        elevationR[i].setCachedData(elevArr[i], false);
      }
      numGates.setCachedData(gatesArr, false);
      // startSweep.setCachedData(sweepArr, false);

      //          -------------------------------------------------
      // int b=(int)ray[0][0].getBins();

      // -- Test of readData() and readToByteChannel() -----------------
      /*
      Range r1=new Range(356, 359);
      Range r2=new Range(0, 15);
      java.util.List arlist=new ArrayList();
      arlist.add(r1);
      arlist.add(r2);
      Array testArr=readData(v[0], new Section(arlist));
      NCdump.printArray(testArr, "Total_Power_sweep_1", System.out, null);
      WritableByteChannel channel=new FileOutputStream(new File("C:\\netcdf\\tt.dat")).getChannel();
      long ikk=readToByteChannel(v[0], new Section(arlist), channel);
      System.out.println("IKK="+ikk);
      channel.close();
            */
      // ---------------------------------------------------

    } catch (Exception e) {
      System.out.println(e.toString());
      e.printStackTrace();
    }
  } // ----------- end of doNetcdf ----------------------------------
  public void addGeocoding(Product product) throws IOException {

    double pixelX = 0.5;
    double pixelY = 0.5;
    double easting;
    double northing;
    double pixelSizeX;
    double pixelSizeY;
    boolean pixelRegistered = true;
    if (productReader.getProductType() == SeadasProductReader.ProductType.ANCNRT) {
      pixelRegistered = false;
    }
    if (productReader.getProductType() == SeadasProductReader.ProductType.OISST) {

      Variable lon = ncFile.findVariable("lon");
      Variable lat = ncFile.findVariable("lat");
      Array lonData = lon.read();
      // TODO: handle the 180 degree shift with the NOAA products - need to modify
      // SeaDasFileReader:readBandData
      // Below is a snippet from elsewhere in BEAM that deals with this issue...
      // SPECIAL CASE: check if we have a global geographic lat/lon with lon from 0..360 instead of
      // -180..180
      //            if (isShifted180(lonData)) {
      //                // if this is true, subtract 180 from all longitudes and
      //                // add a global attribute which will be analyzed when setting up the
      // image(s)
      //                final List<Variable> variables = ncFile.getVariables();
      //                for (Variable next : variables) {
      //                    next.getAttributes().add(new Attribute("LONGITUDE_SHIFTED_180", 1));
      //                }
      //                for (int i = 0; i < lonData.getSize(); i++) {
      //                    final Index ii = lonData.getIndex().set(i);
      //                    final double theLon = lonData.getDouble(ii) - 180.0;
      //                    lonData.setDouble(ii, theLon);
      //                }
      //            }
      final Array latData = lat.read();

      final int lonSize = lon.getShape(0);
      final Index i0 = lonData.getIndex().set(0);
      final Index i1 = lonData.getIndex().set(lonSize - 1);

      pixelSizeX =
          (lonData.getDouble(i1) - lonData.getDouble(i0)) / (product.getSceneRasterWidth() - 1);
      easting = lonData.getDouble(i0);

      final int latSize = lat.getShape(0);
      final Index j0 = latData.getIndex().set(0);
      final Index j1 = latData.getIndex().set(latSize - 1);
      pixelSizeY =
          (latData.getDouble(j1) - latData.getDouble(j0)) / (product.getSceneRasterHeight() - 1);

      // this should be the 'normal' case
      if (pixelSizeY < 0) {
        pixelSizeY = -pixelSizeY;
        northing = latData.getDouble(latData.getIndex().set(0));
      } else {
        northing = latData.getDouble(latData.getIndex().set(latSize - 1));
      }
      northing -= pixelSizeX / 2.0;
      easting += pixelSizeY / 2.0;

      try {
        product.setGeoCoding(
            new CrsGeoCoding(
                DefaultGeographicCRS.WGS84,
                product.getSceneRasterWidth(),
                product.getSceneRasterHeight(),
                easting,
                northing,
                pixelSizeX,
                pixelSizeY,
                pixelX,
                pixelY));
      } catch (FactoryException e) {
        throw new IllegalStateException(e);
      } catch (TransformException e) {
        throw new IllegalStateException(e);
      }

    } else {

      String east = "Easternmost_Longitude";
      String west = "Westernmost_Longitude";
      String north = "Northernmost_Latitude";
      String south = "Southernmost_Latitude";
      Attribute latmax = ncFile.findGlobalAttributeIgnoreCase("geospatial_lat_max");
      if (latmax != null) {
        east = "geospatial_lon_max";
        west = "geospatial_lon_min";
        north = "geospatial_lat_max";
        south = "geospatial_lat_min";
      } else {
        latmax = ncFile.findGlobalAttributeIgnoreCase("upper_lat");
        if (latmax != null) {
          east = "right_lon";
          west = "left_lon";
          north = "upper_lat";
          south = "lower_lat";
        }
      }

      final MetadataElement globalAttributes =
          product.getMetadataRoot().getElement("Global_Attributes");
      easting = (float) globalAttributes.getAttribute(east).getData().getElemDouble();
      float westing = (float) globalAttributes.getAttribute(west).getData().getElemDouble();
      pixelSizeX = Math.abs(easting - westing) / product.getSceneRasterWidth();
      northing = (float) globalAttributes.getAttribute(north).getData().getElemDouble();
      float southing = (float) globalAttributes.getAttribute(south).getData().getElemDouble();
      if (northing < southing) {
        mustFlipY = true;
        northing = (float) globalAttributes.getAttribute(south).getData().getElemDouble();
        southing = (float) globalAttributes.getAttribute(north).getData().getElemDouble();
      }
      pixelSizeY = Math.abs(northing - southing) / product.getSceneRasterHeight();
      if (pixelRegistered) {
        northing -= pixelSizeY / 2.0;
        westing += pixelSizeX / 2.0;
      } else {
        pixelX = 0.0;
        pixelY = 0.0;
      }
      try {
        product.setGeoCoding(
            new CrsGeoCoding(
                DefaultGeographicCRS.WGS84,
                product.getSceneRasterWidth(),
                product.getSceneRasterHeight(),
                westing,
                northing,
                pixelSizeX,
                pixelSizeY,
                pixelX,
                pixelY));
      } catch (FactoryException e) {
        throw new IllegalStateException(e);
      } catch (TransformException e) {
        throw new IllegalStateException(e);
      }
    }
  }
  @Override
  public Product createProduct() throws ProductIOException {

    int[] dims;
    int sceneHeight = 0;
    int sceneWidth = 0;
    Group geodata = ncFile.findGroup("geophysical_data");
    if (productReader.getProductType() == SeadasProductReader.ProductType.OISST) {
      dims = ncFile.getVariables().get(4).getShape();
      sceneHeight = dims[2];
      sceneWidth = dims[3];
      mustFlipY = true;
    } else if (productReader.getProductType() == SeadasProductReader.ProductType.ANCCLIM) {
      List<Variable> vars = ncFile.getVariables();
      for (Variable v : vars) {
        if (v.getRank() == 2) {
          dims = v.getShape();
          sceneHeight = dims[0];
          sceneWidth = dims[1];
        }
      }
    } else {
      if (geodata != null) {
        dims = geodata.getVariables().get(0).getShape();
        sceneHeight = dims[0];
        sceneWidth = dims[1];
      } else {
        ucar.nc2.Dimension latdim = ncFile.findDimension("lat");
        ucar.nc2.Dimension londim = ncFile.findDimension("lon");
        if (latdim != null) {
          sceneHeight = latdim.getLength();
          sceneWidth = londim.getLength();
        } else {
          dims = ncFile.getVariables().get(0).getShape();
          sceneHeight = dims[0];
          sceneWidth = dims[1];
        }
      }
    }

    String productName = productReader.getInputFile().getName();
    try {
      productName = getStringAttribute("Product_Name");
    } catch (Exception ignored) {

    }

    SeadasProductReader.ProductType productType = productReader.getProductType();

    Product product = new Product(productName, productType.toString(), sceneWidth, sceneHeight);
    product.setDescription(productName);

    product.setFileLocation(productReader.getInputFile());
    product.setProductReader(productReader);

    addGlobalMetadata(product);
    addSmiMetadata(product);
    //        variableMap = addBands(product, ncFile.getVariables());
    variableMap = addSmiBands(product, ncFile.getVariables());
    try {
      addGeocoding(product);
    } catch (Exception ignored) {
    }
    addFlagsAndMasks(product);
    if (productReader.getProductType() == SeadasProductReader.ProductType.Bathy) {
      mustFlipY = true;
      Dimension tileSize = new Dimension(640, 320);
      product.setPreferredTileSize(tileSize);
    }
    return product;
  }
  protected Map<Band, Variable> addSmiBands(Product product, List<Variable> variables) {
    final int sceneRasterWidth = product.getSceneRasterWidth();
    final int sceneRasterHeight = product.getSceneRasterHeight();
    Map<Band, Variable> bandToVariableMap = new HashMap<Band, Variable>();
    for (Variable variable : variables) {
      int variableRank = variable.getRank();
      if (variableRank == 2) {
        final int[] dimensions = variable.getShape();
        final int height = dimensions[0];
        final int width = dimensions[1];
        if (height == sceneRasterHeight && width == sceneRasterWidth) {
          String name = variable.getShortName();
          if (name.equals("l3m_data")) {
            try {
              name = getStringAttribute("Parameter") + " " + getStringAttribute("Measure");
            } catch (Exception e) {
              e.printStackTrace();
            }
          }

          final int dataType = getProductDataType(variable);
          final Band band = new Band(name, dataType, width, height);
          //                    band = new Band(name, dataType, width, height);

          product.addBand(band);

          try {
            Attribute fillvalue = variable.findAttribute("_FillValue");
            if (fillvalue == null) {
              fillvalue = variable.findAttribute("Fill");
            }
            if (fillvalue != null) {
              band.setNoDataValue((double) fillvalue.getNumericValue().floatValue());
              band.setNoDataValueUsed(true);
            }
          } catch (Exception ignored) {

          }
          bandToVariableMap.put(band, variable);
          // Set units, if defined
          try {
            band.setUnit(getStringAttribute("Units"));
          } catch (Exception ignored) {

          }

          final List<Attribute> list = variable.getAttributes();
          double[] validMinMax = {0.0, 0.0};
          for (Attribute hdfAttribute : list) {
            final String attribName = hdfAttribute.getShortName();
            if ("units".equals(attribName)) {
              band.setUnit(hdfAttribute.getStringValue());
            } else if ("long_name".equalsIgnoreCase(attribName)) {
              band.setDescription(hdfAttribute.getStringValue());
            } else if ("slope".equalsIgnoreCase(attribName)) {
              band.setScalingFactor(hdfAttribute.getNumericValue(0).doubleValue());
            } else if ("intercept".equalsIgnoreCase(attribName)) {
              band.setScalingOffset(hdfAttribute.getNumericValue(0).doubleValue());
            } else if ("scale_factor".equals(attribName)) {
              band.setScalingFactor(hdfAttribute.getNumericValue(0).doubleValue());
            } else if ("add_offset".equals(attribName)) {
              band.setScalingOffset(hdfAttribute.getNumericValue(0).doubleValue());
            } else if (attribName.startsWith("valid_")) {
              if ("valid_min".equals(attribName)) {
                validMinMax[0] = hdfAttribute.getNumericValue(0).doubleValue();
              } else if ("valid_max".equals(attribName)) {
                validMinMax[1] = hdfAttribute.getNumericValue(0).doubleValue();
              } else if ("valid_range".equals(attribName)) {
                validMinMax[0] = hdfAttribute.getNumericValue(0).doubleValue();
                validMinMax[1] = hdfAttribute.getNumericValue(1).doubleValue();
              }
            }
          }
          if (validMinMax[0] != validMinMax[1]) {
            double[] minmax = {0.0, 0.0};
            minmax[0] = validMinMax[0];
            minmax[1] = validMinMax[1];

            if (band.getScalingFactor() != 1.0) {
              minmax[0] *= band.getScalingFactor();
              minmax[1] *= band.getScalingFactor();
            }
            if (band.getScalingOffset() != 0.0) {
              minmax[0] += band.getScalingOffset();
              minmax[1] += band.getScalingOffset();
            }

            String validExp = format("%s >= %.2f && %s <= %.2f", name, minmax[0], name, minmax[1]);
            band.setValidPixelExpression(
                validExp); // .format(name, validMinMax[0], name, validMinMax[1]));
          }
        }
      } else if (variableRank == 4) {
        final int[] dimensions = variable.getShape();
        final int height = dimensions[2];
        final int width = dimensions[3];
        if (height == sceneRasterHeight && width == sceneRasterWidth) {
          String name = variable.getShortName();

          final int dataType = getProductDataType(variable);
          final Band band = new Band(name, dataType, width, height);
          //                    band = new Band(name, dataType, width, height);

          Variable sliced = null;
          try {
            sliced = variable.slice(0, 0).slice(0, 0);
          } catch (InvalidRangeException e) {
            e.printStackTrace(); // Todo change body of catch statement.
          }

          bandToVariableMap.put(band, sliced);
          product.addBand(band);

          try {
            Attribute fillvalue = variable.findAttribute("_FillValue");
            if (fillvalue != null) {
              band.setNoDataValue((double) fillvalue.getNumericValue().floatValue());
              band.setNoDataValueUsed(true);
            }
          } catch (Exception ignored) {

          }
          // Set units, if defined
          try {
            band.setUnit(getStringAttribute("units"));
          } catch (Exception ignored) {

          }

          final List<Attribute> list = variable.getAttributes();
          for (Attribute hdfAttribute : list) {
            final String attribName = hdfAttribute.getShortName();
            if ("scale_factor".equals(attribName)) {
              band.setScalingFactor(hdfAttribute.getNumericValue(0).doubleValue());
            } else if ("add_offset".equals(attribName)) {
              band.setScalingOffset(hdfAttribute.getNumericValue(0).doubleValue());
            }
          }
        }
      }
    }
    return bandToVariableMap;
  }
Example #27
0
  public static void generateObs(NetcdfDataset netcdfdataset) {
    try {
      ucar.nc2.Variable lon = netcdfdataset.findVariable("lon");
      ucar.nc2.Variable lat = netcdfdataset.findVariable("lat");
      ucar.nc2.Variable time = netcdfdataset.findVariable("time");
      ucar.nc2.Variable BAssta = netcdfdataset.findVariable("BAssta");

      Array dataLat = lat.read();
      int[] shapeLat = lat.getShape();
      Index indexLat = dataLat.getIndex();

      Array dataLon = lon.read();
      int[] shapeLon = lon.getShape();
      Index indexLon = dataLon.getIndex();

      Array dataTime = time.read();
      Index indexTime = dataTime.getIndex();

      // mbari
      double minLat = 34.955;
      double maxLat = 38.260;
      double minLon = -124.780;
      double maxLon = -121.809;
      int minIndexLat = -Integer.MAX_VALUE;
      int minIndexLon = Integer.MAX_VALUE;
      int maxIndexlat = Integer.MIN_VALUE;
      int maxIndexLon = Integer.MIN_VALUE;

      // lon is ginve as +

      //			int[][] indexLatLon = new int[shapeLat[0]][shapeLon[0]];
      List lats = new ArrayList<Integer>();
      List lons = new ArrayList<Integer>();

      // get lats

      for (int i = 0; i < shapeLat[0]; i++) {
        Double lat_d = dataLat.getDouble(indexLat.set(i));
        if (lat_d >= minLat && lat_d <= maxLat) {
          minIndexLat = i;
          lats.add(i);
        }
      }

      for (int j = 0; j < shapeLon[0]; j++) {
        Double lon_d = dataLon.getDouble(indexLon.set(j));
        if (lon_d > 180) {
          lon_d = -360 + lon_d;
        }
        if (lon_d >= minLon && lon_d <= maxLon) {
          lons.add(j);
        }
      }

      // get values:

      ucar.nc2.Variable bassta = netcdfdataset.findVariable("BAssta");
      int indMinLat = (Integer) lats.get(0);
      int indMaxLat = (Integer) lats.get(lats.size() - 1);
      int indMinLon = (Integer) lons.get(0);
      int indMaxLon = (Integer) lons.get(lons.size() - 1);
      int indMinTime = 0;
      int indMaxTime = time.getShape()[0] - 1;

      String s = createString(indMinLat, indMaxLat, indMinLon, indMaxLon, indMinTime, indMaxTime);
      // doVar(netcdfdataset, "BAssta", s);
      // Array dataVar= bassta.read();
      // bassta.getShape()

      // System.out.println("lats size: " + lats.size()+" "+"lons size:
      // "+lons.size());

      // for a given lat lon get time series
      // 1st lat lon
      s = createString(indMinLat, indMinLat, indMinLon, indMinLon, indMinTime, indMaxTime);

      Array arr = BAssta.read(s);
      int[] shapeArr = arr.getShape();
      Index index = arr.getIndex();
      System.out.println(shapeArr);
      // [74, 1, 1, 1]
      for (int i = 0; i < shapeArr[0]; i++) {

        Double d = arr.getDouble(index.set(i, 0, 0, 0));

        Double t = dataTime.getDouble(indexTime.set(i));

        String timeS = TimeUtil.getISOFromMillisec(t * 1000);

        System.out.println(t + " " + timeS + " " + d);
      }

      // lat lon are fixed, only varying time
      //			basta[time,alt,lat,lon]

      //			NCdump.printArray(arr, "array", System.out, null);

      //			doVar(netcdfdataset, "BAssta", s);
      //			doVar(netcdfdataset, "time", indMinTime + ":" + indMaxTime);

      // while (i < lon.getSize()) {
      // System.out.println("obs" + i + " " + lat + " " + lon);
      // i++;
      // }

    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private void makeCoordinateData(
      int datatype,
      Variable time,
      Variable elev,
      Variable azi,
      Variable nradialsVar,
      Variable ngatesVar,
      List groups) {

    Array timeData = Array.factory(time.getDataType().getPrimitiveClassType(), time.getShape());
    IndexIterator timeDataIter = timeData.getIndexIterator();

    Array elevData = Array.factory(elev.getDataType().getPrimitiveClassType(), elev.getShape());
    IndexIterator elevDataIter = elevData.getIndexIterator();

    Array aziData = Array.factory(azi.getDataType().getPrimitiveClassType(), azi.getShape());
    IndexIterator aziDataIter = aziData.getIndexIterator();

    Array nradialsData =
        Array.factory(nradialsVar.getDataType().getPrimitiveClassType(), nradialsVar.getShape());
    IndexIterator nradialsIter = nradialsData.getIndexIterator();

    Array ngatesData =
        Array.factory(ngatesVar.getDataType().getPrimitiveClassType(), ngatesVar.getShape());
    IndexIterator ngatesIter = ngatesData.getIndexIterator();

    int last_msecs = Integer.MIN_VALUE;
    int nscans = groups.size();
    int maxRadials = volScan.getMaxRadials();
    for (int i = 0; i < nscans; i++) {
      List scanGroup = (List) groups.get(i);
      int nradials = scanGroup.size();

      Cinrad2Record first = null;
      for (int j = 0; j < nradials; j++) {
        Cinrad2Record r = (Cinrad2Record) scanGroup.get(j);
        if (first == null) first = r;

        timeDataIter.setIntNext(r.data_msecs);
        elevDataIter.setFloatNext(r.getElevation());
        aziDataIter.setFloatNext(r.getAzimuth());

        if (r.data_msecs < last_msecs)
          logger.warn("makeCoordinateData time out of order " + r.data_msecs);
        last_msecs = r.data_msecs;
      }

      for (int j = nradials; j < maxRadials; j++) {
        timeDataIter.setIntNext(MISSING_INT);
        elevDataIter.setFloatNext(MISSING_FLOAT);
        aziDataIter.setFloatNext(MISSING_FLOAT);
      }

      nradialsIter.setIntNext(nradials);
      ngatesIter.setIntNext(first.getGateCount(datatype));
    }

    time.setCachedData(timeData, false);
    elev.setCachedData(elevData, false);
    azi.setCachedData(aziData, false);
    nradialsVar.setCachedData(nradialsData, false);
    ngatesVar.setCachedData(ngatesData, false);
  }
Example #29
0
 /**
  * Make the station variables from a representative station
  *
  * @param stations list of stations
  * @param dim station dimension
  * @return the list of variables
  */
 protected List<Variable> makeStationVars(List<GempakStation> stations, Dimension dim) {
   int numStations = stations.size();
   boolean useSTID = true;
   for (GempakStation station : stations) {
     if (station.getSTID().equals("")) {
       useSTID = false;
       break;
     }
   }
   List<Variable> vars = new ArrayList<Variable>();
   List<String> stnKeyNames = gemreader.getStationKeyNames();
   for (String varName : stnKeyNames) {
     Variable v = makeStationVariable(varName, dim);
     // use STNM or STID as the name or description
     Attribute stIDAttr = new Attribute("standard_name", "station_id");
     if (varName.equals(GempakStation.STID) && useSTID) {
       v.addAttribute(stIDAttr);
     }
     if (varName.equals(GempakStation.STNM) && !useSTID) {
       v.addAttribute(stIDAttr);
     }
     vars.add(v);
   }
   // see if we fill these in completely now
   if ((dim != null) && (numStations > 0)) {
     for (Variable v : vars) {
       Array varArray;
       if (v.getDataType().equals(DataType.CHAR)) {
         int[] shape = v.getShape();
         varArray = new ArrayChar.D2(shape[0], shape[1]);
       } else {
         varArray = get1DArray(v.getDataType(), numStations);
       }
       int index = 0;
       String varname = v.getFullName();
       for (GempakStation stn : stations) {
         String test = "";
         if (varname.equals(GempakStation.STID)) {
           test = stn.getName();
         } else if (varname.equals(GempakStation.STNM)) {
           ((ArrayInt.D1) varArray)
               .set(
                   index,
                   // (int) (stn.getSTNM() / 10));
                   (int) (stn.getSTNM()));
         } else if (varname.equals(GempakStation.SLAT)) {
           ((ArrayFloat.D1) varArray).set(index, (float) stn.getLatitude());
         } else if (varname.equals(GempakStation.SLON)) {
           ((ArrayFloat.D1) varArray).set(index, (float) stn.getLongitude());
         } else if (varname.equals(GempakStation.SELV)) {
           ((ArrayFloat.D1) varArray).set(index, (float) stn.getAltitude());
         } else if (varname.equals(GempakStation.STAT)) {
           test = stn.getSTAT();
         } else if (varname.equals(GempakStation.COUN)) {
           test = stn.getCOUN();
         } else if (varname.equals(GempakStation.STD2)) {
           test = stn.getSTD2();
         } else if (varname.equals(GempakStation.SPRI)) {
           ((ArrayInt.D1) varArray).set(index, stn.getSPRI());
         } else if (varname.equals(GempakStation.SWFO)) {
           test = stn.getSWFO();
         } else if (varname.equals(GempakStation.WFO2)) {
           test = stn.getWFO2();
         }
         if (!test.equals("")) {
           ((ArrayChar.D2) varArray).setString(index, test);
         }
         index++;
       }
       v.setCachedData(varArray, false);
     }
   }
   return vars;
 }
Example #30
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();
  }