ucar.nc2.iosp.hdf4.HdfEos Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.nc2.iosp.hdf4;
import ucar.ma2.ArrayObject;
import ucar.nc2.*;
import ucar.nc2.constants.*;
import ucar.nc2.dataset.CoordinateSystem;
import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.ma2.ArrayChar;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Formatter;
import org.jdom2.Element;
/**
* Parse structural metadata from HDF-EOS.
* This allows us to use shared dimensions, identify Coordinate Axes, and the FeatureType.
*
*
from HDF-EOS.status.ppt:
*
* HDF-EOS is format for EOS Standard Products
*
* - Landsat 7 (ETM+)
*
- Terra (CERES, MISR, MODIS, ASTER, MOPITT)
*
- Meteor-3M (SAGE III)
*
- Aqua (AIRS, AMSU-A, AMSR-E, CERES, MODIS)
*
- Aura(MLS, TES, HIRDLS, OMI
*
* HDF is used by other EOS missions
*
* - OrbView 2 (SeaWIFS)
*
- TRMM (CERES, VIRS, TMI, PR)
*
- Quickscat (SeaWinds)
*
- EO-1 (Hyperion, ALI)
*
- ICESat (GLAS)
*
- Calypso
*
*
*
*
* @author caron
* @since Jul 23, 2007
*/
public class HdfEos {
static public final String HDF5_GROUP = "HDFEOS_INFORMATION";
static public final String HDFEOS_CRS = "_HDFEOS_CRS";
static public final String HDFEOS_CRS_Projection = "Projection";
static public final String HDFEOS_CRS_UpperLeft = "UpperLeftPointMtrs";
static public final String HDFEOS_CRS_LowerRight = "LowerRightMtrs";
static public final String HDFEOS_CRS_ProjParams = "ProjParams";
static public final String HDFEOS_CRS_SphereCode = "SphereCode";
static private org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(HdfEos.class);
static boolean showWork = false; // set in debug
static private final String GEOLOC_FIELDS = "Geolocation Fields";
static private final String GEOLOC_FIELDS2 = "Geolocation_Fields";
static private final String DATA_FIELDS = "Data Fields";
static private final String DATA_FIELDS2 = "Data_Fields";
/**
* Amend the given NetcdfFile with metadata from HDF-EOS structMetadata.
* All Variables named StructMetadata.n, where n= 1, 2, 3 ... are read in and their contents concatenated
* to make the structMetadata String.
*
* @param ncfile Amend this file
* @param eosGroup the group containing variables named StructMetadata.*
* @return true if HDF-EOS info was found
* @throws IOException on read error
*/
static public boolean amendFromODL(NetcdfFile ncfile, Group eosGroup) throws IOException {
String smeta = getStructMetadata(eosGroup);
if (smeta == null) { return false; }
HdfEos fixer = new HdfEos();
fixer.fixAttributes(ncfile.getRootGroup());
fixer.amendFromODL(ncfile, smeta);
return true;
}
/**
*
*/
static public boolean getEosInfo(NetcdfFile ncfile, Group eosGroup, Formatter f) throws IOException {
String smeta = getStructMetadata(eosGroup);
if (smeta == null) {
f.format("No StructMetadata variables in group %s %n", eosGroup.getFullName());
return false;
}
f.format("raw = %n%s%n", smeta);
ODLparser parser = new ODLparser();
parser.parseFromString(smeta); // now we have the ODL in JDOM elements
StringWriter sw = new StringWriter(5000);
parser.showDoc(new PrintWriter(sw));
f.format("parsed = %n%s%n", sw.toString());
return true;
}
/**
*
*/
static private String getStructMetadata(Group eosGroup) throws IOException {
StringBuilder sbuff = null;
String structMetadata = null;
int n = 0;
while (true) {
Variable structMetadataVar = eosGroup.findVariable("StructMetadata." + n);
if (structMetadataVar == null) { break; }
if ((structMetadata != null) && (sbuff == null)) { // more than 1 StructMetadata
sbuff = new StringBuilder(64000);
sbuff.append(structMetadata);
}
// read and parse the ODL
Array A = structMetadataVar.read();
if (A instanceof ArrayChar.D1) {
ArrayChar ca = (ArrayChar) A;
structMetadata = ca.getString(); // common case only StructMetadata.0, avoid extra copy
} else if (A instanceof ArrayObject.D0) {
ArrayObject ao = (ArrayObject) A;
structMetadata = (String) ao.getObject(0);
} else {
log.error("Unsupported array type {} for StructMetadata", A.getElementType());
}
if (sbuff != null) {
sbuff.append(structMetadata);
}
n++;
}
return (sbuff != null) ? sbuff.toString() : structMetadata;
}
/**
* Amend the given NetcdfFile with metadata from HDF-EOS structMetadata
*
* @param ncfile Amend this file
* @param structMetadata structMetadata as String
* @throws IOException on read error
*/
private void amendFromODL(NetcdfFile ncfile, String structMetadata) throws IOException {
Group rootg = ncfile.getRootGroup();
ODLparser parser = new ODLparser();
Element root = parser.parseFromString(structMetadata); // now we have the ODL in JDOM elements
FeatureType featureType = null;
// SWATH
Element swathStructure = root.getChild("SwathStructure");
if (swathStructure != null) {
List