00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <string.h>
00037 #include <cpl.h>
00038
00039 #include "hawki_distortion.h"
00040 #include "hawki_save.h"
00041 #include "hawki_load.h"
00042 #include "hawki_pfits.h"
00043 #include "hawki_utils.h"
00044 #include "hawki_dfs.h"
00045
00046
00051
00052
00055
00071
00072 int hawki_imagelist_save(
00073 cpl_frameset * allframes,
00074 const cpl_parameterlist * parlist,
00075 const cpl_frameset * usedframes,
00076 const cpl_imagelist * images,
00077 const char * recipe,
00078 const char * procat,
00079 const char * protype,
00080 const cpl_propertylist * applist,
00081 const cpl_propertylist ** applists,
00082 const char * filename)
00083 {
00084 const char * fname;
00085 cpl_propertylist * pro_list;
00086 cpl_type_bpp pixeltype;
00087 char sval[16];
00088 int chip_nb;
00089 int iext;
00090
00091
00092 if (allframes == NULL) return -1 ;
00093
00094
00095 if((fname = hawki_get_extref_file(allframes)) == NULL)
00096 {
00097 cpl_msg_error(__func__, "Could not find a suitable reference frame");
00098 return -1;
00099 }
00100
00101
00102 if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
00103 else pro_list = cpl_propertylist_new() ;
00104 if (protype != NULL)
00105 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
00106 if (procat != NULL)
00107 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
00108
00109
00110 if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
00111 CPL_BPP_IEEE_FLOAT, recipe, pro_list, NULL,
00112 PACKAGE "/" PACKAGE_VERSION,
00113 filename) != CPL_ERROR_NONE) {
00114 cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
00115 filename);
00116 cpl_propertylist_delete(pro_list);
00117 return -1 ;
00118 }
00119
00120
00121 cpl_propertylist_delete(pro_list);
00122
00123
00124 if (cpl_image_get_type(cpl_imagelist_get_const(images, 0)) == CPL_TYPE_INT)
00125 pixeltype = CPL_BPP_32_SIGNED ;
00126 else
00127 pixeltype = CPL_BPP_IEEE_FLOAT ;
00128
00129
00130 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
00131 cpl_propertylist * qc_ext_list;
00132
00133
00134 if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
00135 cpl_msg_error(__func__, "Cannot get the chip for extension %d when "
00136 "writing file %s", iext+1, filename);
00137 return -1 ;
00138 }
00139 if ((applists != NULL) && (applists[chip_nb-1] != NULL))
00140 qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
00141 else
00142 qc_ext_list = cpl_propertylist_new() ;
00143
00144 snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
00145 cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
00146 if(cpl_image_save(cpl_imagelist_get_const(images, chip_nb-1), filename,
00147 pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
00148 {
00149 cpl_msg_error(__func__, "Cannot save extension %d of file %s",
00150 iext+1, filename);
00151 cpl_propertylist_delete(qc_ext_list) ;
00152 return -1;
00153 }
00154 cpl_propertylist_delete(qc_ext_list) ;
00155 }
00156
00157 return 0 ;
00158 }
00159
00160
00176
00177 int hawki_main_header_save
00178 (cpl_frameset * allframes,
00179 const cpl_parameterlist * parlist,
00180 const cpl_frameset * usedframes,
00181 const char * recipe,
00182 const char * procat,
00183 const char * protype,
00184 const cpl_propertylist * applist,
00185 const char * filename)
00186 {
00187 cpl_propertylist * pro_list;
00188
00189
00190 if (allframes == NULL) return -1 ;
00191
00192
00193 if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
00194 else pro_list = cpl_propertylist_new() ;
00195 if (protype != NULL)
00196 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
00197 if (procat != NULL)
00198 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
00199
00200
00201 if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
00202 CPL_BPP_IEEE_FLOAT, recipe, pro_list, NULL,
00203 PACKAGE "/" PACKAGE_VERSION,
00204 filename) != CPL_ERROR_NONE) {
00205 cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
00206 filename);
00207 cpl_propertylist_delete(pro_list);
00208 return -1 ;
00209 }
00210
00211
00212 cpl_propertylist_delete(pro_list);
00213
00214 return 0 ;
00215 }
00216
00217
00225
00226 int hawki_image_ext_save
00227 (const cpl_frameset * allframes,
00228 const cpl_image * image,
00229 int iext,
00230 const cpl_propertylist * ext_prop_list,
00231 const char * filename)
00232 {
00233 const char * fname;
00234 cpl_type_bpp pixeltype;
00235 cpl_propertylist * ext_prop;
00236 char sval[16];
00237 int idet;
00238
00239
00240 if (allframes == NULL) return -1 ;
00241
00242
00243 if((fname = hawki_get_extref_file(allframes)) == NULL)
00244 {
00245 cpl_msg_error(__func__, "Could not find a suitable reference frame");
00246 return -1;
00247 }
00248
00249
00250 if (cpl_image_get_type(image) == CPL_TYPE_INT)
00251 pixeltype = CPL_BPP_32_SIGNED ;
00252 else
00253 pixeltype = CPL_BPP_IEEE_FLOAT ;
00254
00255
00256
00257 if ((idet = hawki_get_detector_from_ext(fname, iext)) == -1) {
00258 cpl_msg_error(__func__, "Cannot get the chip id for extension %d "
00259 "when saving %s", iext, filename);
00260 return -1;
00261 }
00262 if (ext_prop_list != NULL)
00263 ext_prop = cpl_propertylist_duplicate(ext_prop_list) ;
00264 else
00265 ext_prop = cpl_propertylist_new() ;
00266
00267 snprintf(sval, 16, "CHIP%d.INT1", idet) ;
00268 cpl_propertylist_prepend_string(ext_prop, "EXTNAME", sval) ;
00269 if(cpl_image_save(image, filename,
00270 pixeltype, ext_prop, CPL_IO_EXTEND) != CPL_ERROR_NONE)
00271 {
00272 cpl_msg_error(__func__,"Could not save extension %d of file %s",
00273 iext, filename);
00274 cpl_propertylist_delete(ext_prop);
00275 return -1;
00276 }
00277 cpl_propertylist_delete(ext_prop) ;
00278
00279 return 0 ;
00280 }
00281
00282
00301
00302 int hawki_images_save(
00303 cpl_frameset * allframes,
00304 const cpl_parameterlist * parlist,
00305 const cpl_frameset * usedframes,
00306 const cpl_image ** images,
00307 const char * recipe,
00308 const char * procat,
00309 const char * protype,
00310 const cpl_propertylist * applist,
00311 const cpl_propertylist ** applists,
00312 const char * filename)
00313 {
00314 const char * fname ;
00315 cpl_propertylist * pro_list ;
00316 cpl_type_bpp pixeltype ;
00317 cpl_propertylist * qc_ext_list ;
00318 char sval[16] ;
00319 int chip_nb ;
00320 int iext;
00321
00322
00323 if (allframes == NULL) return -1 ;
00324
00325
00326 if((fname = hawki_get_extref_file(allframes)) == NULL)
00327 {
00328 cpl_msg_error(__func__, "Could not find a suitable reference frame");
00329 return -1;
00330 }
00331
00332
00333 if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
00334 else pro_list = cpl_propertylist_new() ;
00335 if (protype != NULL)
00336 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
00337 if (procat != NULL)
00338 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
00339
00340
00341 if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
00342 CPL_BPP_IEEE_FLOAT, recipe, pro_list, NULL,
00343 PACKAGE "/" PACKAGE_VERSION,
00344 filename) != CPL_ERROR_NONE) {
00345 cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
00346 filename);
00347 cpl_propertylist_delete(pro_list) ;
00348 return -1 ;
00349 }
00350
00351
00352 cpl_propertylist_delete(pro_list) ;
00353
00354
00355 if (cpl_image_get_type(images[0]) == CPL_TYPE_INT)
00356 pixeltype = CPL_BPP_32_SIGNED ;
00357 else
00358 pixeltype = CPL_BPP_IEEE_FLOAT ;
00359
00360
00361 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
00362
00363 if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
00364 cpl_msg_error(__func__, "Cannot get the chip for extension %d "
00365 "when saving file %s", iext+1, filename);
00366 return -1 ;
00367 }
00368
00369 if ((applists != NULL) && (applists[chip_nb-1] != NULL))
00370 qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
00371 else
00372 qc_ext_list = cpl_propertylist_new() ;
00373
00374 snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
00375 cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
00376 if(cpl_image_save(images[chip_nb-1], filename,
00377 pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
00378 {
00379 cpl_msg_error(__func__,"Cannot save extension %d of file %s",
00380 iext+1, filename);
00381 cpl_propertylist_delete(qc_ext_list);
00382 return -1;
00383 }
00384 cpl_propertylist_delete(qc_ext_list) ;
00385 }
00386 return 0 ;
00387 }
00388
00389
00405
00406 int hawki_tables_save(
00407 cpl_frameset * allframes,
00408 const cpl_parameterlist * parlist,
00409 const cpl_frameset * usedframes,
00410 const cpl_table ** tables,
00411 const char * recipe,
00412 const char * procat,
00413 const char * protype,
00414 const cpl_propertylist * applist,
00415 const cpl_propertylist ** applists,
00416 const char * filename)
00417 {
00418 cpl_propertylist * qc_ext_list ;
00419 cpl_propertylist * pro_list ;
00420 const char * fname ;
00421 char sval[16] ;
00422 int chip_nb ;
00423 int iext;
00424
00425
00426 if (allframes == NULL) return -1 ;
00427
00428
00429 if((fname = hawki_get_extref_file(allframes)) == NULL)
00430 {
00431 cpl_msg_error(__func__, "Could not find a suitable reference frame");
00432 return -1;
00433 }
00434
00435
00436 if (applist != NULL) pro_list = cpl_propertylist_duplicate(applist) ;
00437 else pro_list = cpl_propertylist_new() ;
00438 if (protype != NULL)
00439 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_TYPE, protype) ;
00440 if (procat != NULL)
00441 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procat) ;
00442
00443
00444 if ((applists != NULL) && (applists[0] != NULL))
00445 qc_ext_list = cpl_propertylist_duplicate(applists[0]) ;
00446 else
00447 qc_ext_list = cpl_propertylist_new() ;
00448 cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", "CHIP1.INT1") ;
00449 if (cpl_dfs_save_table(allframes, NULL, parlist, usedframes, NULL, tables[0],
00450 qc_ext_list, recipe, pro_list, NULL,
00451 PACKAGE "/" PACKAGE_VERSION,
00452 filename) != CPL_ERROR_NONE) {
00453 cpl_msg_error(__func__,
00454 "Cannot save the first extension table of file %s", filename);
00455 cpl_propertylist_delete(qc_ext_list) ;
00456 cpl_propertylist_delete(pro_list) ;
00457 return -1 ;
00458 }
00459 cpl_propertylist_delete(qc_ext_list) ;
00460 cpl_propertylist_delete(pro_list) ;
00461
00462
00463 for (iext=1 ; iext<HAWKI_NB_DETECTORS; iext++) {
00464
00465 if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
00466 cpl_msg_error(__func__, "Cannot get the chip for extension %d "
00467 "when writing file %s", iext+1, filename);
00468 return -1 ;
00469 }
00470 if ((applists != NULL) && (applists[chip_nb-1] != NULL))
00471 qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
00472 else
00473 qc_ext_list = cpl_propertylist_new() ;
00474
00475 snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
00476 cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
00477 if(cpl_table_save(tables[chip_nb-1], NULL, qc_ext_list, filename,
00478 CPL_IO_EXTEND) != CPL_ERROR_NONE)
00479 {
00480 cpl_msg_error(__func__, "Cannot save extension %d of table %s",
00481 iext+1, filename);
00482 cpl_propertylist_delete(qc_ext_list) ;
00483 return -1;
00484 }
00485
00486 cpl_propertylist_delete(qc_ext_list) ;
00487 }
00488
00489 return 0 ;
00490 }
00491
00492
00512
00513 int hawki_distortion_save
00514 (cpl_frameset * allframes,
00515 const cpl_parameterlist * parlist,
00516 const cpl_frameset * usedframes,
00517 const hawki_distortion ** distortion,
00518 const char * recipe,
00519 const cpl_propertylist * applist,
00520 const cpl_propertylist ** applists,
00521 const char * filename_x,
00522 const char * filename_y)
00523 {
00524 const char * fname ;
00525 cpl_propertylist * pro_list_x;
00526 cpl_propertylist * pro_list_y;
00527 cpl_type_bpp pixeltype ;
00528 cpl_propertylist * qc_ext_list ;
00529 char sval[16] ;
00530 int chip_nb ;
00531 int iext;
00532
00533
00534 if (allframes == NULL) return -1 ;
00535
00536
00537 if((fname = hawki_get_extref_file(allframes)) == NULL)
00538 {
00539 cpl_msg_error(__func__, "Could not find a suitable reference frame");
00540 return -1;
00541 }
00542
00543
00544 if(applist != NULL)
00545 pro_list_x = cpl_propertylist_duplicate(applist);
00546 else
00547 pro_list_x = cpl_propertylist_new();
00548 if(applist != NULL)
00549 pro_list_y = cpl_propertylist_duplicate(applist);
00550 else
00551 pro_list_y = cpl_propertylist_new();
00552
00553 cpl_propertylist_append_string(pro_list_x, CPL_DFS_PRO_TYPE,
00554 HAWKI_PROTYPE_DISTORTION_X);
00555 cpl_propertylist_append_string(pro_list_y, CPL_DFS_PRO_TYPE,
00556 HAWKI_PROTYPE_DISTORTION_Y);
00557 cpl_propertylist_append_string(pro_list_x, CPL_DFS_PRO_CATG,
00558 HAWKI_CALPRO_DISTORTION_X);
00559 cpl_propertylist_append_string(pro_list_y, CPL_DFS_PRO_CATG,
00560 HAWKI_CALPRO_DISTORTION_Y);
00561
00562
00563 if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
00564 CPL_BPP_IEEE_FLOAT, recipe, pro_list_x, NULL,
00565 PACKAGE "/" PACKAGE_VERSION,
00566 filename_x) != CPL_ERROR_NONE) {
00567 cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
00568 filename_x);
00569 cpl_propertylist_delete(pro_list_x);
00570 cpl_propertylist_delete(pro_list_y);
00571 return -1 ;
00572 }
00573 if (cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, NULL,
00574 CPL_BPP_IEEE_FLOAT, recipe, pro_list_y, NULL,
00575 PACKAGE "/" PACKAGE_VERSION,
00576 filename_y) != CPL_ERROR_NONE) {
00577 cpl_msg_error(__func__, "Cannot save the empty primary HDU of file %s",
00578 filename_y);
00579 cpl_propertylist_delete(pro_list_x);
00580 cpl_propertylist_delete(pro_list_y);
00581 return -1 ;
00582 }
00583
00584
00585 cpl_propertylist_delete(pro_list_x);
00586 cpl_propertylist_delete(pro_list_y);
00587
00588
00589 if (cpl_image_get_type(distortion[0]->dist_x) == CPL_TYPE_INT)
00590 pixeltype = CPL_BPP_32_SIGNED ;
00591 else
00592 pixeltype = CPL_BPP_IEEE_FLOAT ;
00593
00594
00595 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
00596
00597 if ((chip_nb = hawki_get_detector_from_ext(fname, iext+1)) == -1) {
00598 cpl_msg_error(__func__, "Cannot get the chip for extension %d "
00599 "when saving %s and %s", iext+1, filename_x, filename_y);
00600 return -1 ;
00601 }
00602
00603 if ((applists != NULL) && (applists[chip_nb-1] != NULL))
00604 qc_ext_list = cpl_propertylist_duplicate(applists[chip_nb-1]) ;
00605 else
00606 qc_ext_list = cpl_propertylist_new() ;
00607
00608
00609 cpl_propertylist_prepend_double(qc_ext_list, "CRPIX2", 1);
00610 cpl_propertylist_prepend_double(qc_ext_list, "CDELT2",
00611 distortion[chip_nb-1]->y_cdelt);
00612 cpl_propertylist_prepend_double(qc_ext_list, "CRVAL2",
00613 distortion[chip_nb-1]->y_crval);
00614 cpl_propertylist_prepend_double(qc_ext_list, "CRPIX1", 1);
00615 cpl_propertylist_prepend_double(qc_ext_list, "CDELT1",
00616 distortion[chip_nb-1]->x_cdelt);
00617 cpl_propertylist_prepend_double(qc_ext_list, "CRVAL1",
00618 distortion[chip_nb-1]->x_crval);
00619
00620 snprintf(sval, 16, "CHIP%d.INT1", chip_nb) ;
00621 cpl_propertylist_prepend_string(qc_ext_list, "EXTNAME", sval) ;
00622 if(cpl_image_save(distortion[chip_nb-1]->dist_x, filename_x,
00623 pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
00624 {
00625 cpl_msg_error(__func__,"Cannot save extension %d of file %s",
00626 iext+1, filename_x);
00627 cpl_propertylist_delete(qc_ext_list);
00628 return -1;
00629 }
00630 if(cpl_image_save(distortion[chip_nb-1]->dist_y, filename_y,
00631 pixeltype, qc_ext_list, CPL_IO_EXTEND) != CPL_ERROR_NONE)
00632 {
00633 cpl_msg_error(__func__,"Cannot save extension %d of file %s",
00634 iext+1, filename_y);
00635 cpl_propertylist_delete(qc_ext_list);
00636 return -1;
00637 }
00638 cpl_propertylist_delete(qc_ext_list) ;
00639 }
00640
00641 return 0 ;
00642 }
00643
00644
00654
00655
00656 const char * hawki_get_extref_file(const cpl_frameset * frameset)
00657 {
00658 int iframe;
00659 const char * fname = NULL;
00660
00661 for(iframe = 0 ; iframe < cpl_frameset_get_size(frameset); ++iframe)
00662 {
00663 const cpl_frame * frame;
00664
00665 frame = cpl_frameset_get_frame_const(frameset, iframe);
00666
00667
00668
00669 if(cpl_frame_get_group(frame) == CPL_FRAME_GROUP_RAW &&
00670 cpl_frame_get_nextensions(frame) == HAWKI_NB_DETECTORS)
00671 {
00672 fname = cpl_frame_get_filename(frame);
00673 return fname;
00674 }
00675 }
00676
00677
00678
00679
00680 for(iframe = 0 ; iframe < cpl_frameset_get_size(frameset); ++iframe)
00681 {
00682 const cpl_frame * frame;
00683
00684 frame = cpl_frameset_get_frame_const(frameset, iframe);
00685
00686
00687
00688 if(cpl_frame_get_group(frame) == CPL_FRAME_GROUP_PRODUCT &&
00689 cpl_frame_get_nextensions(frame) == HAWKI_NB_DETECTORS)
00690 {
00691 fname = cpl_frame_get_filename(frame);
00692 return fname;
00693 }
00694 }
00695
00696 fname = cpl_frame_get_filename(cpl_frameset_get_first_const(frameset));
00697
00698 return fname;
00699 }
00700
00701