OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
bin_util.cpp
Go to the documentation of this file.
1 #include "bin_util.h"
2 #include "L3Shape.h"
3 
4 #include <mfhdf.h>
5 
6 using namespace std;
7 
8 namespace Hdf {
9 
10 int create_vdata(int32_t fileid, int32_t vgid, int32_t *vdata_id,
11  const char *vdata_name, const char *class_name, int32_t n_flds,
12  char const * const fldname[], int32_t type[], int32_t noext, int32_t *aid) {
13  int32_t status;
14  char buffer[1000];
15  static int32_t prod_count = 0;
16 
17  // Setup new vdata
18  // ---------------
19  *vdata_id = VSattach(fileid, -1, "w");
20  // printf("Setting Up: %s %ld\n", vdata_name, *vdata_id);
21 
22  for (int32_t i = 0; i < n_flds; i++) {
23  status = VSfdefine(*vdata_id, fldname[i], type[i], 1);
24  if (status != 0) {
25  printf("Error defining \"%s\".\n", fldname[i]);
26  }
27  }
28 
29  /* Set fieldnames */
30  /* -------------- */
31  strcpy(buffer, fldname[0]);
32  for (int32_t i = 1; i < n_flds; i++) {
33  strcat(buffer, ",");
34  strcat(buffer, fldname[i]);
35  }
36  status = VSsetfields(*vdata_id, buffer);
37 
38  status = Vinsert(vgid, *vdata_id);
39  status = VSsetname(*vdata_id, vdata_name);
40  status = VSsetclass(*vdata_id, class_name);
41 
42  VSsetblocksize(*vdata_id, 4096 * 6);
43 
44  // Setup External Data Vdatas
45  // --------------------------
46  if (strcmp(class_name, "DataSubordinate") == 0 && noext == 0) {
47 
48  static FILE *sfile;
49 
50  memset(buffer, 0, sizeof (buffer));
51  status = VSwrite(*vdata_id, (uint8_t *) buffer, 1, FULL_INTERLACE);
52  VSdetach(*vdata_id);
53  *vdata_id = VSattach(fileid, VSfind(fileid, vdata_name), "w");
54 
55  // synthesize file name
56  char *outname;
57  intn access, attach;
58  Hfidinquire(fileid, &outname, &access, &attach);
59  strcpy(buffer, outname);
60 
61  // Remove trailing ".main" if it exists
62  char *cptr = strstr(buffer, ".main");
63  if (strlen(buffer) + buffer - cptr == 5)
64  *cptr = 0;
65 
66  sprintf(&buffer[strlen(buffer)], ".x%02d", prod_count);
67 
68  // open file and write name of main file
69  // in first 512 bytes
70  sfile = fopen(buffer, "w");
71 
72  if (strrchr(outname, '/') != NULL)
73  strcpy(buffer, strrchr(outname, '/') + 1);
74  else
75  strcpy(buffer, outname);
76 
77  cptr = strstr(buffer, ".main");
78  if (strlen(buffer) + buffer - cptr == 5)
79  *cptr = 0;
80 
81  status = fwrite(buffer, 512, 1, sfile);
82  fclose(sfile);
83 
84  // convert to external element
85  strcpy(buffer, outname);
86 
87  cptr = strstr(buffer, ".main");
88  if (strlen(buffer) + buffer - cptr == 5)
89  *cptr = 0;
90 
91  sprintf(&buffer[strlen(buffer)], ".x%02d", prod_count);
92  *aid = HXcreate(fileid, DFTAG_VS, (uint16_t) VSfind(fileid, vdata_name),
93  buffer, 512, 0);
94 
95  VSdetach(*vdata_id);
96  *vdata_id = VSattach(fileid, VSfind(fileid, vdata_name), "w");
97  VSseek(*vdata_id, 0);
98 
99  prod_count++;
100  }
101 
102  return 0;
103 }
104 
105 int32_t write_vdata(int vdata_id, int32_t n_recs_to_write, void *data) {
106  int32_t n_write = VSwrite(vdata_id, (const uint8_t*) data,
107  n_recs_to_write, FULL_INTERLACE);
108 
109  if (n_write != n_recs_to_write) {
110  cout << "Error in VSwrite (write_vdata): "
111  << n_recs_to_write << " " << n_write << endl;
112  exit(-1);
113  }
114 
115  return n_write;
116 }
117 
118 int read_binList(int n_elem, int32_t vdata_id_binlist, binListStruct* binList, l3::L3Shape* shape) {
119  if (n_elem == 0) {
120  printf("The number of elements requested to be read is 0.\n");
121  }
122 
123  // Offset within BinList memory struct
124  int32_t binlist_offset[] = {0, 4, 6, 8, 12, 16, 20};
125 
126  // Size of fields in BinList Vdata
127  int32_t binlist_size[] = {4, 2, 2, 2, 4, 1, 4};
128 
129  uint8_t *bytebuf;
130  bytebuf = (uint8_t *) malloc(n_elem * 19);
131 
132  int records_read;
133 
134  records_read = VSread(vdata_id_binlist, bytebuf, n_elem, FULL_INTERLACE);
135 
136  // Loop through bins
137  for (int32_t j = 0; j < n_elem; j++) {
138  int binlist_idx = j * 19;
139  for (size_t i = 0; i < 7; i++) {
140 
141  // Point to output location in BinList structure (memory)
142  char *ptr = (char *) &binList[j] + binlist_offset[i];
143  memcpy(ptr, &bytebuf[binlist_idx], binlist_size[i]);
144 
145  binlist_idx += binlist_size[i];
146 
147  if (i == 0) {
148  int32_t bin;
149  memcpy(&bin, &bytebuf[j * 19], 4);
150  float lat;
151  float lon;
152  shape->bin2latlon(bin, lat, lon);
153  memcpy((char *) &binList[j] + 24, &lat, 4);
154  memcpy((char *) &binList[j] + 28, &lon, 4);
155  }
156  }
157  }
158  free(bytebuf);
159 
160  return records_read;
161 } // end read_binList
162 
163 int write_binList(int n_elem, int32_t vdata_id_binlist, binListStruct* binList) {
164  if (n_elem == 0) {
165  printf("No elements are requested to be written in \"write_binList\".\n");
166  }
167 
168  // Offset within BinList memory struct
169  int32_t binlist_struct_offset[] = {0, 4, 6, 8, 12, 16, 20};
170 
171  // Offset within BinList Vdata
172  int32_t binlist_vdata_offset[] = {0, 4, 6, 8, 10, 14, 15};
173 
174  // Size of fields in BinList Vdata
175  int32_t binlist_size[] = {4, 2, 2, 2, 4, 1, 4};
176 
177  uint8_t *bytebuf;
178  bytebuf = (uint8_t *) malloc(n_elem * 19);
179 
180  for (int32_t j = 0; j < n_elem; j++) {
181  // Loop through BinList fields
182  for (size_t i = 0; i < 7; i++) {
183  char *ptr = (char *) &binList[j] + binlist_struct_offset[i];
184  memcpy(&bytebuf[j * 19 + binlist_vdata_offset[i]], ptr,
185  binlist_size[i]);
186  }
187  }
188  int records_written =
189  VSwrite(vdata_id_binlist, bytebuf, n_elem, FULL_INTERLACE);
190 
191  if (records_written != n_elem) {
192  cout << "Error in VSwrite (write_binList): "
193  << n_elem << " " << records_written << endl;
194  exit(-1);
195  }
196 
197  free(bytebuf);
198 
199  return records_written;
200 } // end write_binList
201 
202 int write_prodData(int n_elem, int32_t vdata_id_proddata, float *data,
203  binListStruct* binList) {
204  float dta[2];
205  binListStruct binListRec;
206  int records_written;
207 
208  for (int32_t j = 0; j < n_elem; j++) {
209  memcpy((void *) &binListRec, &binList[j], sizeof (binListStruct));
210  // cout << binListRec.weights << endl;
211  dta[0] = data[j] * binListRec.weights;
212 
213  // Can't really compute sum_2 for a single data value per bin.
214  // dta[1] = data[j] * data[j] * binListRec.weights;
215  dta[1] = 0.0;
216  records_written = VSwrite(vdata_id_proddata, (const uint8_t*) dta, 1,
217  FULL_INTERLACE);
218 
219  if (records_written != 1) {
220  cout << "Error in VSwrite (write_prodData): "
221  << 1 << " " << records_written << endl;
222  exit(-1);
223  }
224 
225  }
226  return 0;
227 }
228 
229 int copy_prodData(int n_elem, int32_t *binsToCopy,
230  char const * const fldname3[],
231  int32_t in_vdata_id_proddata,
232  int32_t out_vdata_id_proddata) {
233  float dta[2];
234  char buffer[1000];
235 
236  strcpy(buffer, fldname3[0]);
237  strcat(buffer, ",");
238  strcat(buffer, fldname3[1]);
239  intn status = VSsetfields(in_vdata_id_proddata, buffer);
240  if (status != 0) {
241  printf("Error setting fields: %s\n", buffer);
242  exit(1);
243  }
244 
245  for (int32_t j = 0; j < n_elem; j++) {
246  VSseek(in_vdata_id_proddata, binsToCopy[j]);
247  int records_read = VSread(in_vdata_id_proddata,
248  (uint8_t *) & dta[0], 1, FULL_INTERLACE);
249 
250  int records_written = VSwrite(out_vdata_id_proddata,
251  (const uint8_t*) dta, 1, FULL_INTERLACE);
252 
253  if (records_read != records_written) {
254  cout << "Error in copy: "
255  << records_read << " " << records_written << endl;
256  exit(-1);
257  }
258  }
259  return 0;
260 }
261 
262 int create_compound(hid_t group_id, const char *dataset_name, hid_t *dataset_id,
263  hid_t *type_id, size_t typesize, int32_t n_flds,
264  char const * const fldname[], size_t offset[], hid_t type[],
265  hid_t *filespace, hid_t dataspace) {
266  herr_t status;
267 
268  *type_id = H5Tcreate(H5T_COMPOUND, typesize);
269  for (int32_t i = 0; i < n_flds; i++) {
270  status = H5Tinsert(*type_id, fldname[i], offset[i], type[i]);
271  if (status < 0) {
272  cout << "H5Tinsert error for: " << dataset_name
273  << " (field: " << fldname[i] << ")." << endl;
274  exit(1);
275  }
276  }
277 
278  hid_t cparms = H5Pcreate(H5P_DATASET_CREATE);
279 
280  hsize_t chuck_dims[1] = {1};
281  status = H5Pset_chunk(cparms, 1, chuck_dims);
282  *dataset_id = H5Dcreate1(group_id, dataset_name, *type_id, dataspace,
283  cparms);
284  if (*dataset_id < 0) {
285  cout << "Cannot create dataset for: " << dataset_name << "." << endl;
286  exit(1);
287  }
288 
289  status = H5Pclose(cparms);
290 
291  *filespace = H5Dget_space(*dataset_id);
292  if (*filespace < 0) {
293  cout << "Cannot get filespace for: " << dataset_name << "." << endl;
294  exit(1);
295  }
296 
297  cout << "\"" << dataset_name << "\"" << " successfully created." << endl;
298  cout << "Dataset id: " << *dataset_id << endl;
299  cout << "Datatype id: " << *type_id << endl;
300  cout << "Filespace id: " << *filespace << endl << endl;
301  return 0;
302 }
303 }
Definition: bin_io.cpp:23
int j
Definition: decode_rs.h:73
int status
Definition: l1_czcs_hdf.c:32
#define NULL
Definition: decode_rs.h:63
int create_compound(hid_t group_id, const char *dataset_name, hid_t *dataset_id, hid_t *type_id, size_t typesize, int32_t n_flds, char const *const fldname[], size_t offset[], hid_t type[], hid_t *filespace, hid_t dataspace)
Definition: bin_util.cpp:262
float * lat
int write_prodData(int n_elem, int32_t vdata_id_proddata, float *data, binListStruct *binList)
Definition: bin_util.cpp:202
int write_binList(int n_elem, int32_t vdata_id_binlist, binListStruct *binList)
Definition: bin_util.cpp:163
int create_vdata(int32_t fileid, int32_t vgid, int32_t *vdata_id, const char *vdata_name, const char *class_name, int32_t n_flds, char const *const fldname[], int32_t type[], int32_t noext, int32_t *aid)
Definition: bin_util.cpp:10
virtual void bin2latlon(int64_t bin, double &lat, double &lon)
Definition: L3Shape.cpp:99
int32_t write_vdata(int vdata_id, int32_t n_recs_to_write, void *data)
Definition: bin_util.cpp:105
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
int read_binList(int n_elem, int32_t vdata_id_binlist, binListStruct *binList, l3::L3Shape *shape)
Definition: bin_util.cpp:118
float * lon
l2prod offset
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int copy_prodData(int n_elem, int32_t *binsToCopy, char const *const fldname3[], int32_t in_vdata_id_proddata, int32_t out_vdata_id_proddata)
Definition: bin_util.cpp:229