38 #include "omega_utils.h"
39 #include "omega_dfs.h"
40 #include "omega_pfits.h"
42 #define MAXNAMESZ 4096
69 const char * omega_license =
70 "This file is part of the OMEGA Instrument Pipeline\n"
71 "Copyright (C) 2002,2003 European Southern Observatory\n"
73 "This program is free software; you can redistribute it and/or modify\n"
74 "it under the terms of the GNU General Public License as published by\n"
75 "the Free Software Foundation; either version 2 of the License, or\n"
76 "(at your option) any later version.\n"
78 "This program is distributed in the hope that it will be useful,\n"
79 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
80 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
81 "GNU General Public License for more details.\n"
83 "You should have received a copy of the GNU General Public License\n"
84 "along with this program; if not, write to the Free Software\n"
85 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, \n"
87 return omega_license ;
113 if ((paf=fopen(filename,
"w"))==NULL)
return NULL ;
114 fprintf(paf,
"PAF.HDR.START ;# start of header\n");
115 fprintf(paf,
"PAF.TYPE \"pipeline product\" ;\n");
116 fprintf(paf,
"PAF.ID \"%s\"\n", paf_id);
117 fprintf(paf,
"PAF.NAME \"%s\"\n", filename);
118 fprintf(paf,
"PAF.DESC \"%s\"\n", paf_desc);
119 fprintf(paf,
"PAF.CHCK.CHECKSUM \"\"\n");
120 fprintf(paf,
"PAF.HDR.END ;# end of header\n");
160 static char path[MAXNAMESZ+1];
163 if (strlen(filename)>MAXNAMESZ)
return NULL ;
164 memset(path, MAXNAMESZ, 0);
165 strcpy(path, filename);
166 lastdot = strrchr(path,
'.');
167 if (lastdot == NULL)
return path ;
168 if ((!strcmp(lastdot,
".fits")) || (!strcmp(lastdot,
".FITS")) ||
169 (!strcmp(lastdot,
".paf")) || (!strcmp(lastdot,
".PAF")) ||
170 (!strcmp(lastdot,
".dat")) || (!strcmp(lastdot,
".DAT")) ||
171 (!strcmp(lastdot,
".txt")) || (!strcmp(lastdot,
".TXT")) ||
172 (!strcmp(lastdot,
".tfits")) || (!strcmp(lastdot,
".TFITS")) ||
173 (!strcmp(lastdot,
".ascii")) || (!strcmp(lastdot,
".ASCII")))
175 lastdot[0] = (char)0;
211 if (frame1 == NULL || frame2 == NULL)
216 if ((v1 = (
char *)cpl_frame_get_tag(frame1)) == NULL)
218 if ((v2 = (
char *)cpl_frame_get_tag(frame2)) == NULL)
258 cpl_size *labels, cpl_size nlab,
261 cpl_frameset *cur_set,*ret_set;
262 cpl_frame *cur_frame;
266 for (i = 0; i < nlab; i++) {
267 cur_set = cpl_frameset_extract(frameset,labels,i);
270 cur_frame = cpl_frameset_get_frame(cur_set,0);
271 cur_tag = (
char *)cpl_frame_get_tag(cur_frame);
272 if (!strcmp(cur_tag,tag)) {
276 cpl_frameset_delete(cur_set);
311 else if (inexten == 0) {
357 maxextension = cpl_fits_count_extensions(cpl_frame_get_filename(frame));
358 if(maxextension==32){
359 *out2 = maxextension;
362 cpl_msg_warning(cpl_func,
"Number of extension is %d and not 32. "
363 "The pipeline will still try to reduce all 32 "
364 "expected extensions", maxextension);
368 else if (inexten == 0) {
405 int ext, cpl_stats **diffstats)
408 cpl_image *refimage = NULL;
409 cpl_image *diff = NULL;
411 if ((master == NULL) || (refframe == NULL)){
412 cpl_msg_warning(cpl_func,
"NULL input");
416 refimage = cpl_image_load(cpl_frame_get_filename(refframe),CPL_TYPE_FLOAT,0, ext);
417 if (refimage == NULL){
418 cpl_msg_warning(cpl_func,
"Cannot load reference image");
422 diff = cpl_image_subtract_create(master, refimage);
423 *diffstats = cpl_stats_new_from_image(diff,CPL_STATS_ALL);
424 if (*diffstats == NULL){
427 cpl_msg_warning(cpl_func,
"Cannot calculate statistics of difference image");
460 static char path[MAXNAMESZ];
463 if (strlen(filename)>MAXNAMESZ)
return NULL ;
464 strcpy(path, filename);
466 last_slash = path != NULL ? strrchr (path,
'/') : NULL;
468 if (last_slash == path)
472 else if (last_slash != NULL && last_slash[1] ==
'\0')
474 last_slash = memchr (path, last_slash - path,
'/');
476 if (last_slash != NULL)
478 last_slash[0] =
'\0';
503 if ((filename == NULL) || (tag == NULL)){
507 frame = cpl_frame_new();
508 cpl_frame_set_filename(frame, filename);
509 cpl_frame_set_tag(frame, tag);
510 cpl_frame_set_type(frame, type);
511 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
512 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
534 cpl_type_bpp bitpix,
const char *outfile,
const cpl_propertylist *plist,
535 const char *recipe, cpl_frame *product_frame)
539 const char delkeys[] =
"^(ORIGIN|TELESCOPE|INSTRUME|OBJECT|RA|DEC|EPOCH|EQUINOX|RADECSYS|DATE-OBS|" \
540 "MJD-OBS|UTC|LST|PI-COI|OBSERVER|PIPEFILE)$";
541 cpl_image *image = NULL;
542 cpl_table *table = NULL;
543 cpl_propertylist *list = NULL;
548 list = cpl_propertylist_duplicate(plist);
550 if (cpl_dfs_setup_product_header(list, product_frame,
set, parlist,
551 recipe,PIPEID,DICID,NULL) != CPL_ERROR_NONE) {
553 cpl_msg_warning(cpl_func,
"Problem in the extension header of product DFS-compliance") ;
557 cpl_propertylist_erase_regexp(list, delkeys, 0);
560 if(type == CPL_FRAME_TYPE_IMAGE){
561 image = cpl_image_new(naxis1, naxis2, CPL_TYPE_FLOAT);
562 if (cpl_image_save(image,outfile,bitpix,list,
563 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
564 cpl_msg_error(cpl_func,
"Cannot save product %s", cpl_error_get_message());
572 table = cpl_table_new(1);
573 if (cpl_table_save(table, NULL, list,outfile, CPL_IO_EXTEND) != CPL_ERROR_NONE) {
574 cpl_msg_error(cpl_func,
"Cannot save the extension table");
602 const char *outfile,
const char *recipe,
const cpl_propertylist *plist,
603 cpl_frame *product_frame,
const cpl_frame *inherit)
606 cpl_propertylist *pplist;
610 pplist = cpl_propertylist_new();
612 pplist = cpl_propertylist_duplicate(plist);
615 if (cpl_dfs_setup_product_header(pplist, product_frame,
set, parlist,
616 recipe,PIPEID,DICID,inherit) != CPL_ERROR_NONE) {
618 cpl_msg_debug(cpl_func,
"Problem in the main header of product DFS-compliance") ;
625 cpl_propertylist_erase_regexp(pplist,REM_PRIM_KEYS,0);
628 if (cpl_propertylist_save(pplist,outfile,CPL_IO_DEFAULT) != CPL_ERROR_NONE){
634 cpl_frameset_insert(
set,product_frame);
662 nx = cpl_image_get_size_x(in);
663 ny = cpl_image_get_size_y(in);
664 type = cpl_image_get_type(in);
666 kernel = cpl_matrix_new(size, size);
669 cpl_matrix_delete(kernel);
673 mask = cpl_mask_new(size, size);
674 for (i = 0; i < size; ++i) {
676 for (j = 0; j < size; j++)
678 cpl_mask_set(mask, i + 1, j + 1, CPL_BINARY_1);
683 out = cpl_image_new(nx, ny, type);
684 if(cpl_image_filter_mask(out, in, mask, CPL_FILTER_AVERAGE, CPL_BORDER_FILTER)
686 cpl_msg_debug(cpl_func,
"Cannot filter image. %s", cpl_error_get_message());
714 const char *end =
"fits";
716 if(instrument == NULL || name == NULL){
722 sprintf(outname,
"%s_%s.%s", instrument, name, suffix);
740 if((image == NULL) || (bias == NULL))
744 if(cpl_image_subtract(image, bias) != CPL_ERROR_NONE){
769 chipid =
"ESO_CCD_#65";
772 chipid =
"ESO_CCD_#66";
775 chipid =
"ESO_CCD_#67";
778 chipid =
"ESO_CCD_#68";
781 chipid =
"ESO_CCD_#73";
784 chipid =
"ESO_CCD_#74";
787 chipid =
"ESO_CCD_#75";
790 chipid =
"ESO_CCD_#76";
793 chipid =
"ESO_CCD_#81";
796 chipid =
"ESO_CCD_#82";
799 chipid =
"ESO_CCD_#83";
802 chipid =
"ESO_CCD_#84";
805 chipid =
"ESO_CCD_#89";
808 chipid =
"ESO_CCD_#90";
811 chipid =
"ESO_CCD_#91";
814 chipid =
"ESO_CCD_#92";
817 chipid =
"ESO_CCD_#69";
820 chipid =
"ESO_CCD_#70";
823 chipid =
"ESO_CCD_#71";
826 chipid =
"ESO_CCD_#72";
829 chipid =
"ESO_CCD_#77";
832 chipid =
"ESO_CCD_#78";
835 chipid =
"ESO_CCD_#79";
838 chipid =
"ESO_CCD_#80";
841 chipid =
"ESO_CCD_#85";
844 chipid =
"ESO_CCD_#86";
847 chipid =
"ESO_CCD_#87";
850 chipid =
"ESO_CCD_#88";
853 chipid =
"ESO_CCD_#93";
856 chipid =
"ESO_CCD_#94";
859 chipid =
"ESO_CCD_#95";
862 chipid =
"ESO_CCD_#96";
865 cpl_msg_warning(cpl_func,
"Unable to find CHIP ID for extension %d",ext);
887 if(((ext >= 1) && (ext <= 8)) || ((ext >=17) && (ext <= 24)))
889 else if(((ext >= 9) && (ext <=16)) || ((ext >= 25) && (ext <= 32)))
892 cpl_msg_warning(cpl_func,
"Unable to get chip orientation. Using default 1");
917 cpl_propertylist *alist,
const cpl_propertylist *qclist,cpl_type_bpp bpp,
918 const char *name,
const char *recipe, cpl_frame *frame,
919 const cpl_frame *inherit,
int isfirst)
922 cpl_propertylist *plist;
924 if((img == NULL) || (
set == NULL) || (pars == NULL) ||
925 (name == NULL) || (frame == NULL))
931 cpl_msg_error(cpl_func,
"Cannot save primary header of product");
937 plist = cpl_propertylist_new();
943 if (cpl_dfs_setup_product_header(plist, frame,
set, pars,
944 recipe,PIPEID,DICID,inherit) != CPL_ERROR_NONE) {
945 cpl_msg_warning(cpl_func,
"Unable to setup extension header of %s. %s",name,
946 cpl_error_get_message()) ;
950 cpl_propertylist_erase_regexp(plist, REM_EXT_KEYS, 0);
954 cpl_propertylist_append(plist, qclist);
957 if (cpl_image_save(img,name,bpp,plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
958 cpl_msg_error(cpl_func,
"Cannot save product. %s", cpl_error_get_message());
988 cpl_propertylist *alist,
const cpl_propertylist *qclist,
const char *name,
989 const char *recipe, cpl_frame *frame,
const cpl_frame *inherit,
int isfirst)
992 cpl_propertylist *plist;
994 if((tbl == NULL) || (
set == NULL) || (pars == NULL) ||
995 (name == NULL) || (frame == NULL))
1001 cpl_msg_error(cpl_func,
"Cannot save primary header of product");
1007 plist = cpl_propertylist_new();
1013 if (cpl_dfs_setup_product_header(plist, frame,
set, pars,
1014 recipe,PIPEID,DICID,inherit) != CPL_ERROR_NONE) {
1016 cpl_msg_warning(cpl_func,
"Unable to setup extension header of %s. %s",name,
1017 cpl_error_get_message()) ;
1021 cpl_propertylist_erase_regexp(plist, REM_EXT_KEYS, 0);
1025 cpl_propertylist_append(plist, qclist);
1028 if (cpl_table_save(tbl,NULL,plist,name,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
1029 cpl_msg_error(cpl_func,
"Cannot save product. %s", cpl_error_get_message());
1057 const cpl_propertylist *qclist, cpl_type_bpp bpp,
const char *name,
1058 const char *recipe,cpl_frame *product_frame,
const cpl_frame *inherit,
1062 cpl_propertylist *plist;
1064 if((scifits == NULL) || (
set == NULL) || (pars == NULL) ||
1065 (name == NULL) || (product_frame == NULL))
1074 cpl_msg_error(cpl_func,
"Cannot save primary header of product");
1086 if (cpl_dfs_setup_product_header(plist, product_frame,
set, pars,
1087 recipe,PIPEID,DICID,inherit) != CPL_ERROR_NONE) {
1089 cpl_msg_warning(cpl_func,
"Unable to setup extension header of %s. %s",name,
1090 cpl_error_get_message()) ;
1094 cpl_propertylist_erase_regexp(plist, REM_EXT_KEYS, 0);
1098 cpl_propertylist_append(plist, qclist);
1102 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
1103 cpl_msg_error(cpl_func,
"Cannot save product %s. %s", name, cpl_error_get_message());
1113 cpl_error_code omega_get_pixelscale(cpl_propertylist *plist,
1114 double * pixscale_x,
double * pixscale_y){
1121 if(cpl_propertylist_has(plist,
"CD1_1") &&
1122 cpl_propertylist_has(plist,
"CD1_2") &&
1123 cpl_propertylist_has(plist,
"CD2_1") &&
1124 cpl_propertylist_has(plist,
"CD2_2")){
1125 cd11= cpl_propertylist_get_double(plist,
"CD1_1");
1126 cd12= cpl_propertylist_get_double(plist,
"CD1_2");
1127 cd21= cpl_propertylist_get_double(plist,
"CD2_1");
1128 cd22= cpl_propertylist_get_double(plist,
"CD2_2");
1131 cpl_error_set_message(cpl_func,CPL_ERROR_DATA_NOT_FOUND,
"CD matrix not"
1132 " complete. Pixel scale can not be computed");
1133 return cpl_error_get_code();
1136 *pixscale_x = sqrt (cd11*cd11 + cd21*cd21);
1137 *pixscale_y = sqrt (cd12*cd12 + cd22*cd22);
1139 cpl_msg_debug(cpl_func,
"Pixelscale "
1140 "x: %g y: %g",*pixscale_x, *pixscale_y);
1142 return cpl_error_get_code();
1158 cpl_propertylist * wcslist2,
double * xshift,
double * yshift){
1160 cpl_wcs * wcs1=NULL;
1161 cpl_wcs * wcs2=NULL;
1163 wcs1=cpl_wcs_new_from_propertylist(wcslist1);
1164 wcs2=cpl_wcs_new_from_propertylist(wcslist2);
1166 if(wcs1 == NULL || wcs2 == NULL )
1168 cpl_msg_warning(cpl_func,
"WCS informations missing - assuming no shift "
1169 "of the x and y coordinate");
1172 cpl_msg_debug(cpl_func,
"Coordinateshift: xshif=%g, yshift=%g",
1174 cpl_wcs_delete(wcs1);
1175 cpl_wcs_delete(wcs2);
1176 return cpl_error_get_code();
1179 *xshift=cpl_array_get(cpl_wcs_get_crpix(wcs1),0,NULL) -
1180 cpl_array_get(cpl_wcs_get_crpix(wcs2),0,NULL);
1181 *yshift=cpl_array_get(cpl_wcs_get_crpix(wcs1),1,NULL) -
1182 cpl_array_get(cpl_wcs_get_crpix(wcs2),1,NULL);
1185 cpl_wcs_delete(wcs1);
1186 cpl_wcs_delete(wcs2);
1188 cpl_msg_debug(cpl_func,
"Coordinateshift: xshif=%g, yshift=%g",
1191 return cpl_error_get_code();