Exemplo n.º 1
0
  @Test
  /**
   * Find min/max/average precipitation for a randomly-positioned but fixed-size region from a nc
   * file output: filename,origin,size key: value:min, max, average
   *
   * @throws Exception
   */
  public void testProcessingNASANexDataInNetCDF() throws Exception {
    final int SIZE = 100;
    File file = new File(this.getClass().getResource("/ncar.nc").getPath());
    byte[] netcdfinbyte = FileUtils.readFileToByteArray(file);
    // use any dummy filename for file in memory
    NetcdfFile netCDFfile = NetcdfFile.openInMemory("inmemory.nc", netcdfinbyte);

    Variable time = netCDFfile.findVariable("time");
    ArrayDouble.D1 days = (ArrayDouble.D1) time.read();
    Variable lat = netCDFfile.findVariable("lat");
    if (lat == null) {
      logger.error("Cannot find Variable latitude(lat)");
      return;
    }
    ArrayFloat.D1 absolutelat = (ArrayFloat.D1) lat.read();
    Variable lon = netCDFfile.findVariable("lon");
    if (lon == null) {
      logger.error("Cannot find Variable longitude(lon)");
      return;
    }
    ArrayFloat.D1 absolutelon = (ArrayFloat.D1) lon.read();
    Variable pres = netCDFfile.findVariable("pr");
    if (pres == null) {
      logger.error("Cannot find Variable precipitation(pr)");
      return;
    }

    Random rand = new Random();
    int orig_lat = rand.nextInt((int) lat.getSize());
    orig_lat = Math.min(orig_lat, (int) (lat.getSize() - SIZE));
    int orig_lon = rand.nextInt((int) lon.getSize());
    orig_lon = Math.min(orig_lon, (int) (lon.getSize() - SIZE));

    int[] origin = new int[] {0, orig_lat, orig_lon};
    int[] size = new int[] {1, SIZE, SIZE};
    ArrayFloat.D3 data3D = (ArrayFloat.D3) pres.read(origin, size);
    double max = Double.NEGATIVE_INFINITY;
    double min = Double.POSITIVE_INFINITY;
    double sum = 0;
    for (int j = 0; j < SIZE; j++) {
      for (int k = 0; k < SIZE; k++) {
        double current = data3D.get(0, j, k);
        max = (current > max ? current : max);
        min = (current < min ? current : min);
        sum += current;
      }
    }
    logger.info(
        days
            + ","
            + absolutelat.get(orig_lat)
            + ","
            + absolutelon.get(orig_lon)
            + ","
            + SIZE
            + ":"
            + min
            + ","
            + max
            + ","
            + sum / (SIZE * SIZE));
  }