File: VDWriteToVdata.java

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (142 lines) | stat: -rw-r--r-- 5,727 bytes parent folder | download | duplicates (2)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF Products. The full HDF copyright                 *
 * notice, including terms governing use, modification, and redistribution,  *
 * is contained in the file, COPYING.  COPYING can be found at the root of   *
 * the source code distribution tree. You can also access it online  at      *
 * https://www.hdfgroup.org/licenses.  If you do not have access to the      *
 * file, you may request a copy from help@hdfgroup.org.                      *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import hdf.hdflib.HDFConstants;
import hdf.hdflib.HDFLibrary;

/**
 * <p>
 * Title: HDF Native Package (Java) Example
 * </p>
 * <p>
 * Description: this example shows how to create a HDF4 vdata file using the
 * "HDF Native Package (Java)".
 * </p>
 */
public class VDWriteToVdata {
    private static String fname          = "General_Vdatas.hdf";
    private static String CLASS_NAME     = "Particle Data";
    private static String VDATA_NAME     = "Solid Particle";
    private static String FIELD1_NAME    = "Position";                  /* contains x, y, z values */
    private static String FIELD2_NAME    = "Mass";                      /* contains weight values */
    private static String FIELD3_NAME    = "Temperature";               /* contains min and max values */
    private static String FIELDNAME_LIST = "Position,Mass,Temperature"; /* No spaces b/w names */
    private static int N_RECORDS         = 10; /* number of records the vdata contains */
    private static int ORDER_1           = 3;  /* order of first field */
    private static int ORDER_2           = 1;  /* order of second field */
    private static int ORDER_3           = 2;  /* order of third field */
    private static int N_VALS_PER_REC    = 6;  /* number of values per record (ORDER_1 + ORDER_2 + ORDER_3) */

    public static void main(String args[]) throws Exception
    {
        long file_id       = -1;
        long vdata_id      = -1;
        float data_buf[][] = new float[N_RECORDS][N_VALS_PER_REC]; /* buffer for vdata values */

        // Create a new file using default properties.
        try {
            file_id = HDFLibrary.Hopen(fname, HDFConstants.DFACC_CREATE);
            // Initialize the V interface.
            if (file_id >= 0)
                HDFLibrary.Vstart(file_id);
        }
        catch (Exception e) {
            e.printStackTrace();
            System.err.println("Failed to create file:" + fname);
            return;
        }
        System.out.println("File created:" + fname);

        try {
            // Create the vgroup.  Note that the vgroup reference number is set
            // to -1 for creating and the access mode is "w" for writing.
            if (file_id >= 0) {
                vdata_id = HDFLibrary.VSattach(file_id, -1, "w");
                if (vdata_id >= 0) {
                    HDFLibrary.VSsetname(vdata_id, VDATA_NAME);
                    HDFLibrary.VSsetclass(vdata_id, CLASS_NAME);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
            /*
             * Introduce each field's name, data type, and order.  This is the first
             * part in defining a field.
             */
            HDFLibrary.VSfdefine(vdata_id, FIELD1_NAME, HDFConstants.DFNT_FLOAT32, ORDER_1);
            HDFLibrary.VSfdefine(vdata_id, FIELD2_NAME, HDFConstants.DFNT_FLOAT32, ORDER_2);
            HDFLibrary.VSfdefine(vdata_id, FIELD3_NAME, HDFConstants.DFNT_FLOAT32, ORDER_3);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
            /*
             * Finalize the definition of the fields.
             */
            HDFLibrary.VSsetfields(vdata_id, FIELDNAME_LIST);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        /*
         * Buffer the data by the record for fully interlaced mode.  Note that the
         * first three elements contain the three values of the first field, the
         * fourth element contains the value of the second field, and the last two
         * elements contain the two values of the third field.
         */
        for (int rec_num = 0; rec_num < N_RECORDS; rec_num++) {
            data_buf[rec_num][0] = (float)(1.0 * rec_num);
            data_buf[rec_num][1] = (float)(2.0 * rec_num);
            data_buf[rec_num][2] = (float)(3.0 * rec_num);
            data_buf[rec_num][3] = (float)(0.1 + rec_num);
            data_buf[rec_num][4] = (float)0.0;
            data_buf[rec_num][5] = (float)65.0;
        }

        try {
            /*
             * Write the data from data_buf to the vdata with full interlacing mode.
             */
            HDFLibrary.VSwrite(vdata_id, data_buf, N_RECORDS, HDFConstants.FULL_INTERLACE);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // Close the groups.
        try {
            if (vdata_id >= 0)
                HDFLibrary.VSdetach(vdata_id);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // Close the file.
        try {
            if (file_id >= 0) {
                HDFLibrary.Vend(file_id);
                HDFLibrary.Hclose(file_id);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}