ucar.nc2.dt.point.DapperDataset Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of netcdf Show documentation
Show all versions of netcdf Show documentation
The NetCDF-Java Library is a Java interface to NetCDF files,
as well as to many other types of scientific data formats.
/*
* 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.nc2.*;
import ucar.nc2.constants.AxisType;
import ucar.nc2.dods.DODSNetcdfFile;
import ucar.nc2.units.DateUnit;
import ucar.nc2.dt.*;
import ucar.nc2.util.CancelTask;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.dataset.CoordinateAxis;
import ucar.nc2.dataset.StructureDS;
import ucar.unidata.geoloc.LatLonRect;
import ucar.unidata.geoloc.LatLonPointImpl;
import ucar.unidata.geoloc.LatLonPoint;
import ucar.ma2.ArrayStructure;
import ucar.ma2.StructureMembers;
import ucar.ma2.StructureData;
import java.util.*;
import java.io.IOException;
/**
* Handles datasets using Dapper doubley nested sequences.
*
* @author caron
*/
public class DapperDataset extends PointObsDatasetImpl implements TypedDatasetFactoryIF {
static private final String ID = "_id";
static public boolean isValidFile(NetcdfFile ds) {
String conv = ds.findAttValueIgnoreCase(null, "Conventions", null);
if (conv == null) return false;
StringTokenizer stoke = new StringTokenizer(conv, ",");
while (stoke.hasMoreTokens()) {
String toke = stoke.nextToken().trim();
if (toke.equalsIgnoreCase("epic-insitu-1.0"))
return true;
}
return false;
}
static public PointObsDataset factory(NetcdfDataset ds) throws IOException {
Variable latVar = null, timeVar = null;
// identify key variables
List axes = ds.getCoordinateAxes();
for (int i = 0; i < axes.size(); i++) {
CoordinateAxis axis = (CoordinateAxis) axes.get(i);
if (axis.getAxisType() == AxisType.Lat)
latVar = axis;
if (axis.getAxisType() == AxisType.Time)
timeVar = axis;
}
// lat, lon are always in the outer; gotta use name to fetch wrapping variable
Structure outerSequence = getWrappingParent(ds, latVar);
// depth may be in inner or outer
boolean isProfile = getWrappingParent(ds, timeVar) == outerSequence;
if (isProfile)
return new DapperPointDataset( ds);
else
return new DapperStationDataset( ds);
}
static private StructureDS getWrappingParent( NetcdfDataset ds, Variable v) {
String name = v.getParentStructure().getName();
return (StructureDS) ds.findVariable(name);
}
/////////////////////////////////////////////////
// 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 DapperDataset( ncd);
}
public DapperDataset() {}
/////////////////////////////////////////////////
protected DODSNetcdfFile dodsFile;
protected Variable latVar, lonVar, altVar, timeVar;
protected StructureDS innerSequence, outerSequence;
protected boolean isProfile = false, fatal = false;
public DapperDataset(NetcdfDataset ds) throws IOException {
super(ds);
// identify key variables
List axes = ds.getCoordinateAxes();
for (int i = 0; i < axes.size(); i++) {
CoordinateAxis axis = (CoordinateAxis) axes.get(i);
if (axis.getAxisType() == AxisType.Lat)
latVar = axis;
if (axis.getAxisType() == AxisType.Lon)
lonVar = axis;
if (axis.getAxisType() == AxisType.Height)
altVar = axis;
if (axis.getAxisType() == AxisType.Time)
timeVar = axis;
}
if (latVar == null) {
parseInfo.append("Missing latitude variable");
fatal = true;
}
if (lonVar == null) {
parseInfo.append("Missing longitude variable");
fatal = true;
}
if (altVar == null) {
parseInfo.append("Missing altitude variable");
}
if (timeVar == null) {
parseInfo.append("Missing time variable");
fatal = true;
}
// lat, lon are always in the outer; gotta use name to fetch wrapping variable
outerSequence = getWrappingParent(ds, latVar);
// depth may be in inner or outer
boolean isProfile = getWrappingParent(ds, timeVar) == outerSequence;
innerSequence = isProfile ? getWrappingParent(ds, altVar) : getWrappingParent(ds, timeVar);
// Need the underlying DODSNetcdfFile
NetcdfFile refFile = ds.getReferencedFile();
while (dodsFile == null) {
if (refFile instanceof DODSNetcdfFile)
dodsFile = (DODSNetcdfFile) refFile;
else if (refFile instanceof NetcdfDataset)
refFile = ((NetcdfDataset)refFile).getReferencedFile();
else
throw new IllegalArgumentException("Must be a DODSNetcdfFile");
}
// create member variables
List recordMembers = outerSequence.getVariables();
for (int i = 0; i < recordMembers.size(); i++) {
Variable v = (Variable) recordMembers.get(i);
dataVariables.add( v);
}
recordMembers = innerSequence.getVariables();
for (int i = 0; i < recordMembers.size(); i++) {
Variable v = (Variable) recordMembers.get(i);
dataVariables.add( v);
}
dataVariables.remove(latVar);
dataVariables.remove(lonVar);
dataVariables.remove(altVar);
dataVariables.remove(timeVar);
dataVariables.remove(innerSequence);
dataVariables.remove( ds.findVariable("_id"));
dataVariables.remove( ds.findVariable("attributes"));
dataVariables.remove( ds.findVariable("variable_attributes"));
setBoundingBox();
try {
timeUnit = new DateUnit(timeVar.getUnitsString());
} catch (Exception e) {
parseInfo.append("Bad time units= "+ timeVar.getUnitsString());
fatal = true;
}
Attribute time_range = ncfile.findGlobalAttribute("time_range");
double time_start = time_range.getNumericValue(0).doubleValue();
double time_end = time_range.getNumericValue(1).doubleValue();
startDate = timeUnit.makeDate(time_start);
endDate = timeUnit.makeDate(time_end);
title = ds.findAttValueIgnoreCase(null,"title","");
desc = ds.findAttValueIgnoreCase(null,"description", "");
}
protected void setTimeUnits() {}
protected void setStartDate() {}
protected void setEndDate() {}
protected void setBoundingBox() {
Attribute lon_range = ncfile.findGlobalAttribute("lon_range");
double lon_start = lon_range.getNumericValue(0).doubleValue();
double lon_end = lon_range.getNumericValue(1).doubleValue();
Attribute lat_range = ncfile.findGlobalAttribute("lat_range");
double lat_start = lat_range.getNumericValue(0).doubleValue();
double lat_end = lat_range.getNumericValue(1).doubleValue();
boundingBox = new LatLonRect(new LatLonPointImpl(lat_start, lon_start), new LatLonPointImpl(lat_end, lon_end));
}
///////////////////////////////////////////////////////////////////////////////////////////
public int getDataCount() {
return -1;
}
public List getData(CancelTask cancel) throws IOException {
String CE = outerSequence.getName();
ArrayStructure as = (ArrayStructure) dodsFile.readWithCE(outerSequence, CE);
extractMembers(as);
int n = (int) as.getSize();
List dataList = new ArrayList(n);
for (int i=0; i="+bb.getLowerLeftPoint().getLatitude()+"&"+
latVar.getName()+"<="+bb.getUpperRightPoint().getLatitude()+"&"+
lonVar.getName()+">="+bb.getLowerLeftPoint().getLongitude()+"&"+
lonVar.getName()+"<="+bb.getUpperRightPoint().getLongitude();
}
private String makeTimeRange( Date start, Date end) {
double startValue = timeUnit.makeValue(start);
double endValue = timeUnit.makeValue(end);
return timeVar.getName()+">="+startValue+"&"+ // LOOK
timeVar.getName()+"<="+endValue;
}
private StructureMembers.Member latMember, lonMember, innerMember, altMember, timeMember;
private void extractMembers( ArrayStructure as) {
StructureMembers members = as.getStructureMembers();
latMember = members.findMember(latVar.getShortName());
lonMember = members.findMember(lonVar.getShortName());
innerMember = members.findMember(innerSequence.getShortName());
StructureData first = as.getStructureData(0);
StructureData innerFirst = first.getScalarStructure(innerMember);
StructureMembers innerMembers = innerFirst.getStructureMembers();
if (isProfile) {
timeMember = members.findMember(timeVar.getShortName());
altMember = innerMembers.findMember(altVar.getShortName());
} else {
timeMember = innerMembers.findMember(timeVar.getShortName());
altMember = members.findMember(altVar.getShortName());
}
}
// return List of Station
public void readStations(List stations) throws IOException {
String CE = latVar.getShortName()+","+lonVar.getShortName()+","+altVar.getShortName()+
","+ID;
ArrayStructure as = (ArrayStructure) dodsFile.readWithCE(outerSequence, CE);
StructureMembers members = as.getStructureMembers();
StructureMembers.Member latMember = members.findMember(latVar.getShortName());
StructureMembers.Member lonMember = members.findMember(lonVar.getShortName());
StructureMembers.Member altMember = members.findMember(altVar.getShortName());
StructureMembers.Member idMember = members.findMember(ID);
int n = (int) as.getSize();
for (int i=0; i