// @todo Make sure units are degrees_east
 public double getLongitude(int point) throws IOException // required, units degrees_east
     {
   Array array = null;
   try {
     array = getLongitude(this.getPointRange(point));
   } catch (InvalidRangeException e) {
     IllegalArgumentException iae =
         new IllegalArgumentException(
             "Point <"
                 + point
                 + "> not in valid range <0, "
                 + (this.getNumberPoints() - 1)
                 + ">: "
                 + e.getMessage());
     iae.initCause(e);
     throw iae;
   }
   if (array instanceof ArrayDouble) {
     return (array.getDouble(array.getIndex()));
   } else if (array instanceof ArrayFloat) {
     return (array.getFloat(array.getIndex()));
   } else {
     throw new IOException(
         "Longitude variable not float or double <" + array.getElementType().toString() + ">.");
   }
 }
예제 #2
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());
      }
    }
  }
 // @todo Make sure units are meters
 public double getElevation(int point)
     throws IOException // optional; units meters;  missing = NaN.
     {
   Array array = null;
   try {
     array = getElevation(this.getPointRange(point));
   } catch (InvalidRangeException e) {
     IllegalArgumentException iae =
         new IllegalArgumentException(
             "Point <"
                 + point
                 + "> not in valid range <0, "
                 + (this.getNumberPoints() - 1)
                 + ">: "
                 + e.getMessage());
     iae.initCause(e);
     throw iae;
   }
   if (array instanceof ArrayDouble) {
     return array.getDouble(array.getIndex());
   } else if (array instanceof ArrayFloat) {
     return array.getFloat(array.getIndex());
   } else {
     throw new IOException(
         "Elevation variable not float or double <" + array.getElementType().toString() + ">.");
   }
 }
 public double getTimeValue(int point) throws IOException {
   Array array = null;
   try {
     array = getTime(this.getPointRange(point));
   } catch (InvalidRangeException e) {
     IllegalArgumentException iae =
         new IllegalArgumentException(
             "Point <"
                 + point
                 + "> not in valid range <0, "
                 + (this.getNumberPoints() - 1)
                 + ">: "
                 + e.getMessage());
     iae.initCause(e);
     throw iae;
   }
   if (array instanceof ArrayDouble) {
     return (array.getDouble(array.getIndex()));
   } else if (array instanceof ArrayFloat) {
     return (array.getFloat(array.getIndex()));
   } else if (array instanceof ArrayInt) {
     return (array.getInt(array.getIndex()));
   } else {
     throw new IOException(
         "Time variable not float, double, or integer <"
             + array.getElementType().toString()
             + ">.");
   }
 }
 public StructureData getData() throws IOException {
   if (sdata != null) return sdata;
   try {
     return (SingleTrajectory.this.getData(point));
   } catch (InvalidRangeException e) {
     throw new IllegalStateException(e.getMessage());
   }
 }
  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++;
  }
예제 #7
0
 Array readFully() throws IOException {
   if (origin == null) {
     return binVariable.read();
   } else {
     try {
       return binVariable.read(origin, shape).reduce();
     } catch (InvalidRangeException e) {
       throw new IOException(e.getMessage());
     }
   }
 }
예제 #8
0
 Array read(int firstIndex, int length) throws IOException {
   try {
     if (origin == null) {
       return binVariable.read(new int[] {firstIndex}, new int[] {length});
     } else {
       int[] originFull = origin.clone();
       int[] shapeFull = shape.clone();
       originFull[binDimIndex] = firstIndex;
       shapeFull[binDimIndex] = length;
       return binVariable.read(originFull, shapeFull).reduce();
     }
   } catch (InvalidRangeException e) {
     throw new IOException(e.getMessage());
   }
 }
 public Range getFullRange() {
   Range range = null;
   try {
     range = new Range(0, this.getNumberPoints() - 1);
   } catch (InvalidRangeException e) {
     IllegalStateException ise =
         new IllegalStateException(
             "Full trajectory range invalid <0, "
                 + (this.getNumberPoints() - 1)
                 + ">: "
                 + e.getMessage());
     ise.initCause(e);
     throw (ise);
   }
   return range;
 }
 public Array getData(int point, String parameterName) throws IOException {
   try {
     return (getData(this.getPointRange(point), parameterName));
   } catch (InvalidRangeException e) {
     IllegalArgumentException iae =
         new IllegalArgumentException(
             "Point <"
                 + point
                 + "> not in valid range <0, "
                 + (this.getNumberPoints() - 1)
                 + ">: "
                 + e.getMessage());
     iae.initCause(e);
     throw iae;
   }
 }
