HAWKI Pipeline Reference Manual  1.8.12
hawki_save.c
1 /* $Id: hawki_save.c,v 1.11 2010/11/29 03:37:15 cgarcia Exp $
2  *
3  * This file is part of the HAWKI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2010/11/29 03:37:15 $
24  * $Revision: 1.11 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <string.h>
37 #include <cpl.h>
38 
39 #include "hawki_distortion.h"
40 #include "hawki_save.h"
41 #include "hawki_load.h"
42 #include "hawki_pfits.h"
43 #include "hawki_utils.h"
44 #include "hawki_dfs.h"
45 
46 /*----------------------------------------------------------------------------*/
51 /*----------------------------------------------------------------------------*/
52 
55 /*----------------------------------------------------------------------------*/
71 /*----------------------------------------------------------------------------*/
73  cpl_frameset * allframes,
74  const cpl_parameterlist * parlist,
75  const cpl_frameset * usedframes,
76  const cpl_imagelist * images,
77  const char * recipe,
78  const char * procat,
79  const char * protype,
80  const cpl_propertylist * applist,
81  const cpl_propertylist ** applists,
82  const char * filename)
83 {
84  const char * fname;
85  cpl_propertylist * pro_list;
86  cpl_type_bpp pixeltype;
87  char sval[16];
88  int chip_nb;
89  int iext;
90 
91  /* Test entries */
92  if (allframes == NULL) return -1 ;
93 
94  /* Get a suitable reference frame */
95  if((fname = hawki_get_extref_file(allframes)) == NULL)
96  {
97  cpl_msg_error(__func__, "Could not find a suitable reference frame");
98  return -1;
99  }
100 
101  /* Add the PRO keys */
102  if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
103  else pro_list = cpl_propertylist_new() ;
104  if (protype != NULL)
105  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
106  if (procat != NULL)
107  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
108 
109  /* File with extensions */
110  if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
111  CPL_BPP_IEEE_FLOAT, recipe, pro_list, NULL,
112  PACKAGE "/" PACKAGE_VERSION,
113  filename) != CPL_ERROR_NONE) {
114  cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
115  filename);
116  cpl_propertylist_delete(pro_list);
117  return -1 ;
118  }
119 
120  /* Delete PRO LIST */
121  cpl_propertylist_delete(pro_list);
122 
123  /* Get the file type */
124  if (cpl_image_get_type(cpl_imagelist_get_const(images, 0)) == CPL_TYPE_INT)
125  pixeltype = CPL_BPP_32_SIGNED ;
126  else
127  pixeltype = CPL_BPP_IEEE_FLOAT ;
128 
129  /* Save the extensions */
130  for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
131  cpl_propertylist * qc_ext_list;
132 
133  /* Get the chip number to store in this extension */
134  if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
135  cpl_msg_error(__func__, "Cannot get the chip for extension %d when "
136  "writing file %s", iext+1, filename);
137  return -1 ;
138  }
139  if ((applists != NULL) && (applists[chip_nb-1] != NULL))
140  qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
141  else
142  qc_ext_list = cpl_propertylist_new() ;
143 
144  snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
145  cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
146  if(cpl_image_save(cpl_imagelist_get_const(images, chip_nb-1), filename,
147  pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
148  {
149  cpl_msg_error(__func__, "Cannot save extension %d of file %s",
150  iext+1, filename);
151  cpl_propertylist_delete(qc_ext_list) ;
152  return -1;
153  }
154  cpl_propertylist_delete(qc_ext_list) ;
155  }
156 
157  return 0 ;
158 }
159 
160 /*----------------------------------------------------------------------------*/
176 /*----------------------------------------------------------------------------*/
178 (cpl_frameset * allframes,
179  const cpl_parameterlist * parlist,
180  const cpl_frameset * usedframes,
181  const char * recipe,
182  const char * procat,
183  const char * protype,
184  const cpl_propertylist * applist,
185  const char * filename)
186 {
187  cpl_propertylist * pro_list;
188 
189  /* Test entries */
190  if (allframes == NULL) return -1 ;
191 
192  /* Add the PRO keys */
193  if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
194  else pro_list = cpl_propertylist_new() ;
195  if (protype != NULL)
196  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
197  if (procat != NULL)
198  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
199 
200  /* File with extensions */
201  if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
202  CPL_BPP_IEEE_FLOAT, recipe, pro_list, NULL,
203  PACKAGE "/" PACKAGE_VERSION,
204  filename) != CPL_ERROR_NONE) {
205  cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
206  filename);
207  cpl_propertylist_delete(pro_list);
208  return -1 ;
209  }
210 
211  /* Delete PRO LIST */
212  cpl_propertylist_delete(pro_list);
213 
214  return 0 ;
215 }
216 
217 /*----------------------------------------------------------------------------*/
225 /*----------------------------------------------------------------------------*/
227 (const cpl_frameset * allframes,
228  const cpl_image * image,
229  int iext,
230  const cpl_propertylist * ext_prop_list,
231  const char * filename)
232 {
233  const char * fname;
234  cpl_type_bpp pixeltype;
235  cpl_propertylist * ext_prop;
236  char sval[16];
237  int idet;
238 
239  /* Test entries */
240  if (allframes == NULL) return -1 ;
241 
242  /* Get a suitable reference frame */
243  if((fname = hawki_get_extref_file(allframes)) == NULL)
244  {
245  cpl_msg_error(__func__, "Could not find a suitable reference frame");
246  return -1;
247  }
248 
249  /* Get the file type */
250  if (cpl_image_get_type(image) == CPL_TYPE_INT)
251  pixeltype = CPL_BPP_32_SIGNED ;
252  else
253  pixeltype = CPL_BPP_IEEE_FLOAT ;
254 
255  /* Save the extension */
256  /* Get the chip number to store in this extension */
257  if ((idet = hawki_get_detector_from_ext(fname, iext)) == -1) {
258  cpl_msg_error(__func__, "Cannot get the chip id for extension %d "
259  "when saving %s", iext, filename);
260  return -1;
261  }
262  if (ext_prop_list != NULL)
263  ext_prop = cpl_propertylist_duplicate(ext_prop_list) ;
264  else
265  ext_prop = cpl_propertylist_new() ;
266 
267  snprintf(sval, 16, "CHIP%d.INT1", idet) ;
268  cpl_propertylist_prepend_string(ext_prop, "EXTNAME", sval) ;
269  if(cpl_image_save(image, filename,
270  pixeltype, ext_prop, CPL_IO_EXTEND) != CPL_ERROR_NONE)
271  {
272  cpl_msg_error(__func__,"Could not save extension %d of file %s",
273  iext, filename);
274  cpl_propertylist_delete(ext_prop);
275  return -1;
276  }
277  cpl_propertylist_delete(ext_prop) ;
278 
279  return 0 ;
280 }
281 
282 /*----------------------------------------------------------------------------*/
301 /*----------------------------------------------------------------------------*/
303  cpl_frameset * allframes,
304  const cpl_parameterlist * parlist,
305  const cpl_frameset * usedframes,
306  const cpl_image ** images,
307  const char * recipe,
308  const char * procat,
309  const char * protype,
310  const cpl_propertylist * applist,
311  const cpl_propertylist ** applists,
312  const char * filename)
313 {
314  const char * fname ;
315  cpl_propertylist * pro_list ;
316  cpl_type_bpp pixeltype ;
317  cpl_propertylist * qc_ext_list ;
318  char sval[16] ;
319  int chip_nb ;
320  int iext;
321 
322  /* Test entries */
323  if (allframes == NULL) return -1 ;
324 
325  /* Get a suitable reference frame */
326  if((fname = hawki_get_extref_file(allframes)) == NULL)
327  {
328  cpl_msg_error(__func__, "Could not find a suitable reference frame");
329  return -1;
330  }
331 
332  /* Add the PRO keys */
333  if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
334  else pro_list = cpl_propertylist_new() ;
335  if (protype != NULL)
336  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
337  if (procat != NULL)
338  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
339 
340  /* File with extensions */
341  if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
342  CPL_BPP_IEEE_FLOAT, recipe, pro_list, NULL,
343  PACKAGE "/" PACKAGE_VERSION,
344  filename) != CPL_ERROR_NONE) {
345  cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
346  filename);
347  cpl_propertylist_delete(pro_list) ;
348  return -1 ;
349  }
350 
351  /* Delete PRO LIST */
352  cpl_propertylist_delete(pro_list) ;
353 
354  /* Get the file type */
355  if (cpl_image_get_type(images[0]) == CPL_TYPE_INT)
356  pixeltype = CPL_BPP_32_SIGNED ;
357  else
358  pixeltype = CPL_BPP_IEEE_FLOAT ;
359 
360  /* Save the extensions */
361  for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
362  /* Get the chip number to store in this extension */
363  if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
364  cpl_msg_error(__func__, "Cannot get the chip for extension %d "
365  "when saving file %s", iext+1, filename);
366  return -1 ;
367  }
368  /* Get the QC list */
369  if ((applists != NULL) && (applists[chip_nb-1] != NULL))
370  qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
371  else
372  qc_ext_list = cpl_propertylist_new() ;
373 
374  snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
375  cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
376  if(cpl_image_save(images[chip_nb-1], filename,
377  pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
378  {
379  cpl_msg_error(__func__,"Cannot save extension %d of file %s",
380  iext+1, filename);
381  cpl_propertylist_delete(qc_ext_list);
382  return -1;
383  }
384  cpl_propertylist_delete(qc_ext_list) ;
385  }
386  return 0 ;
387 }
388 
389 /*----------------------------------------------------------------------------*/
405 /*----------------------------------------------------------------------------*/
407  cpl_frameset * allframes,
408  const cpl_parameterlist * parlist,
409  const cpl_frameset * usedframes,
410  const cpl_table ** tables,
411  const char * recipe,
412  const char * procat,
413  const char * protype,
414  const cpl_propertylist * applist,
415  const cpl_propertylist ** applists,
416  const char * filename)
417 {
418  cpl_propertylist * qc_ext_list ;
419  cpl_propertylist * pro_list ;
420  const char * fname ;
421  char sval[16] ;
422  int chip_nb ;
423  int iext;
424 
425  /* Test entries */
426  if (allframes == NULL) return -1 ;
427 
428  /* Get a suitable reference frame */
429  if((fname = hawki_get_extref_file(allframes)) == NULL)
430  {
431  cpl_msg_error(__func__, "Could not find a suitable reference frame");
432  return -1;
433  }
434 
435  /* Add the PRO keys */
436  if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
437  else pro_list = cpl_propertylist_new() ;
438  if (protype != NULL)
439  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
440  if (procat != NULL)
441  cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
442 
443  /* File with extensions */
444  if ((applists != NULL) && (applists[0] != NULL))
445  qc_ext_list = cpl_propertylist_duplicate(applists[0]) ;
446  else
447  qc_ext_list = cpl_propertylist_new() ;
448  cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", "CHIP1.INT1") ;
449  if (cpl_dfs_save_table(allframes, NULL, parlist, usedframes, NULL, tables[0],
450  qc_ext_list, recipe, pro_list, NULL,
451  PACKAGE "/" PACKAGE_VERSION,
452  filename) != CPL_ERROR_NONE) {
453  cpl_msg_error(__func__,
454  "Cannot save the first extension table of file %s", filename);
455  cpl_propertylist_delete(qc_ext_list) ;
456  cpl_propertylist_delete(pro_list) ;
457  return -1 ;
458  }
459  cpl_propertylist_delete(qc_ext_list) ;
460  cpl_propertylist_delete(pro_list) ;
461 
462  /* Save the extensions */
463  for (iext=1 ; iext<HAWKI_NB_DETECTORS; iext++) {
464  /* Get the chip number to store in this extension */
465  if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
466  cpl_msg_error(__func__, "Cannot get the chip for extension %d "
467  "when writing file %s", iext+1, filename);
468  return -1 ;
469  }
470  if ((applists != NULL) && (applists[chip_nb-1] != NULL))
471  qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
472  else
473  qc_ext_list = cpl_propertylist_new() ;
474 
475  snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
476  cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
477  if(cpl_table_save(tables[chip_nb-1], NULL, qc_ext_list, filename,
478  CPL_IO_EXTEND) != CPL_ERROR_NONE)
479  {
480  cpl_msg_error(__func__, "Cannot save extension %d of table %s",
481  iext+1, filename);
482  cpl_propertylist_delete(qc_ext_list) ;
483  return -1;
484  }
485 
486  cpl_propertylist_delete(qc_ext_list) ;
487  }
488 
489  return 0 ;
490 }
491 
492 /*----------------------------------------------------------------------------*/
512 /*----------------------------------------------------------------------------*/
514 (cpl_frameset * allframes,
515  const cpl_parameterlist * parlist,
516  const cpl_frameset * usedframes,
517  const hawki_distortion ** distortion,
518  const char * recipe,
519  const cpl_propertylist * applist,
520  const cpl_propertylist ** applists,
521  const char * filename_x,
522  const char * filename_y)
523 {
524  const char * fname ;
525  cpl_propertylist * pro_list_x;
526  cpl_propertylist * pro_list_y;
527  cpl_type_bpp pixeltype ;
528  cpl_propertylist * qc_ext_list ;
529  char sval[16] ;
530  int chip_nb ;
531  int iext;
532 
533  /* Test entries */
534  if (allframes == NULL) return -1 ;
535 
536  /* Get a suitable reference frame */
537  if((fname = hawki_get_extref_file(allframes)) == NULL)
538  {
539  cpl_msg_error(__func__, "Could not find a suitable reference frame");
540  return -1;
541  }
542 
543  /* Add the PRO keys */
544  if(applist != NULL)
545  pro_list_x = cpl_propertylist_duplicate(applist);
546  else
547  pro_list_x = cpl_propertylist_new();
548  if(applist != NULL)
549  pro_list_y = cpl_propertylist_duplicate(applist);
550  else
551  pro_list_y = cpl_propertylist_new();
552 
553  cpl_propertylist_append_string(pro_list_x, CPL_DFS_PRO_TYPE,
554  HAWKI_PROTYPE_DISTORTION_X);
555  cpl_propertylist_append_string(pro_list_y, CPL_DFS_PRO_TYPE,
556  HAWKI_PROTYPE_DISTORTION_Y);
557  cpl_propertylist_append_string(pro_list_x, CPL_DFS_PRO_CATG,
558  HAWKI_CALPRO_DISTORTION_X);
559  cpl_propertylist_append_string(pro_list_y, CPL_DFS_PRO_CATG,
560  HAWKI_CALPRO_DISTORTION_Y);
561 
562  /* File with extensions */
563  if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
564  CPL_BPP_IEEE_FLOAT, recipe, pro_list_x, NULL,
565  PACKAGE "/" PACKAGE_VERSION,
566  filename_x) != CPL_ERROR_NONE) {
567  cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
568  filename_x);
569  cpl_propertylist_delete(pro_list_x);
570  cpl_propertylist_delete(pro_list_y);
571  return -1 ;
572  }
573  if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
574  CPL_BPP_IEEE_FLOAT, recipe, pro_list_y, NULL,
575  PACKAGE "/" PACKAGE_VERSION,
576  filename_y) != CPL_ERROR_NONE) {
577  cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
578  filename_y);
579  cpl_propertylist_delete(pro_list_x);
580  cpl_propertylist_delete(pro_list_y);
581  return -1 ;
582  }
583 
584  /* Delete PRO LIST */
585  cpl_propertylist_delete(pro_list_x);
586  cpl_propertylist_delete(pro_list_y);
587 
588  /* Get the file type */
589  if (cpl_image_get_type(distortion[0]->dist_x) == CPL_TYPE_INT)
590  pixeltype = CPL_BPP_32_SIGNED ;
591  else
592  pixeltype = CPL_BPP_IEEE_FLOAT ;
593 
594  /* Save the extensions */
595  for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
596  /* Get the chip number to store in this extension */
597  if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
598  cpl_msg_error(__func__, "Cannot get the chip for extension %d "
599  "when saving %s and %s", iext+1, filename_x, filename_y);
600  return -1 ;
601  }
602  /* Get the QC list */
603  if ((applists != NULL) && (applists[chip_nb-1] != NULL))
604  qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
605  else
606  qc_ext_list = cpl_propertylist_new() ;
607 
608  /* Add the CR* keywords */
609  cpl_propertylist_prepend_double(qc_ext_list, "CRPIX2", 1);
610  cpl_propertylist_prepend_double(qc_ext_list, "CDELT2",
611  distortion[chip_nb-1]->y_cdelt);
612  cpl_propertylist_prepend_double(qc_ext_list, "CRVAL2",
613  distortion[chip_nb-1]->y_crval);
614  cpl_propertylist_prepend_double(qc_ext_list, "CRPIX1", 1);
615  cpl_propertylist_prepend_double(qc_ext_list, "CDELT1",
616  distortion[chip_nb-1]->x_cdelt);
617  cpl_propertylist_prepend_double(qc_ext_list, "CRVAL1",
618  distortion[chip_nb-1]->x_crval);
619 
620  snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
621  cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
622  if(cpl_image_save(distortion[chip_nb-1]->dist_x, filename_x,
623  pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
624  {
625  cpl_msg_error(__func__,"Cannot save extension %d of file %s",
626  iext+1, filename_x);
627  cpl_propertylist_delete(qc_ext_list);
628  return -1;
629  }
630  if(cpl_image_save(distortion[chip_nb-1]->dist_y, filename_y,
631  pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
632  {
633  cpl_msg_error(__func__,"Cannot save extension %d of file %s",
634  iext+1, filename_y);
635  cpl_propertylist_delete(qc_ext_list);
636  return -1;
637  }
638  cpl_propertylist_delete(qc_ext_list) ;
639  }
640 
641  return 0 ;
642 }
643 
644 /*----------------------------------------------------------------------------*/
654 /*----------------------------------------------------------------------------*/
655 
656 const char * hawki_get_extref_file(const cpl_frameset * frameset)
657 {
658  int iframe;
659  const char * fname = NULL;
660 
661  for(iframe = 0 ; iframe < cpl_frameset_get_size(frameset); ++iframe)
662  {
663  const cpl_frame * frame;
664 
665  frame = cpl_frameset_get_frame_const(frameset, iframe);
666  /* Check that it belongs to the raw frames
667  * and then check if there are 4 extensions
668  */
669  if(cpl_frame_get_group(frame) == CPL_FRAME_GROUP_RAW &&
670  cpl_frame_get_nextensions(frame) == HAWKI_NB_DETECTORS)
671  {
672  fname = cpl_frame_get_filename(frame);
673  return fname;
674  }
675  }
676  //Now look into the products, in case that there is no actual raw
677  //reference frame. This is kind of a hack for hawki_util_gendist,
678  //which allows to save the distortion images using the distortion
679  //table as a reference
680  for(iframe = 0 ; iframe < cpl_frameset_get_size(frameset); ++iframe)
681  {
682  const cpl_frame * frame;
683 
684  frame = cpl_frameset_get_frame_const(frameset, iframe);
685  /* Check that it belongs to the raw frames
686  * and then check if there are 4 extensions
687  */
688  if(cpl_frame_get_group(frame) == CPL_FRAME_GROUP_PRODUCT &&
689  cpl_frame_get_nextensions(frame) == HAWKI_NB_DETECTORS)
690  {
691  fname = cpl_frame_get_filename(frame);
692  return fname;
693  }
694  }
695  //And if it didn't suceed, use the first frame:
696  fname = cpl_frame_get_filename(cpl_frameset_get_first_const(frameset));
697 
698  return fname;
699 }
700 
701