ucar.nc2.time.CalendarDate Java Examples

The following examples show how to use ucar.nc2.time.CalendarDate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NcssParamsBean.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public boolean intersectsTime(CalendarDateRange have, Formatter errs) {
  if (have == null)
    return true;
  Calendar dataCal = have.getStart().getCalendar(); // use the same calendar as the dataset

  CalendarDateRange want = getCalendarDateRange(dataCal);
  if (want != null) {
    if (have.intersects(want)) {
      return true;
    } else {
      errs.format("Requested time range %s does not intersect actual time range %s", want, have);
      return false;
    }
  }

  CalendarDate wantTime = getRequestedDate(dataCal);
  if (wantTime == null)
    return true;
  if (!have.includes(wantTime)) {
    errs.format("Requested time %s does not intersect actual time range %s", wantTime, have);
    return false;
  }
  return true;
}
 
Example #2
Source File: GridAsPointDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Point readData(GridDatatype grid, CalendarDate date, double zCoord, double lat, double lon)
    throws java.io.IOException {
  GridCoordSystem gcs = grid.getCoordinateSystem();

  int tidx = -1;
  // Date may be null if the grid does not have time axis
  if (date != null)
    tidx = findTimeIndexForCalendarDate(gcs, date);

  CoordinateAxis1D zAxis = gcs.getVerticalAxis();
  int zidx = zAxis.findCoordElement(zCoord);

  int[] xy = gcs.findXYindexFromLatLon(lat, lon, null);

  Array data = grid.readDataSlice(tidx, zidx, xy[1], xy[0]);

  // use actual grid midpoint
  LatLonPoint latlon = gcs.getLatLon(xy[0], xy[1]);

  Point p = new Point();
  p.lat = latlon.getLatitude();
  p.lon = latlon.getLongitude();
  p.z = zAxis.getCoordValue(zidx);
  p.dataValue = data.getDouble(data.getIndex());
  return p;
}
 
Example #3
Source File: EcmwfParamTableCompare.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) {
  final String PATH =
      "/usr/local/google/home/jlcaron/github/thredds/grib/src/main/resources/resources/grib2/ecmwf/tables/21";
  System.out.printf("EcmwfParamTableCompare on %s%n", CalendarDate.present());
  System.out.printf("  ECMWF = %s%n", PATH);
  System.out.printf("  WMO   = %s%n", WmoCodeFlagTables.standard.getResourceName());

  File root = new File(PATH);
  ImmutableList<String> children = ImmutableList.copyOf(root.list());
  children.stream().sorted().forEach(filename -> {
    if (filename.startsWith("4.2.")) {
      String[] split = filename.split("\\.");
      int discipline = Integer.parseInt(split[2]);
      int category = Integer.parseInt(split[3]);
      readParamTable(discipline, category);
    }
  });
}
 
Example #4
Source File: CoordinateTime2D.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the time coordinate at the given indices, into the 2D time coordinate array
 * 
 * @param runIdx run index
 * @param timeIdx time index
 * @return time coordinate
 */
public Time2D getOrgValue(int runIdx, int timeIdx) {
  CoordinateTimeAbstract time = getTimeCoordinate(runIdx);
  CalendarDate runDate = runtime.getRuntimeDate(runIdx);
  if (isTimeInterval) {
    TimeCoordIntvValue valIntv = (TimeCoordIntvValue) time.getValue(timeIdx);
    if (valIntv == null)
      throw new IllegalArgumentException();
    return new Time2D(runDate, null, valIntv);
  } else {
    Integer val = (Integer) time.getValue(timeIdx);
    if (val == null)
      throw new IllegalArgumentException();
    return new Time2D(runDate, val, null);
  }
}
 
Example #5
Source File: Grib2Pds.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected CalendarDate calcTime(int startIndex) {

    int year = GribNumbers.int2(getOctet(startIndex++), getOctet(startIndex++));
    int month = getOctet(startIndex++);
    int day = getOctet(startIndex++);
    int hour = getOctet(startIndex++);
    int minute = getOctet(startIndex++);
    int second = getOctet(startIndex++);

    if ((year == 0) && (month == 0) && (day == 0) && (hour == 0) && (minute == 0) && (second == 0))
      return CalendarDate.UNKNOWN;

    // href.t00z.prob.f36.grib2
    if (hour > 23) {
      day += (hour / 24);
      hour = hour % 24;
    }

    return CalendarDate.of(null, year, month, day, hour, minute, second);
  }
 
