-
Notifications
You must be signed in to change notification settings - Fork 7
/
fbh5_util.c
285 lines (248 loc) · 10.8 KB
/
fbh5_util.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* fbh5_util.c *
* ----------- *
* Utility functions . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "fbh5_defs.h"
#define LEN_TIMESTAMP 22
/***
Get a timestamp.
***/
void get_timestamp(char * buffer) {
time_t time_t_time;
struct tm * tm_struct;
time(&time_t_time);
tm_struct = localtime(&time_t_time);
strftime(buffer, LEN_TIMESTAMP, "%Y-%m-%d_%H:%M:%S ", tm_struct);
}
/***
Report information.
***/
void fbh5_info(const char *format, ...) {
char buffer[256];
va_list va_array;
va_start(va_array, format);
get_timestamp(buffer);
strcat(buffer, format);
vfprintf(stdout, buffer, va_array);
va_end(va_array);
}
/***
Report bad news as a warning message.
***/
void fbh5_warning(char * srcfile, int linenum, char * msg) {
char timestamp[LEN_TIMESTAMP];
get_timestamp(timestamp);
fprintf(stderr, "%sFBH5-WARNING file %s line %d :: %s\n", timestamp, srcfile, linenum, msg);
}
/***
Report bad news as an error message.
***/
void fbh5_error(char * srcfile, int linenum, char * msg) {
char timestamp[LEN_TIMESTAMP];
get_timestamp(timestamp);
fprintf(stderr, "%sFBH5-ERROR file %s line %d :: %s\n", timestamp, srcfile, linenum, msg);
}
/***
Set a file-level or dataset-level attribute in string-value format.
***/
void fbh5_set_str_attr(hid_t file_or_dataset_id, char * tag, char * p_value, int debug_callback) {
herr_t status;
hid_t id_scalar, atype, id_attr;
char warning[256];
if(debug_callback)
fbh5_info("fbh5_set_str_attr: %s = %s\n", tag, p_value);
id_scalar = H5Screate(H5S_SCALAR);
atype = H5Tcopy(H5T_C_S1); // attr type = string
H5Tset_size(atype, strlen(p_value)); // set create the string
H5Tset_strpad(atype, H5T_STR_NULLTERM);
id_attr = H5Acreate(file_or_dataset_id, tag, atype, id_scalar, H5P_DEFAULT, H5P_DEFAULT);
if(id_attr < 0) {
sprintf(warning, "fbh5_set_str_attr/H5Acreate FAILED, key=%s, value=%s", tag, p_value);
fbh5_warning(__FILE__, __LINE__, warning);
return;
}
status = H5Awrite(id_attr, atype, p_value);
if(status != 0) {
sprintf(warning, "fbh5_set_str_attr/H5Awrite FAILED, key=%s, value=%s", tag, p_value);
fbh5_warning(__FILE__, __LINE__, warning);
}
status = H5Aclose(id_attr);
if(status != 0) {
sprintf(warning, "fbh5_set_str_attr/H5Aclose FAILED, key=%s, value=%s", tag, p_value);
fbh5_warning(__FILE__, __LINE__, warning);
}
}
/***
Set a dataset-level attribute in double-value format.
***/
void fbh5_set_dataset_double_attr(hid_t dataset_id, char * tag, double * p_value, int debug_callback) {
herr_t status;
hid_t id_attr, id_scalar;
char warning[256];
if(debug_callback)
fbh5_info("fbh5_set_dataset_double_attr: %s = %f\n", tag, *p_value);
id_scalar = H5Screate(H5S_SCALAR);
if(id_scalar < 0) {
sprintf(warning, "fbh5_set_dataset_double_attr/H5Screate FAILED, key=%s, value=%f", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
return;
}
id_attr = H5Acreate2(dataset_id, tag, H5T_NATIVE_DOUBLE, id_scalar, H5P_DEFAULT, H5P_DEFAULT);
if(id_attr < 0) {
sprintf(warning, "fbh5_set_dataset_double_attr/H5Acreate2 FAILED, key=%s, value=%f", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
return;
}
status = H5Awrite(id_attr, H5T_NATIVE_DOUBLE, p_value);
if(status < 0) {
sprintf(warning, "fbh5_set_dataset_double_attr/H5Awrite FAILED, key=%s, value=%f", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
}
status = H5Aclose(id_attr);
if(status < 0) {
sprintf(warning, "fbh5_set_dataset_double_attr/H5Aclose FAILED, key=%s, value=%f", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
}
}
/***
Set a dataset-level attribute in int-value format.
***/
void fbh5_set_dataset_int_attr(hid_t dataset_id, char * tag, int * p_value, int debug_callback) {
herr_t status;
hid_t id_attr, id_scalar;
char warning[256];
if(debug_callback)
fbh5_info("fbh5_set_dataset_int_attr: %s = %d\n", tag, *p_value);
id_scalar = H5Screate(H5S_SCALAR);
if(id_scalar < 0) {
sprintf(warning, "fbh5_set_dataset_int_attr/H5Screate FAILED, key=%s, value=%d", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
return;
}
id_attr = H5Acreate2(dataset_id, tag, H5T_NATIVE_INT, id_scalar, H5P_DEFAULT, H5P_DEFAULT);
if(id_attr < 0) {
sprintf(warning, "fbh5_set_dataset_int_attr/H5Acreate2 FAILED, key=%s, value=%d", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
return;
}
status = H5Awrite(id_attr, H5T_NATIVE_INT, p_value);
if(status != 0) {
sprintf(warning, "fbh5_set_dataset_int_attr/H5Awrite FAILED, key=%s, value=%d", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
}
status = H5Aclose(id_attr);
if(status != 0) {
sprintf(warning, "fbh5_set_dataset_int_attr/H5Aclose FAILED, key=%s, value=%d", tag, *p_value);
fbh5_warning(__FILE__, __LINE__, warning);
}
}
/***
Write metadata to FBH5 file dataset.
***/
void fbh5_write_metadata(hid_t dataset_id, fb_hdr_t *p_md, int debug_callback) {
fbh5_set_dataset_int_attr(dataset_id, "machine_id", &(p_md->machine_id), debug_callback);
fbh5_set_dataset_int_attr(dataset_id, "telescope_id", &(p_md->telescope_id), debug_callback);
fbh5_set_dataset_int_attr(dataset_id, "data_type", &(p_md->data_type), debug_callback);
fbh5_set_dataset_int_attr(dataset_id, "nchans", &(p_md->nchans), debug_callback);
fbh5_set_dataset_int_attr(dataset_id, "nfpc", &(p_md->nfpc), debug_callback);
fbh5_set_dataset_int_attr(dataset_id, "nbeams", &(p_md->nbeams), debug_callback);
fbh5_set_dataset_int_attr(dataset_id, "ibeam", &(p_md->ibeam), debug_callback);
if (p_md->refbeam != -1) {
fbh5_set_dataset_int_attr(dataset_id, "refbeam", &(p_md->refbeam), debug_callback);
}
fbh5_set_dataset_int_attr(dataset_id, "nbits", &(p_md->nbits), debug_callback);
fbh5_set_dataset_int_attr(dataset_id, "nifs", &(p_md->nifs), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "src_raj", &(p_md->src_raj), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "src_dej", &(p_md->src_dej), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "az_start", &(p_md->az_start), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "za_start", &(p_md->za_start), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "fch1", &(p_md->fch1), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "foff", &(p_md->foff), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "tstart", &(p_md->tstart), debug_callback);
fbh5_set_dataset_double_attr(dataset_id, "tsamp", &(p_md->tsamp), debug_callback);
fbh5_set_str_attr(dataset_id, "source_name", &(p_md->source_name[0]), debug_callback);
fbh5_set_str_attr(dataset_id, "rawdatafile", &(p_md->rawdatafile[0]), debug_callback);
}
/***
Set up a dimension scale label.
* Create a secondary dataset, dscale_id.
* Attach dscale_id to the file's primary dataset as a Dimension Scale label.
***/
void fbh5_set_ds_label(fbh5_context_t * p_fbh5_ctx, char * label, int dims_index, int debug_callback) {
herr_t status;
char wstr[256];
if(debug_callback)
fbh5_info("fbh5_set_ds_label: label = %s, dims_index = %d\n", label, dims_index);
status = H5DSset_label(p_fbh5_ctx->dataset_id, // Dataset ID
dims_index, // Dimension index to which dscale_id applies to
label); // Label
if(status < 0) {
sprintf(wstr, "fbh5_set_ds_label/H5DSset_label FAILED (%s)", label);
fbh5_warning(__FILE__, __LINE__, wstr);
}
}
/***
Display some of the fbh5_context values.
***/
void fbh5_show_context(char * caller, fbh5_context_t * p_fbh5_ctx) {
if(p_fbh5_ctx == NULL) {
fbh5_info("*** fbh5_show_context: p_fbh5_ctx = NULL !!!");
return;
}
fbh5_info("fbh5_show_context(%s): active = %d\n", caller, p_fbh5_ctx->active);
fbh5_info("fbh5_show_context(%s): elem_size = %d\n", caller, p_fbh5_ctx->elem_size);
fbh5_info("fbh5_show_context(%s): tint_size = %ld\n", caller, p_fbh5_ctx->tint_size);
fbh5_info("fbh5_show_context(%s): offset_dims = (%lld, %lld, %lld)\n",
caller, p_fbh5_ctx->offset_dims[0], p_fbh5_ctx->offset_dims[1], p_fbh5_ctx->offset_dims[2]);
fbh5_info("fbh5_show_context(%s): filesz_dims = (%lld, %lld, %lld)\n",
caller, p_fbh5_ctx->filesz_dims[0], p_fbh5_ctx->filesz_dims[1], p_fbh5_ctx->filesz_dims[2]);
fbh5_info("fbh5_show_context(%s): byte_count = %ld\n", caller, p_fbh5_ctx->byte_count);
fbh5_info("fbh5_show_context(%s): dump_count = %ld\n", caller, p_fbh5_ctx->dump_count);
}
/***
Algorithm (GBT) to calculate the chunk dimensions depending on the file type.
Python3 source: blimpy waterfall.py _get_chunk_dimensions()
* High frequency resolution (HFR) --> (1,1,1048576)
* High time resolution (HTR) --> (2048,1,512)
* Intermediate frequency resolution --> (10,1,65536)
* None of the above ------------------> (1,1,512)
***/
void fbh5_blimpy_chunking(fb_hdr_t * p_fb_hdr, hsize_t * p_cdims) {
// GBT: '.0000.' is HFR
// GBT: 1048576 is the number of channels in a coarse channel.
if(p_fb_hdr->foff < 1.0e-5) {
*p_cdims = 1;
*(p_cdims + 1) = 1;
*(p_cdims + 2) = 1048576;
if(p_fb_hdr->nchans < 1048576)
*(p_cdims + 2) = p_fb_hdr->nchans;
return;
}
// GBT: .0001. is HTR
// GBT: 512 is the total number of channels per single band
if(p_fb_hdr->tsamp < 1.0e-3) {
*p_cdims = 2048;
*(p_cdims + 1) = 1;
*(p_cdims + 2) = 512;
if(p_fb_hdr->nchans < 512)
*(p_cdims + 2) = p_fb_hdr->nchans;
return;
}
// GBT: .0002. is intermediate
// GBT: 65536 is the total number of channels per single band
if(p_fb_hdr->foff < 1.0e-2 && p_fb_hdr->foff >= 1.0e-5) {
*p_cdims = 10;
*(p_cdims + 1) = 1;
*(p_cdims + 2) = 65536;
if(p_fb_hdr->nchans < 65536)
*(p_cdims + 2) = p_fb_hdr->nchans;
return;
}
// None of the above.
*p_cdims = 1;
*(p_cdims + 1) = 1;
*(p_cdims + 2) = 512;
if(p_fb_hdr->nchans < 512)
*(p_cdims + 2) = p_fb_hdr->nchans;
}