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_load.h"
00040 #include "hawki_pfits.h"
00041 #include "hawki_utils.h"
00042 #include "hawki_dfs.h"
00043
00044
00049
00050
00053
00062
00063 cpl_frameset * hawki_extract_frameset(
00064 const cpl_frameset * in,
00065 const char * tag)
00066 {
00067 cpl_frameset * out ;
00068 const cpl_frame * cur_frame ;
00069 cpl_frame * loc_frame ;
00070 int nbframes, nbext ;
00071 int i ;
00072
00073
00074 if (in == NULL) return NULL ;
00075 if (tag == NULL) return NULL ;
00076
00077
00078 nbframes = cpl_frameset_get_size(in) ;
00079
00080
00081 if ((nbext = cpl_frameset_count_tags(in, tag)) == 0) return NULL ;
00082
00083
00084 out = cpl_frameset_new() ;
00085
00086
00087 nbext = 0 ;
00088 for (i=0 ; i<nbframes ; i++) {
00089 cur_frame = cpl_frameset_get_frame_const(in, i) ;
00090 if (!strcmp(cpl_frame_get_tag(cur_frame), tag)) {
00091 loc_frame = cpl_frame_duplicate(cur_frame) ;
00092 cpl_frameset_insert(out, loc_frame) ;
00093 nbext ++ ;
00094 }
00095 }
00096 return out ;
00097 }
00098
00099
00109
00110 cpl_image * hawki_load_quadrant(
00111 const cpl_frameset * fset,
00112 int fnum,
00113 int chip,
00114 int quad,
00115 cpl_type ptype)
00116 {
00117 const cpl_frame * frame ;
00118 const char * fname ;
00119 cpl_image * ima ;
00120
00121
00122 if (fset == NULL) return NULL ;
00123 if (chip < 1 || chip > HAWKI_NB_DETECTORS) return NULL ;
00124 if (quad < 1 || quad > 4) return NULL ;
00125
00126
00127 frame = cpl_frameset_get_frame_const(fset, fnum) ;
00128 fname = cpl_frame_get_filename(frame) ;
00129
00130
00131 if ((ima=hawki_load_quadrant_from_file(fname, chip, quad, ptype)) == NULL) {
00132 cpl_msg_error(__func__, "Cannot load %dth frame (chip %d quarter %d)",
00133 fnum+1, chip, quad) ;
00134 return NULL ;
00135 }
00136 return ima ;
00137 }
00138
00139
00148
00149 cpl_image * hawki_load_quadrant_from_file(
00150 const char * fname,
00151 int chip,
00152 int quad,
00153 cpl_type ptype)
00154 {
00155 cpl_image * ima ;
00156 int ext_nb ;
00157 int llx, lly, urx, ury ;
00158
00159
00160 if (fname == NULL) return NULL ;
00161 if (chip < 1 || chip > HAWKI_NB_DETECTORS) return NULL ;
00162 if (quad < 1 || quad > 4) return NULL ;
00163
00164
00165 if (quad == 1) {
00166 llx = lly = 1 ;
00167 urx = ury = 1024 ;
00168 } else if (quad == 2) {
00169 llx = 1025 ;
00170 lly = 1 ;
00171 urx = 2048 ;
00172 ury = 1024 ;
00173 } else if (quad == 3) {
00174 llx = 1 ;
00175 lly = 1025 ;
00176 urx = 1024 ;
00177 ury = 2048 ;
00178 } else if (quad == 4) {
00179 llx = lly = 1025 ;
00180 urx = ury = 2048 ;
00181 } else return NULL ;
00182
00183
00184 if ((ext_nb = hawki_get_ext_from_detector(fname, chip)) == -1) {
00185 cpl_msg_error(__func__, "Cannot get the extension with chip %d",
00186 chip+1) ;
00187 return NULL ;
00188 }
00189
00190
00191 if ((ima=cpl_image_load_window(fname, ptype, 0, ext_nb, llx, lly, urx,
00192 ury)) == NULL) {
00193 cpl_msg_error(__func__, "Cannot load chip %d quarter %d from %s",
00194 chip, quad, fname) ;
00195 return NULL ;
00196 }
00197 return ima ;
00198 }
00199
00200
00208
00209 cpl_imagelist * hawki_load_detector(
00210 const cpl_frameset * fset,
00211 int chip,
00212 cpl_type ptype)
00213 {
00214 int nframes ;
00215 cpl_imagelist * out ;
00216 const cpl_frame * frame ;
00217 const char * fname ;
00218 cpl_image * ima ;
00219 int ext_nb ;
00220 int i ;
00221
00222
00223 if (fset == NULL) return NULL ;
00224 if (chip < 1 || chip > HAWKI_NB_DETECTORS) return NULL ;
00225 nframes = cpl_frameset_get_size(fset) ;
00226
00227
00228 out = cpl_imagelist_new() ;
00229
00230
00231 for (i=0 ; i<nframes ; i++) {
00232
00233
00234 frame = cpl_frameset_get_frame_const(fset, i) ;
00235 fname = cpl_frame_get_filename(frame) ;
00236
00237
00238 if ((ext_nb = hawki_get_ext_from_detector(fname, chip)) == -1) {
00239 cpl_msg_error(__func__, "Cannot get the extension with chip %d",
00240 chip) ;
00241 cpl_imagelist_delete(out);
00242 return NULL ;
00243 }
00244
00245 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL) {
00246 cpl_msg_error(__func__, "Cannot load %dth frame (chip %d)",
00247 i+1, chip) ;
00248 cpl_imagelist_delete(out) ;
00249 return NULL ;
00250 }
00251 cpl_imagelist_set(out, ima, i) ;
00252 }
00253 return out ;
00254 }
00255
00256
00264
00265 cpl_imagelist * hawki_load_extensions
00266 (const cpl_frameset * fset,
00267 int extension,
00268 cpl_type ptype)
00269 {
00270 int nframes ;
00271 cpl_imagelist * out ;
00272 const cpl_frame * frame ;
00273 const char * fname ;
00274 cpl_image * ima ;
00275 int iframe;
00276
00277
00278 if (fset == NULL) return NULL ;
00279 nframes = cpl_frameset_get_size(fset) ;
00280
00281
00282 out = cpl_imagelist_new() ;
00283
00284
00285 for (iframe=0 ; iframe<nframes ; iframe++) {
00286
00287
00288 frame = cpl_frameset_get_frame_const(fset, iframe) ;
00289 fname = cpl_frame_get_filename(frame) ;
00290
00291
00292 if ((ima=cpl_image_load(fname, ptype, 0, extension)) == NULL) {
00293 cpl_msg_error(__func__, "Cannot load %dth frame (extension %d)",
00294 iframe+1, extension) ;
00295 cpl_imagelist_delete(out) ;
00296 return NULL ;
00297 }
00298 cpl_imagelist_set(out, ima, iframe) ;
00299 }
00300 return out ;
00301 }
00302
00303
00312
00313 cpl_image * hawki_load_image(
00314 const cpl_frameset * fset,
00315 int fnum,
00316 int chip,
00317 cpl_type ptype)
00318 {
00319 const cpl_frame * frame ;
00320 const char * fname ;
00321 cpl_image * ima ;
00322 int ext_nb ;
00323
00324
00325 if (fset == NULL) return NULL ;
00326 if (chip < 1 || chip > HAWKI_NB_DETECTORS) return NULL ;
00327
00328
00329 frame = cpl_frameset_get_frame_const(fset, fnum) ;
00330 fname = cpl_frame_get_filename(frame) ;
00331
00332
00333 if(cpl_frame_get_nextensions(frame) != HAWKI_NB_DETECTORS)
00334 {
00335 cpl_msg_error(__func__, "File %s contains less than %d extensions",
00336 fname, HAWKI_NB_DETECTORS);
00337 return NULL ;
00338 }
00339
00340
00341 if ((ext_nb = hawki_get_ext_from_detector(fname, chip)) == -1) {
00342 cpl_msg_error(__func__, "Cannot get the extension with chip %d",
00343 chip+1) ;
00344 return NULL ;
00345 }
00346
00347 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL) {
00348 cpl_msg_error(__func__, "Cannot load %dth frame (chip %d)",
00349 fnum+1, chip) ;
00350 return NULL ;
00351 }
00352 return ima ;
00353 }
00354
00355
00364
00365 cpl_imagelist * hawki_load_quadrants(
00366 const cpl_frameset * fset,
00367 int chip,
00368 int quad,
00369 cpl_type ptype)
00370 {
00371 cpl_imagelist * out ;
00372 cpl_image * ima ;
00373 int i ;
00374
00375
00376 if (fset == NULL) return NULL ;
00377 if (chip < 1 || chip > HAWKI_NB_DETECTORS) return NULL ;
00378 if (quad < 1 || quad > 4) return NULL ;
00379
00380
00381 out = cpl_imagelist_new() ;
00382
00383
00384 for (i=0 ; i<cpl_frameset_get_size(fset) ; i++) {
00385 ima = hawki_load_quadrant(fset, i, chip, quad, ptype) ;
00386 if (ima == NULL) {
00387 cpl_msg_error(__func__, "Cannot load %dth frame (chip %d, quad %d)",
00388 i+1, chip, quad) ;
00389 cpl_imagelist_delete(out) ;
00390 return NULL ;
00391 }
00392 cpl_imagelist_set(out, ima, i) ;
00393 }
00394 return out ;
00395 }
00396
00397
00406
00407 cpl_imagelist * hawki_load_frameset(
00408 const cpl_frameset * fset,
00409 int chip,
00410 cpl_type ptype)
00411 {
00412 cpl_imagelist * out ;
00413 cpl_image * ima ;
00414 int i ;
00415
00416
00417 if (fset == NULL) return NULL ;
00418 if (chip < 1 || chip > HAWKI_NB_DETECTORS) return NULL ;
00419
00420
00421 out = cpl_imagelist_new() ;
00422
00423
00424 for (i=0 ; i<cpl_frameset_get_size(fset) ; i++) {
00425 ima = hawki_load_image(fset, i, chip, ptype) ;
00426 if (ima == NULL) {
00427 cpl_msg_error(__func__, "Cannot load %dth frame (chip %d)",
00428 i+1, chip) ;
00429 cpl_imagelist_delete(out) ;
00430 return NULL ;
00431 }
00432 cpl_imagelist_set(out, ima, i) ;
00433 }
00434 return out ;
00435 }
00436
00444
00445 cpl_imagelist * hawki_load_frame(
00446 const cpl_frame * frame,
00447 cpl_type ptype)
00448 {
00449 cpl_imagelist * out;
00450 cpl_image * ima;
00451 const char * fname;
00452 int idet;
00453 int ext_nb;
00454 int * ext_chip_mapping;
00455
00456
00457 if (frame == NULL) return NULL ;
00458
00459
00460 out = cpl_imagelist_new() ;
00461
00462
00463 fname = cpl_frame_get_filename(frame);
00464
00465
00466 if(cpl_frame_get_nextensions(frame) != HAWKI_NB_DETECTORS)
00467 {
00468 cpl_msg_error(__func__, "File %s contains less than %d extensions",
00469 fname, HAWKI_NB_DETECTORS);
00470 cpl_imagelist_delete(out);
00471 return NULL ;
00472 }
00473
00474
00475 ext_chip_mapping = hawki_get_ext_detector_mapping(fname);
00476 if (ext_chip_mapping == NULL)
00477 {
00478 cpl_msg_error(__func__,"Cannot get mapping between extension and chip");
00479 cpl_imagelist_delete(out);
00480 return NULL;
00481 }
00482
00483
00484 for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
00485 {
00486
00487 ext_nb = ext_chip_mapping[idet];
00488
00489
00490 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL)
00491 {
00492 cpl_msg_error(__func__, "Cannot load frame (detector %d)",
00493 idet);
00494 cpl_imagelist_delete(out);
00495 return NULL;
00496 }
00497 cpl_imagelist_set(out, ima, idet);
00498 }
00499
00500
00501 cpl_free(ext_chip_mapping);
00502 return out;
00503 }
00504
00512
00513 cpl_image * hawki_load_frame_extension(
00514 const cpl_frame * frame,
00515 int iextension,
00516 cpl_type ptype)
00517 {
00518 cpl_image * ima;
00519 const char * fname;
00520
00521
00522 if (frame == NULL) return NULL ;
00523
00524
00525 fname = cpl_frame_get_filename(frame);
00526
00527
00528 if ((ima=cpl_image_load(fname, ptype, 0, iextension)) == NULL)
00529 {
00530 cpl_msg_error(__func__, "Cannot load frame (extension %d)",
00531 iextension) ;
00532 return NULL ;
00533 }
00534
00535
00536 return ima;
00537 }
00538
00546
00547 cpl_image * hawki_load_frame_detector(
00548 const cpl_frame * frame,
00549 int idet,
00550 cpl_type ptype)
00551 {
00552 cpl_image * ima;
00553 const char * fname;
00554 int ext_nb;
00555
00556
00557 if (frame == NULL) return NULL ;
00558
00559
00560 fname = cpl_frame_get_filename(frame);
00561
00562
00563 if ((ext_nb = hawki_get_ext_from_detector(fname, idet)) == -1)
00564 {
00565 cpl_msg_error(__func__, "Cannot get the extension with chip %d",
00566 idet) ;
00567 return NULL ;
00568 }
00569
00570 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL)
00571 {
00572 cpl_msg_error(__func__, "Cannot load frame (chip %d)",
00573 idet) ;
00574 return NULL ;
00575 }
00576
00577
00578 return ima;
00579 }
00580
00588
00589 cpl_table ** hawki_load_tables(const cpl_frame * frame)
00590 {
00591 cpl_table ** tables;
00592 const char * filename;
00593 int idet;
00594 int j;
00595 int ext_nb;
00596
00597
00598 tables = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00599
00600
00601 filename = cpl_frame_get_filename(frame);
00602 for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
00603 {
00604
00605 if ((ext_nb = hawki_get_ext_from_detector(filename, idet+1)) == -1)
00606 {
00607 cpl_msg_error(__func__, "Cannot get the extension with detector %d",
00608 idet+1) ;
00609 return NULL ;
00610 }
00611
00612 if((tables[idet] = cpl_table_load(filename, ext_nb, 0) ) == NULL)
00613 {
00614 for (j=0 ; j< idet ; j++)
00615 {
00616 cpl_table_delete(tables[j]);
00617 }
00618 cpl_free(tables);
00619 return NULL;
00620 }
00621 }
00622
00623 return tables;
00624 }
00625
00632
00633 cpl_bivector ** hawki_load_refined_offsets(const cpl_frame * offsets_frame)
00634 {
00635 cpl_errorstate previous_state;
00636 cpl_table ** offsets_tables;
00637 cpl_bivector ** offsets;
00638 int idet;
00639
00640
00641 previous_state = cpl_errorstate_get();
00642
00643
00644 if((offsets_tables = hawki_load_tables(offsets_frame)) == NULL)
00645 return NULL;
00646
00647
00648 offsets = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_bivector *));
00649 if(offsets == NULL)
00650 {
00651 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00652 cpl_table_delete(offsets_tables[idet]);
00653 return NULL;
00654 }
00655 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00656 {
00657 cpl_vector * off_x;
00658 cpl_vector * off_y;
00659 int noff;
00660 int ioff;
00661 int jdet;
00662
00663 noff = cpl_table_get_nrow(offsets_tables[idet]);
00664 offsets[idet] = cpl_bivector_new(noff);
00665 if(offsets[idet] == NULL)
00666 {
00667 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00668 cpl_table_delete(offsets_tables[idet]);
00669 for(jdet = 0; jdet < idet; ++jdet)
00670 cpl_bivector_delete(offsets[jdet]);
00671 cpl_free(offsets[jdet]);
00672 return NULL;
00673 }
00674 off_x = cpl_bivector_get_x(offsets[idet]);
00675 off_y = cpl_bivector_get_y(offsets[idet]);
00676 for(ioff = 0; ioff < noff; ++ioff)
00677 {
00678 double xoffset, yoffset;
00679 xoffset = cpl_table_get
00680 (offsets_tables[idet], HAWKI_COL_OFFSET_X, ioff, NULL);
00681 yoffset = cpl_table_get
00682 (offsets_tables[idet], HAWKI_COL_OFFSET_Y, ioff, NULL);
00683 cpl_vector_set(off_x, ioff, xoffset);
00684 cpl_vector_set(off_y, ioff, yoffset);
00685 }
00686 }
00687
00688
00689 if(cpl_errorstate_get() != previous_state)
00690 {
00691 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00692 {
00693 cpl_table_delete(offsets_tables[idet]);
00694 cpl_bivector_delete(offsets[idet]);
00695 }
00696 cpl_free(offsets);
00697 return NULL;
00698 }
00699
00700
00701 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
00702 cpl_table_delete(offsets_tables[idet]);
00703 cpl_free(offsets_tables);
00704
00705 return offsets;
00706 }
00707
00708
00717
00718 cpl_image * hawki_rebuild_detectors(
00719 const cpl_image * ima1,
00720 const cpl_image * ima2,
00721 const cpl_image * ima3,
00722 const cpl_image * ima4)
00723 {
00724 cpl_image * ima ;
00725
00726
00727 if (ima1 == NULL) return NULL ;
00728 if (ima2 == NULL) return NULL ;
00729 if (ima3 == NULL) return NULL ;
00730 if (ima4 == NULL) return NULL ;
00731 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima2)) return NULL ;
00732 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima3)) return NULL ;
00733 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima4)) return NULL ;
00734
00735
00736 ima = cpl_image_new(4096, 4096, cpl_image_get_type(ima1)) ;
00737
00738
00739 if (cpl_image_copy(ima, ima1, 1, 1) != CPL_ERROR_NONE) {
00740 cpl_image_delete(ima) ;
00741 return NULL ;
00742 }
00743 if (cpl_image_copy(ima, ima2, 2049, 1) != CPL_ERROR_NONE) {
00744 cpl_image_delete(ima) ;
00745 return NULL ;
00746 }
00747 if (cpl_image_copy(ima, ima3, 2049, 2049) != CPL_ERROR_NONE) {
00748 cpl_image_delete(ima) ;
00749 return NULL ;
00750 }
00751 if (cpl_image_copy(ima, ima4, 1, 2049) != CPL_ERROR_NONE) {
00752 cpl_image_delete(ima) ;
00753 return NULL ;
00754 }
00755 return ima ;
00756 }
00757
00758
00759
00768
00769 cpl_image * hawki_rebuild_quadrants(
00770 const cpl_image * ima1,
00771 const cpl_image * ima2,
00772 const cpl_image * ima3,
00773 const cpl_image * ima4)
00774 {
00775 cpl_image * ima ;
00776
00777
00778 if (ima1 == NULL) return NULL ;
00779 if (ima2 == NULL) return NULL ;
00780 if (ima3 == NULL) return NULL ;
00781 if (ima4 == NULL) return NULL ;
00782 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima2)) return NULL ;
00783 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima3)) return NULL ;
00784 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima4)) return NULL ;
00785
00786
00787 ima = cpl_image_new(2048, 2048, cpl_image_get_type(ima1)) ;
00788
00789
00790 if (cpl_image_copy(ima, ima1, 1, 1) != CPL_ERROR_NONE) {
00791 cpl_image_delete(ima) ;
00792 return NULL ;
00793 }
00794 if (cpl_image_copy(ima, ima2, 1025, 1) != CPL_ERROR_NONE) {
00795 cpl_image_delete(ima) ;
00796 return NULL ;
00797 }
00798 if (cpl_image_copy(ima, ima3, 1, 1025) != CPL_ERROR_NONE) {
00799 cpl_image_delete(ima) ;
00800 return NULL ;
00801 }
00802 if (cpl_image_copy(ima, ima4, 1025, 1025) != CPL_ERROR_NONE) {
00803 cpl_image_delete(ima) ;
00804 return NULL ;
00805 }
00806 return ima ;
00807 }
00808
00809
00816
00817 int hawki_get_detector_from_ext(
00818 const char * fname,
00819 int ext)
00820 {
00821 cpl_propertylist * plist ;
00822 const char * sval ;
00823 int chip ;
00824
00825
00826 if (ext <= 0 || ext > HAWKI_NB_DETECTORS) return -1 ;
00827
00828
00829 plist=cpl_propertylist_load(fname, ext) ;
00830 if (plist == NULL) return -1 ;
00831 sval = hawki_pfits_get_extname(plist) ;
00832 if (sscanf(sval, "CHIP%d.INT1", &chip) != 1) chip = -1 ;
00833 cpl_propertylist_delete(plist) ;
00834
00835 return chip ;
00836 }
00837
00838
00845
00846 int hawki_get_ext_from_detector(
00847 const char * fname,
00848 int chip)
00849 {
00850 cpl_propertylist * plist ;
00851 const char * sval ;
00852 char chipval[512] ;
00853 int ext_nb ;
00854 int iext;
00855
00856
00857 if (fname == NULL) return -1;
00858 if (chip <= 0 || chip > HAWKI_NB_DETECTORS) return -1 ;
00859
00860
00861 ext_nb = -1 ;
00862
00863
00864 snprintf(chipval, 512, "CHIP%d.INT1", chip) ;
00865
00866
00867 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
00868
00869 if ((plist = cpl_propertylist_load(fname, iext+1)) == NULL) {
00870 cpl_msg_error(__func__, "Cannot read the Extname keyword") ;
00871 return -1 ;
00872 }
00873 if ((sval = hawki_pfits_get_extname(plist)) == NULL) {
00874 cpl_msg_error(__func__, "Cannot read the Extname keyword") ;
00875 cpl_propertylist_delete(plist) ;
00876 return -1 ;
00877 }
00878
00879 if (!strcmp(chipval, sval)) {
00880 ext_nb = iext+1 ;
00881 cpl_propertylist_delete(plist) ;
00882 break;
00883 }
00884 cpl_propertylist_delete(plist) ;
00885 }
00886 return ext_nb ;
00887 }
00888
00889
00895
00896 int * hawki_get_ext_detector_mapping
00897 (const char * fname)
00898 {
00899 int iext;
00900 int * ext_chip_mapping;
00901
00902
00903
00904 if (fname == NULL) return NULL;
00905
00906
00907 ext_chip_mapping = cpl_malloc(sizeof(int) * HAWKI_NB_DETECTORS);
00908
00909
00910 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
00911 int ichip = hawki_get_detector_from_ext(fname, iext + 1);
00912 ext_chip_mapping[ichip-1] = iext + 1;
00913 }
00914 return ext_chip_mapping;
00915 }