39 #include "hawki_load.h"
40 #include "hawki_pfits.h"
41 #include "hawki_utils.h"
42 #include "hawki_dfs.h"
64 const cpl_frameset * in,
68 const cpl_frame * cur_frame ;
69 cpl_frame * loc_frame ;
74 if (in == NULL)
return NULL ;
75 if (tag == NULL)
return NULL ;
78 nbframes = cpl_frameset_get_size(in) ;
81 if ((nbext = cpl_frameset_count_tags(in, tag)) == 0)
return NULL ;
84 out = cpl_frameset_new() ;
88 for (i=0 ; i<nbframes ; i++) {
89 cur_frame = cpl_frameset_get_frame_const(in, i) ;
90 if (!strcmp(cpl_frame_get_tag(cur_frame), tag)) {
91 loc_frame = cpl_frame_duplicate(cur_frame) ;
92 cpl_frameset_insert(out, loc_frame) ;
111 const cpl_frameset * fset,
117 const cpl_frame * frame ;
122 if (fset == NULL)
return NULL ;
123 if (chip < 1 || chip > HAWKI_NB_DETECTORS)
return NULL ;
124 if (quad < 1 || quad > 4)
return NULL ;
127 frame = cpl_frameset_get_frame_const(fset, fnum) ;
128 fname = cpl_frame_get_filename(frame) ;
132 cpl_msg_error(__func__,
"Cannot load %dth frame (chip %d quarter %d)",
133 fnum+1, chip, quad) ;
157 int llx, lly, urx, ury ;
160 if (fname == NULL)
return NULL ;
161 if (chip < 1 || chip > HAWKI_NB_DETECTORS)
return NULL ;
162 if (quad < 1 || quad > 4)
return NULL ;
168 }
else if (quad == 2) {
173 }
else if (quad == 3) {
178 }
else if (quad == 4) {
185 cpl_msg_error(__func__,
"Cannot get the extension with chip %d",
191 if ((ima=cpl_image_load_window(fname, ptype, 0, ext_nb, llx, lly, urx,
193 cpl_msg_error(__func__,
"Cannot load chip %d quarter %d from %s",
210 const cpl_frameset * fset,
215 cpl_imagelist * out ;
216 const cpl_frame * frame ;
223 if (fset == NULL)
return NULL ;
224 if (chip < 1 || chip > HAWKI_NB_DETECTORS)
return NULL ;
225 nframes = cpl_frameset_get_size(fset) ;
228 out = cpl_imagelist_new() ;
231 for (i=0 ; i<nframes ; i++) {
234 frame = cpl_frameset_get_frame_const(fset, i) ;
235 fname = cpl_frame_get_filename(frame) ;
239 cpl_msg_error(__func__,
"Cannot get the extension with chip %d",
241 cpl_imagelist_delete(out);
245 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL) {
246 cpl_msg_error(__func__,
"Cannot load %dth frame (chip %d)",
248 cpl_imagelist_delete(out) ;
251 cpl_imagelist_set(out, ima, i) ;
266 (
const cpl_frameset * fset,
271 cpl_imagelist * out ;
272 const cpl_frame * frame ;
278 if (fset == NULL)
return NULL ;
279 nframes = cpl_frameset_get_size(fset) ;
282 out = cpl_imagelist_new() ;
285 for (iframe=0 ; iframe<nframes ; iframe++) {
288 frame = cpl_frameset_get_frame_const(fset, iframe) ;
289 fname = cpl_frame_get_filename(frame) ;
292 if ((ima=cpl_image_load(fname, ptype, 0, extension)) == NULL) {
293 cpl_msg_error(__func__,
"Cannot load %dth frame (extension %d)",
294 iframe+1, extension) ;
295 cpl_imagelist_delete(out) ;
298 cpl_imagelist_set(out, ima, iframe) ;
314 const cpl_frameset * fset,
319 const cpl_frame * frame ;
325 if (fset == NULL)
return NULL ;
326 if (chip < 1 || chip > HAWKI_NB_DETECTORS)
return NULL ;
329 frame = cpl_frameset_get_frame_const(fset, fnum) ;
330 fname = cpl_frame_get_filename(frame) ;
333 if(cpl_frame_get_nextensions(frame) != HAWKI_NB_DETECTORS)
335 cpl_msg_error(__func__,
"File %s contains less than %d extensions",
336 fname, HAWKI_NB_DETECTORS);
342 cpl_msg_error(__func__,
"Cannot get the extension with chip %d",
347 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL) {
348 cpl_msg_error(__func__,
"Cannot load %dth frame (chip %d)",
366 const cpl_frameset * fset,
371 cpl_imagelist * out ;
376 if (fset == NULL)
return NULL ;
377 if (chip < 1 || chip > HAWKI_NB_DETECTORS)
return NULL ;
378 if (quad < 1 || quad > 4)
return NULL ;
381 out = cpl_imagelist_new() ;
384 for (i=0 ; i<cpl_frameset_get_size(fset) ; i++) {
387 cpl_msg_error(__func__,
"Cannot load %dth frame (chip %d, quad %d)",
389 cpl_imagelist_delete(out) ;
392 cpl_imagelist_set(out, ima, i) ;
408 const cpl_frameset * fset,
412 cpl_imagelist * out ;
417 if (fset == NULL)
return NULL ;
418 if (chip < 1 || chip > HAWKI_NB_DETECTORS)
return NULL ;
421 out = cpl_imagelist_new() ;
424 for (i=0 ; i<cpl_frameset_get_size(fset) ; i++) {
427 cpl_msg_error(__func__,
"Cannot load %dth frame (chip %d)",
429 cpl_imagelist_delete(out) ;
432 cpl_imagelist_set(out, ima, i) ;
446 const cpl_frame * frame,
454 int * ext_chip_mapping;
457 if (frame == NULL)
return NULL ;
460 out = cpl_imagelist_new() ;
463 fname = cpl_frame_get_filename(frame);
466 if(cpl_frame_get_nextensions(frame) != HAWKI_NB_DETECTORS)
468 cpl_msg_error(__func__,
"File %s contains less than %d extensions",
469 fname, HAWKI_NB_DETECTORS);
470 cpl_imagelist_delete(out);
476 if (ext_chip_mapping == NULL)
478 cpl_msg_error(__func__,
"Cannot get mapping between extension and chip");
479 cpl_imagelist_delete(out);
484 for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
487 ext_nb = ext_chip_mapping[idet];
490 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL)
492 cpl_msg_error(__func__,
"Cannot load frame (detector %d)",
494 cpl_imagelist_delete(out);
497 cpl_imagelist_set(out, ima, idet);
501 cpl_free(ext_chip_mapping);
514 const cpl_frame * frame,
522 if (frame == NULL)
return NULL ;
525 fname = cpl_frame_get_filename(frame);
528 if ((ima=cpl_image_load(fname, ptype, 0, iextension)) == NULL)
530 cpl_msg_error(__func__,
"Cannot load frame (extension %d)",
548 const cpl_frame * frame,
557 if (frame == NULL)
return NULL ;
560 fname = cpl_frame_get_filename(frame);
565 cpl_msg_error(__func__,
"Cannot get the extension with chip %d",
570 if ((ima=cpl_image_load(fname, ptype, 0, ext_nb)) == NULL)
572 cpl_msg_error(__func__,
"Cannot load frame (chip %d)",
592 const char * filename;
598 tables = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_table *));
601 filename = cpl_frame_get_filename(frame);
602 for (idet=0 ; idet< HAWKI_NB_DETECTORS ; idet++)
607 cpl_msg_error(__func__,
"Cannot get the extension with detector %d",
612 if((tables[idet] = cpl_table_load(filename, ext_nb, 0) ) == NULL)
614 for (j=0 ; j< idet ; j++)
616 cpl_table_delete(tables[j]);
635 cpl_errorstate previous_state;
636 cpl_table ** offsets_tables;
637 cpl_bivector ** offsets;
641 previous_state = cpl_errorstate_get();
648 offsets = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_bivector *));
651 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
652 cpl_table_delete(offsets_tables[idet]);
655 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
663 noff = cpl_table_get_nrow(offsets_tables[idet]);
664 offsets[idet] = cpl_bivector_new(noff);
665 if(offsets[idet] == NULL)
667 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
668 cpl_table_delete(offsets_tables[idet]);
669 for(jdet = 0; jdet < idet; ++jdet)
670 cpl_bivector_delete(offsets[jdet]);
671 cpl_free(offsets[jdet]);
674 off_x = cpl_bivector_get_x(offsets[idet]);
675 off_y = cpl_bivector_get_y(offsets[idet]);
676 for(ioff = 0; ioff < noff; ++ioff)
678 double xoffset, yoffset;
679 xoffset = cpl_table_get
680 (offsets_tables[idet], HAWKI_COL_OFFSET_X, ioff, NULL);
681 yoffset = cpl_table_get
682 (offsets_tables[idet], HAWKI_COL_OFFSET_Y, ioff, NULL);
683 cpl_vector_set(off_x, ioff, xoffset);
684 cpl_vector_set(off_y, ioff, yoffset);
689 if(cpl_errorstate_get() != previous_state)
691 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
693 cpl_table_delete(offsets_tables[idet]);
694 cpl_bivector_delete(offsets[idet]);
701 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
702 cpl_table_delete(offsets_tables[idet]);
703 cpl_free(offsets_tables);
719 const cpl_image * ima1,
720 const cpl_image * ima2,
721 const cpl_image * ima3,
722 const cpl_image * ima4)
727 if (ima1 == NULL)
return NULL ;
728 if (ima2 == NULL)
return NULL ;
729 if (ima3 == NULL)
return NULL ;
730 if (ima4 == NULL)
return NULL ;
731 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima2))
return NULL ;
732 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima3))
return NULL ;
733 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima4))
return NULL ;
736 ima = cpl_image_new(4096, 4096, cpl_image_get_type(ima1)) ;
739 if (cpl_image_copy(ima, ima1, 1, 1) != CPL_ERROR_NONE) {
740 cpl_image_delete(ima) ;
743 if (cpl_image_copy(ima, ima2, 2049, 1) != CPL_ERROR_NONE) {
744 cpl_image_delete(ima) ;
747 if (cpl_image_copy(ima, ima3, 2049, 2049) != CPL_ERROR_NONE) {
748 cpl_image_delete(ima) ;
751 if (cpl_image_copy(ima, ima4, 1, 2049) != CPL_ERROR_NONE) {
752 cpl_image_delete(ima) ;
770 const cpl_image * ima1,
771 const cpl_image * ima2,
772 const cpl_image * ima3,
773 const cpl_image * ima4)
778 if (ima1 == NULL)
return NULL ;
779 if (ima2 == NULL)
return NULL ;
780 if (ima3 == NULL)
return NULL ;
781 if (ima4 == NULL)
return NULL ;
782 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima2))
return NULL ;
783 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima3))
return NULL ;
784 if (cpl_image_get_type(ima1)!=cpl_image_get_type(ima4))
return NULL ;
787 ima = cpl_image_new(2048, 2048, cpl_image_get_type(ima1)) ;
790 if (cpl_image_copy(ima, ima1, 1, 1) != CPL_ERROR_NONE) {
791 cpl_image_delete(ima) ;
794 if (cpl_image_copy(ima, ima2, 1025, 1) != CPL_ERROR_NONE) {
795 cpl_image_delete(ima) ;
798 if (cpl_image_copy(ima, ima3, 1, 1025) != CPL_ERROR_NONE) {
799 cpl_image_delete(ima) ;
802 if (cpl_image_copy(ima, ima4, 1025, 1025) != CPL_ERROR_NONE) {
803 cpl_image_delete(ima) ;
821 cpl_propertylist * plist ;
826 if (ext <= 0 || ext > HAWKI_NB_DETECTORS)
return -1 ;
829 plist=cpl_propertylist_load(fname, ext) ;
830 if (plist == NULL)
return -1 ;
832 if (sscanf(sval,
"CHIP%d.INT1", &chip) != 1) chip = -1 ;
833 cpl_propertylist_delete(plist) ;
850 cpl_propertylist * plist ;
857 if (fname == NULL)
return -1;
858 if (chip <= 0 || chip > HAWKI_NB_DETECTORS)
return -1 ;
864 snprintf(chipval, 512,
"CHIP%d.INT1", chip) ;
867 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
869 if ((plist = cpl_propertylist_load(fname, iext+1)) == NULL) {
870 cpl_msg_error(__func__,
"Cannot read the Extname keyword") ;
874 cpl_msg_error(__func__,
"Cannot read the Extname keyword") ;
875 cpl_propertylist_delete(plist) ;
879 if (!strcmp(chipval, sval)) {
881 cpl_propertylist_delete(plist) ;
884 cpl_propertylist_delete(plist) ;
900 int * ext_chip_mapping;
904 if (fname == NULL)
return NULL;
907 ext_chip_mapping = cpl_malloc(
sizeof(
int) * HAWKI_NB_DETECTORS);
910 for (iext=0 ; iext<HAWKI_NB_DETECTORS ; iext++) {
912 ext_chip_mapping[ichip-1] = iext + 1;
914 return ext_chip_mapping;