예제 #11
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());
    }
  }
예제 #12
0
  public static void doVar(NetcdfDataset netcdfdataset, String varS, String limits) {
    System.out.println("\r---------\r\r");
    ucar.nc2.Variable var = netcdfdataset.findVariable(varS);
    System.out.println(var.getName() + " " + var.getSize());

    Array arr;
    try {
      arr = var.read(limits);
      NCdump.printArray(arr, "array", System.out, null);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
예제 #13
0
파일: MakeW_Daily.java 프로젝트: jkool/NetC
  /** Main-type execution */
  public void go() {

    System.out.println("Writing to " + outputWFile + "...");

    try {
      uFile = NetcdfDataset.openDataset(inputUFile);
      vFile = NetcdfDataset.openDataset(inputVFile);
      bathy = new NetCDF_FloatGrid_3D(inputBathyFile, inLatName, inLonName);
      generate(uFile, vFile);

    } catch (IOException e) {
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      e.printStackTrace();
    }

    System.out.println("Complete.");
  }
  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);
    }
  }
예제 #15
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());
   }
 }
예제 #16
0
  /**
   * Copies the contents of single NetCDF files into a block file
   *
   * @param map
   * @param ncf_out
   */
  private void copyContent(TreeMap<Long, NetcdfFile> map, NetcdfFileWriteable ncf_out) {
    try {

      // Write the static information for 1D axes.

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

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

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

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

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

    } catch (IOException e) {
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      e.printStackTrace();
    }
  }
    public StructureData getFeatureData() throws IOException {
      if (null == sdata) {
        try {
          // deal with files that are updating // LOOK kludge?
          if (recno > getRecordCount()) {
            int n = getRecordCount();
            ncfile.syncExtend();
            log.info(
                "RecordPointObs.getData recno="
                    + recno
                    + " > "
                    + n
                    + "; after sync= "
                    + getRecordCount());
          }

          sdata = recordVar.readStructure(recno);
        } catch (ucar.ma2.InvalidRangeException e) {
          e.printStackTrace();
          throw new IOException(e.getMessage());
        }
      }
      return sdata;
    }
  public void initialize(String dir) throws IOException {
    // Set Time Zone to UTC
    formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));

    this.dir = dir;
    File f = new File(dir);

    // Ensure the path is a directory
    if (!f.isDirectory()) {
      throw new IllegalArgumentException(f.getName() + " is not a directory.");
    }

    // Filter the list of files

    File[] fa = f.listFiles(new FilenamePatternFilter(".*_[uvw].*\\.nc"));

    if (fa == null) {
      throw new IOException("File list is empty.");
    }

    for (File fil : fa) {

      String name = fil.getName();

      NetcdfFile ncf = NetcdfFile.open(fil.getPath());
      tVar = ncf.findVariable(tName);

      if (tVar == null) {
        System.out.println(
            "WARNING: Time variable "
                + tVar
                + " was not found in "
                + fil.getPath()
                + " when initializing the VelocityReader.  This file will be skipped.");
        continue;
      }

      Array arr = tVar.read();

      // Convert into a java array

      double[] ja = (double[]) arr.copyTo1DJavaArray();

      // Determine the minimum and maximum times

      long[] minmax = new long[2];

      minmax[0] = TimeConvert.HYCOMToMillis((long) ja[0]);
      minmax[1] = TimeConvert.HYCOMToMillis((long) ja[ja.length - 1]);

      // Put into an index linking start time with the associated file

      if (name.lastIndexOf("_u") > 0) {
        if (uFiles.containsKey(minmax[0])) {
          System.out.print(
              "WARNING:  Velocity files have duplicate time keys. "
                  + name
                  + "/"
                  + uFiles.get(minmax[0])
                  + " at "
                  + new Date(minmax[0]));
          System.out.println(" Skipping latter file.");
        } else {
          uFiles.put(minmax[0], ncf);
        }
      }

      if (name.lastIndexOf("_v") > 0) {
        if (vFiles.containsKey(minmax[0])) {
          System.out.print(
              "WARNING:  Velocity files have duplicate time keys. "
                  + name
                  + "/"
                  + vFiles.get(minmax[0])
                  + " at "
                  + new Date(minmax[0]));
          System.out.println(" Skipping latter file.");
        } else {
          vFiles.put(minmax[0], ncf);
        }
      }

      if (name.lastIndexOf("_w") > 0) {
        if (wFiles.containsKey(minmax[0])) {
          System.out.print(
              "WARNING:  Velocity files have duplicate time keys. "
                  + name
                  + "/"
                  + wFiles.get(minmax[0])
                  + " at "
                  + new Date(minmax[0]));
          System.out.println(" Skipping latter file.");
        } else {
          wFiles.put(minmax[0], ncf);
        }
      }
    }

    // If there are no files in one of the index collections, then exit.

    if (uFiles.size() == 0 || vFiles.size() == 0 || wFiles.size() == 0) {
      System.out.println(
          "Velocity directory is empty, or files/variables are not named properly."
              + "Files  must be named as *_u*, *_v*, and *_w*.");

      System.exit(0);
    }

    uKeys = new ArrayList<Long>(uFiles.keySet());
    vKeys = new ArrayList<Long>(vFiles.keySet());
    wKeys = new ArrayList<Long>(wFiles.keySet());

    // Populate uFile, vFile and wFile with the first entry so that they
    // are not null

    uFile = uFiles.get(uKeys.get(0));
    vFile = vFiles.get(vKeys.get(0));
    wFile = wFiles.get(wKeys.get(0));

    uVar = uFile.findVariable(uName);
    vVar = vFile.findVariable(vName);
    wVar = wFile.findVariable(wName);

    // Latitude and depth are read here because they should not change
    // and therefore can be input once only.

    latVar = uFile.findVariable(latName);
    lonVar = uFile.findVariable(lonName);
    zVar = uFile.findVariable(zName);

    setXLookup(lonName);
    setYLookup(latName);
    setZLookup(zName);

    if (positiveDown) {
      zloc.setNegate(true);
    }
    bounds[0][0] = uKeys.get(0);
    NetcdfFile lastfile = uFiles.get(uKeys.get(uKeys.size() - 1));
    Variable t = lastfile.findVariable(tName);
    double last;
    try {
      last = t.read(new int[] {t.getShape(0) - 1}, new int[] {1}).getDouble(0);
      bounds[0][1] = TimeConvert.HYCOMToMillis((long) last);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
    }
  }
