001/*
002 * $Id: HDFFile.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.Iterator;
034
035public class HDFFile {
036
037      private int sd_id;
038      private HDF hdf;
039      private int num_SDdatasets;
040      private int num_globalAttrs;
041
042      public HDFFile(String filename) throws Exception {
043        hdf = new HDF("./plug_hdf");
044        sd_id = hdf.start(filename);
045        HDFFileInfo hInfo = hdf.fileinfo(sd_id);
046        num_SDdatasets = hInfo.num_SDdatasets;
047        num_globalAttrs = hInfo.num_globalAttrs;
048      }
049
050      public HDFVariable select(int sds_idx) throws Exception {
051        return new HDFVariable(hdf, hdf.select(sd_id, sds_idx));
052      }
053    
054      public HDFArray readattr(int attr_index) throws Exception {
055        return hdf.readattr(sd_id, attr_index);
056      }
057
058      public int endaccess(int sds_id) throws Exception {
059        return hdf.endaccess(sds_id);
060      }
061
062      public int findattr(String name) throws Exception {
063        return hdf.findattr(sd_id, name);
064      }
065
066      public int nametoindex(String name) throws Exception {
067        return hdf.nametoindex(sd_id, name);
068      }
069
070      public HDFDimension diminfo(int dim_id) throws Exception {
071        return hdf.diminfo(dim_id);
072      }
073
074      public int getNumberSDdatasets() {
075        return num_SDdatasets;
076      }
077
078      public int getNumberGlobalAttrs() {
079        return num_globalAttrs;
080      }
081
082      public void close() throws Exception {
083        hdf.close();
084      }
085
086      public static void main(String[] args) throws Exception {
087        System.out.println("Here args[0]=" + args[0]);
088        HDFVariable var = null;
089        System.out.println("var=" + var);
090        HDFFile hf = new HDFFile(args[0]);
091        System.out.println("hf=" + hf);
092        System.out.println("number of SD datasets: "+hf.num_SDdatasets);
093        System.out.println("number of global attributes: "+hf.num_globalAttrs);
094
095        for (int idx=0; idx<hf.num_SDdatasets; idx++) {
096          var = hf.select(idx);
097          HDFVariableInfo info = var.getinfo();
098          System.out.print("name: "+info.var_name);
099          System.out.print(",  rank: "+info.rank);
100          System.out.print(",  dimension lengths: ");
101          int[] lens = info.dim_lengths;
102          for (int k=0;k<info.rank;k++) System.out.print(lens[k]+" ");
103          System.out.println("");
104        }
105
106        /*
107        double[] dbl_data = null;
108        float[] flt_data = null;
109        short[] sht_data = null;
110        int[] int_data = null;
111
112        HDFArray a = var.readdata(new int[] {0,0,0}, new int[] {1,1,1}, new int[] {1,1,50});
113        System.out.println(a.getType());
114        if (a.getType().equals(Float.TYPE)) {
115          flt_data = (float[]) a.getArray();
116        }
117        else if (a.getType().equals(Double.TYPE)) {
118          dbl_data = (double[]) a.getArray();
119        }
120        */
121
122        /*
123        int attr_idx = hf.findattr("coremetadata");
124        System.out.println("attr_idx: "+attr_idx);
125        HDFAttribute attr = hf.readattr(attr_idx);
126        Iterator iter = attr.getAttribute();
127        while (iter.hasNext()) {
128          System.out.println((String)iter.next());
129        }
130        */
131
132        //- end access, close I/O pipes.
133        hf.close();
134      }
135
136}