Example #6
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private VariableDS makeOffsetCoordinate(NetcdfDataset result, Group group, String dimName, CalendarDate base,
    double[] values) {
  DataType dtype = DataType.DOUBLE;
  VariableDS timeVar = new VariableDS(result, group, null, dimName + "_offset", dtype, dimName, null, null); // LOOK
                                                                                                             // could
                                                                                                             // just
                                                                                                             // make a
                                                                                                             // CoordinateAxis1D
  timeVar.addAttribute(new Attribute(CDM.LONG_NAME, "offset hour from start of run for coordinate = " + dimName));
  timeVar.addAttribute(new ucar.nc2.Attribute("standard_name", "forecast_period"));
  timeVar.addAttribute(new ucar.nc2.Attribute(CF.CALENDAR, base.getCalendar().name()));
  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.UNITS, "hours since " + base));
  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.MISSING_VALUE, Double.NaN));

  // construct the values
  int ntimes = values.length;
  ArrayDouble.D1 timeCoordVals = (ArrayDouble.D1) Array.factory(DataType.DOUBLE, new int[] {ntimes}, values);
  timeVar.setCachedData(timeCoordVals);
  group.addVariable(timeVar);

  return timeVar;
}
 
Example #7
Source File: TestTimedCollection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void doit(String spec, int count, String name, String start, String end) throws IOException {
  Formatter errlog = new Formatter();
  MFileCollectionManager dcm = MFileCollectionManager.open("test", spec, null, errlog);
  TimedCollection collection = new TimedCollection(dcm, errlog);
  System.out.printf("spec= %s%n%s%n", spec, collection);
  String err = errlog.toString();
  if (err.length() > 0)
    System.out.printf("%s%n", err);
  System.out.printf("-----------------------------------%n");
  assertThat(collection.getDatasets()).hasSize(count);
  if (count == 0)
    return;
  Optional<Dataset> opt = collection.getDatasets().stream().filter(d -> d.location.endsWith(name)).findFirst();
  assertThat(opt.isPresent());
  opt.ifPresent(d -> {
    assertThat(d.dateRange.getStart()).isEqualTo(CalendarDate.parseISOformat(null, start));
    assertThat(d.dateRange.getEnd()).isEqualTo(CalendarDate.parseISOformat(null, end));
  });
}
 
Example #8
Source File: InvDatasetFcFmrc.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<DatasetBuilder> makeForecastDatasets(DatasetBuilder parent) throws IOException {

    List<DatasetBuilder> datasets = new ArrayList<>();

    for (CalendarDate forecastDate : fmrc.getForecastDates()) {
      String myname = name + "_" + FORECAST_NAME + forecastDate;
      myname = StringUtil2.replace(myname, ' ', "_");

      DatasetBuilder nested = new DatasetBuilder(parent);
      nested.setName(myname);
      nested.put(Dataset.UrlPath, this.configPath + "/" + FORECAST + "/" + myname);
      nested.put(Dataset.Id, this.configPath + "/" + FORECAST + "/" + myname);
      nested.addToList(Dataset.Documentation, new Documentation(null, null, null, "summary",
          "Data with the same forecast date, " + name + ", across different model runs."));
      nested.put(Dataset.TimeCoverage, new DateRange(CalendarDateRange.of(forecastDate, forecastDate)));
      datasets.add(nested);
    }

    return datasets;
  }
 
Example #9
Source File: FileCacheARC.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Show individual cache entries, add to formatter.
 *
 * @param format add to this
 */
@Override
public void showCache(Formatter format) {
  ArrayList<CacheElement.CacheFile> allFiles = new ArrayList<>(files.size());
  for (CacheElement elem : cache.values()) {
    allFiles.addAll(elem.list);
  }
  Collections.sort(allFiles); // sort so oldest are on top

  format.format("%nFileCacheARC %s (min=%d softLimit=%d hardLimit=%d scour=%d):%n", name, minElements, softLimit,
      hardLimit, period);
  format.format("isLocked  accesses lastAccess                   location %n");
  for (CacheElement.CacheFile file : allFiles) {
    String loc = file.ncfile != null ? file.ncfile.getLocation() : "null";
    CalendarDate cd = CalendarDate.of(file.getLastAccessed());
    format.format("%8s %9d %s %s %n", file.isLocked, file.getCountAccessed(), CalendarDateFormatter.toTimeUnits(cd),
        loc);
  }
  showStats(format);
}
 
