File: TestH4File.java

package info (click to toggle)
jhdf 2.11.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 39,996 kB
  • ctags: 17,959
  • sloc: java: 100,416; ansic: 24,920; sh: 2,256; makefile: 957; cpp: 48
file content (417 lines) | stat: -rw-r--r-- 13,332 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417

/****************************************************************************
 * NCSA HDF                                                                 *
 * National Comptational Science Alliance                                   *
 * University of Illinois at Urbana-Champaign                               *
 * 605 E. Springfield, Champaign IL 61820                                   *
 *                                                                          *
 * For conditions of distribution and use, see the accompanying             *
 * hdf-java/COPYING file.                                                   *
 *                                                                          *
 ****************************************************************************/

package test.object.misc;

import java.lang.reflect.Array;
import java.util.Enumeration;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import ncsa.hdf.hdflib.HDFConstants;
import ncsa.hdf.object.Attribute;
import ncsa.hdf.object.h4.H4File;
import ncsa.hdf.object.h4.H4GRImage;
import ncsa.hdf.object.h4.H4Group;
import ncsa.hdf.object.h4.H4SDS;
import ncsa.hdf.object.h4.H4Vdata;

/**
 * Test object at the ncsa.hdf.object package.
 * <p>
 *
 * @version 1.3.0 10/26/2001
 * @author Peter X. Cao
 *
 */
