例程学习:三维矩阵读写
1 /************************************************************ 2 3 This example shows how to read and write array datatypes 4 to a dataset. The program first writes integers arrays of 5 dimension ADIM0xADIM1 to a dataset with a dataspace of 6 DIM0, then closes the file. Next, it reopens the file, 7 reads back the data, and outputs it to the screen. 8 9 This file is intended for use with HDF5 Library version 1.8 10 11 ************************************************************/ 12 13 #include "hdf5.h" 14 #include <stdio.h> 15 #include <stdlib.h> 16 17 #define FILE "h5ex_t_array.h5" 18 #define DATASET "DS1" 19 #define DIM0 4 20 #define ADIM0 3 21 #define ADIM1 5 22 23 int 24 main (void) 25 { 26 hid_t file, filetype, memtype, space, dset; 27 /* Handles */ 28 herr_t status; 29 hsize_t dims[1] = {DIM0}, 30 adims[2] = {ADIM0, ADIM1}; 31 int wdata[DIM0][ADIM0][ADIM1], /* Write buffer */ 32 ***rdata, /* Read buffer */ 33 ndims; 34 hsize_t i, j, k; 35 36 /* 37 * Initialize data. i is the element in the dataspace, j and k the 38 * elements within the array datatype. 39 */ 40 for (i=0; i<DIM0; i++) 41 for (j=0; j<ADIM0; j++) 42 for (k=0; k<ADIM1; k++) 43 wdata[i][j][k] = i * j - j * k + i * k; 44 45 /* 46 * Create a new file using the default properties. 47 */ 48 file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); 49 50 /* 51 * Create array datatypes for file and memory. 52 */ 53 filetype = H5Tarray_create (H5T_STD_I64LE, 2, adims); 54 memtype = H5Tarray_create (H5T_NATIVE_INT, 2, adims); 55 56 /* 57 * Create dataspace. Setting maximum size to NULL sets the maximum 58 * size to be the current size. 59 */ 60 space = H5Screate_simple (1, dims, NULL); 61 62 /* 63 * Create the dataset and write the array data to it. 64 */ 65 dset = H5Dcreate (file, DATASET, filetype, space, H5P_DEFAULT, H5P_DEFAULT, 66 H5P_DEFAULT); 67 status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, 68 wdata[0][0]); 69 70 /* 71 * Close and release resources. 72 */ 73 status = H5Dclose (dset); 74 status = H5Sclose (space); 75 status = H5Tclose (filetype); 76 status = H5Tclose (memtype); 77 status = H5Fclose (file); 78 79 80 /* 81 * Now we begin the read section of this example. Here we assume 82 * the dataset and array have the same name and rank, but can have 83 * any size. Therefore we must allocate a new array to read in 84 * data using malloc(). 85 */ 86 87 /* 88 * Open file and dataset. 89 */ 90 file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); 91 dset = H5Dopen (file, DATASET, H5P_DEFAULT); 92 93 /* 94 * Get the datatype and its dimensions. 95 */ 96 filetype = H5Dget_type (dset); 97 ndims = H5Tget_array_dims (filetype, adims); 98 99 /* 100 * Get dataspace and allocate memory for read buffer. This is a 101 * three dimensional dataset when the array datatype is included so 102 * the dynamic allocation must be done in steps. 103 */ 104 space = H5Dget_space (dset); 105 ndims = H5Sget_simple_extent_dims (space, dims, NULL); 106 107 /* 108 * Allocate array of pointers to two-dimensional arrays (the 109 * elements of the dataset. 110 */ 111 rdata = (int ***) malloc (dims[0] * sizeof (int **)); 112 113 /* 114 * Allocate two dimensional array of pointers to rows in the data 115 * elements. 116 */ 117 rdata[0] = (int **) malloc (dims[0] * adims[0] * sizeof (int *)); 118 119 /* 120 * Allocate space for integer data. 121 */ 122 rdata[0][0] = (int *) malloc (dims[0] * adims[0] * adims[1] * sizeof (int)); 123 124 /* 125 * Set the members of the pointer arrays allocated above to point 126 * to the correct locations in their respective arrays. 127 */ 128 for (i=0; i<dims[0]; i++) { 129 rdata[i] = rdata[0] + i * adims[0]; 130 for (j=0; j<adims[0]; j++) 131 rdata[i][j] = rdata[0][0] + (adims[0] * adims[1] * i) + 132 (adims[1] * j); 133 } 134 135 /* 136 * Create the memory datatype. 137 */ 138 memtype = H5Tarray_create (H5T_NATIVE_INT, 2, adims); 139 140 /* 141 * Read the data. 142 */ 143 status = H5Dread (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, 144 rdata[0][0]); 145 146 /* 147 * Output the data to the screen. 148 */ 149 for (i=0; i<dims[0]; i++) { 150 printf ("%s[%d]:\n", DATASET, i); 151 for (j=0; j<adims[0]; j++) { 152 printf (" ["); 153 for (k=0; k<adims[1]; k++) 154 printf (" %3d", rdata[i][j][k]); 155 printf ("]\n"); 156 } 157 printf("\n"); 158 } 159 160 /* 161 * Close and release resources. 162 */ 163 free (rdata[0][0]); 164 free (rdata[0]); 165 free (rdata); 166 status = H5Dclose (dset); 167 status = H5Sclose (space); 168 status = H5Tclose (filetype); 169 status = H5Tclose (memtype); 170 status = H5Fclose (file); 171 172 return 0; 173 }
时间: 2024-10-17 09:39:59