
ucar.nc2.dt.point.UnidataStationObsDataset Maven / Gradle / Ivy
/*
* Copyright 1998-2009 University Corporation for Atmospheric Research/Unidata
*
* Portions of this software were developed by the Unidata Program at the
* University Corporation for Atmospheric Research.
*
* Access and use of this software shall impose the following obligations
* and understandings on the user. The user is granted the right, without
* any fee or cost, to use, copy, modify, alter, enhance and distribute
* this software, and any derivative works thereof, and its supporting
* documentation for any purpose whatsoever, provided that this entire
* notice appears in all copies of the software, derivative works and
* supporting documentation. Further, UCAR requests that the user credit
* UCAR/Unidata in any publications that result from the use of this
* software or in any product that includes this software. The names UCAR
* and/or Unidata, however, may not be used in any advertising or publicity
* to endorse or promote any products or commercial entity unless specific
* written permission is obtained from UCAR/Unidata. The user also
* understands that UCAR/Unidata is not obligated to provide the user with
* any support, consulting, training or assistance of any kind with regard
* to the use, operation and performance of this software nor to provide
* the user with any updates, revisions, new versions or "bug fixes."
*
* THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package ucar.nc2.dt.point;
import ucar.ma2.*;
import ucar.nc2.*;
import ucar.nc2.constants.FeatureType;
import ucar.nc2.constants.AxisType;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.util.CancelTask;
import ucar.nc2.dt.*;
import java.io.*;
import java.util.*;
import java.text.ParseException;
/**
* This handles station datasets in "Unidata Observation Dataset v1.0" implemented as linked or contiguous lists.
* For "multidimensional Structures" use UnidataStationObsMultidimDataset
*
* @deprecated use ucar.nc2.ft.point
* @author caron
*/
public class UnidataStationObsDataset extends StationObsDatasetImpl implements TypedDatasetFactoryIF {
static private org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UnidataStationObsDataset.class);
static public boolean isValidFile(NetcdfFile ds) {
if (!ds.findAttValueIgnoreCase(null, "cdm_data_type", "").equalsIgnoreCase(FeatureType.STATION.toString()) &&
!ds.findAttValueIgnoreCase(null, "cdm_datatype", "").equalsIgnoreCase(FeatureType.STATION.toString()))
return false;
String conv = ds.findAttValueIgnoreCase(null, "Conventions", null);
if (conv == null) return false;
boolean convOk = false;
StringTokenizer stoke = new StringTokenizer(conv, ",");
while (stoke.hasMoreTokens()) {
String toke = stoke.nextToken().trim();
if (toke.equalsIgnoreCase("Unidata Observation Dataset v1.0"))
convOk = true;
}
if (!convOk) return false;
// must have this field to be a linked list
Variable stationIndexVar = UnidataObsDatasetHelper.findVariable(ds, "parent_index");
if (stationIndexVar == null) return false;
return true;
}
// TypedDatasetFactoryIF
public boolean isMine(NetcdfDataset ds) {
return isValidFile(ds);
}
public TypedDataset open(NetcdfDataset ncd, ucar.nc2.util.CancelTask task, StringBuilder errlog) throws IOException {
return new UnidataStationObsDataset(ncd);
}
public UnidataStationObsDataset() {
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private Variable latVar, lonVar, altVar, timeVar, timeNominalVar;
private Variable lastVar, prevVar, firstVar, nextVar, numChildrenVar;
private Variable stationIndexVar, stationIdVar, stationDescVar, numStationsVar;
private boolean isForwardLinkedList, isBackwardLinkedList, isContiguousList;
private Structure recordVar;
private RecordDatasetHelper recordHelper;
public UnidataStationObsDataset(NetcdfDataset ds) throws IOException {
super(ds);
// coordinate variables
latVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Lat);
lonVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Lon);
altVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Height);
timeVar = UnidataObsDatasetHelper.getCoordinate(ds, AxisType.Time);
if (latVar == null)
throw new IllegalStateException("Missing latitude variable");
if (lonVar == null)
throw new IllegalStateException("Missing longitude coordinate variable");
if (timeVar == null)
throw new IllegalStateException("Missing time coordinate variable");
// variables that link the structures together
timeNominalVar = UnidataObsDatasetHelper.findVariable(ds, "time_nominal");
lastVar = UnidataObsDatasetHelper.findVariable(ds, "lastChild");
prevVar = UnidataObsDatasetHelper.findVariable(ds, "prevChild");
firstVar = UnidataObsDatasetHelper.findVariable(ds, "firstChild");
nextVar = UnidataObsDatasetHelper.findVariable(ds, "nextChild");
numChildrenVar = UnidataObsDatasetHelper.findVariable(ds, "numChildren");
stationIndexVar = UnidataObsDatasetHelper.findVariable(ds, "parent_index");
isForwardLinkedList = (firstVar != null) && (nextVar != null);
isBackwardLinkedList = (lastVar != null) && (prevVar != null);
isContiguousList = !isForwardLinkedList && !isBackwardLinkedList && (firstVar != null) && (numChildrenVar != null);
if (!isForwardLinkedList && !isBackwardLinkedList && !isContiguousList) {
if (firstVar != null)
throw new IllegalStateException("Missing nextVar (linked list) or numChildrenVar (contiguous list) variable");
if (lastVar != null)
throw new IllegalStateException("Missing prevVar (linked list) variable");
if (nextVar != null)
throw new IllegalStateException("Missing firstVar (linked list) variable");
if (prevVar != null)
throw new IllegalStateException("Missing lastVar (linked list) variable");
}
// station variables
stationIdVar = UnidataObsDatasetHelper.findVariable(ds, "station_id");
stationDescVar = UnidataObsDatasetHelper.findVariable(ds, "station_description");
numStationsVar = UnidataObsDatasetHelper.findVariable(ds, "number_stations");
if (stationIdVar == null)
throw new IllegalStateException("Missing station id variable");
// fire up the record helper - LOOK assumes its the record dimension
recordHelper = new RecordDatasetHelper(ds, timeVar.getShortName(), timeNominalVar == null ? null : timeNominalVar.getShortName(),
dataVariables, parseInfo);
recordHelper.setStationInfo(stationIndexVar.getShortName(), stationDescVar == null ? null : stationDescVar.getShortName());
removeDataVariable(stationIndexVar.getShortName());
removeDataVariable(timeVar.getShortName());
if (timeNominalVar != null)
removeDataVariable(timeNominalVar.getShortName());
if (prevVar != null)
removeDataVariable(prevVar.getShortName());
if (nextVar != null)
removeDataVariable(nextVar.getShortName());
recordVar = recordHelper.recordVar;
timeUnit = recordHelper.timeUnit;
readStations(); // LOOK try to defer this
// get min, max date
startDate = UnidataObsDatasetHelper.getStartDate(ds);
endDate = UnidataObsDatasetHelper.getEndDate(ds);
boundingBox = UnidataObsDatasetHelper.getBoundingBox(ds);
if (null == boundingBox)
setBoundingBox();
// kludge Robb not following spec
if (null == startDate) {
Variable minTimeVar = ds.findVariable("minimum_time_observation");
int minTimeValue = minTimeVar.readScalarInt();
startDate = timeUnit.makeDate(minTimeValue);
}
if (null == endDate) {
Variable maxTimeVar = ds.findVariable("maximum_time_observation");
int maxTimeValue = maxTimeVar.readScalarInt();
endDate = timeUnit.makeDate(maxTimeValue);
}
title = ds.findAttValueIgnoreCase(null, "title", "");
desc = ds.findAttValueIgnoreCase(null, "description", "");
}
private void readStations() throws IOException {
// get the station info
Array stationIdArray = stationIdVar.read();
ArrayChar stationDescArray = null;
if (stationDescVar != null)
stationDescArray = (ArrayChar) stationDescVar.read();
Array latArray = latVar.read();
Array lonArray = lonVar.read();
Array elevArray = (altVar != null) ? altVar.read() : null;
Array firstRecordArray = (isForwardLinkedList || isContiguousList) ? firstVar.read() : lastVar.read();
Array numChildrenArray = null;
if (numChildrenVar != null)
numChildrenArray = numChildrenVar.read();
// how many are valid stations ?
Dimension stationDim = UnidataObsDatasetHelper.findDimension(ncfile, "station");
int n = 0;
if (numStationsVar != null)
n = numStationsVar.readScalarInt();
else
n = stationDim.getLength();
// loop over stations
Index ima = latArray.getIndex();
recordHelper.stnHash = new HashMap
© 2015 - 2025 Weber Informatics LLC | Privacy Policy