public class TestH4File
{
    /**
     * Test tree structure of the HDF4 file.
     * <p>
     * Tested for regular file:
     * c:\winnt\profiles\xcao\desktop\hdf_files\amortest000171999.hdf
     * Tested with a large file (over 700MB, over 800 datasets) at
     * \\Eirene\sdt\mcgrath\EOS-Data\MODIS\L3\MOD08_E3.A2000337.002.2001037044240.hdf
     * it takes about 5 seconds to retrieve the tree structure through the network.
     * Accessing local file can be a lot of faster.
     */
    private static void testTree(String fileName)
    {
        H4File h4file = new H4File(fileName, HDFConstants.DFACC_WRITE);

        long t0 = System.currentTimeMillis();

        try {
            h4file.open();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
        long t = System.currentTimeMillis()-t0;
        System.out.println("Time of retrieving the tree is "+t);

        TreeNode root = h4file.getRootNode();
        if (root != null)
        {
            printNode(root, "    ");
        }

        try {
        h4file.close();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
    }

    private static void printNode(TreeNode node, String indent)
    {
        System.out.println(indent+node);

        int n = node.getChildCount();
        for (int i=0; i<n; i++)
        {
            printNode(node.getChildAt(i), indent+"    ");
        }
    }

    /**
     * Test H4Group.
     */
    private static void testH4Group(String fileName)
    {
        H4File h4file = new H4File(fileName, HDFConstants.DFACC_WRITE);

        try {
            h4file.open();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }

        DefaultMutableTreeNode root = (DefaultMutableTreeNode)h4file.getRootNode();
        H4Group g = null;
        DefaultMutableTreeNode node = null;
        if (root != null)
        {
            Enumeration nodes = root.depthFirstEnumeration();
            while (nodes.hasMoreElements())
            {
                node = (DefaultMutableTreeNode)nodes.nextElement();
                Object obj = node.getUserObject();
                if (obj instanceof H4Group)
                {
                    g = (H4Group)obj;
                    System.out.println(g);

                    // test H4CompoundDS attributes
                    Attribute attr = null;
                    List info = null;
                    try { info = g.getMetadata(); }
                    catch (Exception ex) {}

                    if (info == null) {
                        continue;
                    }

                    int n = info.size();
                    for (int i=0; i<n; i++)
                    {
                        attr = (Attribute)info.get(i);
                        System.out.println(attr);
                    }
                } //if (obj instanceof H4Group
            } //while (nodes.hasMoreElements())
        } //if (root != null)

	try {
        h4file.close();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
    }

    /**
     * Test H4SDS.
     */
    private static void testH4SDS(String fileName)
    {
        H4File h4file = new H4File(fileName, HDFConstants.DFACC_READ);

        try {
            h4file.open();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }

        DefaultMutableTreeNode root = (DefaultMutableTreeNode)h4file.getRootNode();
        H4SDS sds = null;
        DefaultMutableTreeNode node = null;
        if (root != null)
        {
            Enumeration nodes = root.depthFirstEnumeration();
            while (nodes.hasMoreElements())
            {
                node = (DefaultMutableTreeNode)nodes.nextElement();
                Object obj = node.getUserObject();
                if (obj instanceof H4SDS)
                {
                    sds = (H4SDS)obj;
                    System.out.println(sds);

                    // test H4CompoundDS attributes
                    Attribute attr = null;
                    List info = null;
                    try {
                        info = sds.getMetadata();
                    } catch (Exception ex)
                    { System.out.println(ex); }

                    int n = 0;
                    if (info != null)
                    {
                        n = info.size();
                        for (int i=0; i<n; i++)
                        {
                            attr = (Attribute)info.get(i);
                            System.out.println(attr);
                        }
                    }

                    // data
                    Object data = null;
                    try
                    {
                        data = sds.read();
                    } catch (Exception ex) {System.out.println(ex);}

                    if ((data != null) && data.getClass().isArray())
                    {
                        // print out the first 1000 data points
                        n = Math.min(Array.getLength(data), 1000);
                        StringBuffer sb = new StringBuffer();
                        for (int j=0; j<n; j++)
                        {
                            sb.append(Array.get(data, j));
                            sb.append(" ");
                        }
                        System.out.println(sb.toString());
                    }
                } //if (obj instanceof H4Group
            } //while (nodes.hasMoreElements())
        } //if (root != null)

	try {
        h4file.close();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
    }

    /**
     * Test H4Vdata.
     */
    private static void testH4Vdata(String fileName)
    {
        H4File h4file = new H4File(fileName, HDFConstants.DFACC_READ);

        try {
            h4file.open();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }

        DefaultMutableTreeNode root = (DefaultMutableTreeNode)h4file.getRootNode();
        H4Vdata  vdata = null;
        DefaultMutableTreeNode node = null;
        if (root != null)
        {
            Enumeration nodes = root.depthFirstEnumeration();
            while (nodes.hasMoreElements())
            {
                node = (DefaultMutableTreeNode)nodes.nextElement();
                Object obj = node.getUserObject();
                if (obj instanceof H4Vdata)
                {
                    vdata = (H4Vdata)obj;
                    System.out.println(vdata);

                    // test H4CompoundDS attributes
                    Attribute attr = null;
                    List info = null;
                    try {
                        info = vdata.getMetadata();
                    } catch (Exception ex)
                    { System.out.println(ex); }

                    int n = 0;
                    if (info != null)
                    {
                        n = info.size();
                        for (int i=0; i<n; i++)
                        {
                            attr = (Attribute)info.get(i);
                            System.out.println(attr);
                        }
                    }

                    // compound members
                    int rank = vdata.getRank();
                    if (rank <=0 ) {
                        vdata.init();
                    }
                    n = vdata.getMemberCount();
                    String[] names = vdata.getMemberNames();
                    for (int i=0; i<n; i++)
                    {
                        System.out.println(names[i]);
                    }

                    // compound data
                    List list = null;

                    try {
                        list = (List)vdata.read();
                    } catch (Exception ex) { System.out.println(ex);}

                    if (list != null)
                    {
                        n = list.size();
                        Object mdata = null;
                        for (int i=0; i<n; i++)
                        {
                            mdata = list.get(i);
                            if (mdata.getClass().isArray())
                            {
                                StringBuffer sb = new StringBuffer();
                                // print out the first 1000 data points
                                int mn = Math.min(Array.getLength(mdata), 1000);
                                for (int j=0; j<mn; j++)
                                {
                                    sb.append(Array.get(mdata, j));
                                    sb.append(" ");
                                }
                                System.out.println(sb.toString());
                            }
                        } // for (int i=0; i<n; i++)
                    } // if (list != null)
               } //if (obj instanceof H4Vdata
            } //while (nodes.hasMoreElements())
        } //if (root != null)

	try {
        h4file.close();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
    }

    /**
     * Test H4GRImage.
     */
    private static void testH4GRImage(String fileName)
    {
        H4File h4file = new H4File(fileName, HDFConstants.DFACC_READ);

        try {
            h4file.open();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }

        DefaultMutableTreeNode root = (DefaultMutableTreeNode)h4file.getRootNode();
        H4GRImage sds = null;
        DefaultMutableTreeNode node = null;
        if (root != null)
        {
            Enumeration nodes = root.depthFirstEnumeration();
            while (nodes.hasMoreElements())
            {
                node = (DefaultMutableTreeNode)nodes.nextElement();
                Object obj = node.getUserObject();
                if (obj instanceof H4GRImage)
                {
                    sds = (H4GRImage)obj;
                    System.out.println(sds);

                    // test H4CompoundDS attributes
                    Attribute attr = null;
                    List info = null;
                    try {
                        info = sds.getMetadata();
                    } catch (Exception ex)
                    { System.out.println(ex); }

                    int n = 0;
                    if (info != null)
                    {
                        n = info.size();
                        for (int i=0; i<n; i++)
                        {
                            attr = (Attribute)info.get(i);
                            System.out.println(attr);
                        }
                    }

                    // data
                    Object data = null;
                    try
                    {
                        data = sds.read();
                    } catch (Exception ex) {System.out.println(ex);}

                    if ((data != null) && data.getClass().isArray())
                    {
                        // print out the first 1000 data points
                        n = Math.min(Array.getLength(data), 1000);
                        StringBuffer sb = new StringBuffer();
                        for (int j=0; j<n; j++)
                        {
                            sb.append(Array.get(data, j));
                            sb.append(" ");
                        }
                        System.out.println(sb.toString());
                    }
                } //if (obj instanceof H4Group
            } //while (nodes.hasMoreElements())
        } //if (root != null)

	try {
        h4file.close();
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
    }

    public static void main(String[] argv)
    {
        int argc = argv.length;

        if (argc <=0)
        {
            System.exit(1);
        }

        TestH4File.testTree(argv[0]);
    }

}