summaryrefslogtreecommitdiffstats
path: root/doc/html/Tutor/examples/java/CreateGroupAR.java
blob: 672f1d1fdeee6ec33e16244d8bf87ccd96f5ab50 (plain)
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
/******************************************************************
 * CreateGroupAR.java (for HDF5 tutorial lesson 9)
 *
 *   -- Creating groups using absolute and relative names.
 *      (a java conversion from h5_crtgrpar.c)
 *
 ******************************************************************/

import ncsa.hdf.hdf5lib.*;
import ncsa.hdf.hdf5lib.exceptions.*;

public class CreateGroupAR
{
   public static void main(String []argv) 
   {
      final String FILE = "groups.h5";
      int file_id = -1;        // file identifier 
      int group1_id = -1;      // group identifier
      int group2_id = -1;  
      int group3_id = -1;
  
      int status = -1;
   
      // Create a new file using default properties.
      file_id = H5Fcreate_wrap (FILE, HDF5Constants.H5F_ACC_TRUNC, 
				HDF5Constants.H5P_DEFAULT, 
				HDF5Constants.H5P_DEFAULT);

      // Create group "MyGroup" in the root group using absolute name.
      group1_id = H5Gcreate_wrap (file_id, "/MyGroup", 0);


      // Create group "Group_A" in group "MyGroup" using absolute name. 
      group2_id = H5Gcreate_wrap (file_id, "/MyGroup/Group_A", 0);
      
      // Create group "Group_B" in group "MyGroup" using relative name. 
      group3_id = H5Gcreate_wrap (group1_id, "Group_B", 0);
   
      // Close groups. 
      status = H5Gclose_wrap (group1_id);
      status = H5Gclose_wrap (group2_id);
      status = H5Gclose_wrap (group3_id);
      
      // Close the file. 
      status = H5Fclose_wrap (file_id); 
   }


   // Help function for creating a new file 
   public static int H5Fcreate_wrap (String name, int flags,
                                     int create_id, int access_id)
   {
      int file_id = -1;    // file identifier 
      try 
      {
         // Create a new file using default file properties.
         file_id = H5.H5Fcreate (name, flags, create_id, access_id);

      }
      catch (HDF5Exception hdf5e)
      {
	  System.out.println 
	      ("CreateGroupAR.H5Fcreate_wrap() with HDF5Exception: "
	       + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println 
	     ("CreateGroupAR.H5Fcreate_wrap() with other Exception: " 
	      + e.getMessage());
      }
      return file_id;
   }


   // Help function for creating a group named "/MyGroup" in the file.
   public static int H5Gcreate_wrap (int loc_id, String name, int size_hint)
   {
      int group_id = -1;    // group identifier 
      try 
      {
         // Create a group
         group_id = H5.H5Gcreate (loc_id, name, size_hint);

      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	     ("CreateGroupAR.H5Gcreate_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println 
	     ("CreateGroupAR.H5Gcreate_wrap() with other Exception: " 
	      + e.getMessage());
      }
      return group_id;
   }


   // Help function for closing the group
   public static int H5Gclose_wrap (int group_id)
   {
      int status = -1;
      
      try 
      {
	 // Close the group
	 status = H5.H5Gclose (group_id);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("CreateGroupAR.H5Gclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("CreateGroupAR.H5Gclose_wrap() with other exception: " 
	      + e.getMessage());
      } 
      return status;
   }
 

   // Help function for terminating access to the file. 
   public static int H5Fclose_wrap (int file_id)
   {
      int status = -1;
      
      try 
      {
	 // Terminate access to the file. 
	 status = H5.H5Fclose (file_id);
      }
      catch (HDF5Exception hdf5e)
      { 
	 System.out.println 
	     ("CreateGroupAR.H5Fclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("CreateGroupAR.H5Fclose_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }  
}