예제 #19
0
  public File writeCoverageDataToFile(
      WcsRequest.Format format,
      LatLonRect bboxLatLonRect,
      AxisSubset vertSubset,
      List<String> rangeSubset,
      CalendarDateRange timeRange)
      throws WcsException {
    boolean zRangeDone = false;
    boolean tRangeDone = false;

    try {
      // Get the height range.
      Range zRange = vertSubset != null ? vertSubset.getRange() : null;
      zRangeDone = true;

      // Get the time range.
      Range tRange = null;
      if (timeRange != null) {
        CoordinateAxis1DTime timeAxis = this.coordSys.getTimeAxis1D();
        int startIndex = timeAxis.findTimeIndexFromCalendarDate(timeRange.getStart());
        int endIndex = timeAxis.findTimeIndexFromCalendarDate(timeRange.getEnd());
        tRange = new Range(startIndex, endIndex);
        tRangeDone = true;
      }

      if (format == WcsRequest.Format.GeoTIFF || format == WcsRequest.Format.GeoTIFF_Float) {
        if (rangeSubset.size() != 1) {
          String msg =
              "GeoTIFF response encoding only available for single range field selection ["
                  + rangeSubset
                  + "].";
          log.error("writeCoverageDataToFile(): " + msg);
          throw new WcsException(WcsException.Code.InvalidParameterValue, "RangeSubset", msg);
        }
        String reqRangeFieldName = rangeSubset.get(0);

        File dir = new File(getDiskCache().getRootDirectory());
        File tifFile = File.createTempFile("WCS", ".tif", dir);
        if (log.isDebugEnabled())
          log.debug("writeCoverageDataToFile(): tifFile=" + tifFile.getPath());

        WcsRangeField rangeField = this.range.get(reqRangeFieldName);
        GridDatatype subset =
            rangeField.getGridDatatype().makeSubset(tRange, zRange, bboxLatLonRect, 1, 1, 1);
        Array data = subset.readDataSlice(0, 0, -1, -1);

        GeotiffWriter writer = new GeotiffWriter(tifFile.getPath());
        writer.writeGrid(
            this.dataset.getDataset(), subset, data, format == WcsRequest.Format.GeoTIFF);

        writer.close();

        return tifFile;
      } else if (format == WcsRequest.Format.NetCDF3) {
        File dir = new File(getDiskCache().getRootDirectory());
        File outFile = File.createTempFile("WCS", ".nc", dir);
        if (log.isDebugEnabled())
          log.debug("writeCoverageDataToFile(): ncFile=" + outFile.getPath());
        // WTF ?? this.coordSys.getVerticalAxis().isNumeric();

        NetcdfFileWriter writer =
            NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, outFile.getAbsolutePath());
        CFGridWriter2.writeFile(
            this.dataset.getDataset(),
            rangeSubset,
            bboxLatLonRect,
            null,
            1,
            zRange,
            timeRange,
            1,
            true,
            writer);
        return outFile;
      } else {
        log.error(
            "writeCoverageDataToFile(): Unsupported response encoding format [" + format + "].");
        throw new WcsException(
            WcsException.Code.InvalidFormat,
            "Format",
            "Unsupported response encoding format [" + format + "].");
      }
    } catch (InvalidRangeException e) {
      String msg = "Failed to subset coverage [" + this.getName();
      if (!zRangeDone) msg += "] along vertical axis [" + vertSubset + "]. ";
      else if (!tRangeDone) msg += "] along time axis [" + timeRange + "]. ";
      else msg += "] in horizontal plane [" + bboxLatLonRect + "]. ";
      log.error("writeCoverageDataToFile(): " + msg + e.getMessage());
      throw new WcsException(WcsException.Code.CoverageNotDefined, "", msg);
    } catch (IOException e) {
      log.error(
          "writeCoverageDataToFile(): Failed to write file for requested coverage <"
              + this.getName()
              + ">: "
              + e.getMessage());
      throw new WcsException(
          WcsException.Code.UNKNOWN, "", "Problem creating coverage [" + this.getName() + "].");
    }
  }
