Java Code Examples for ucar.nc2.NetcdfFile#findVariable()

The following examples show how to use ucar.nc2.NetcdfFile#findVariable() . 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: CurvilinearGridPointMappingTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test GridCoordSystem.getLatLon()
 *
 * @throws IOException if ...
 * @throws InvalidRangeException if ...
 */
@Test
public void checkGridCoordSystem_getLatLon() throws IOException, InvalidRangeException {
  int[] origin = new int[] {j, i};
  int[] shape = new int[] {1, 1};

  NetcdfFile ncf = NetcdfFiles.open(datasetLocation);
  Variable latVar = ncf.findVariable("lat");
  Array latArray = latVar.read(origin, shape);
  Variable lonVar = ncf.findVariable("lon");
  Array lonArray = lonVar.read(origin, shape);

  double latVal = latArray.getDouble(latArray.getIndex());
  double lonVal = lonArray.getDouble(lonArray.getIndex());

  GridDataset gd = GridDataset.open(datasetLocation);
  GeoGrid gg = gd.findGridByName("hs");
  GridCoordSystem gridCoordSys = gg.getCoordinateSystem();
  // gridCoordSys.getXHorizAxis().;
  // gridCoordSys.getYHorizAxis();
  LatLonPoint llPnt = gridCoordSys.getLatLon(170, 62);

  Assert.assertEquals(lat, llPnt.getLatitude(), 0.001);
  Assert.assertEquals(lon, llPnt.getLongitude(), 0.001);
}
 
Example 2
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Array read(TimeInventory.Instance timeInstance, String fullNameEsc, List<Range> innerSection,
    HashMap<String, NetcdfDataset> openFilesRead) throws IOException, InvalidRangeException {
  NetcdfFile ncfile = open(timeInstance.getDatasetLocation(), openFilesRead);
  if (ncfile == null)
    return null; // file might be deleted ??

  Variable v = ncfile.findVariable(fullNameEsc);
  if (v == null)
    return null; // v could be missing, return missing data i think

  // assume time is first dimension LOOK: out of-order; ensemble; section different ??
  Range timeRange = new Range(timeInstance.getDatasetIndex(), timeInstance.getDatasetIndex());
  Section.Builder sb = Section.builder().appendRanges(innerSection);
  sb.insertRange(0, timeRange);
  return v.read(sb.build());
}
 
Example 3
Source File: WriterCFPointAbstract.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeExtraVariables() throws IOException {
  if (extra == null)
    return;

  for (Variable v : extra) {
    NetcdfFile ncfile = writer.getOutputFile();
    Variable mv = ncfile.findVariable(v.getFullName());
    if (mv == null)
      continue; // may be removed
    try {
      writer.write(mv, v.read());
    } catch (InvalidRangeException e) {
      e.printStackTrace(); // cant happen haha
    }
  }
}
 
Example 4
Source File: AggregationOuter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array read(AggDatasetOuter dset, NetcdfFile ncfile) throws IOException {
  invocation++;

  Array data = getData(dset.getId());
  if (data != null)
    return data;
  if (type == Type.joinNew)
    return null;

  Variable v = ncfile.findVariable(varName);
  data = v.read();
  putData(dset.getId(), data);
  if (debugCache)
    System.out.println("caching " + varName + " complete data");
  return data;
}
 
Example 5
Source File: Suomi.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @param ncfile the NetcdfFile to test
 * @return true if we think this is a Zebra file.
 */
public static boolean isMine(NetcdfFile ncfile) {

  Variable v = ncfile.findVariable("time_offset");
  if (v == null || !v.isCoordinateVariable())
    return false;
  String desc = v.getDescription();
  if (desc == null || (!desc.equals("Time delta from start_time")
      && !desc.equals("PWV window midpoint time delta from start_time")))
    return false;

  if (null == ncfile.findGlobalAttribute("start_date"))
    return false;
  return null != ncfile.findGlobalAttribute("start_time");

}
 
Example 6
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Variable findVariable(NetcdfFile ncfile, Variable client) {
  Variable v = ncfile.findVariable(client.getFullNameEscaped());
  if (v == null) { // might be renamed
    VariableEnhanced ve = (VariableEnhanced) client;
    v = ncfile.findVariable(ve.getOriginalName());
  }
  return v;
}
 