Example #10
Source File: WriterCFPointAbstract.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void finish() throws IOException {
  if (llbb != null) {
    writer.updateAttribute(null, new Attribute(ACDD.LAT_MIN, llbb.getLowerLeftPoint().getLatitude()));
    writer.updateAttribute(null, new Attribute(ACDD.LAT_MAX, llbb.getUpperRightPoint().getLatitude()));
    writer.updateAttribute(null, new Attribute(ACDD.LON_MIN, llbb.getLowerLeftPoint().getLongitude()));
    writer.updateAttribute(null, new Attribute(ACDD.LON_MAX, llbb.getUpperRightPoint().getLongitude()));
  }

  if (!config.isNoTimeCoverage()) {
    if (minDate == null)
      minDate = CalendarDate.present();
    if (maxDate == null)
      maxDate = CalendarDate.present();
    writer.updateAttribute(null, new Attribute(ACDD.TIME_START, CalendarDateFormatter.toDateTimeStringISO(minDate)));
    writer.updateAttribute(null, new Attribute(ACDD.TIME_END, CalendarDateFormatter.toDateTimeStringISO(maxDate)));
  }

  writer.close();
}
 
Example #11
Source File: TestAggFmrc.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void FmrcRunDates() {

  List<CalendarDate> scanRunTimes = fmrcScan.getRunDates();
  List<CalendarDate> explicitRunTimes = fmrcExplicit.getRunDates();

  Assert.assertEquals(scanRunTimes.size(), 3);
  Assert.assertEquals(explicitRunTimes.size(), scanRunTimes.size());

  for (int i = 0; i < scanRunTimes.size(); i++) {
    Assert.assertTrue(explicitRunTimes.get(i).equals(scanRunTimes.get(i)));
  }

}
 
Example #12
Source File: TestDtWithCoverageReadingP.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void readAllEnsembles(Coverage cover, GridDatatype dt, CalendarDate rt_val, int rt_idx,
    CalendarDate time_val, int time_idx, CoordinateAxis1D ensAxis, CoordinateAxis1D vertAxis) {
  if (ensAxis == null)
    readAllVertLevels(cover, dt, rt_val, rt_idx, time_val, time_idx, 0, -1, vertAxis);
  else {
    for (int i = 0; i < ensAxis.getSize(); i++)
      readAllVertLevels(cover, dt, rt_val, rt_idx, time_val, time_idx, ensAxis.getCoordValue(i), i, vertAxis);
  }
}
 
Example #13
Source File: Grib1SectionProductDefinition.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Reference Date (octet 13-17).
 * Reference time of data – date and time of start of averaging or accumulation period.
 *
 * @return Reference Date as CalendarDate.
 */
public final CalendarDate getReferenceDate() {
  int century = getReferenceCentury() - 1;
  if (century == -1)
    century = 20;

  int year = getOctet(13);
  int month = getOctet(14);
  int day = getOctet(15);
  int hour = getOctet(16);
  int minute = getOctet(17);
  return CalendarDate.of(null, century * 100 + year, month, day, hour, minute, 0);
}
 
Example #14
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testTimeDelta(Variable timeVar, TimeDelta expectedDiff) throws IOException {
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  int[] shape = timeVar.getShape();
  Array vals = timeVar.read();
  for (int val = 1; val < shape[0]; val++) {
    CalendarDate today = calendarDateUnit.makeCalendarDate(vals.getInt(val));
    CalendarDate yesterday = calendarDateUnit.makeCalendarDate(vals.getInt(val - 1));
    long diff = today.getDifference(yesterday, expectedDiff.getUnit());
    assertThat(diff).isEqualTo(expectedDiff.getValue());
  }
}
 
Example #15
Source File: TestAggExisting.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testTimeDelta(Variable timeVar, TimeDelta expectedDiff) throws IOException {
  CalendarDateUnit calendarDateUnit = getCalendarDateUnit(timeVar);
  int[] shape = timeVar.getShape();
  Array vals = timeVar.read();
  for (int val = 1; val < shape[0]; val++) {
    CalendarDate today = calendarDateUnit.makeCalendarDate(vals.getInt(val));
    CalendarDate yesterday = calendarDateUnit.makeCalendarDate(vals.getInt(val - 1));
    long diff = today.getDifference(yesterday, expectedDiff.getUnit());
    assertThat(diff).isEqualTo(expectedDiff.getValue());
  }
}
 
