001: package it.geosolutions.imageio.plugins.jhdf.tovs;
002:
003: import it.geosolutions.imageio.plugins.jhdf.BaseHDFImageReader;
004: import it.geosolutions.imageio.plugins.jhdf.SubDatasetInfo;
005:
006: import java.io.IOException;
007: import java.util.Iterator;
008: import java.util.LinkedHashMap;
009: import java.util.List;
010:
011: import javax.imageio.metadata.IIOMetadata;
012: import javax.imageio.spi.ImageReaderSpi;
013:
014: import ncsa.hdf.object.Attribute;
015: import ncsa.hdf.object.Dataset;
016: import ncsa.hdf.object.Datatype;
017: import ncsa.hdf.object.Group;
018: import ncsa.hdf.object.HObject;
019: import ncsa.hdf.object.ScalarDS;
020:
021: public class TOVSImageReader extends BaseHDFImageReader {
022: private TOVSImageMetadata imageMetadata;
023:
024: public TOVSImageReader(ImageReaderSpi originatingProvider) {
025: super (originatingProvider);
026: }
027:
028: protected int getBandNumberFromProduct(String productName) {
029: return TOVSPathAProperties.tovsProducts.getHDFProduct(
030: productName).getNBands();
031: }
032:
033: public IIOMetadata getImageMetadata(int imageIndex)
034: throws IOException {
035: SubDatasetInfo sdInfo = sourceStructure
036: .getSubDatasetInfo(retrieveSubDatasetIndex(imageIndex));
037: if (imageMetadata == null)
038: imageMetadata = new TOVSImageMetadata(sdInfo);
039: return imageMetadata;
040: }
041:
042: public IIOMetadata getStreamMetadata() throws IOException {
043: // TODO Auto-generated method stub
044: return null;
045: }
046:
047: public int getNumImages(boolean allowSearch) throws IOException {
048: // TODO Auto-generated method stub
049: return 0;
050: }
051:
052: protected boolean isAcceptedItem(String productName) {
053: if (TOVSPathAProperties.tovsProducts.getHDFProduct(productName) != null)
054: return true;
055: return false;
056: }
057:
058: /**
059: * Initializing TOVS datasets properties.
060: *
061: * @param root
062: *
063: * @throws Exception
064: */
065: protected void initializeProfile() throws Exception {
066: // Getting the Member List from the provided root
067: final List membersList = ((Group) root).getMemberList();
068:
069: final int listSize = membersList.size();
070: int subDatasets = 0;
071:
072: // Mutex on the subDatasetMap and sourceStructure initialization
073: subDatasetsMap = new LinkedHashMap(8);
074: sourceStructure = new SourceStructure();
075:
076: // Scanning all the datasets
077: for (int i = 0; i < listSize; i++) {
078: final HObject member = (HObject) membersList.get(i);
079: if (member instanceof ScalarDS) {
080: if (member.hasAttribute()) {
081: List metadataList = member.getMetadata();
082: Iterator metadataIt = metadataList.iterator();
083: while (metadataIt.hasNext()) {
084: final Attribute attrib = (Attribute) metadataIt
085: .next();
086: final String attribName = attrib.getName();
087: if (attribName.equals("long_name")) {
088: Object valuesList = attrib.getValue();
089: final String[] values = (String[]) valuesList;
090: final String name = values[0];
091:
092: // Checking if the actual dataset is accepted.
093: if (isAcceptedItem(name)) {
094: subDatasets++;
095: // Updating the subDatasetsMap map
096: subDatasetsMap.put(name, member);
097:
098: // retrieving subDataset main properties
099: // (Rank, dims, chunkSize)
100: final int rank = ((Dataset) member)
101: .getRank();
102: final long[] dims = ((Dataset) member)
103: .getDims();
104: final long[] chunkSize = ((Dataset) member)
105: .getChunkSize();
106:
107: final long[] subDatasetDims = new long[rank];
108: final long[] subDatasetChunkSize;
109: long datasetSize = 1;
110:
111: // copying values to avoid altering dataset
112: // fields.
113: for (int k = 0; k < rank; k++) {
114: subDatasetDims[k] = dims[k];
115:
116: // when rank > 2, X and Y are the last
117: // 2 coordinates. As an instance, for a
118: // 3D subdatasets, 3rd dimension has
119: // index 0.
120: if (k < rank - 2)
121: datasetSize *= dims[k];
122: }
123: if (chunkSize != null) {
124: subDatasetChunkSize = new long[rank];
125: for (int k = 0; k < rank; k++)
126: subDatasetChunkSize[k] = chunkSize[k];
127: } else
128: subDatasetChunkSize = null;
129: final Datatype dt = ((Dataset) member)
130: .getDatatype();
131: // instantiating a SubDatasetInfo
132: SubDatasetInfo dsInfo = new SubDatasetInfo(
133: name, rank, subDatasetDims,
134: subDatasetChunkSize, dt);
135: sourceStructure
136: .addSubDatasetProperties(
137: dsInfo, datasetSize);
138: }
139: }
140: }
141: }
142: }
143: }
144: }
145: }
|