Example 8
Source File: CFGridWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addCoordinateTransform(GridCoordSystem gcs, NetcdfFile ncd, List<String> varNameList,
    List<Variable> varList) {

  for (CoordinateTransform ct : gcs.getCoordinateTransforms()) {
    Variable v = ncd.findVariable(ct.getName());
    if (!varNameList.contains(ct.getName()) && (null != v)) {
      varNameList.add(ct.getName());
      varList.add(v);
    }
  }

}
 
Example 9
Source File: DAPDownloader.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
static List<Dimension> filterDimensions(List<String> variables, NetcdfFile netcdfFile) {
    final List<Dimension> filteredDimensions = new ArrayList<Dimension>();

    for (String variableName : variables) {
        final Variable variable = netcdfFile.findVariable(variableName);
        for (Dimension dimension : variable.getDimensions()) {
            if (!filteredDimensions.contains(dimension)) {
                filteredDimensions.add(dimension);
            }
        }
    }

    return filteredDimensions;
}
 
Example 10
Source File: Suomi.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean isMine(NetcdfFile ncfile) {
  Variable v = ncfile.findVariable("time_offset");
  if (v == null || !v.isCoordinateVariable())
    return false;
  String desc = v.getDescription();
  if (desc == null || (!desc.equals("Time delta from start_time")
      && !desc.equals("PWV window midpoint time delta from start_time")))
    return false;

  if (null == ncfile.findGlobalAttribute("start_date"))
    return false;
  return null != ncfile.findGlobalAttribute("start_time");
}
 
Example 11
Source File: TestCurvilinearGridPointMapping.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test CoverageCoordSys.HorizCoordSys.getLatLon()
 *
 * @throws IOException if ...
 * @throws InvalidRangeException if ...
 */
@Test
public void checkGridCoordSystem_getLatLon() throws IOException, InvalidRangeException {
  int[] origin = new int[] {j, i};
  int[] shape = new int[] {1, 1};

  NetcdfFile ncf = NetcdfFiles.open(datasetLocation);
  Variable latVar = ncf.findVariable("lat");
  Array latArray = latVar.read(origin, shape);
  Variable lonVar = ncf.findVariable("lon");
  Array lonArray = lonVar.read(origin, shape);

  double latVal = latArray.getDouble(0);
  double lonVal = lonArray.getDouble(0);
  logger.debug("{}, {}", latVal, lonVal);

  try (FeatureDatasetCoverage cc = CoverageDatasetFactory.open(datasetLocation)) {
    Assert.assertNotNull(datasetLocation, cc);
    CoverageCollection gcs = cc.findCoverageDataset(FeatureType.CURVILINEAR);
    Assert.assertNotNull("gcs", gcs);
    Coverage cover = gcs.findCoverage(covName);
    Assert.assertNotNull(covName, cover);

    CoverageCoordSys gridCoordSys = cover.getCoordSys();
    Assert.assertNotNull("CoverageCoordSys", gridCoordSys);
    HorizCoordSys hcs = gridCoordSys.getHorizCoordSys();
    Assert.assertNotNull("HorizCoordSys", hcs);

    LatLonPoint llPnt = hcs.getLatLon(j, i);
    Assert2.assertNearlyEquals(lat, llPnt.getLatitude());
    Assert2.assertNearlyEquals(lon, llPnt.getLongitude());
  }
}
 
Example 12
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param ncfile the NetcdfFile to test
 * @return true if we think this is a IFPSConvention file.
 */
public static boolean isMine(NetcdfFile ncfile) {
  // check that file has a latitude and longitude variable, and that latitude has an attribute called
  // projectionType
  boolean geoVarsCheck;
  Variable v = ncfile.findVariable("latitude");
  if (null != ncfile.findVariable("longitude") && (null != v)) {
    geoVarsCheck = null != ncfile.findAttValueIgnoreCase(v, "projectionType", null);
  } else {
    // bail early
    return false;
  }

  // check that there is a global attribute called fileFormatVersion, and that it has one
  // of two known values
  boolean fileFormatCheck;
  Attribute ff = ncfile.findGlobalAttributeIgnoreCase("fileFormatVersion");
  if (ff != null) {
    String ffValue = ff.getStringValue();
    // two possible values (as of now)
    fileFormatCheck = (ffValue.equalsIgnoreCase("20030117") || ffValue.equalsIgnoreCase("20010816"));
  } else {
    // bail
    return false;
  }

  // both must be true
  return (geoVarsCheck && fileFormatCheck);
}
 