Example #16
Source File: DateExtractorFromName.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CalendarDate getCalendarDateFromPath(String path) {
  Date d;
  if (useName) {
    Path p = Paths.get(path);
    d = DateFromString.getDateUsingDemarkatedCount(p.getFileName().toString(), dateFormatMark, '#');
  } else
    d = DateFromString.getDateUsingDemarkatedMatch(path, dateFormatMark, '#');

  return (d == null) ? null : CalendarDate.of(d);
}
 
Example #17
Source File: MFileCollectionManager.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
CalendarDate extractRunDateWithError(MFile mfile) {
  CalendarDate result = super.extractRunDateWithError(mfile);
  // if there isn't a DateExtractor, see if a mapping exists between
  // filenames and runtimes as defied by the coordValue attribute
  // in explicitly defined file aggregations (i.e. not a directory scan)
  if (result == null)
    if (!this.filesRunDateMap.isEmpty()) {
      String dateString = filesRunDateMap.get(mfile.getPath());
      result = CalendarDate.parseISOformat(null, dateString);
    }
  if (result == null)
    logger.error("Failed to find a run date associated with file {}", mfile.getPath());
  return result;
}
 
Example #18
Source File: TestGrib1Records.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public TestGrib1Records(String ds, int gdsTemplate, int param, long datalen, CalendarDate refdate) {
  this.filename = "../grib/src/test/data/" + ds;
  this.gdsTemplate = gdsTemplate;
  this.param = param;
  this.datalen = datalen;
  this.refdate = refdate;
  this.check = gdsTemplate >= 0;
}
 
Example #19
Source File: AbstractStationSubsetWriter.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected ClosestTimeStationFeatureSubset(StationTimeSeriesFeatureImpl stationFeat, CalendarDate wantedTime)
    throws IOException {
  super(stationFeat, stationFeat.getTimeUnit(), stationFeat.getAltUnits(), -1);
  this.stationFeat = stationFeat;
  CalendarDateRange cdr = stationFeat.getCalendarDateRange();
  if (cdr != null) {
    getInfo();
    info.setCalendarDateRange(cdr);
  }

  long smallestDiff = Long.MAX_VALUE;

  stationFeat.resetIteration();
  try {
    while (stationFeat.hasNext()) {
      PointFeature pointFeat = stationFeat.next();
      CalendarDate obsTime = pointFeat.getObservationTimeAsCalendarDate();
      long diff = Math.abs(obsTime.getMillis() - wantedTime.getMillis());

      if (diff < smallestDiff) {
        closestTime = obsTime;
      }
    }
  } finally {
    stationFeat.finish();
  }
}
 
Example #20
Source File: TestDtWithCoverageReadingSingleP.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public TestDtWithCoverageReadingSingleP(String endpoint, FeatureType type, String covName, String rt_val,
    Double ens_val, String time_val, Double vert_val, Integer time_idx) {
  this.endpoint = endpoint;
  this.type = type;
  this.covName = covName;
  this.gridName = (type == FeatureType.FMRC) ? "TwoD/" + covName : "Best/" + covName;
  this.rt_val = rt_val == null ? null : CalendarDate.parseISOformat(null, rt_val);
  this.ens_val = ens_val;
  this.time_val = time_val == null ? null : CalendarDate.parseISOformat(null, time_val);
  this.vert_val = vert_val;
  this.time_idx = time_idx;
}
 
Example #21
Source File: GridCoordSys.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<CalendarDate> getCalendarDates() {
  if (timeTaxis != null)
    return timeTaxis.getCalendarDates();

  else if (getRunTimeAxis() != null)
    return makeCalendarDates2D();

  else
    return new ArrayList<>();
}
 
Example #22
Source File: FmrcInvLite.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
RunTimeDatasetInventory(CalendarDate run) throws FileNotFoundException {
  double offset = FmrcInv.getOffsetInHours(base, run);
  for (int i = 0; i < runOffset.length; i++) {
    if (Misc.nearlyEquals(runOffset[i], offset)) {
      runIdx = i;
      break;
    }
  }
  if (runIdx < 0)
    throw new FileNotFoundException("No run date of " + run);
}
 
