MIDI Pipeline Reference Manual  2.8.3
midiAppendPropertylist.c
1 /******************************************************************************
2 *******************************************************************************
3 * European Southern Observatory
4 * VLTI MIDI Data Reduction Software
5 *
6 * Module name: midiAppendPropertylist.c
7 * Description: Contains a routine to add a property list
8 *
9 * History:
10 * 04-June-07 (agabasch) Created
11 *******************************************************************************
12 ******************************************************************************/
13 
14 /******************************************************************************
15 * Include files
16 ******************************************************************************/
17 #include <stdio.h>
18 #include <cpl.h>
19 #include "midiAppendPropertylist.h"
20 #include <config.h>
21 #include <string.h>
22 
23 cpl_error_code appendPropertylist(const char *ProductFile,
24  cpl_frame_type type,
25  const char *tag,
26  cpl_frameset *frameset,
27  cpl_parameterlist *parlist)
28 {
29  cpl_image * pImage = NULL;
30  cpl_frame * pFrameProduct = NULL;
31  cpl_propertylist * pHeader = NULL;
32 
33 
34  const char *szProduct=ProductFile;
35  const char *szRawFile=ProductFile;
36 
37 
38  pImage = cpl_image_load(szRawFile, CPL_TYPE_FLOAT, 0, 0 );
39 
40 
41 /* pHeader = cpl_propertylist_new(); */
42  pHeader = cpl_propertylist_load(szRawFile, 0 );
43 
44 
45  pFrameProduct = cpl_frame_new();
46  cpl_frame_set_filename( pFrameProduct, szProduct );
47  cpl_frame_set_type ( pFrameProduct, type );
48  cpl_frame_set_tag ( pFrameProduct, tag );
49  cpl_frame_set_group ( pFrameProduct, CPL_FRAME_GROUP_PRODUCT );
50  cpl_frame_set_level ( pFrameProduct, CPL_FRAME_LEVEL_FINAL );
51 
52 
53 // cpl_frame_set_group(cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
54 
55 
56 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
57  if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID, NULL))
58  {
59  cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
60  }
61 #else
62  if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID))
63  {
64  cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
65  }
66 #endif
67 
68 
69  /* if (CPL_ERROR_NONE != irplib_image_save(frameset, */
70 /* parlist, */
71 /* frameset, */
72 /* pImage, */
73 /* CPL_BPP_IEEE_FLOAT, */
74 /* "Test", */
75 /* tag, */
76 /* pHeader, */
77 /* NULL, */
78 /* "Test", */
79 /* szProduct)) */
80 /* { */
81 /* cpl_msg_error(cpl_func,"Error in irplib_image_save: %s %s",tag,cpl_error_get_message()); */
82 /* } */
83 
84 
85 
86 
87  if (CPL_ERROR_NONE != cpl_image_save(pImage, szProduct, CPL_BPP_IEEE_FLOAT, pHeader, CPL_IO_DEFAULT ))
88  {
89  cpl_msg_error(cpl_func,"Error in cpl_image_save");
90  }
91 
92  if (CPL_ERROR_NONE != cpl_frameset_insert( frameset, pFrameProduct ))
93  {
94  cpl_msg_error(cpl_func,"Error in cpl_frameset_insert");
95  }
96 
97  cpl_image_delete( pImage );
98  cpl_propertylist_delete(pHeader );
99 
100  return 0;
101 }
102 
103 cpl_error_code appendPropertylist_table(const char *ProductFile,
104  cpl_frame_type type,
105  const char *tag,
106  cpl_frameset *frameset,
107  cpl_parameterlist *parlist)
108 {
109 
110  int extnum,i;
111 
112 /* cpl_table * pTable = NULL; */
113  cpl_table ** pTable_ext = NULL;
114 
115  cpl_propertylist * pHeader = NULL;
116  cpl_propertylist ** pHeader_ext = NULL;
117 
118  cpl_frame * pFrameProduct = NULL;
119  cpl_frame * pFrame_tmp = NULL;
120 
121 
122  const char *szProduct=ProductFile;
123  const char *szRawFile=ProductFile;
124  char * current_extension;
125 
126  pFrameProduct = cpl_frame_new();
127  cpl_frame_set_filename( pFrameProduct, szProduct );
128  cpl_frame_set_type ( pFrameProduct, type );
129  cpl_frame_set_tag ( pFrameProduct, tag );
130  cpl_frame_set_group ( pFrameProduct, CPL_FRAME_GROUP_PRODUCT );
131  cpl_frame_set_level ( pFrameProduct, CPL_FRAME_LEVEL_FINAL );
132 
133 // cpl_frame_set_group(cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
134 
135 
136 /* read the number of extensions from the rawfile */
137  pFrame_tmp = cpl_frame_new();
138  cpl_frame_set_filename( pFrame_tmp, szRawFile );
139  extnum=cpl_frame_get_nextensions(pFrame_tmp);
140  cpl_frame_delete(pFrame_tmp);
141 
142  pHeader = cpl_propertylist_load(szRawFile, 0 );
143 
144 
145  if (pHeader ==NULL)
146  {
147  cpl_msg_error(cpl_func,"Error loading the propertylist of %s",szRawFile);
148  return -1;
149  }
150 
151  pTable_ext = cpl_malloc((extnum) * sizeof(cpl_table *));
152  pHeader_ext = cpl_malloc((extnum) * sizeof(cpl_propertylist *));
153 
154 
155 
156  for (i=0; i<extnum; i++)
157  {
158  pHeader_ext[i] = cpl_propertylist_load(szRawFile, i+1 );
159  pTable_ext[i] = cpl_table_load(szRawFile, i+1, 1 );
160 
161  if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
162  {
163  cpl_msg_error(cpl_func,"Error loading the extended header or table of %s",szRawFile);
164 
165  for (i=0; i<extnum; i++)
166  {
167  cpl_propertylist_delete(pHeader_ext[i]);
168  cpl_table_delete(pTable_ext[i]);
169  }
170  cpl_free (pHeader_ext);
171  cpl_free (pTable_ext);
172 
173 
174  return -1;
175  }
176 
177 /* ------------------------------------------------------------------- */
178 /* OI_FITS standard requires to saves the FLAG column as boolean */
179  current_extension=(char *)cpl_propertylist_get_string(pHeader_ext[i],"EXTNAME");
180 
181  if (strcmp(current_extension,"OI_VIS")==0 || strcmp(current_extension,"OI_VIS2")==0){
182 
183  if (cpl_table_has_column(pTable_ext[i],"FLAG")){
184  cpl_table_set_column_savetype(pTable_ext[i], "FLAG", CPL_TYPE_BOOL);
185  }
186  }
187 
188 /* ------------------------------------------------------------------- */
189 
190  }
191 
192 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
193  if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID, NULL))
194  {
195  cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
196  }
197 #else
198  if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID))
199  {
200  cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
201  }
202 #endif
203 
204 
205  if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0],pHeader, pHeader_ext[0], szProduct, CPL_IO_DEFAULT ))
206  {
207  cpl_msg_error(cpl_func,"Error when saving primary header: %s %s",cpl_error_get_message(),cpl_error_get_where());
208  }
209 
210  for (i=1; i<extnum; i++)
211  {
212  if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[i], NULL, pHeader_ext[i], szProduct, CPL_IO_EXTEND ))
213  {
214  cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
215  }
216 
217  }
218 
219 
220 
221  if (CPL_ERROR_NONE != cpl_frameset_insert( frameset, pFrameProduct ))
222  {
223  cpl_msg_error(cpl_func,"Error in cpl_frameset_insert");
224  }
225 
226 
227  cpl_propertylist_delete(pHeader);
228 
229  for (i=0; i<extnum; i++)
230  {
231  cpl_propertylist_delete(pHeader_ext[i]);
232  cpl_table_delete (pTable_ext[i]);
233  }
234  cpl_free (pTable_ext);
235  cpl_free (pHeader_ext);
236 
237 
238  return 0;
239 
240 }
241 
242 
243 cpl_error_code appendPropertylist_statistics(const char *InputFile,
244  const char *ProductFile,
245  cpl_frame_type type,
246  const char *tag,
247  cpl_frameset *frameset,
248  cpl_parameterlist *parlist,
249  struct plots *plotfiles_lambda,
250  struct plots *plotfiles_frames,
251  struct plots *plotfiles_scans,
252  int dimen_lambda,
253  int dimen_frames,
254  int dimen_scans)
255 {
256 
257  int extnum,i;
258 
259 /* cpl_table * pTable = NULL; */
260  cpl_table ** pTable_ext = NULL;
261 
262  cpl_propertylist * pHeader = NULL;
263  cpl_propertylist ** pHeader_ext = NULL;
264  cpl_propertylist ** pHeader_ext_append = NULL;
265 
266  cpl_frame * pFrameProduct = NULL;
267  cpl_frame * pFrame_tmp = NULL;
268 
269 
270  const char *szProduct=ProductFile;
271  const char *szRawFile=InputFile;
272  char * current_extension;
273 
274 
275 
276 /* cpl_msg_info(cpl_func,"Entries: %s ",plotfiles_lambda[0].filename); */
277 /* cpl_msg_info(cpl_func,"dimen_lambda: %d ",dimen_lambda); */
278 
279 
280  cpl_vector *vec_lambda;
281  cpl_vector *vec_frames;
282  cpl_vector *vec_scans;
283 
284 /* cpl_vector *vec; */
285 
286  cpl_table *table_lambda;
287  cpl_table *table_frames;
288  cpl_table *table_scans;
289 
290 
291 /* Determine the size of the Table */
292 
293  vec_lambda= vector_read(plotfiles_lambda[0].filename) ;
294  table_lambda=cpl_table_new(cpl_vector_get_size(vec_lambda));
295  cpl_vector_delete(vec_lambda);
296 
297  vec_frames= vector_read(plotfiles_frames[0].filename) ;
298  table_frames=cpl_table_new(cpl_vector_get_size(vec_frames));
299  cpl_vector_delete(vec_frames);
300 
301  vec_scans= vector_read(plotfiles_scans[0].filename) ;
302  table_scans=cpl_table_new(cpl_vector_get_size(vec_scans));
303  cpl_vector_delete(vec_scans);
304 
305 
306 /* Fill the Table */
307  for(i=0; i<dimen_lambda; i++)
308  {
309  vec_lambda= vector_read(plotfiles_lambda[i].filename) ;
310  cpl_table_wrap_double(table_lambda, cpl_vector_get_data(vec_lambda), plotfiles_lambda[i].label) ;
311  cpl_vector_unwrap(vec_lambda);
312  }
313 
314  for(i=0; i<dimen_frames; i++)
315  {
316  vec_frames= vector_read(plotfiles_frames[i].filename) ;
317  cpl_table_wrap_double(table_frames, cpl_vector_get_data(vec_frames), plotfiles_frames[i].label) ;
318  cpl_vector_unwrap(vec_frames);
319  }
320 
321  for(i=0; i<dimen_scans; i++)
322  {
323  vec_scans= vector_read(plotfiles_scans[i].filename) ;
324  cpl_table_wrap_double(table_scans, cpl_vector_get_data(vec_scans), plotfiles_scans[i].label) ;
325  cpl_vector_unwrap(vec_scans);
326  }
327 
328 
329 
330 /* read the number of extensions from the rawfile */
331  pFrame_tmp = cpl_frame_new();
332  cpl_frame_set_filename( pFrame_tmp, szRawFile );
333  extnum=cpl_frame_get_nextensions(pFrame_tmp);
334  cpl_frame_delete(pFrame_tmp);
335 
336 
337 
338  pFrameProduct = cpl_frame_new();
339  cpl_frame_set_filename( pFrameProduct, szProduct );
340  cpl_frame_set_type ( pFrameProduct, type );
341  cpl_frame_set_tag ( pFrameProduct, tag );
342  cpl_frame_set_group ( pFrameProduct, CPL_FRAME_GROUP_PRODUCT );
343  cpl_frame_set_level ( pFrameProduct, CPL_FRAME_LEVEL_FINAL );
344 
345 // cpl_frame_set_group(cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
346 
347 
348 /* cpl_msg_error(cpl_func,"extnum %d",extnum); */
349 
350 
351  pHeader = cpl_propertylist_load(szRawFile, 0 );
352  if (pHeader ==NULL)
353  {
354  cpl_msg_error(cpl_func,"Error loading the propertylist of %s",szRawFile);
355  return -1;
356  }
357 
358  pTable_ext = cpl_malloc((extnum) * sizeof(cpl_table *));
359  pHeader_ext = cpl_malloc((extnum) * sizeof(cpl_propertylist *));
360 
361 
362 
363  for (i=0; i<extnum; i++)
364  {
365  pHeader_ext[i] = cpl_propertylist_load(szRawFile, i+1 );
366  pTable_ext[i] = cpl_table_load(szRawFile, i+1, 1 );
367 
368  if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
369  {
370  cpl_msg_error(cpl_func,"Error loading the extended header or table of %s",szRawFile);
371 
372  for (i=0; i<extnum; i++)
373  {
374  cpl_propertylist_delete(pHeader_ext[i]);
375  cpl_table_delete(pTable_ext[i]);
376  }
377  cpl_free (pHeader_ext);
378  cpl_free (pTable_ext);
379 
380 
381 
382  return -1;
383  }
384 
385 /* ------------------------------------------------------------------- */
386 /* OI_FITS standard requires to saves the FLAG column as boolean */
387 
388  current_extension=(char *)cpl_propertylist_get_string(pHeader_ext[i],"EXTNAME");
389 
390  if (strcmp(current_extension,"OI_VIS")==0 || strcmp(current_extension,"OI_VIS2")==0){
391 
392  if (cpl_table_has_column(pTable_ext[i],"FLAG")){
393  cpl_table_set_column_savetype(pTable_ext[i], "FLAG", CPL_TYPE_BOOL);
394  }
395  }
396 
397 /* ------------------------------------------------------------------- */
398 
399  }
400 
401 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
402  if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID, NULL))
403  {
404  cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
405  }
406 #else
407  if (CPL_ERROR_NONE != cpl_dfs_setup_product_header(pHeader, pFrameProduct, frameset, parlist, "dummy-recid", PACKAGE "/" PACKAGE_VERSION, PRODUCT_DID))
408  {
409  cpl_msg_error(cpl_func,"Error in cpl_dfs_setup_product_header: %s %s",cpl_error_get_message(),cpl_error_get_where());
410  }
411 #endif
412 
413 
414  if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0],pHeader, pHeader_ext[0], szProduct, CPL_IO_DEFAULT ))
415  {
416  cpl_msg_error(cpl_func,"Error when saving primary header: %s %s",cpl_error_get_message(),cpl_error_get_where());
417  }
418 
419 
420  for (i=1; i<extnum; i++)
421  {
422  if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[i], NULL, pHeader_ext[i], szProduct, CPL_IO_EXTEND ))
423  {
424  cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
425  }
426 
427  }
428 
429 
430 
431 /* Create and append 3 new extensions */
432 
433  pHeader_ext_append = cpl_malloc(3 * sizeof(cpl_propertylist *));
434 
435  pHeader_ext_append[0]=cpl_propertylist_new();
436  pHeader_ext_append[1]=cpl_propertylist_new();
437  pHeader_ext_append[2]=cpl_propertylist_new();
438 
439 
440  cpl_propertylist_append_string (pHeader_ext_append[0],"EXTNAME","STATISTICS1");
441  cpl_propertylist_append_string (pHeader_ext_append[1],"EXTNAME","STATISTICS2");
442  cpl_propertylist_append_string (pHeader_ext_append[2],"EXTNAME","STATISTICS3");
443 
444  if (CPL_ERROR_NONE != cpl_table_save(table_lambda, NULL, pHeader_ext_append[0], szProduct, CPL_IO_EXTEND ))
445  {
446  cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
447  }
448 
449 
450  if (CPL_ERROR_NONE != cpl_table_save(table_frames, NULL, pHeader_ext_append[1], szProduct, CPL_IO_EXTEND ))
451  {
452  cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
453  }
454 
455  if (CPL_ERROR_NONE != cpl_table_save(table_scans, NULL, pHeader_ext_append[2], szProduct, CPL_IO_EXTEND ))
456  {
457  cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where());
458  }
459 
460 
461 
462  if (CPL_ERROR_NONE != cpl_frameset_insert( frameset, pFrameProduct ))
463  {
464  cpl_msg_error(cpl_func,"Error in cpl_frameset_insert");
465  }
466 
467 
468  /* Free the memory */
469 
470  cpl_table_delete(table_lambda);
471  cpl_table_delete(table_frames);
472  cpl_table_delete(table_scans);
473 
474 
475 
476 
477 
478 
479  cpl_propertylist_delete(pHeader);
480 
481  for (i=0; i<extnum; i++)
482  {
483  cpl_propertylist_delete(pHeader_ext[i]);
484  cpl_table_delete(pTable_ext[i]);
485  }
486  cpl_free (pHeader_ext);
487  cpl_free (pTable_ext);
488 
489 
490  cpl_propertylist_delete (pHeader_ext_append[0]);
491  cpl_propertylist_delete (pHeader_ext_append[1]);
492  cpl_propertylist_delete (pHeader_ext_append[2]);
493  cpl_free(pHeader_ext_append);
494 
495 
496 
497  return 0;
498 
499 }
500 
501 
502 /* cpl_vector_read modified to work wit one column: "%*d %lg" -> "%lg" */
503 
504 /*----------------------------------------------------------------------------*/
524 /*----------------------------------------------------------------------------*/
525 cpl_vector * vector_read(const char * filename)
526 {
527  FILE * in;
528  cpl_vector * v;
529  int np = 0;
530  int size = 1000; /* Default size */
531  char line[1024];
532  double x;
533 
534  cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
535 
536  /* Open the file */
537  in = fopen(filename, "r");
538  cpl_ensure(in != NULL, CPL_ERROR_FILE_IO, NULL);
539 
540  /* Create and fill the vector */
541  v = cpl_vector_new(size);
542 
543  while (fgets(line, 1024, in) != NULL) {
544  if (line[0] != '#' && sscanf(line, "%lg", &x) == 1) {
545  /* Found new element
546  - increase vector size if necessary,
547  - insert element at end and
548  - increment size counter */
549  if (np == size) cpl_vector_set_size(v, size *= 2);
550  cpl_vector_set(v, np++, x);
551  }
552  }
553 
554  /* Check that the loop ended due to eof and not an error */
555  if (ferror(in)) {
556  fclose(in);
557  cpl_vector_delete(v);
558  cpl_ensure(0, CPL_ERROR_FILE_IO, NULL);
559  }
560 
561  fclose(in);
562 
563  /* Check that the file was not empty and set the size to its true value */
564  if (np == 0 || cpl_vector_set_size(v, np)) {
565  cpl_vector_delete(v);
566  cpl_ensure(0, CPL_ERROR_BAD_FILE_FORMAT, NULL);
567  }
568 
569  return v;
570 }