本文整理汇总了Java中ucar.nc2.NetcdfFile类的典型用法代码示例。如果您正苦于以下问题:Java NetcdfFile类的具体用法?Java NetcdfFile怎么用?Java NetcdfFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NetcdfFile类属于ucar.nc2包,在下文中一共展示了NetcdfFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readAndStoreStationIdsMap
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
/**
* Read the data for (optional) variable "station_id" from the netcdf file and store in the stationIdsMap.
*
* For each external location this method stores the external station name and the index
* that is used for the location in the netcdf file in stationIdsMap.
*
* Code copied and adapted from class nl.wldelft.fews.system.plugin.dataImport.NetcdfTimeSeriesTSParser
*
* @throws IOException
*/
public static Map<Integer, String> readAndStoreStationIdsMap(NetcdfFile netcdfDataset, String stationNameVarName) throws IOException {
// if no stations found, return empty hash map
Map<Integer, String> stationIdsMap = new LinkedHashMap<Integer, String>();
Variable stationIdsVar = netcdfDataset.findVariable(stationNameVarName);
if (stationIdsVar != null) {
// stations found, create hash map
int numberOfDimensions = stationIdsVar.getDimensions().size();
switch (numberOfDimensions) {
case 0:
stationIdsMap = readAndStoreStationIdVariable(stationIdsVar);
break;
case 1:
stationIdsMap = readAndStoreOneDimensionalStationIdsVariable(netcdfDataset, stationNameVarName);
break;
case 2: default:
stationIdsMap = readAndStoreTwoDimensionalStationIdsVariable(stationIdsVar, netcdfDataset, stationNameVarName);
break;
}
}
return stationIdsMap;
}
示例2: getHeader
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
public static String[] getHeader( String fileName) throws IOException {
//String file1 = fileName;
NetcdfFile ncFile = null;
String[] attributeNetCDF;
try {
ncFile = NetcdfFile.open(fileName);
} catch(java.lang.IllegalArgumentException ex) {
IOException e = new IOException("Not a netcdf file");
e.fillInStackTrace();
throw e;
}
List<?> globalAttList = ncFile.getGlobalAttributes();
attributeNetCDF = new String[globalAttList.size()];
for(int i=0; i < globalAttList.size(); i++ ) {
attributeNetCDF[i] = globalAttList.get(i).toString();
}
return attributeNetCDF;
}
示例3: readDataInfo
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
/**
* Read data info for mixed GRIB-1 and GRIB-2 data file
* @param fileName File name
* @param mdt Meteo data type
*/
public void readDataInfo(String fileName, MeteoDataType mdt) {
this.setFileName(fileName);
String iospClassName = "ucar.nc2.grib.collection.Grib2Iosp";
switch (mdt){
case GRIB1:
iospClassName = "ucar.nc2.grib.collection.Grib1Iosp";
break;
}
try {
ncfile = NetcdfFile.open(fileName, iospClassName, 0, null, null);
readDataInfo();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | IOException ex) {
Logger.getLogger(NetCDFDataInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例4: getDimensionValues
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
private boolean getDimensionValues(NetcdfFile ncfile) throws IOException, ParseException {
switch (_convention) {
case CF:
if (this._isHDFEOS) {
getDimValues_HDFEOS_SWATH();
} else {
getDimValues_CF();
if (this.getXDimension() == null || this.getYDimension() == null) {
if (this.findNCVariable("Longitude") != null && this.findNCVariable("Latitude") != null) {
this._isSWATH = true;
}
}
}
break;
case IOAPI:
getDimValues_IOAPI();
break;
case WRFOUT:
getDimValues_WRF(ncfile);
break;
default:
return false;
}
return true;
}
示例5: verifyHeightAxis
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
private void verifyHeightAxis(NetcdfFile netcdfFile, int size, double minHeight, double maxHeight) throws IOException {
NetcdfDataset netcdfDataset = NetcdfDataset.wrap(netcdfFile, Sets.newHashSet(Enhance.CoordSystems));
assertNotNull(netcdfDataset);
List<CoordinateSystem> coordinateSystems = netcdfDataset.getCoordinateSystems();
// assertEquals(1, coordinateSystems.size());
boolean heightAxisFound = false;
for (CoordinateSystem coordinateSystem : coordinateSystems) {
CoordinateAxis heightAxis = coordinateSystem.getHeightAxis();
if (heightAxis != null) {
heightAxisFound = true;
assertEquals(size, heightAxis.getSize());
assertEquals(maxHeight, heightAxis.getMaxValue(), 0.0);
assertEquals(minHeight, heightAxis.getMinValue(), 0.0);
}
}
assertTrue("Height axis not found in any coordinate system", heightAxisFound);
}
示例6: test
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
@Test
public void test() throws IOException, InvalidRangeException {
File file = new File("target/temp.nc");
file.delete();
NetCdfWriter n = new NetCdfWriter(file, "0.1");
Var<Long> v = n.addVariable("time", Long.class).longName("time in epoch milliseconds")
.units("epoch milliseconds").numRecords(2).build();
v.add(100L);
v.add(200L);
n.close();
// now read the file just written and assert
NetcdfFile nc = NetcdfFile.open(file.getCanonicalPath());
List<Attribute> attributes = nc.findGroup(null).getAttributes();
System.out.println(attributes);
assertFalse(attributes.isEmpty());
System.out.println(nc.getDimensions());
{
Array array = nc.readSection("time");
assertEquals(100, array.getLong(0));
assertEquals(200, array.getLong(1));
assertEquals(2, array.getSize());
}
}
示例7: read
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
@Override
public boolean read(final FastqSeq<A, Q> seq) throws ParseException {
String path;
for (path = getNextFile(); path != null; path = getNextFile()) {
logger.debug("Opening {}", path);
try (final NetcdfFile file = NetcdfFile.open(path)) {
logger.debug("Opened");
if (readSequence(file, seq)) {
return true;
}
} catch (final IOException e) {
throw new ParseException(e);
}
}
return false;
}
示例8: setValues
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
public void setValues(Object values) {
if (values instanceof NetcdfFile) {
this.ncFile = (NetcdfFile) values;
return;
}
throw new RuntimeException(this.getClass().getName() + ": cannot digest this type to set values");
}
示例9: initialize
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
public void initialize(File workingDir, String[] arguments) {
if (arguments.length != 1) {
throw new RuntimeException("NetcdfD3dHisDataObject expects one argument: netcdfFileName (relative to working dir)");
}
File netcdfFilePath = new File(workingDir, arguments[0]);
try {
netcdfFile = NetcdfFile.open(netcdfFilePath.getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException("NetcdfD3dHisDataObject could not open netcdf file " + netcdfFilePath.getAbsolutePath());
}
readNetCdfVariables();
}
示例10: createTimeInfo
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
/**
* Creates and returns an ITimeInfo object for the given variable in the given netcdfFile.
* If no time info available, then returns null.
*
* @param variable
* @param netcdfFile
* @param timeInfoCache
* @return timeInfo or null.
*/
public static IArrayTimeInfo createTimeInfo(Variable variable, NetcdfFile netcdfFile,
Map<Variable, IArrayTimeInfo> timeInfoCache) {
Variable timeVariable = findTimeVariableForVariable(variable, netcdfFile);
if (timeVariable == null) {//if data does not depend on time.
return null;
}
//if data depends on time.
IArrayTimeInfo timeInfo = timeInfoCache.get(timeVariable);
if (timeInfo == null) {
//create timeInfo.
double[] times;
try {
times = readTimes(timeVariable);
} catch (IOException e) {
throw new RuntimeException("Error while reading data from netcdf time variable '" + timeVariable.getShortName()
+ "'. Message was: " + e.getMessage(), e);
}
timeInfo = new TimeInfo(times);
//cache timeInfo.
timeInfoCache.put(timeVariable, timeInfo);
}
return timeInfo;
}
示例11: createGeometryInfo
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
/**
* Creates and returns an IGeometryInfo object for the given variable in the given netcdfFile.
* If no geometry info available, then returns null.
*
* @param variable
* @param netcdfFile
* @return geometryInfo or null.
*/
public static IArrayGeometryInfo createGeometryInfo(Variable variable, NetcdfFile netcdfFile) {
Variable latitudeVariable = findLatitudeVariableForVariable(variable, netcdfFile);
Variable longitudeVariable = findLongitudeVariableForVariable(variable, netcdfFile);
//currently only 2D grids are supported.
if (latitudeVariable == null || longitudeVariable == null) {//if data does not depend on space.
return null;
}
//if 2D grid.
IQuantityInfo latitudeQuantityInfo = new QuantityInfo(LATITUDE_STANDARD_NAME, latitudeVariable.getUnitsString());
IQuantityInfo longitudeQuantityInfo = new QuantityInfo(LONGITUDE_STANDARD_NAME, longitudeVariable.getUnitsString());
List<Dimension> latitudeVariableDimensions = latitudeVariable.getDimensions();
int[] latitudeValueIndices = new int[latitudeVariableDimensions.size()];
for (int n = 0; n < latitudeValueIndices.length; n++) {
latitudeValueIndices[n] = variable.findDimensionIndex(latitudeVariableDimensions.get(n).getShortName());
}
List<Dimension> longitudeVariableDimensions = longitudeVariable.getDimensions();
int[] longitudeValueIndices = new int[longitudeVariableDimensions.size()];
for (int n = 0; n < longitudeValueIndices.length; n++) {
longitudeValueIndices[n] = variable.findDimensionIndex(longitudeVariableDimensions.get(n).getShortName());
}
IArray latitudeArray = (IArray) readData(latitudeVariable);
IArray longitudeArray = (IArray) readData(longitudeVariable);
//the latitude and longitude coordinates are stored in the same order as in the netcdf file.
ArrayGeometryInfo geometryInfo = new ArrayGeometryInfo(latitudeArray, latitudeValueIndices,
latitudeQuantityInfo, longitudeArray, longitudeValueIndices, longitudeQuantityInfo,null,null,null);
return geometryInfo;
}
示例12: getLatitudeDimensionIndexForVariable
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
public static int getLatitudeDimensionIndexForVariable(Variable variable, NetcdfFile netcdfFile) {
Variable latitudeVariable = NetcdfUtils.findLatitudeVariableForVariable(variable, netcdfFile);
if (latitudeVariable == null) {
return -1;
}
List<Dimension> latitudeVariableDimensions = latitudeVariable.getDimensions();
return variable.findDimensionIndex(latitudeVariableDimensions.get(0).getShortName());
}
示例13: getDimensionIndexToFlipFor2DGrid
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
public static int getDimensionIndexToFlipFor2DGrid(Variable variable, NetcdfFile netcdfFile, GridStartCorner internalGridStartCorner) {
int latitudeDimensionIndex = NetcdfUtils.getLatitudeDimensionIndexForVariable(variable, netcdfFile);
if (latitudeDimensionIndex == -1) {
return -1;
}
Variable latitudeVariable = findLatitudeVariableForVariable(variable, netcdfFile);
if (latitudeVariable == null) {
return -1;
}
double[] latitudeValues = ((IArray) readData(latitudeVariable)).getValuesAsDoubles();
boolean ascending = isAscending(latitudeValues);
switch (internalGridStartCorner) {
case NORTH_WEST:
if (ascending) {
return latitudeDimensionIndex;
} else {
return -1;
}
case SOUTH_WEST:
if (ascending) {
return -1;
} else {
return latitudeDimensionIndex;
}
case UNKNOWN: default:
return -1;
}
}
示例14: getVariableForExchangeItem
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
public static Variable getVariableForExchangeItem(NetcdfFile netcdfFile, IExchangeItem item) {
String variableName = getVariableName(item);
Variable variable = netcdfFile.findVariable(variableName);
if (variable == null) {
throw new IllegalStateException("Cannot find variable '" + variableName + "' for exchangeItem with id '" + item.getId() + "' in netcdf file " + netcdfFile.getLocation());
}
return variable;
}
示例15: testDelft3dHisFileConcatenation
import ucar.nc2.NetcdfFile; //导入依赖的package包/类
public void testDelft3dHisFileConcatenation() throws IOException {
File firstFile = new File(this.testRunDataDir, "westerscheldt_part1_his.nc");
File targetFile = new File(this.testRunDataDir, "westerscheldt_his.nc");
if (targetFile.exists()) BBUtils.deleteFileOrDir(targetFile);
assertFalse(targetFile.exists());
NetcdfFileConcatenater.main(new String[]{targetFile.getAbsolutePath(), firstFile.getAbsolutePath()});
assertTrue(targetFile.exists());
File secondFile = new File(this.testRunDataDir, "westerscheldt_part2_his.nc");
NetcdfFileConcatenater.main(new String[]{targetFile.getAbsolutePath(), secondFile.getAbsolutePath()});
// find size of time array for original files and concatenated file
long size1 = NetcdfFile.open(firstFile.toString()).findVariable("time").read().getSize();
long size2 = NetcdfFile.open(secondFile.toString()).findVariable("time").read().getSize();
long size3 = NetcdfFile.open(targetFile.toString()).findVariable("time").read().getSize();
assertEquals(size3, size1 + size2);
}