예제 #20
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();
    }
  }
예제 #21
0
  /**
   * 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;
  }
  /**
   * Retrieves velocities as a vector [u,v,w] based on given positions
   *
   * @param time - time coordinate in milliseconds
   * @param z - depth coordinate
   * @param lon - longitude (decimal degrees)
   * @param lat - latitude (decimal degrees)
   */
  @Override
  public synchronized double[] getVelocities(long time, double z, double lon, double lat) {

    if (Double.isNaN(lon) || Double.isNaN(lat)) {
      throw new IllegalArgumentException("Latitude or Longitude value is NaN");
    }

    // Completely outside the time bounds

    if (time < bounds[0][0] || time > bounds[0][1]) {
      this.notifyAll();
      return null;
    }

    // Completely outside the vertical bounds

    if (z < bounds[1][0] || z > bounds[1][1]) {
      this.notifyAll();
      return null;
    }

    // Completely outside the north-south bounds - return null as opposed
    // to NODATA

    if (lat < bounds[2][0] || lat > bounds[2][1]) {
      this.notifyAll();
      return null;
    }

    // Completely outside the east-west bounds

    if (lon < bounds[3][0] || lon > bounds[3][1]) {
      this.notifyAll();
      return null;
    }

    checkTime(time);

    try {
      int js, is, ks, ts;

      // Searching for the cell indices nearest to the given location

      is = yloc.lookup(lat);
      js = xloc.lookup(lon);
      ks = zloc.lookup(z);
      ts = tloc.lookup(TimeConvert.millisToHYCOM(time));

      // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      // ATTENTION!!!! THE ORDER IS VERY IMPORTANT HERE!!!! Latitude (i/y)
      // and then Longitude (j/x). That's the way it is set up in the
      // NetCDF File. The best way would be to have automatic order
      // detection
      // somehow....
      // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      // Handling data edges

      int i_lhs = halfKernel;
      int i_rhs = halfKernel;
      int j_lhs = halfKernel;
      int j_rhs = halfKernel;
      int k_lhs = zHalfKernel;
      int k_rhs = zHalfKernel;

      if (is < halfKernel) {
        i_lhs = is;
      }
      if (is + halfKernel >= yloc.arraySize()) {
        i_rhs = yloc.arraySize() - is - 1;
      }
      if (js < halfKernel) {
        j_lhs = js;
      }
      if (js + halfKernel >= xloc.arraySize()) {
        j_rhs = xloc.arraySize() - js - 1;
      }
      if (ks < zHalfKernel) {
        k_lhs = ks;
      }
      if (ks + zHalfKernel >= zloc.arraySize()) {
        k_rhs = zloc.arraySize() - ks - 1;
      }

      int istart = is - i_lhs;
      int jstart = js - j_lhs;
      int kstart = ks - k_lhs;

      // LHS + RHS + middle

      int idim = i_lhs + i_rhs + 1;
      int jdim = j_lhs + j_rhs + 1;
      int kdim = k_lhs + k_rhs + 1;

      // Splines cannot be used with only 2 points. If we are using a
      // kernel size of 3 and it is reduced due to edge effects, then
      // slide the window.

      if (kdim == 2) {
        if (kstart != 0) {
          kstart -= (k_rhs + 1);
        }
        kdim = zKernelSize;
      }

      int blocksize = idim * jdim * kdim;
      // int semiblock = (idim * (jdim + 1) * kdim) / 3;

      int[] origin = new int[] {ts, kstart, istart, jstart};
      int[] shape = new int[] {1, kdim, idim, jdim};

      try {
        uArr = uVar.read(origin, shape);
        uArr = uArr.reduce();
      } catch (InvalidRangeException e) {
        // Should not occur. Checking done above.
        e.printStackTrace();
      }

      try {
        vArr = vVar.read(origin, shape);
        vArr = vArr.reduce();
      } catch (InvalidRangeException e) {
        // Should not occur. Checking done above.
        e.printStackTrace();
      }

      if (zloc.isIn_Bounds() >= 0) {

        try {
          wArr = wVar.read(origin, shape);
          wArr = wArr.reduce();

        } catch (InvalidRangeException e) {
          // Should not occur. Checking done above.
          e.printStackTrace();
        }
      }

      float[][][] autmp = (float[][][]) uArr.copyToNDJavaArray();
      float[][][] avtmp = (float[][][]) vArr.copyToNDJavaArray();
      float[][][] awtmp = (float[][][]) wArr.copyToNDJavaArray();

      // Mitigate NODATA values

      int uct = 0;
      double usum = 0;
      double uavg = 0;
      double ussq = 0;
      double uvar = 0;

      int vct = 0;
      double vsum = 0;
      double vavg = 0;
      double vssq = 0;
      double vvar = 0;

      int wct = 0;
      double wsum = 0;
      double wavg = 0;
      double wssq = 0;
      double wvar = 0;

      for (int i = 0; i < idim; i++) {
        for (int j = 0; j < jdim; j++) {
          for (int k = 0; k < kdim; k++) {

            if (autmp[k][i][j] < cutoff && !Double.isNaN(autmp[k][i][j])) {
              uct++;
              usum += autmp[k][i][j];
              uavg = usum / uct;
              ussq += autmp[k][i][j] * autmp[k][i][j];
              uvar = ussq / uct - uavg * uavg;
              // } else {
              // if ((k > 0) && Double.isNaN(autmp[k - 1][i][j]))
              // {
              // autmp[k][i][j] = 0;
              // }
            }
          }
        }
      }

      for (int i = 0; i < idim; i++) {
        for (int j = 0; j < jdim; j++) {
          for (int k = 0; k < kdim; k++) {
            if (avtmp[k][i][j] < cutoff && !Double.isNaN(avtmp[k][i][j])) {
              vct++;
              vsum += avtmp[k][i][j];
              vavg = vsum / vct;
              vssq += avtmp[k][i][j] * avtmp[k][i][j];
              vvar = vssq / vct - vavg * vavg;
              // } else {
              // if ((k > 0)
              // && Double.isNaN(avtmp[k - 1][i][j])) {
              // avtmp[k][i][j] = 0;
              // }
            }
          }
        }
      }

      if (zloc.isIn_Bounds() >= 0) {

        for (int i = 0; i < idim; i++) {
          for (int j = 0; j < jdim; j++) {
            for (int k = 0; k < kdim; k++) {

              if (awtmp[k][i][j] < cutoff && !Double.isNaN(awtmp[k][i][j])) {
                wct++;
                wsum += awtmp[k][i][j];
                wavg = wsum / wct;
                wssq += awtmp[k][i][j] * awtmp[k][i][j];
                wvar = wssq / wct - wavg * wavg;
                // } else {
                // if ((k > 0)
                // && Double.isNaN(awtmp[k - 1][i][j])) {
                // awtmp[k][i][j] = 0;
                // }
              }
            }
          }
        }
      }

      nearNoData = false;

      // If there are null values...

      if (uct < blocksize) {

        nearNoData = true;

        // If there are more than (io-1)^2, return null

        /*
         * if (uct < semiblock) { velocities = NODATA; averages =
         * NODATA; variances = NODATA; return NODATA; }
         */

        // Otherwise mitigate by replacing using the average value.

        for (int i = 0; i < idim; i++) {
          for (int j = 0; j < jdim; j++) {
            for (int k = 0; k < kdim; k++) {

              if (autmp[k][i][j] > cutoff || Double.isNaN(autmp[k][i][j])) {

                if (ks == 0) {
                  autmp[k][i][j] = 0;
                }

                autmp[k][i][j] = (float) uavg;
              }
            }
          }
        }
      }

      if (vct < blocksize) {

        nearNoData = true;

        // If there are more than halfKernel/2, return null

        /*
         * if (vct < semiblock) { this.notifyAll(); velocities = NODATA;
         * averages = NODATA; variances = NODATA; return NODATA; }
         */

        // Otherwise mitigate by replacing using the average value.

        for (int i = 0; i < idim; i++) {
          for (int j = 0; j < jdim; j++) {
            for (int k = 0; k < kdim; k++) {

              if (avtmp[k][i][j] > cutoff || Double.isNaN(avtmp[k][i][j])) {

                avtmp[k][i][j] = (float) vavg;
              }
            }
          }
        }
      }
      if (zloc.isIn_Bounds() >= 0) {
        if (wct < blocksize) {

          nearNoData = true;

          // If there are more than (halfKernel-1)^2, return null

          /*
           * if (wct < semiblock) { this.notifyAll(); velocities =
           * NODATA; averages = NODATA; variances = NODATA; return
           * NODATA; }
           */

          // Otherwise mitigate by replacing using the average value.

          for (int i = 0; i < idim; i++) {
            for (int j = 0; j < jdim; j++) {
              for (int k = 0; k < kdim; k++) {

                if (awtmp[k][i][j] > cutoff || Double.isNaN(awtmp[k][i][j])) {

                  awtmp[k][i][j] = (float) wavg;
                }
              }
            }
          }
        }
      }

      /*
       * Convert the Arrays into Java arrays - because latitude and z
       * should be consistent among files, we subset from a constant
       * array.
       */

      double[] latja = Arrays.copyOfRange(yloc.getJavaArray(), istart, istart + idim);
      double[] lonja = Arrays.copyOfRange(xloc.getJavaArray(), jstart, jstart + jdim);
      double[] zja = Arrays.copyOfRange(zloc.getJavaArray(), kstart, kstart + kdim);

      // Obtain the interpolated values

      TricubicSplineInterpolator tci = new TricubicSplineInterpolator();
      TricubicSplineInterpolatingFunction tsf = tci.interpolate(zja, latja, lonja, autmp);

      // u = tcs.interpolate(z, lat, lon);
      u = tsf.value(z, lat, lon);
      // tcs.setValues(avtmp);
      // v = tcs.interpolate(z, lat, lon);
      tsf = tci.interpolate(zja, latja, lonja, avtmp);
      v = tsf.value(z, lat, lon);

      if (zloc.isIn_Bounds() >= 0) {
        // tcs.setValues(awtmp);
        // w = tcs.interpolate(z, lat, lon);
        tsf = tci.interpolate(zja, latja, lonja, awtmp);
        w = tsf.value(z, lat, lon);
      } else {
        w = 0;
      }

      // If there is something strange with the values, return NODATA.

      if (Math.abs(u) > cutoff) {
        u = 0.0f;
        v = 0.0f;
        w = 0.0f;
        this.notifyAll();
        velocities = NODATA;
        averages = NODATA;
        variances = NODATA;
        return NODATA;
      } else if (Math.abs(v) > cutoff) {
        u = 0.0f;
        v = 0.0f;
        w = 0.0f;
        this.notifyAll();
        velocities = NODATA;
        averages = NODATA;
        variances = NODATA;
        return NODATA;
      } else if (Math.abs(w) > cutoff) {
        u = 0.0f;
        v = 0.0f;
        w = 0.0f;
        this.notifyAll();
        velocities = NODATA;
        averages = NODATA;
        variances = NODATA;
        return NODATA;
      }

      if (Double.isNaN(u) || Double.isNaN(v) || Double.isNaN(w)) {
        u = 0.0f;
        v = 0.0f;
        w = 0.0f;
        this.notifyAll();
        velocities = NODATA;
        averages = NODATA;
        variances = NODATA;
        return NODATA;
      }

      // Otherwise return the interpolated values.

      this.notifyAll();
      velocities = new double[] {u, v, w};
      averages = new double[] {uavg, vavg, wavg};

      // Correct running population variance to be sample variance.
      double ucorr = (double) uct / (double) (uct - 1);
      double vcorr = (double) vct / (double) (vct - 1);
      double wcorr = (double) wct / (double) (wct - 1);
      variances = new double[] {uvar * ucorr, vvar * vcorr, wvar * wcorr};

      return velocities;

      // If for some reason there was an error reading from the file,
      // return null.

    } catch (IOException e) {
      System.out.println("WARNING:  Error reading from velocity files.\n\n");
      e.printStackTrace();
    }
    this.notifyAll();
    return null;
  }
  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)));
  }