Example 13
Source File: TestN4problems.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void utestEnum() throws IOException {
  H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header"));
  String filename = TestN4reading.testDir + "nc4/tst_enum_data.nc";
  NetcdfFile ncfile = NetcdfFile.open(filename);
  Variable v = ncfile.findVariable("primary_cloud");
  Array data = v.read();
  System.out.println("\n**** testReadNetcdf4 done\n\n" + ncfile);
  logger.debug(Ncdump.printArray(data, "primary_cloud", null));
  ncfile.close();
  H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl());
}
 
Example 14
Source File: Aggregation.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Variable findVariable(NetcdfFile ncfile, Variable mainV) {
  Variable v = ncfile.findVariable(mainV.getFullNameEscaped());
  if (v == null) { // might be renamed
    VariableEnhanced ve = (VariableEnhanced) mainV;
    v = ncfile.findVariable(ve.getOriginalName()); // LOOK not escaped
  }
  return v;
}
 
Example 15
Source File: Aggregation.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Variable findVariable(NetcdfFile ncfile, Variable mainV) {
  Variable v = ncfile.findVariable(mainV.getFullNameEscaped());
  if (v == null) { // might be renamed
    VariableEnhanced ve = (VariableEnhanced) mainV;
    v = ncfile.findVariable(ve.getOriginalName()); // LOOK not escaped
  }
  return v;
}
 
Example 16
Source File: BufrMessageViewer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void read() {
  try {
    NetcdfFile ncd = makeBufrMessageAsDataset(m);
    SequenceDS v = (SequenceDS) ncd.findVariable(BufrIosp2.obsRecordName);
    try (StructureDataIterator iter = v.getStructureIterator(-1)) {
      while (iter.hasNext())
        iter.next();

    }
    setReadOk(true);
  } catch (IOException e) {
    setReadOk(false);
  }
}
 
Example 17
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Searches all the variables that the given variable depends on,
 * and returns the first variable that is a valid latitude variable.
 * If cannot find a valid latitude variable, then returns null.
 *
 * @param variable
 * @param netcdfFile
 * @return latitudeVariable or null.
 */
private static Variable findLatitudeVariableForVariable(Variable variable, NetcdfFile netcdfFile) {
	//search coordinate variables.
	List<Dimension> dimensions = variable.getDimensions();
	for (Dimension dimension : dimensions) {
		Variable dimensionVariable = netcdfFile.findVariable(dimension.getShortName());
		if (dimensionVariable == null || !dimensionVariable.isCoordinateVariable()) {
			continue;
		}

		if (isLatitudeVariable(dimensionVariable)) {
			return dimensionVariable;
		}
	}

	//search auxiliary coordinate variables.
	//according to the CF-convention (see http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5/cf-conventions.html#coordinate-system):
	//"An application that is trying to find the latitude coordinate of a variable should always look first to see
	//if any of the variable's dimensions correspond to a latitude coordinate variable. If the latitude coordinate
	//is not found this way, then the auxiliary coordinate variables listed by the coordinates attribute should
	//be checked. Note that it is permissible, but optional, to list coordinate variables as well as auxiliary
	//coordinate variables in the coordinates attribute. The axis attribute is not allowed for auxiliary coordinate
	//variables. Auxiliary coordinate variables which lie on the horizontal surface can be identified as such by their
	//dimensions being horizontal. Horizontal dimensions are those whose coordinate variables have an axis attribute
	//of X or Y, or a units attribute indicating latitude and longitude (see Chapter 4, Coordinate Types ).
	String coordinates = getAttributeStringValue(variable, COORDINATES_ATTRIBUTE_NAME);
	if (coordinates != null) {
		String[] strings = coordinates.split("\\s+");
		for (String auxiliaryCoordinateVariableName : strings) {
			Variable auxiliaryCoordinateVariable = netcdfFile.findVariable(auxiliaryCoordinateVariableName);
			if (auxiliaryCoordinateVariable == null) {
				continue;
			}

			if (isLatitudeVariable(auxiliaryCoordinateVariable)) {
				return auxiliaryCoordinateVariable;
			}
		}
	}

	return null;
}
 
Example 18
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Searches all the variables that the given variable depends on,
 * and returns the first variable that is a valid time variable.
 * If cannot find a valid time variable, then returns null.
 *
 * @param variable
 * @param netcdfFile
 * @return timeVariable or null.
 */
