1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * Copyright by The HDF Group. * 3 * Copyright by the Board of Trustees of the University of Illinois. * 4 * All rights reserved. * 5 * * 6 * This file is part of HDF5. The full HDF5 copyright notice, including * 7 * terms governing use, modification, and redistribution, is contained in * 8 * the COPYING file, which can be found at the root of the source code * 9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. * 10 * If you do not have access to either file, you may request a copy from * 11 * help@hdfgroup.org. * 12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 13 14 /************************************************************ 15 This example shows how to read and write string datatypes 16 to a dataset. The program first writes strings to a 17 dataset with a dataspace of DIM0, then closes the file. 18 Next, it reopens the file, reads back the data, and 19 outputs it to the screen. 20 ************************************************************/ 21 22 package examples.datatypes; 23 24 import hdf.hdf5lib.H5; 25 import hdf.hdf5lib.HDF5Constants; 26 27 public class H5Ex_T_String { 28 private static String FILENAME = "H5Ex_T_String.h5"; 29 private static String DATASETNAME = "DS1"; 30 private static final int DIM0 = 4; 31 private static final int SDIM = 8; 32 private static final int RANK = 1; 33 CreateDataset()34 private static void CreateDataset() { 35 long file_id = -1; 36 long memtype_id = -1; 37 long filetype_id = -1; 38 long dataspace_id = -1; 39 long dataset_id = -1; 40 long[] dims = { DIM0 }; 41 byte[][] dset_data = new byte[DIM0][SDIM]; 42 StringBuffer[] str_data = { new StringBuffer("Parting"), new StringBuffer("is such"), 43 new StringBuffer("sweet"), new StringBuffer("sorrow.") }; 44 45 // Create a new file using default properties. 46 try { 47 file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, 48 HDF5Constants.H5P_DEFAULT); 49 } 50 catch (Exception e) { 51 e.printStackTrace(); 52 } 53 54 // Create file and memory datatypes. For this example we will save 55 // the strings as FORTRAN strings, therefore they do not need space 56 // for the null terminator in the file. 57 try { 58 filetype_id = H5.H5Tcopy(HDF5Constants.H5T_FORTRAN_S1); 59 if (filetype_id >= 0) 60 H5.H5Tset_size(filetype_id, SDIM - 1); 61 } 62 catch (Exception e) { 63 e.printStackTrace(); 64 } 65 try { 66 memtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); 67 if (memtype_id >= 0) 68 H5.H5Tset_size(memtype_id, SDIM); 69 } 70 catch (Exception e) { 71 e.printStackTrace(); 72 } 73 74 // Create dataspace. Setting maximum size to NULL sets the maximum 75 // size to be the current size. 76 try { 77 dataspace_id = H5.H5Screate_simple(RANK, dims, null); 78 } 79 catch (Exception e) { 80 e.printStackTrace(); 81 } 82 83 // Create the dataset and write the string data to it. 84 try { 85 if ((file_id >= 0) && (filetype_id >= 0) && (dataspace_id >= 0)) 86 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, filetype_id, dataspace_id, HDF5Constants.H5P_DEFAULT, 87 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); 88 } 89 catch (Exception e) { 90 e.printStackTrace(); 91 } 92 93 // Write the data to the dataset. 94 try { 95 for (int indx = 0; indx < DIM0; indx++) { 96 for (int jndx = 0; jndx < SDIM; jndx++) { 97 if (jndx < str_data[indx].length()) 98 dset_data[indx][jndx] = (byte) str_data[indx].charAt(jndx); 99 else 100 dset_data[indx][jndx] = 0; 101 } 102 } 103 if ((dataset_id >= 0) && (memtype_id >= 0)) 104 H5.H5Dwrite(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 105 HDF5Constants.H5P_DEFAULT, dset_data); 106 } 107 catch (Exception e) { 108 e.printStackTrace(); 109 } 110 111 // End access to the dataset and release resources used by it. 112 try { 113 if (dataset_id >= 0) 114 H5.H5Dclose(dataset_id); 115 } 116 catch (Exception e) { 117 e.printStackTrace(); 118 } 119 120 // Terminate access to the data space. 121 try { 122 if (dataspace_id >= 0) 123 H5.H5Sclose(dataspace_id); 124 } 125 catch (Exception e) { 126 e.printStackTrace(); 127 } 128 129 // Terminate access to the file type. 130 try { 131 if (filetype_id >= 0) 132 H5.H5Tclose(filetype_id); 133 } 134 catch (Exception e) { 135 e.printStackTrace(); 136 } 137 138 // Terminate access to the mem type. 139 try { 140 if (memtype_id >= 0) 141 H5.H5Tclose(memtype_id); 142 } 143 catch (Exception e) { 144 e.printStackTrace(); 145 } 146 147 // Close the file. 148 try { 149 if (file_id >= 0) 150 H5.H5Fclose(file_id); 151 } 152 catch (Exception e) { 153 e.printStackTrace(); 154 } 155 156 } 157 ReadDataset()158 private static void ReadDataset() { 159 long file_id = -1; 160 long filetype_id = -1; 161 long memtype_id = -1; 162 long dataspace_id = -1; 163 long dataset_id = -1; 164 long sdim = 0; 165 long[] dims = { DIM0 }; 166 byte[][] dset_data; 167 StringBuffer[] str_data; 168 169 // Open an existing file. 170 try { 171 file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); 172 } 173 catch (Exception e) { 174 e.printStackTrace(); 175 } 176 177 // Open an existing dataset. 178 try { 179 if (file_id >= 0) 180 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); 181 } 182 catch (Exception e) { 183 e.printStackTrace(); 184 } 185 186 // Get the datatype and its size. 187 try { 188 if (dataset_id >= 0) 189 filetype_id = H5.H5Dget_type(dataset_id); 190 if (filetype_id >= 0) { 191 sdim = H5.H5Tget_size(filetype_id); 192 sdim++; // Make room for null terminator 193 } 194 } 195 catch (Exception e) { 196 e.printStackTrace(); 197 } 198 199 // Get dataspace and allocate memory for read buffer. 200 try { 201 if (dataset_id >= 0) 202 dataspace_id = H5.H5Dget_space(dataset_id); 203 } 204 catch (Exception e) { 205 e.printStackTrace(); 206 } 207 208 try { 209 if (dataspace_id >= 0) 210 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); 211 } 212 catch (Exception e) { 213 e.printStackTrace(); 214 } 215 216 // Allocate space for data. 217 dset_data = new byte[(int) dims[0]][(int)sdim]; 218 str_data = new StringBuffer[(int) dims[0]]; 219 220 // Create the memory datatype. 221 try { 222 memtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); 223 if (memtype_id >= 0) 224 H5.H5Tset_size(memtype_id, sdim); 225 } 226 catch (Exception e) { 227 e.printStackTrace(); 228 } 229 230 // Read data. 231 try { 232 if ((dataset_id >= 0) && (memtype_id >= 0)) 233 H5.H5Dread(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 234 HDF5Constants.H5P_DEFAULT, dset_data); 235 byte[] tempbuf = new byte[(int)sdim]; 236 for (int indx = 0; indx < (int) dims[0]; indx++) { 237 for (int jndx = 0; jndx < sdim; jndx++) { 238 tempbuf[jndx] = dset_data[indx][jndx]; 239 } 240 str_data[indx] = new StringBuffer(new String(tempbuf).trim()); 241 } 242 } 243 catch (Exception e) { 244 e.printStackTrace(); 245 } 246 247 // Output the data to the screen. 248 for (int indx = 0; indx < dims[0]; indx++) { 249 System.out.println(DATASETNAME + " [" + indx + "]: " + str_data[indx]); 250 } 251 System.out.println(); 252 253 // End access to the dataset and release resources used by it. 254 try { 255 if (dataset_id >= 0) 256 H5.H5Dclose(dataset_id); 257 } 258 catch (Exception e) { 259 e.printStackTrace(); 260 } 261 262 // Terminate access to the data space. 263 try { 264 if (dataspace_id >= 0) 265 H5.H5Sclose(dataspace_id); 266 } 267 catch (Exception e) { 268 e.printStackTrace(); 269 } 270 271 // Terminate access to the file type. 272 try { 273 if (filetype_id >= 0) 274 H5.H5Tclose(filetype_id); 275 } 276 catch (Exception e) { 277 e.printStackTrace(); 278 } 279 280 // Terminate access to the mem type. 281 try { 282 if (memtype_id >= 0) 283 H5.H5Tclose(memtype_id); 284 } 285 catch (Exception e) { 286 e.printStackTrace(); 287 } 288 289 // Close the file. 290 try { 291 if (file_id >= 0) 292 H5.H5Fclose(file_id); 293 } 294 catch (Exception e) { 295 e.printStackTrace(); 296 } 297 298 } 299 main(String[] args)300 public static void main(String[] args) { 301 H5Ex_T_String.CreateDataset(); 302 // Now we begin the read section of this example. Here we assume 303 // the dataset and array have the same name and rank, but can have 304 // any size. Therefore we must allocate a new array to read in 305 // data using malloc(). 306 H5Ex_T_String.ReadDataset(); 307 } 308 309 } 310