예제 #24
0
  // return the last value written to the file
  private int createFile(String variableName, DataType dataType, int writeSeed) {

    // hacky init
    this._unlimitedDim = null;

    ArrayList<ArrayList<VariableEntry>> variableListList =
        new ArrayList<ArrayList<VariableEntry>>();

    // seperate method to define the variables for this file
    /*
    ArrayList<VariableEntry> variableList3 = defineDataForThisFile3();
    variableListList.add(variableList3);

    ArrayList<VariableEntry> variableList1 = defineDataForThisFile1();
    variableListList.add(variableList1);
    */

    ArrayList<VariableEntry> variableList2 = defineDataForThisFile2();
    variableListList.add(variableList2);

    Date now = new Date();
    Random generator = new Random(now.getTime());
    int valueCounter = writeSeed;

    String filename = Long.toString(now.getTime()) + ".nc";

    try {
      // create the file
      NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(filename, false);

      // this loop needs to define all the meta-data prior to any data being written
      // set the metadata
      System.out.println("\t calling writeNetcdfMetadata for file " + ncfile.getLocation());
      ncfile = this.writeNetcdfMetadata(ncfile, variableName, variableListList);

      // this is only called once per file
      ncfile.create();

      int suffixInt = 1;
      for (ArrayList<VariableEntry> variableList : variableListList) {
        // write out coordinate variables
        for (VariableEntry entry : variableList) {
          writeCoordinateVariable(entry, ncfile);
        }
        // pull out the dimensions of the variables from variableList
        int[] dimensions = new int[variableList.size()];
        for (int i = 0; i < dimensions.length; i++) {
          dimensions[i] = variableList.get(i).getSize();
        }

        // write data to the file
        valueCounter =
            this.populateFile2(
                ncfile,
                variableName + Integer.toString(suffixInt),
                dataType,
                dimensions,
                valueCounter,
                generator);

        suffixInt++;
      }

      // close the file
      ncfile.close();
    } catch (IOException ioe) {
      System.out.println("IOException: " + ioe.toString());
    } catch (InvalidRangeException e) {
      System.out.println("InvalidRangeException: " + e.toString());
    }

    return valueCounter;
  }
