Gateau User Manual
Atmospheric simulation of astronomical signals
fio.h
Go to the documentation of this file.
1 /*! \file fio.h
2  * \brief File input/output operations.
3  **/
4 
5 #include <iostream>
6 #include <iomanip>
7 #include <cmath>
8 #include <fstream>
9 #include <string>
10 #include <sstream>
11 #include <filesystem>
12 
13 #include "hdf5.h"
14 
15 #include "structs.h"
16 
17 #ifndef __FILEIO_H
18 #define __FILEIO_H
19 
20 namespace fs = std::filesystem;
21 
22 #define NPWVATM 55
23 #define NFREQ 8301
24 #define NATMGRID 3
25 #define COLBUFF 16
26 #define FMTBUFF 4
27 
28 #define OBSATTRS_NAME "OBSATTRS"
29 #define SPAX_NAME "SPAXEL"
30 #define FREQS_NAME "frequencies"
31 #define TIME_NAME "times"
32 #define AZ_NAME "az"
33 #define EL_NAME "el"
34 
35 #define AZ_SPAX_NAME "az_spax"
36 #define EL_SPAX_NAME "el_spax"
37 #define OUT_NAME "data"
38 
39 #define CHBUFF 100
40 #define RANK1D 1
41 #define RANK2D 2
42 
43 void readAtmMeta(int **meta, std::string path);
44 
45 template <typename T, typename U>
46 void readEtaATM(
47  T **eta_array,
48  U *pwv_atm,
49  U *freq_atm
50  );
51 
52 template <typename T, typename U>
53 void readAtmScreen(
54  T **PWV_screen,
55  U *x_spec,
56  U *y_spec,
57  std::string path,
58  std::string datp
59  );
60 
61 class OutputFile
62 {
63  private:
64  // Handles for hdf5 file and groups
65  hid_t file_id;
66  hid_t obsattrs_id;
67  hid_t spax_id;
68 
69  // Handles for hdf5 dataspace/set creation
70  hid_t dspace_id, dspace_slab_id, dset_id;
71 
72  // Output dimensions
73  hsize_t dims_1D[RANK1D],
74  dims_2D[RANK2D],
75  dims_2D_chunk[RANK2D],
76  dims_2D_stride[RANK2D];
77 
78  hsize_t dims_2D_null[2] = {0,0};
79 
80  // Hyperslab dimensions
81  hsize_t start[2], count[2];
82 
83  int ntimes, nfreqs;
84  int offset_times = 0;
85 
86  void check_API_call_status(
87  herr_t status,
88  int loc
89  )
90  {
91  if(status < 0)
92  {
93  printf("HDF5 API error occured on line %d\n", loc);
94  }
95  }
96 
97  public:
98  OutputFile(
99  const char *filename,
100  int ntimes,
101  int nfreqs,
102  float *freqs,
103  float *times,
104  float *az,
105  float *el
106  )
107  {
108  this->ntimes = ntimes;
109  this->nfreqs = nfreqs;
110 
111  dims_2D[0] = ntimes;
112  dims_2D[1] = nfreqs;
113 
114  start[1] = 0;
115  count[1] = nfreqs;
116 
117  // Make file and obsattrs group
118  file_id = H5Fcreate(
119  filename,
120  H5F_ACC_TRUNC,
121  H5P_DEFAULT,
122  H5P_DEFAULT
123  );
124 
125  obsattrs_id = H5Gcreate(
126  file_id,
127  OBSATTRS_NAME,
128  H5P_DEFAULT,
129  H5P_DEFAULT,
130  H5P_DEFAULT
131  );
132 
133  // Write dataspace for frequencies
134  dims_1D[0] = nfreqs;
135 
136  dspace_id = H5Screate_simple(
137  RANK1D,
138  dims_1D,
139  NULL
140  );
141 
142  dset_id = H5Dcreate(
143  obsattrs_id,
144  FREQS_NAME,
145  H5T_IEEE_F32LE,
146  dspace_id,
147  H5P_DEFAULT,
148  H5P_DEFAULT,
149  H5P_DEFAULT
150  );
151 
152  check_API_call_status(
153  H5Dwrite(
154  dset_id,
155  H5T_IEEE_F32LE,
156  H5S_ALL,
157  H5S_ALL,
158  H5P_DEFAULT,
159  freqs
160  ),
161  __LINE__
162  );
163 
164  check_API_call_status(
165  H5Dclose(dset_id),
166  __LINE__
167  );
168 
169  check_API_call_status(
170  H5Sclose(dspace_id),
171  __LINE__
172  );
173 
174  // Initialise time and az-el arrays
175  dims_1D[0] = ntimes;
176 
177  dspace_id = H5Screate_simple(
178  RANK1D,
179  dims_1D,
180  NULL
181  );
182 
183  dset_id = H5Dcreate(
184  obsattrs_id,
185  TIME_NAME,
186  H5T_IEEE_F32LE,
187  dspace_id,
188  H5P_DEFAULT,
189  H5P_DEFAULT,
190  H5P_DEFAULT
191  );
192 
193  check_API_call_status(
194  H5Dwrite(
195  dset_id,
196  H5T_IEEE_F32LE,
197  H5S_ALL,
198  H5S_ALL,
199  H5P_DEFAULT,
200  times
201  ),
202  __LINE__
203  );
204 
205  check_API_call_status(
206  H5Dclose(dset_id),
207  __LINE__
208  );
209 
210 
211  dset_id = H5Dcreate(
212  obsattrs_id,
213  AZ_NAME,
214  H5T_IEEE_F32LE,
215  dspace_id,
216  H5P_DEFAULT,
217  H5P_DEFAULT,
218  H5P_DEFAULT
219  );
220 
221  check_API_call_status(
222  H5Dwrite(
223  dset_id,
224  H5T_IEEE_F32LE,
225  H5S_ALL,
226  H5S_ALL,
227  H5P_DEFAULT,
228  az
229  ),
230  __LINE__
231  );
232 
233  check_API_call_status(
234  H5Dclose(dset_id),
235  __LINE__
236  );
237 
238  dset_id = H5Dcreate(
239  obsattrs_id,
240  EL_NAME,
241  H5T_IEEE_F32LE,
242  dspace_id,
243  H5P_DEFAULT,
244  H5P_DEFAULT,
245  H5P_DEFAULT
246  );
247 
248  check_API_call_status(
249  H5Dwrite(
250  dset_id,
251  H5T_IEEE_F32LE,
252  H5S_ALL,
253  H5S_ALL,
254  H5P_DEFAULT,
255  el
256  ),
257  __LINE__
258  );
259 
260  check_API_call_status(
261  H5Dclose(dset_id),
262  __LINE__
263  );
264 
265  check_API_call_status(
266  H5Sclose(dspace_id),
267  __LINE__
268  );
269 
270  check_API_call_status(
271  H5Gclose(obsattrs_id),
272  __LINE__
273  );
274  }
275 
276  void open_spaxel(
277  int spax_index,
278  float az_spax,
279  float el_spax
280  )
281  {
282  offset_times = 0;
283 
284  char spax_name[CHBUFF] = SPAX_NAME;
285  char buffer[CHBUFF];
286 
287  sprintf(
288  buffer,
289  "%d",
290  spax_index
291  );
292 
293  strcat(
294  spax_name,
295  buffer
296  );
297 
298  spax_id = H5Gcreate(
299  file_id,
300  spax_name,
301  H5P_DEFAULT,
302  H5P_DEFAULT,
303  H5P_DEFAULT
304  );
305 
306  dspace_id = H5Screate(H5S_SCALAR);
307 
308  dset_id = H5Dcreate(
309  spax_id,
310  AZ_SPAX_NAME,
311  H5T_IEEE_F32LE,
312  dspace_id,
313  H5P_DEFAULT,
314  H5P_DEFAULT,
315  H5P_DEFAULT
316  );
317 
318  check_API_call_status(
319  H5Dwrite(
320  dset_id,
321  H5T_IEEE_F32LE,
322  H5S_ALL,
323  H5S_ALL,
324  H5P_DEFAULT,
325  &az_spax
326  ),
327  __LINE__
328  );
329  check_API_call_status(
330  H5Dclose(dset_id),
331  __LINE__
332  );
333 
334  dset_id = H5Dcreate(
335  spax_id,
336  EL_SPAX_NAME,
337  H5T_IEEE_F32LE,
338  dspace_id,
339  H5P_DEFAULT,
340  H5P_DEFAULT,
341  H5P_DEFAULT
342  );
343 
344  check_API_call_status(
345  H5Dwrite(
346  dset_id,
347  H5T_IEEE_F32LE,
348  H5S_ALL,
349  H5S_ALL,
350  H5P_DEFAULT,
351  &el_spax
352  ),
353  __LINE__
354  );
355 
356  check_API_call_status(
357  H5Dclose(dset_id),
358  __LINE__
359  );
360 
361  check_API_call_status(
362  H5Sclose(dspace_id),
363  __LINE__
364  );
365 
366  // Allocate output array for this spaxel
367  dspace_id = H5Screate_simple(
368  RANK2D,
369  dims_2D,
370  NULL
371  );
372 
373  dset_id = H5Dcreate(
374  spax_id,
375  OUT_NAME,
376  H5T_IEEE_F32LE,
377  dspace_id,
378  H5P_DEFAULT,
379  H5P_DEFAULT,
380  H5P_DEFAULT
381  );
382  }
383 
384  void write_chunk_to_spaxel(
385  int ntimes_chunk,
386  float *data
387  )
388  {
389  start[0] = offset_times;
390  count[0] = ntimes_chunk;
391 
392  dims_1D[0] = ntimes_chunk * nfreqs;
393 
394  check_API_call_status(
395  H5Sselect_hyperslab(
396  dspace_id,
397  H5S_SELECT_SET,
398  start,
399  NULL,
400  count,
401  NULL
402  ),
403  __LINE__
404  );
405 
406  dspace_slab_id = H5Screate_simple(
407  RANK1D,
408  dims_1D,
409  NULL
410  );
411 
412  check_API_call_status(
413  H5Dwrite(
414  dset_id,
415  H5T_IEEE_F32LE,
416  dspace_slab_id,
417  dspace_id,
418  H5P_DEFAULT,
419  data
420  ),
421  __LINE__
422  );
423 
424  offset_times += ntimes_chunk;
425  check_API_call_status(
426  H5Sclose(dspace_slab_id),
427  __LINE__
428  );
429  }
430 
431  void close_spaxel(int spax_index)
432  {
433  check_API_call_status(
434  H5Dclose(dset_id),
435  __LINE__
436  );
437 
438  check_API_call_status(
439  H5Sclose(dspace_id),
440  __LINE__
441  );
442 
443  check_API_call_status(
444  H5Gclose(spax_id),
445  __LINE__
446  );
447  }
448 
449  ~OutputFile()
450  {
451  check_API_call_status(
452  H5Fclose(file_id),
453  __LINE__
454  );
455  }
456 };
457 
458 #endif
459 
460 void readAtmMeta(
461  int **meta,
462  std::string path
463  )
464 {
465  fs::path dir(path);
466  fs::path file("atm_meta.datp");
467  fs::path abs_loc = dir / file;
468 
469  *meta = new int[NATMGRID];
470 
471  std::string store;
472 
473  std::ifstream myfile(abs_loc);
474  std::string line;
475 
476  int idx = 0;
477 
478  if (!myfile)
479  {
480  std::cerr
481  << "Could not open the file at "
482  << abs_loc
483  << std::endl;
484  exit(5);
485  }
486  else
487  {
488  while(std::getline(myfile, line))
489  {
490  std::istringstream iss(line);
491  while(std::getline(iss, store, ' '))
492  {
493  if (store=="") {continue;}
494  (*meta)[idx] = std::stoi(store);
495  idx++;
496  }
497  }
498  myfile.close();
499  }
500 }
501 
502 template <typename T, typename U>
503 void readEtaATM(
504  T **eta_array,
505  U *pwv_atm,
506  U *freq_atm,
507  const char* filepath
508  )
509 {
510  // TODO read these in from file? Ask Arend
511  pwv_atm->start = 0.1;
512  pwv_atm->step = 0.1;
513  pwv_atm->num = NPWVATM;
514 
515  freq_atm->start = 70.e9;
516  freq_atm->step = 0.1e9;
517  freq_atm->num = NFREQ;
518 
519  *eta_array = new T[NPWVATM * NFREQ];
520 
521  std::string store;
522  //std::cout << abi::__cxa_demangle(typeid(store).name(), NULL, NULL, &status) << std::endl;
523 
524  std::ifstream myfile(filepath);
525  std::string line;
526 
527  int line_nr = 0;
528  int idx = 0;
529 
530  if (!myfile)
531  {
532  std::cerr
533  << "Could not open the resource file at "
534  << filepath
535  << std::endl;
536  exit(5);
537  /* TODO Standardize exit codes */
538  }
539 
540  while(std::getline(myfile, line))
541  {
542  std::istringstream iss(line);
543  if(!line_nr)
544  {
545  line_nr++;
546  continue;
547  }
548 
549  while(std::getline(iss, store, ' '))
550  {
551  if(!idx)
552  {
553  idx++;
554  continue;
555  }
556  else if (store=="")
557  {
558  continue;
559  }
560 
561  (*eta_array)[NFREQ * (idx-1) + (line_nr - 1)] = std::stof(store);
562  idx++;
563  }
564  line_nr++;
565  idx = 0;
566  }
567  myfile.close();
568 }
569 
570 template <typename T, typename U>
571 void readAtmScreen(
572  T **PWV_screen,
573  U *x_spec,
574  U *y_spec,
575  std::string path,
576  std::string datp)
577 {
578  fs::path dir(path);
579  fs::path file(datp);
580  fs::path abs_loc = dir / file;
581 
582  *PWV_screen = new T[x_spec->num * y_spec->num];
583 
584  std::string store;
585 
586  std::ifstream myfile(abs_loc);
587  std::string line;
588 
589  int line_nr = 0;
590  int idx = 0;
591 
592  if (!myfile)
593  {
594  std::cerr
595  << "Could not open the file!"
596  << std::endl;
597  }
598  else
599  {
600  while(std::getline(myfile, line))
601  {
602  std::istringstream iss(line);
603  while(std::getline(iss, store, ' '))
604  {
605  if (store=="")
606  {
607  continue;
608  }
609 
610  (*PWV_screen)[y_spec->num * line_nr + idx] = std::stof(store);
611  idx++;
612  }
613  line_nr++;
614  idx = 0;
615  }
616  myfile.close();
617  }
618 }
structs.h
Data structures for receiving data from Python interface.
OutputFile
Definition: fio.h:61