forked from UCBerkeleySETI/rawspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbh5_write.c
168 lines (152 loc) · 6.49 KB
/
fbh5_write.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* fbh5_write.c *
* ------------ *
* Write an FBH5 dump. . *
* *
* HDF 5 library functions used: *
* - H5Dset_extent - Define new file size to include this new dump *
* - H5Dget_space - Get a space handle for writing *
* - H5Sselect_hyperslab - Define hyperslab offset and length in to write *
* - H5Dwrite - Write the hyperslab *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "fbh5_defs.h"
/***
Main entry point.
***/
int fbh5_write(fbh5_context_t * p_fbh5_ctx, fb_hdr_t * p_fb_hdr, void * p_buffer, size_t bufsize, int debug_callback) {
herr_t status; // Status from HDF5 function call
size_t ntints; // Number of time integrations in the current dump
hid_t filespace_id; // Identifier for a copy of the dataspace
hsize_t selection[3]; // Current selection
clock_t clock_1; // Debug time measurement
double cpu_time_used; // Debug time measurement
/*
* Initialise write loop.
*/
if(debug_callback)
fbh5_show_context("fbh5_write", p_fbh5_ctx);
ntints = bufsize / p_fbh5_ctx->tint_size; // Compute the number of time integrations in the current dump.
p_fbh5_ctx->dump_count += 1; // Bump the dump count.
/*
* Bump the count of time integrations.
* One was already accounted for at open time - required by HDF5 library.
* So,
* If this is the very first write, bump by (ntints - 1);
* Else, bumpy by ntints.
*/
if(p_fbh5_ctx->offset_dims[0] > 0)
p_fbh5_ctx->filesz_dims[0] += ntints;
else
p_fbh5_ctx->filesz_dims[0] += (ntints - 1);
/*
* Define the current slab selection in terms of its shape.
*/
selection[0] = ntints;
selection[1] = p_fb_hdr->nifs;
selection[2] = p_fb_hdr->nchans;
if(debug_callback) {
fbh5_info("fbh5_write: dump %ld, offset=(%lld, %lld, %lld), selection=(%lld, %lld, %lld), filesize=(%lld, %lld, %lld)\n",
p_fbh5_ctx->dump_count,
p_fbh5_ctx->offset_dims[0],
p_fbh5_ctx->offset_dims[1],
p_fbh5_ctx->offset_dims[2],
selection[0],
selection[1],
selection[2],
p_fbh5_ctx->filesz_dims[0],
p_fbh5_ctx->filesz_dims[1],
p_fbh5_ctx->filesz_dims[2]);
clock_1 = clock();
}
/*
* Extend dataset.
*/
status = H5Dset_extent(p_fbh5_ctx->dataset_id, // Dataset handle
p_fbh5_ctx->filesz_dims); // New dataset shape
if(status < 0) {
fbh5_error(__FILE__, __LINE__, "fbh5_write: H5Dset_extent/dataset_id FAILED");
fbh5_show_context("fbh5_write", p_fbh5_ctx);
p_fbh5_ctx->active = 0;
return 1;
}
/*
* Reset dataspace extent to match current slab selection.
*/
status = H5Sset_extent_simple(p_fbh5_ctx->dataspace_id, // Dataspace handle
NDIMS, // Repeat rank from previous API calls
selection, // New dataspace size shape
p_fbh5_ctx->filesz_dims); // Max dataspace dimensions
if(status < 0) {
fbh5_error(__FILE__, __LINE__, "fbh5_write: H5Dset_extent/dataset_id FAILED");
fbh5_show_context("fbh5_write", p_fbh5_ctx);
p_fbh5_ctx->active = 0;
return 1;
}
/*
* Get filespace.
*/
filespace_id = H5Dget_space(p_fbh5_ctx->dataset_id); // Dataset handle
if(filespace_id < 0) {
fbh5_error(__FILE__, __LINE__, "fbh5_write: H5Dget_space FAILED");
fbh5_show_context("fbh5_write", p_fbh5_ctx);
p_fbh5_ctx->active = 0;
return 1;
}
/*
* Select the filespace hyperslab.
*/
status = H5Sselect_hyperslab(filespace_id, // Filespace handle
H5S_SELECT_SET, // Replace preexisting selection
p_fbh5_ctx->offset_dims, // Starting offset dimensions of first element
NULL, // Not "striding"
selection, // Selection dimensions
NULL); // Block parameter : default value
if(status < 0) {
fbh5_error(__FILE__, __LINE__, "fbh5_write: H5Sselect_hyperslab/filespace FAILED");
fbh5_show_context("fbh5_write", p_fbh5_ctx);
p_fbh5_ctx->active = 0;
return 1;
}
/*
* Write out current time integration to the hyperslab.
*/
status = H5Dwrite(p_fbh5_ctx->dataset_id, // Dataset handle
p_fbh5_ctx->elem_type, // HDF5 element type
p_fbh5_ctx->dataspace_id, // Dataspace handle
filespace_id, // Filespace_id
H5P_DEFAULT, // Default data transfer properties
p_buffer); // Buffer holding the data
if(status < 0) {
fbh5_error(__FILE__, __LINE__, "fbh5_write: H5Dwrite FAILED");
fbh5_show_context("fbh5_write", p_fbh5_ctx);
p_fbh5_ctx->active = 0;
return 1;
}
/*
* Point ahead for the next call to fbh5_write.
*/
p_fbh5_ctx->offset_dims[0] += ntints;
/*
* Close temporary filespace handle.
*/
status = H5Sclose(filespace_id);
if(status < 0) {
fbh5_error(__FILE__, __LINE__, "fbh5_close H5Sclose/filespace_id FAILED\n");
fbh5_show_context("fbh5_write", p_fbh5_ctx);
p_fbh5_ctx->active = 0;
return 1;
}
if(debug_callback) {
cpu_time_used = ((double) (clock() - clock_1)) / CLOCKS_PER_SEC;
fbh5_info("fbh5_write: dump %ld E.T. = %.3f s\n", p_fbh5_ctx->dump_count, cpu_time_used);
}
/*
* Bump counters.
*/
p_fbh5_ctx->byte_count += bufsize;
/*
* Bye-bye.
*/
return 0;
}