public static Variable findTimeVariableForVariable(Variable variable, NetcdfFile netcdfFile) {
	List<Dimension> dimensions = variable.getDimensions();
	for (Dimension dimension : dimensions) {
		Variable dimensionVariable = netcdfFile.findVariable(dimension.getShortName());
		if (dimensionVariable == null || !dimensionVariable.isCoordinateVariable()) {
			continue;
		}

		if(isTimeVariable(dimensionVariable)) {
			return dimensionVariable;
		}
	}

	//search auxiliary coordinate variables.
	//according to the CF-convention (see http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5/cf-conventions.html#coordinate-system):
	//"An application that is trying to find the latitude coordinate of a variable should always look first to see
	//if any of the variable's dimensions correspond to a latitude coordinate variable. If the latitude coordinate
	//is not found this way, then the auxiliary coordinate variables listed by the coordinates attribute should
	//be checked. Note that it is permissible, but optional, to list coordinate variables as well as auxiliary
	//coordinate variables in the coordinates attribute. The axis attribute is not allowed for auxiliary coordinate
	//variables. Auxiliary coordinate variables which lie on the horizontal surface can be identified as such by their
	//dimensions being horizontal. Horizontal dimensions are those whose coordinate variables have an axis attribute
	//of X or Y, or a units attribute indicating latitude and longitude (see Chapter 4, Coordinate Types ).
	String coordinates = getAttributeStringValue(variable, COORDINATES_ATTRIBUTE_NAME);
	if (coordinates != null) {
		String[] strings = coordinates.split("\\s+");
		for (String auxiliaryCoordinateVariableName : strings) {
			Variable auxiliaryCoordinateVariable = netcdfFile.findVariable(auxiliaryCoordinateVariableName);
			if (auxiliaryCoordinateVariable == null) {
				continue;
			}

			if (isTimeVariable(auxiliaryCoordinateVariable)) {
				return auxiliaryCoordinateVariable;
			}
		}
	}

	return null;
}
 
Example 19
Source File: CEDRICRadarConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean isMine(NetcdfFile ncfile) {
  Dimension s = ncfile.findDimension("cedric_general_scaling_factor");
  Variable v = ncfile.findVariable("cedric_run_date");
  return v != null && s != null;
}
 
Example 20
Source File: NetcdfUtils.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Searches all the variables that the given variable depends on,
 * and returns the first variable that is a valid longitude variable.
 * If cannot find a valid longitude variable, then returns null.
 *
 * @param variable
 * @param netcdfFile
 * @return longitudeVariable or null.
 */
private static Variable findLongitudeVariableForVariable(Variable variable, NetcdfFile netcdfFile) {
	//search coordinate variables.
	List<Dimension> dimensions = variable.getDimensions();
	for (Dimension dimension : dimensions) {
		Variable dimensionVariable = netcdfFile.findVariable(dimension.getShortName());
		if (dimensionVariable == null || !dimensionVariable.isCoordinateVariable()) {
			continue;
		}

		if (isLongitudeVariable(dimensionVariable)) {
			return dimensionVariable;
		}
	}

	//search auxiliary coordinate variables.
	//according to the CF-convention (see http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5/cf-conventions.html#coordinate-system):
	//"An application that is trying to find the latitude coordinate of a variable should always look first to see
	//if any of the variable's dimensions correspond to a latitude coordinate variable. If the latitude coordinate
	//is not found this way, then the auxiliary coordinate variables listed by the coordinates attribute should
	//be checked. Note that it is permissible, but optional, to list coordinate variables as well as auxiliary
	//coordinate variables in the coordinates attribute. The axis attribute is not allowed for auxiliary coordinate
	//variables. Auxiliary coordinate variables which lie on the horizontal surface can be identified as such by their
	//dimensions being horizontal. Horizontal dimensions are those whose coordinate variables have an axis attribute
	//of X or Y, or a units attribute indicating latitude and longitude (see Chapter 4, Coordinate Types ).
	String coordinates = getAttributeStringValue(variable, COORDINATES_ATTRIBUTE_NAME);
	if (coordinates != null) {
		String[] strings = coordinates.split("\\s+");
		for (String auxiliaryCoordinateVariableName : strings) {
			Variable auxiliaryCoordinateVariable = netcdfFile.findVariable(auxiliaryCoordinateVariableName);
			if (auxiliaryCoordinateVariable == null) {
				continue;
			}

			if (isLongitudeVariable(auxiliaryCoordinateVariable)) {
				return auxiliaryCoordinateVariable;
			}
		}
	}

	return null;
}