001/*
002 * $Id: HDF4File.java,v 1.8 2011/03/24 16:06:33 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
007 * Space Science and Engineering Center (SSEC)
008 * University of Wisconsin - Madison
009 * 1225 W. Dayton Street, Madison, WI 53706, USA
010 * https://www.ssec.wisc.edu/mcidas
011 * 
012 * All Rights Reserved
013 * 
014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015 * some McIDAS-V source code is based on IDV and VisAD source code.  
016 * 
017 * McIDAS-V is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU Lesser Public License as published by
019 * the Free Software Foundation; either version 3 of the License, or
020 * (at your option) any later version.
021 * 
022 * McIDAS-V is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025 * GNU Lesser Public License for more details.
026 * 
027 * You should have received a copy of the GNU Lesser Public License
028 * along with this program.  If not, see http://www.gnu.org/licenses.
029 */
030
031package edu.wisc.ssec.mcidasv.data.hydra;
032
033import java.util.HashMap;
034
035public class HDF4File implements MultiDimensionReader {
036
037   HashMap<String, HDFVariable> varMap = new HashMap<String, HDFVariable>();
038   HashMap<String, String[]> varDimNames = new HashMap<String, String[]>();
039   HashMap<String, int[]> varDimLengths = new HashMap<String, int[]>();
040   HashMap<String, Class> varDataType = new HashMap<String, Class>();
041
042   HDFFile hdf = null;
043
044   public HDF4File(String filename) throws Exception {
045     hdf = new HDFFile(filename);
046     
047     for (int kk=0; kk<hdf.getNumberSDdatasets(); kk++) {
048       HDFVariable var = hdf.select(kk);
049       HDFVariableInfo info = var.getinfo();
050       varMap.put(info.var_name, var);
051       varDataType.put(info.var_name, getArrayClass(info.data_type));
052 
053       int[] dim_lengths = info.dim_lengths;
054       int[] new_dim_lengths = new int[info.rank];
055       String[] dim_names = new String[info.rank];
056       for (int tt=0; tt<info.rank; tt++) {
057         int dim_id = var.getdimid(tt);
058         HDFDimension hdim = hdf.diminfo(dim_id);
059         dim_names[tt] = hdim.name;
060         new_dim_lengths[tt] = dim_lengths[tt];
061       }
062       varDimNames.put(info.var_name, dim_names);
063       varDimLengths.put(info.var_name, dim_lengths);
064     }
065   }
066
067   private Class getArrayClass(int hdf_data_type_id) {
068     if (hdf_data_type_id == 3 || hdf_data_type_id == 4) {
069       return Character.TYPE;
070     } else if (hdf_data_type_id == 20 || hdf_data_type_id == 21) {
071       return Byte.TYPE;
072     } else if ( hdf_data_type_id == 22 || hdf_data_type_id == 23) {
073       return Short.TYPE;
074     } else if ( hdf_data_type_id == 5 ) {
075       return Float.TYPE;
076     } else if ( hdf_data_type_id == 25 || hdf_data_type_id == 24) {
077       return Integer.TYPE;
078     } else if ( hdf_data_type_id == 6 ) {
079       return Double.TYPE;
080     }
081     return null;
082   }
083
084   public Class getArrayType(String array_name) {
085     return varDataType.get(array_name);
086   }
087
088   public String[] getDimensionNames(String array_name) {
089     return varDimNames.get(array_name);
090   }
091
092   public int[] getDimensionLengths(String array_name) {
093     return varDimLengths.get(array_name);
094   }
095
096   public float[] getFloatArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
097     HDFVariable var = varMap.get(array_name);
098     HDFArray array = var.readdata(start, stride, count);
099     return (float[]) array.getArray();
100   }
101
102   public double[] getDoubleArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
103     HDFVariable var = varMap.get(array_name);
104     HDFArray array = var.readdata(start, stride, count);
105     return (double[]) array.getArray();
106   }
107
108   public short[] getShortArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
109     HDFVariable var = varMap.get(array_name);
110     HDFArray array = var.readdata(start,stride,count);
111     return (short[]) array.getArray();
112   }
113
114   public int[] getIntArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
115     HDFVariable var = varMap.get(array_name);
116     HDFArray array = var.readdata(start,stride,count);
117     return (int[]) array.getArray();
118   }
119
120   public byte[] getByteArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
121     HDFVariable var = varMap.get(array_name);
122     HDFArray array = (HDFArray) var.readdata(start, stride, count);
123     return (byte[]) array.getArray();
124   }
125
126   public Object getArray(String array_name, int[] start, int[] count, int[] stride) throws Exception {
127     throw new Exception("getArray not implemented");
128   }
129
130   public HDFArray getGlobalAttribute(String attr_name) throws Exception {
131     int attr_idx = hdf.findattr(attr_name);
132     return hdf.readattr(attr_idx);
133   }
134
135   public HDFArray getArrayAttribute(String array_name, String attr_name) throws Exception {
136     HDFVariable var = varMap.get(array_name);
137     int attr_idx = var.findattr(attr_name);
138     return var.readattr(attr_idx);
139   }
140
141   public void close() throws Exception {
142     hdf.close();
143   }
144
145   public static void main(String[] args) throws Exception {
146     HDF4File hfile = new HDF4File(args[0]);
147     hfile.close();
148   }
149}