Example #23
Source File: GridCoordSys.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the list of time names, to be used for user selection.
 * The ith one refers to the ith time coordinate.
 *
 * @return List of ucar.nc2.util.NamedObject, or empty list.
 * @deprecated will move in ver 6
 */
@Deprecated
public List<NamedObject> getTimes() {
  List<CalendarDate> cdates = getCalendarDates();
  List<NamedObject> times = new ArrayList<>(cdates.size());
  for (CalendarDate cd : cdates) {
    times.add(new ucar.nc2.util.NamedAnything(cd.toString(), "calendar date"));
  }
  return times;
}
 
Example #24
Source File: NcTimePositionType.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static TimePositionType initBeginPosition(TimePositionType beginPosition, CalendarDate date)
    throws IOException {
  // TEXT
  beginPosition.setStringValue(date.toString());

  return beginPosition;
}
 
Example #25
Source File: TestCFRadial.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDates() throws IOException {
  try (RadialDatasetSweep ds = testData()) {
    CalendarDate trueStart = CalendarDate.of(null, 2008, 6, 4, 0, 15, 3);
    Assert.assertEquals(trueStart, ds.getCalendarDateStart());
    CalendarDate trueEnd = CalendarDate.of(null, 2008, 6, 4, 0, 22, 17);
    Assert.assertEquals(trueEnd, ds.getCalendarDateEnd());
  }
}
 
Example #26
Source File: CFRadialAdapter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void setStartDate() {
  String datetime = ds.getRootGroup().findAttributeString("time_coverage_start", null);
  if (datetime != null) {
    startDate = CalendarDate.parseISOformat(null, datetime).toDate();
  } else {
    startDate = calDateUnits.makeCalendarDate(time[0]).toDate();
  }
}
 
Example #27
Source File: NcssParamsBean.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CalendarDate getRequestedDate(Calendar cal) {
  if (date == null)
    return null;
  if (cal.equals(Calendar.getDefault()))
    return date;

  // otherwise must reparse
  return CalendarDateFormatter.isoStringToCalendarDate(cal, getTime());
}
 
Example #28
Source File: NcssPointParamsBean.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public SubsetParams makeSubset() {

    SubsetParams subset = new SubsetParams();

    // vars
    subset.set(SubsetParams.variables, var);

    // horiz
    if (stns != null)
      subset.set(SubsetParams.stations, stns);
    else if (hasLatLonBB())
      subset.set(SubsetParams.latlonBB, getLatLonBoundingBox());
    else if (hasLatLonPoint())
      subset.set(SubsetParams.latlonPoint, LatLonPoint.create(getLatitude(), getLongitude()));

    // time
    CalendarDate date = getRequestedDate(Calendar.getDefault());
    CalendarDateRange dateRange = getCalendarDateRange(Calendar.getDefault());
    if (isAllTimes()) {
      subset.set(SubsetParams.timeAll, true);

    } else if (date != null) {
      subset.set(SubsetParams.time, date);

    } else if (dateRange != null) {
      subset.set(SubsetParams.timeRange, dateRange);

    } else {
      subset.set(SubsetParams.timePresent, true);
    }

    if (timeWindow != null) {
      CalendarPeriod period = CalendarPeriod.of(timeWindow);
      subset.set(SubsetParams.timeWindow, period);
    }

    return subset;
  }
 
Example #29
Source File: TestGrib2Records.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public TestGrib2Records(String ds, int gdsTemplate, int param, long datalen, String refdateIso) {
  this.filename = "../grib/src/test/data/" + ds;
  this.gdsTemplate = gdsTemplate;
  this.pdsTemplate = param;
  this.datalen = datalen;
  this.refdate = CalendarDate.parseISOformat("ISO8601", refdateIso);
  this.check = gdsTemplate >= 0;
}
 
Example #30
Source File: PartitionCollectionImmutable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toString() {
  return "Partition{ " + name + '\'' +
  // ", directory='" + directory + '\'' +
      ", filename='" + filename + '\'' + ", partitionDate='" + partitionDate + '\'' + ", lastModified='"
      + CalendarDate.of(lastModified) + '\'' + ", fileSize='" + fileSize + '\'' + '}';
}