Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Coverage API Demo Cross seam Subset

Sean Arms edited this page Jun 2, 2016 · 2 revisions

Code:

import java.io.IOException;

import ucar.ma2.InvalidRangeException;

import ucar.nc2.ft2.coverage.Coverage;
import ucar.nc2.ft2.coverage.CoverageCollection;
import ucar.nc2.ft2.coverage.CoverageCoordSys;
import ucar.nc2.ft2.coverage.CoverageDatasetFactory;
import ucar.nc2.ft2.coverage.FeatureDatasetCoverage;
import ucar.nc2.ft2.coverage.GeoReferencedArray;
import ucar.nc2.ft2.coverage.SubsetParams;

import ucar.unidata.geoloc.LatLonPoint;
import ucar.unidata.geoloc.LatLonPointImpl;
import ucar.unidata.geoloc.LatLonRect;

public class CoverageSubsetDemo {
  static public void main(String[] args) {
    String loc = "http://tds.gisclimatechange.ucar.edu/thredds/dodsC/globalmonthly/files/tasmin_Amon_CCSM4_historical_r3i1p1_185001-200512.nc";
    // setup spatial subset (lat +/- 90, lon +/- 180)
    double llLat = -10;
    double llLon = -25;
    double urLat = 15;
    double urLon = 20;
    LatLonPoint lowerLeft = new LatLonPointImpl(llLat, llLon);
    LatLonPoint upperRight = new LatLonPointImpl(urLat, urLon);
    LatLonRect llbb = new LatLonRect(lowerLeft, upperRight);
    SubsetParams subsetParams = new SubsetParams();
    subsetParams.setLatLonBoundingBox(llbb);
    try (FeatureDatasetCoverage ds = CoverageDatasetFactory.open(loc);) {
      // only one coverage collection in this file, so go ahead and get it
      CoverageCollection cc = ds.getCoverageCollections().get(0);
      // get the individual coverage in the coverage collection
      Coverage cov = cc.findCoverage("tasmin");
      // subset
      GeoReferencedArray mySubset = cov.readData(subsetParams);
      // check if coordsys of subset makes sense with our subset request
      // by checking out summaries of the time, lat, and log axes
      CoverageCoordSys subsetCoordSys = mySubset.getCoordSysForData();
      System.out.println(subsetCoordSys.getAxis("lat").getSummary());
      System.out.println(subsetCoordSys.getAxis("lon").getSummary());
    } catch (IOException | InvalidRangeException e) {
      e.printStackTrace();
    }
  }
}

Output:

 read tasmin result shape=1872,27,37,
  composite read section=0:1871,85:111,268:287
  sectionInResult=0:1871,0:26,0:19
  composite read section=0:1871,85:111,0:16
  sectionInResult=0:1871,0:26,20:36
start=-9.895288 end=14.607330 degrees_north regularPoint resolution=0.942408 (npts=27)
start=335.000000 end=381.250000 degrees_east regularPoint resolution=1.250000 (npts=37)