예제 #25
0
  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;
  }
예제 #26
0
  public static void main2(String args[]) throws Exception {

    final int NLVL = 2;
    final int NLAT = 6;
    final int NLON = 12;
    final int NumberOfRecords = 2;

    final float SAMPLE_PRESSURE = 900.0f;
    final float SAMPLE_TEMP = 9.0f;
    final float START_LAT = 25.0f;
    final float START_LON = -125.0f;

    // Create the file.
    String filename = "pres_temp_4D.nc";
    NetcdfFileWriteable dataFile = null;

    try {
      // Create new netcdf-3 file with the given filename
      dataFile = NetcdfFileWriteable.createNew(filename, false);

      // add dimensions where time dimension is unlimit
      Dimension lvlDim = dataFile.addDimension("level", NLVL);
      Dimension latDim = dataFile.addDimension("latitude", NLAT);
      Dimension lonDim = dataFile.addDimension("longitude", NLON);
      Dimension timeDim = dataFile.addDimension("time", 1000); // should
      // not be
      // need
      // second
      // argument

      ArrayList dims = null;

      // Define the coordinate variables.
      dataFile.addVariable("latitude", DataType.FLOAT, new Dimension[] {latDim});
      dataFile.addVariable("longitude", DataType.FLOAT, new Dimension[] {lonDim});

      // Define units attributes for data variables.
      dataFile.addVariableAttribute("latitude", "units", "degrees_north");
      dataFile.addVariableAttribute("longitude", "units", "degrees_east");

      // Define the netCDF variables for the pressure and temperature
      // data.
      dims = new ArrayList();
      dims.add(timeDim);
      dims.add(lvlDim);
      dims.add(latDim);
      dims.add(lonDim);
      dataFile.addVariable("pressure", DataType.FLOAT, dims);
      dataFile.addVariable("temperature", DataType.FLOAT, dims);

      // Define units attributes for data variables.
      dataFile.addVariableAttribute("pressure", "units", "hPa");
      dataFile.addVariableAttribute("temperature", "units", "celsius");

      // Create some pretend data. If this wasn't an example program, we
      // would have some real data to write for example, model output.
      ArrayFloat.D1 lats = new ArrayFloat.D1(latDim.getLength());
      ArrayFloat.D1 lons = new ArrayFloat.D1(lonDim.getLength());
      int i, j;

      for (i = 0; i < latDim.getLength(); i++) {
        lats.set(i, START_LAT + 5.f * i);
      }

      for (j = 0; j < lonDim.getLength(); j++) {
        lons.set(j, START_LON + 5.f * j);
      }

      // Create the pretend data. This will write our surface pressure and
      // surface temperature data.
      ArrayFloat.D4 dataTemp =
          new ArrayFloat.D4(
              NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength());
      ArrayFloat.D4 dataPres =
          new ArrayFloat.D4(
              NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength());

      for (int record = 0; record < NumberOfRecords; record++) {
        i = 0;
        for (int lvl = 0; lvl < NLVL; lvl++)
          for (int lat = 0; lat < NLAT; lat++)
            for (int lon = 0; lon < NLON; lon++) {
              dataPres.set(record, lvl, lat, lon, SAMPLE_PRESSURE + i);
              dataTemp.set(record, lvl, lat, lon, SAMPLE_TEMP + i++);
            }
      }

      // Create the file. At this point the (empty) file will be written
      // to disk
      dataFile.create();

      // A newly created Java integer array to be initialized to zeros.
      int[] origin = new int[4];

      dataFile.write("latitude", lats);
      dataFile.write("longitude", lons);
      dataFile.write("pressure", origin, dataPres);
      dataFile.write("temperature", origin, dataTemp);
      dataFile.close();

    } catch (IOException e) {
      e.printStackTrace(System.err);
    } catch (InvalidRangeException e) {
      e.printStackTrace(System.err);
    } finally {
      // if (dataFile != null) {
      // try {
      // dataFile.close();
      // } catch (IOException ioe) {
      // ioe.printStackTrace();
      // }
      // }
    }
    System.out.println("*** SUCCESS writing example file " + filename);
  }