![]() |
RTXI 1.3
|
00001 /* 00002 Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornel Medical College 00003 00004 This program is free software: you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation, either version 3 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 */ 00018 00019 #include <errno.h> 00020 #include <stdlib.h> 00021 #include <stdio.h> 00022 #include <unistd.h> 00023 #include <sstream> 00024 #include <sys/stat.h> 00025 #include <time.h> 00026 #include <hdf5.h> 00027 #include <hdf5_hl.h> 00028 00029 int main(int argc, char *argv[]) { 00030 00031 // hide HDF5 stack tracks on all error returns 00032 H5Eset_auto2(H5E_DEFAULT, NULL, NULL); 00033 00034 if (argc != 2) { 00035 fprintf(stderr, "Usage: %s <filename>\n", argv[0]); 00036 return -EINVAL; 00037 } 00038 00039 char cmdBuff[200]; 00040 sprintf(cmdBuff, "cp %s %s.old", argv[1], argv[1]); 00041 system(cmdBuff); 00042 struct tm* clock; 00043 struct stat attrib; 00044 stat(argv[1], &attrib); 00045 clock = localtime( &(attrib.st_mtime) ); // last modified time 00046 00047 hid_t fid = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT); 00048 if (fid < 0) { 00049 fprintf(stderr, "Failed to open %s.\n", argv[0]); 00050 return fid; 00051 } 00052 00053 // create a temporary file to store the large amount of stream data 00054 char tempfile_template[] = "rtxi_hdf_matlabize.XXXXXX"; 00055 int tmpfd = mkstemp(tempfile_template); 00056 if (tmpfd < 0) { 00057 fprintf(stderr, 00058 "Failed to create a temporary file for buffering data.\n"); 00059 return -errno; 00060 } 00061 unlink(tempfile_template); 00062 00063 int trial_num = 1; 00064 std::stringstream trial_name; 00065 trial_name << "/Trial1"; 00066 H5G_info_t junk; 00067 00068 while (H5Gget_info_by_name(fid, trial_name.str().c_str(), &junk, 00069 H5P_DEFAULT) >= 0) { 00070 00071 // determine the dimension of the data 00072 trial_name << "/Synchronous Data/Channel Data"; 00073 hid_t table = H5Dopen(fid, trial_name.str().c_str(), H5P_DEFAULT); 00074 if (table >= 0) { 00075 00076 hid_t type = H5Dget_type(table); 00077 if (H5Tequal(type, H5T_IEEE_F64LE)) { 00078 H5Dclose(type); 00079 H5Dclose(table); 00080 00081 ++trial_num; 00082 trial_name.str(""); 00083 trial_name << "/Trial" << trial_num; 00084 continue; 00085 } 00086 00087 hsize_t cols = H5Tget_size(type) / sizeof(double); 00088 H5Dclose(type); 00089 H5Dclose(table); 00090 00091 hsize_t rows; 00092 table = H5PTopen(fid, trial_name.str().c_str()); 00093 H5PTget_num_packets(table, &rows); 00094 00095 lseek(tmpfd, SEEK_SET, 0); 00096 for (int i = 0; i < rows; ++i) { 00097 double data[cols]; 00098 00099 H5PTget_next(table, 1, data); 00100 write(tmpfd, data, sizeof(data)); 00101 } 00102 00103 H5PTclose(table); 00104 H5Ldelete(fid, trial_name.str().c_str(), H5P_DEFAULT); 00105 00106 hsize_t dims[] = { rows, cols }; 00107 hid_t space = H5Screate_simple(2, dims, dims); 00108 table = H5Dcreate(fid, trial_name.str().c_str(), H5T_IEEE_F64LE, 00109 space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); 00110 H5Sclose(space); 00111 00112 lseek(tmpfd, SEEK_SET, 0); 00113 { 00114 00115 hid_t data_space = H5Screate_simple(1, &cols, &cols); 00116 H5Sselect_all(data_space); 00117 00118 double data[cols]; 00119 hsize_t start[2] = { 0, 0 }; 00120 hsize_t count[2] = { 1, cols }; 00121 00122 hid_t select = H5Dget_space(table); 00123 00124 for (int i = 0; i < rows; ++i) { 00125 00126 read(tmpfd, data, sizeof(data)); 00127 00128 start[0] = i; 00129 H5Sselect_hyperslab(select, H5S_SELECT_SET, start, NULL, 00130 count, NULL); 00131 00132 H5Dwrite(table, H5T_IEEE_F64LE, data_space, select, 00133 H5P_DEFAULT, data); 00134 } 00135 H5Sclose(select); 00136 H5Sclose(data_space); 00137 } 00138 00139 H5Dclose(table); 00140 } 00141 00142 trial_name.str(""); 00143 trial_name << "/Trial" << trial_num; 00144 trial_name << "/Syncronous Data/Channel Data"; 00145 table = H5Dopen(fid, trial_name.str().c_str(), H5P_DEFAULT); 00146 if (table >= 0) { 00147 00148 hid_t type = H5Dget_type(table); 00149 if (H5Tequal(type, H5T_IEEE_F64LE)) { 00150 H5Dclose(type); 00151 H5Dclose(table); 00152 00153 ++trial_num; 00154 trial_name.str(""); 00155 trial_name << "/Trial" << trial_num; 00156 continue; 00157 } 00158 00159 hsize_t cols = H5Tget_size(type) / sizeof(double); 00160 H5Dclose(type); 00161 H5Dclose(table); 00162 00163 hsize_t rows; 00164 table = H5PTopen(fid, trial_name.str().c_str()); 00165 H5PTget_num_packets(table, &rows); 00166 00167 lseek(tmpfd, SEEK_SET, 0); 00168 for (int i = 0; i < rows; ++i) { 00169 double data[cols]; 00170 00171 H5PTget_next(table, 1, data); 00172 write(tmpfd, data, sizeof(data)); 00173 } 00174 00175 H5PTclose(table); 00176 H5Ldelete(fid, trial_name.str().c_str(), H5P_DEFAULT); 00177 00178 hsize_t dims[] = { rows, cols }; 00179 hid_t space = H5Screate_simple(2, dims, dims); 00180 table = H5Dcreate(fid, trial_name.str().c_str(), H5T_IEEE_F64LE, 00181 space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); 00182 H5Sclose(space); 00183 00184 lseek(tmpfd, SEEK_SET, 0); 00185 { 00186 00187 hid_t data_space = H5Screate_simple(1, &cols, &cols); 00188 H5Sselect_all(data_space); 00189 00190 double data[cols]; 00191 hsize_t start[2] = { 0, 0 }; 00192 hsize_t count[2] = { 1, cols }; 00193 00194 hid_t select = H5Dget_space(table); 00195 00196 for (int i = 0; i < rows; ++i) { 00197 00198 read(tmpfd, data, sizeof(data)); 00199 00200 start[0] = i; 00201 H5Sselect_hyperslab(select, H5S_SELECT_SET, start, NULL, 00202 count, NULL); 00203 00204 H5Dwrite(table, H5T_IEEE_F64LE, data_space, select, 00205 H5P_DEFAULT, data); 00206 } 00207 H5Sclose(select); 00208 H5Sclose(data_space); 00209 } 00210 00211 H5Dclose(table); 00212 } 00213 00214 ++trial_num; 00215 trial_name.str(""); 00216 trial_name << "/Trial" << trial_num; 00217 } 00218 00219 H5Fclose(fid); 00220 close(tmpfd); 00221 sprintf(cmdBuff, "touch -m --date=\"%s\" %s", asctime(clock), argv[1]); 00222 sprintf(cmdBuff, "touch -m --date=\"%s\" %s.old", asctime(clock), argv[1]); 00223 printf("%s\n",asctime(clock)); 00224 system(cmdBuff); 00225 00226 return 0; 00227 00228 }