00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022
00023 #include <cpl.h>
00024 #include "sinfo_pro_save.h"
00025 #include "sinfo_key_names.h"
00026 #include "sinfo_functions.h"
00027 #include "sinfo_utilities.h"
00028 #include "sinfo_globals.h"
00029 #include <irplib_error.h>
00030
00031 static void
00032 sinfo_log_pro(char* name_o, const char* pro_catg, int frm_type,
00033 cpl_frameset** set, cpl_propertylist** plist,
00034 cpl_parameterlist* parlist, const char* recid);
00035
00036
00037 static void
00038 sinfo_check_name(const char* in, char** ou, int type, char** paf);
00039
00040 static void
00041 sinfo_clean_header(cpl_propertylist** header);
00042 static void
00043 sinfo_clean_cube_header(cpl_propertylist** header);
00044
00045
00046 static FILE *
00047 sinfo_paf_print_header(
00048 const char * filename,
00049 const char * paf_id,
00050 const char * paf_desc,
00051 const char * login_name,
00052 char * datetime) ;
00053
00054
00055
00062
00087
00088 static FILE *
00089 sinfo_paf_print_header(
00090 const char * filename,
00091 const char * paf_id,
00092 const char * paf_desc,
00093 const char * login_name,
00094 char * datetime)
00095 {
00096 FILE * paf ;
00097
00098 if ((paf=fopen(filename, "w"))==NULL) {
00099 sinfo_msg_error("cannot create PAF file [%s]", filename);
00100 return NULL ;
00101 }
00102 fprintf(paf, "PAF.HDR.START ;# start of header\n");
00103 fprintf(paf, "PAF.TYPE \"pipeline product\" ;\n");
00104 fprintf(paf, "PAF.ID \"%s\"\n", paf_id);
00105 fprintf(paf, "PAF.NAME \"%s\"\n", filename);
00106 fprintf(paf, "PAF.DESC \"%s\"\n", paf_desc);
00107 fprintf(paf, "PAF.CRTE.NAME \"%s\"\n", login_name) ;
00108 fprintf(paf, "PAF.CRTE.DAYTIM \"%s\"\n", datetime) ;
00109 fprintf(paf, "PAF.LCHG.NAME \"%s\"\n", login_name) ;
00110 fprintf(paf, "PAF.LCHG.DAYTIM \"%s\"\n", datetime) ;
00111 fprintf(paf, "PAF.CHCK.CHECKSUM \"\"\n");
00112 fprintf(paf, "PAF.HDR.END ;# end of header\n");
00113 fprintf(paf, "\n");
00114 return paf ;
00115 }
00125 int
00126 sinfo_update_fits_card_int(const char* file,const char* card,int value)
00127 {
00128 cpl_propertylist * plist =NULL;
00129 if ((plist = cpl_propertylist_load(file, 0)) == NULL) {
00130 sinfo_msg_error( "getting header from file %s",file);
00131 cpl_propertylist_delete(plist) ;
00132 return -1 ;
00133 }
00134
00135 if (CPL_ERROR_NONE!=cpl_propertylist_set_int(plist,card,value)){
00136 sinfo_msg_error( "setting header of file %s",file);
00137 cpl_propertylist_delete(plist) ;
00138 return -1 ;
00139 }
00140 cpl_propertylist_delete(plist) ;
00141 return 0;
00142 }
00143
00153 int
00154 sinfo_update_fits_card_float(const char* file,const char* card,float value)
00155 {
00156 cpl_propertylist * plist =NULL;
00157 if ((plist = cpl_propertylist_load(file, 0)) == NULL) {
00158 sinfo_msg_error( "getting header from file %s",file);
00159 sinfo_msg_error((char* ) cpl_error_get_message());
00160 sinfo_msg_error((char* ) cpl_error_get_where());
00161 cpl_propertylist_delete(plist) ;
00162 return -1 ;
00163 }
00164
00165 if (CPL_ERROR_NONE!=cpl_propertylist_set_float(plist,card,value)){
00166 sinfo_msg_error( "setting header of file %s",file);
00167 sinfo_msg_error((char* ) cpl_error_get_message());
00168 sinfo_msg_error((char* ) cpl_error_get_where());
00169 cpl_propertylist_delete(plist) ;
00170 return -1 ;
00171 }
00172 cpl_propertylist_delete(plist) ;
00173 return 0;
00174 }
00175
00176
00186 int
00187 sinfo_update_fits_card_double(const char* file,const char* card,double value)
00188 {
00189 cpl_propertylist * plist =NULL;
00190 if ((plist = cpl_propertylist_load(file, 0)) == NULL) {
00191 sinfo_msg_error( "getting header from file %s",file);
00192 cpl_propertylist_delete(plist) ;
00193 return -1 ;
00194 }
00195
00196 if (CPL_ERROR_NONE!=cpl_propertylist_set_double(plist,card,value)){
00197 sinfo_msg_error( "gsetting header of file %s",file);
00198 cpl_propertylist_delete(plist) ;
00199 return -1 ;
00200 }
00201 cpl_propertylist_delete(plist) ;
00202 return 0;
00203 }
00204
00214 int
00215 sinfo_update_fits_card_long(const char* file,const char* card,long value)
00216 {
00217 cpl_propertylist * plist =NULL;
00218 if ((plist = cpl_propertylist_load(file, 0)) == NULL) {
00219 sinfo_msg_error( "getting header from file %s",file);
00220 cpl_propertylist_delete(plist) ;
00221 return -1 ;
00222 }
00223 if (CPL_ERROR_NONE!=cpl_propertylist_set_long(plist,card,value)){
00224 sinfo_msg_error( "setting header of file %s",file);
00225 cpl_propertylist_delete(plist) ;
00226 return -1 ;
00227 }
00228 cpl_propertylist_delete(plist) ;
00229 return 0;
00230 }
00231
00241 int
00242 sinfo_update_ims_fits_card_string(cpl_imagelist* iml,
00243 const char* file,
00244 const char* card,
00245 const char* value)
00246 {
00247
00248 cpl_propertylist * plist =NULL;
00249 if ((plist = cpl_propertylist_load(file, 0)) == NULL) {
00250 sinfo_msg_error( "getting header from reference ima frame %s",file);
00251 cpl_propertylist_delete(plist) ;
00252 return -1 ;
00253 }
00254
00255
00256 if (CPL_ERROR_NONE!=cpl_propertylist_set_string(plist,card,value)){
00257 sinfo_msg_error( "getting header from reference ima frame %s",file);
00258 cpl_propertylist_delete(plist) ;
00259 return -1 ;
00260 }
00261
00262 if (cpl_imagelist_save(iml,file,CPL_BPP_DEFAULT,
00263 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00264 sinfo_msg_error( "getting header from reference ima frame %s",file);
00265 cpl_propertylist_delete(plist) ;
00266 }
00267 cpl_propertylist_delete(plist) ;
00268 return 0;
00269 }
00270
00271 static int sinfo_save_paf(char* name_p,
00272 const char* rec_id,
00273 cpl_table* qclog,
00274 cpl_propertylist* plist,
00275 const char* pro_catg)
00276 {
00277
00278
00279
00280 FILE * paf ;
00281 const char * sval ;
00282 char key_name[FILE_NAME_SZ] ;
00283 char key_paf[FILE_NAME_SZ] ;
00284 char key_dpaf[FILE_NAME_SZ] ;
00285 char key_type[FILE_NAME_SZ] ;
00286 char key_value[FILE_NAME_SZ] ;
00287 double dval=0;
00288
00289 int i =0;
00290 int n=0;
00291 sinfo_msg( "Writing %s" , name_p) ;
00292
00293 if ((paf = sinfo_paf_print_header(name_p,rec_id,"QC file","login-name",
00294 sinfo_get_datetime_iso8601())) == NULL) {
00295 sinfo_msg_error( "cannot open file [%s] for output", name_p) ;
00296 return -1 ;
00297 }
00298 if (sinfo_check_rec_status(0) == -1) {
00299 sinfo_msg_error( "Something was wrong") ;
00300 return -1 ;
00301 }
00302
00303
00304 sinfo_blank2dot(PAF_NAME_PIPE_ID,key_dpaf);
00305 fprintf(paf,"%-21s \"%s\" ;# %s\" \n", key_dpaf,
00306 VERSION,KEY_HELP_PIPE_ID);
00307
00308 strcpy(key_name,KEY_NAME_PIPEFILE);
00309 strcpy(key_paf,KEY_NAME_PIPEFILE);
00310 sinfo_blank2dot(key_paf,key_dpaf);
00311 if (cpl_propertylist_contains(plist, key_name)) {
00312 fprintf(paf,"%-21s \"%s\" ;# %s\" \n",key_dpaf,
00313 cpl_propertylist_get_string(plist,key_name),KEY_HELP_PIPEFILE);
00314 }
00315
00316
00317 strcpy(key_name,KEY_NAME_PRO_TYPE);
00318 strcpy(key_paf,PAF_NAME_PRO_TYPE);
00319 sinfo_blank2dot(key_paf,key_dpaf);
00320 if (cpl_propertylist_contains(plist, key_name)) {
00321 fprintf(paf,"%-21s \"%s\" ;# %s\" \n", key_dpaf,
00322 cpl_propertylist_get_string(plist,key_name),KEY_HELP_PRO_TYPE);
00323 }
00324
00325 strcpy(key_name,KEY_NAME_PRO_DATANCOM);
00326 strcpy(key_paf,PAF_NAME_PRO_DATANCOM);
00327 sinfo_blank2dot(key_paf,key_dpaf);
00328 if (cpl_propertylist_contains(plist,key_name)) {
00329 fprintf(paf,"%-21s %d ;# %s\" \n",key_dpaf,
00330 cpl_propertylist_get_int(plist,key_name),KEY_HELP_PRO_DATANCOM);
00331 }
00332
00333 strcpy(key_name,KEY_NAME_NCORRS_NAME);
00334 strcpy(key_paf,PAF_NAME_NCORRS_NAME);
00335 sinfo_blank2dot(key_paf,key_dpaf);
00336 if (cpl_propertylist_contains(plist, key_name)) {
00337 fprintf(paf,"%-21s \"%s\" ;# %s \n", key_dpaf,
00338 cpl_propertylist_get_string(plist,key_name),KEY_HELP_NCORRS_NAME);
00339 }
00340
00341 strcpy(key_name,KEY_NAME_DET_NDSAMPLES);
00342 strcpy(key_paf,PAF_NAME_DET_NDSAMPLES);
00343 sinfo_blank2dot(key_paf,key_dpaf);
00344 if (cpl_propertylist_contains(plist, key_name)) {
00345 fprintf(paf,"%-21s %d ;# %s \n",key_dpaf,
00346 cpl_propertylist_get_int(plist,key_name),KEY_HELP_DET_NDSAMPLES);
00347 }
00348
00349 strcpy(key_name,KEY_NAME_FILT_NAME);
00350 strcpy(key_paf,PAF_NAME_FILT_NAME);
00351 sinfo_blank2dot(key_paf,key_dpaf);
00352 if (cpl_propertylist_contains(plist, key_name)) {
00353 fprintf(paf,"%-21s \"%s\" ;# %s \n", key_dpaf,
00354 cpl_propertylist_get_string(plist,key_name),KEY_HELP_FILT_NAME);
00355 }
00356
00357 strcpy(key_name,KEY_NAME_FILT_ID);
00358 strcpy(key_paf,PAF_NAME_FILT_ID);
00359 sinfo_blank2dot(key_paf,key_dpaf);
00360 if (cpl_propertylist_contains(plist, key_name)) {
00361 fprintf(paf,"%-21s \"%s\" ;# %s \n",key_dpaf,
00362 cpl_propertylist_get_string(plist,key_name),KEY_HELP_FILT_ID);
00363 }
00364
00365 strcpy(key_name,KEY_NAME_PREOPTICS);
00366 strcpy(key_paf,PAF_NAME_PREOPTICS);
00367 sinfo_blank2dot(key_paf,key_dpaf);
00368 if (cpl_propertylist_contains(plist, key_name)) {
00369 fprintf(paf,"%-21s \"%s\" ;# %s \n",key_dpaf,
00370 cpl_propertylist_get_string(plist,key_name),KEY_HELP_PREOPTICS);
00371 }
00372
00373 strcpy(key_name,KEY_NAME_GRAT_NAME);
00374 strcpy(key_paf,PAF_NAME_GRAT_NAME);
00375 sinfo_blank2dot(key_paf,key_dpaf);
00376 if (cpl_propertylist_contains(plist, key_name)) {
00377 fprintf(paf, "%-21s \"%s\" ;# %s \n",key_dpaf,
00378 cpl_propertylist_get_string(plist,key_name),KEY_HELP_GRAT_NAME);
00379 }
00380
00381 strcpy(key_name,KEY_NAME_GRAT_WLEN);
00382 strcpy(key_paf,PAF_NAME_GRAT_WLEN);
00383 sinfo_blank2dot(key_paf,key_dpaf);
00384 if (cpl_propertylist_contains(plist, key_name)) {
00385 fprintf(paf, "%-21s %f ;# %s \n", key_dpaf,
00386 cpl_propertylist_get_double(plist,key_name),KEY_HELP_GRAT_WLEN);
00387 }
00388
00389 strcpy(key_name,KEY_NAME_PRO_RECID);
00390 strcpy(key_paf,PAF_NAME_PRO_RECID);
00391 sinfo_blank2dot(key_paf,key_dpaf);
00392 if (cpl_propertylist_contains(plist, key_name)) {
00393 fprintf(paf,"%-21s \"%s\" ;# %s \n", key_dpaf,
00394 cpl_propertylist_get_string(plist,key_name),KEY_HELP_PRO_RECID);
00395 }
00396
00397
00398
00399 strcpy(key_name,KEY_NAME_PRO_DRSID);
00400 strcpy(key_paf,PAF_NAME_PRO_DRSID);
00401 sinfo_blank2dot(key_paf,key_dpaf);
00402 if (cpl_propertylist_contains(plist, key_name)) {
00403 fprintf(paf,"%-21s \"%s\" ;# %s \n",key_dpaf,
00404 cpl_propertylist_get_string(plist,key_name),KEY_HELP_PRO_DRSID);
00405 }
00406
00407 if (cpl_propertylist_contains(plist,KEY_NAME_DATE_OBS)) {
00408 sval = sinfo_pfits_get_date_obs(plist);
00409 strcpy(key_paf,KEY_NAME_DATE_OBS);
00410 sinfo_blank2dot(key_paf,key_dpaf);
00411 fprintf(paf, "%-21s \"%s\" ;# %s\n",key_dpaf,
00412 sval,KEY_HELP_DATE_OBS) ;
00413 }
00414
00415
00416 if (cpl_propertylist_contains(plist,KEY_NAME_TEL_AIRM_START)) {
00417 dval = sinfo_pfits_get_airmass_start(plist);
00418 strcpy(key_paf,PAF_NAME_TEL_AIRM_START);
00419 sinfo_blank2dot(key_paf,key_dpaf);
00420 fprintf(paf, "%-21s \"%f\" ;# %s \n",key_dpaf,
00421 dval,KEY_HELP_TEL_AIRM_START) ;
00422 }
00423
00424 if (cpl_propertylist_contains(plist,KEY_NAME_ARCFILE)) {
00425 sval = sinfo_pfits_get_arcfile(plist);
00426 strcpy(key_paf,KEY_NAME_ARCFILE);
00427 sinfo_blank2dot(key_paf,key_dpaf);
00428 fprintf(paf, "%-21s \"%s\" ;# %s \n", key_dpaf,sval,KEY_HELP_ARCFILE) ;
00429
00430 } else if (cpl_propertylist_contains(plist,KEY_NAME_PRO_REC1_RAW1_NAME)) {
00431 sval = sinfo_pfits_get_rec1raw1name(plist);
00432 strcpy(key_paf,KEY_NAME_ARCFILE);
00433 sinfo_blank2dot(key_paf,key_dpaf);
00434 fprintf(paf, "%-21s \"%s\" ;# %s \n", key_dpaf,sval,KEY_HELP_ARCFILE) ;
00435 } else {
00436 sinfo_msg_error("%s is missing QC LOG will fail!",KEY_NAME_ARCFILE);
00437 }
00438
00439 if (cpl_propertylist_contains(plist,KEY_NAME_TPL_ID)) {
00440 sval = sinfo_pfits_get_templateid(plist);
00441 strcpy(key_paf,PAF_NAME_TPL_ID);
00442 sinfo_blank2dot(key_paf,key_dpaf);
00443 fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
00444 sval,KEY_HELP_TPL_ID) ;
00445
00446 }
00447
00448 if (cpl_propertylist_contains(plist,KEY_NAME_DET_DIT)) {
00449 strcpy(key_paf,PAF_NAME_DET_DIT);
00450 sinfo_blank2dot(key_paf,key_dpaf);
00451 fprintf(paf,"%-21s %f ; # %s\n", key_dpaf,
00452 sinfo_pfits_get_dit(plist),KEY_HELP_DET_DIT) ;
00453 }
00454
00455
00456 if (cpl_propertylist_contains(plist,KEY_NAME_DET_NDIT)) {
00457 strcpy(key_paf,PAF_NAME_DET_NDIT);
00458 sinfo_blank2dot(key_paf,key_dpaf);
00459 fprintf(paf,"%-21s %d ; # %s\n", key_dpaf,
00460 sinfo_pfits_get_ndit(plist),KEY_HELP_DET_NDIT) ;
00461 }
00462
00463 if (cpl_propertylist_contains(plist,KEY_NAME_NCORRS_NAME)) {
00464 sval = sinfo_pfits_get_ncorrs_name(plist);
00465 strcpy(key_paf,PAF_NAME_NCORRS_NAME);
00466 sinfo_blank2dot(key_paf,key_dpaf);
00467
00468 fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
00469 sval, KEY_HELP_NCORRS_NAME) ;
00470 }
00471
00472 if (cpl_propertylist_contains(plist,KEY_NAME_DPR_TYPE)) {
00473 sval = sinfo_pfits_get_dpr_type(plist);
00474 strcpy(key_paf,PAF_NAME_DPR_TYPE);
00475 sinfo_blank2dot(key_paf,key_dpaf);
00476 fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
00477 sval, KEY_HELP_DPR_TYPE) ;
00478 }
00479
00480 if (cpl_propertylist_contains(plist,KEY_NAME_DPR_TECH)) {
00481 sval = sinfo_pfits_get_dpr_tech(plist);
00482 strcpy(key_paf,PAF_NAME_DPR_TECH);
00483 sinfo_blank2dot(key_paf,key_dpaf);
00484 fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
00485 sval, KEY_HELP_DPR_TECH) ;
00486 }
00487
00488
00489 if (cpl_propertylist_contains(plist,KEY_NAME_DPR_CATG)) {
00490 sval = sinfo_pfits_get_dpr_catg(plist);
00491 strcpy(key_paf,PAF_NAME_DPR_CATG);
00492 sinfo_blank2dot(key_paf,key_dpaf);
00493 fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
00494 sval, KEY_HELP_DPR_CATG) ;
00495 }
00496
00497 strcpy(key_paf,PAF_NAME_PRO_CATG);
00498 sinfo_blank2dot(key_paf,key_dpaf);
00499 fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
00500 pro_catg, KEY_HELP_PRO_CATG) ;
00501
00502 if (cpl_propertylist_contains(plist,KEY_NAME_INS_SETUP)) {
00503 sval = sinfo_pfits_get_ins_setup(plist);
00504 strcpy(key_paf,PAF_NAME_INS_SETUP);
00505 sinfo_blank2dot(key_paf,key_dpaf);
00506 fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
00507 sval, KEY_HELP_INS_SETUP) ;
00508 }
00509
00510 n=cpl_table_get_nrow(qclog);
00511 for(i=0;i<n;i++) {
00512 strcpy(key_paf,cpl_table_get_string(qclog,"key_name",i));
00513 sinfo_blank2dot(key_paf,key_name);
00514 strcpy(key_type,cpl_table_get_string(qclog,"key_type",i));
00515 strcpy(key_value,cpl_table_get_string(qclog,"key_value",i));
00516 if(strcmp(key_type,"CPL_TYPE_STRING") == 0) {
00517 strcat(key_name," \"%s\"\n");
00518 fprintf(paf, key_name, key_value) ;
00519 } else if(strcmp(key_type,"CPL_TYPE_BOOL") == 0) {
00520 strcat(key_name," ");
00521 strcat(key_name,"%c\n");
00522 fprintf(paf, key_name, atoi(key_value)) ;
00523 } else if(strcmp(key_type,"CPL_TYPE_INT") == 0) {
00524 strcat(key_name," ");
00525 strcat(key_name,"%d\n");
00526 fprintf(paf, key_name, atoi(key_value)) ;
00527 } else if(strcmp(key_type,"CPL_TYPE_FLOAT") == 0) {
00528 strcat(key_name," ");
00529 strcat(key_name,"%g\n");
00530 fprintf(paf, key_name, (float) atof(key_value)) ;
00531 } else if(strcmp(key_type,"CPL_TYPE_DOUBLE") == 0) {
00532 strcat(key_name," ");
00533 strcat(key_name,"%g\n");
00534 fprintf(paf, key_name, atof(key_value)) ;
00535 }
00536
00537 }
00538 fprintf(paf, "\n");
00539 fclose(paf) ;
00540
00541 if (sinfo_check_rec_status(1) == -1) {
00542 sinfo_msg_error( "Something was wrong reading FITS keys") ;
00543 return -1 ;
00544 }
00545 return 0;
00546
00547 }
00548
00549
00550
00551
00559
00560
00561
00562 int sinfo_pro_save_ima(
00563 cpl_image * ima,
00564 cpl_frameset * ref,
00565 cpl_frameset * set,
00566 const char * out_file,
00567 const char * pro_catg,
00568 cpl_table * qclog,
00569 const char * recid,
00570 cpl_parameterlist* parlist)
00571
00572 {
00573 char * name_o ;
00574 char * name_p ;
00575
00576 cpl_propertylist * plist =NULL;
00577 cpl_frame * first_frame=NULL;
00578 char * ref_file=NULL;
00579
00580
00581 first_frame = cpl_frameset_get_frame(ref, 0) ;
00582 ref_file = (char*) cpl_strdup(cpl_frame_get_filename(first_frame)) ;
00583
00584 name_o = cpl_malloc(FILE_NAME_SZ * sizeof(char));
00585 name_p = cpl_malloc(FILE_NAME_SZ * sizeof(char));
00586 sinfo_check_name(out_file, &name_o, CPL_FRAME_TYPE_IMAGE, &name_p);
00587 sinfo_msg( "Writing ima %s pro catg %s" , name_o, pro_catg) ;
00588
00589
00590 if ((cpl_error_code)((plist=cpl_propertylist_load(ref_file, 0)) == NULL)) {
00591 sinfo_msg_error( "getting header from reference ima frame %s",ref_file);
00592 cpl_propertylist_delete(plist) ;
00593 cpl_free(ref_file);
00594 return -1 ;
00595 }
00596
00597 sinfo_clean_header(&plist);
00598 if ( ( strstr(pro_catg,"MASTER_PSF") != NULL ) ||
00599 ( strstr(pro_catg,"STD_STAR_SPECTRUM") != NULL ) ||
00600 ( strstr(pro_catg,"STD_STAR_SPECTRA") != NULL ) ) {
00601 sinfo_clean_cube_header(&plist);
00602 }
00603
00604
00605 sinfo_log_pro(name_o, pro_catg, CPL_FRAME_TYPE_IMAGE,
00606 &set,&plist,parlist,recid);
00607 if(qclog != NULL) {
00608 sinfo_pfits_put_qc(plist, qclog) ;
00609 }
00610
00611
00612 if (cpl_image_save(ima, name_o, CPL_BPP_DEFAULT,
00613 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00614 sinfo_msg_error( "Cannot save the product %s",name_o);
00615 cpl_propertylist_delete(plist) ;
00616 cpl_free(ref_file);
00617 cpl_free(name_o);
00618 cpl_free(name_p);
00619 return -1 ;
00620 }
00621
00622
00623 if( qclog != NULL) {
00624 sinfo_save_paf(name_p,recid,qclog,plist,pro_catg);
00625 }
00626
00627 cpl_propertylist_delete(plist) ;
00628 cpl_msg_indent_less() ;
00629 cpl_free(name_o);
00630 cpl_free(name_p);
00631 cpl_free(ref_file);
00632
00633 return 0 ;
00634 }
00635
00636
00637
00645
00646
00647
00648
00649 int sinfo_pro_save_tbl(
00650 cpl_table * table,
00651 cpl_frameset * ref,
00652 cpl_frameset * set,
00653 const char * out_file,
00654 const char * pro_catg,
00655 cpl_table * qclog,
00656 const char * recid,
00657 cpl_parameterlist* parlist)
00658
00659 {
00660 char * name_o =NULL;
00661 char * name_p =NULL;
00662 cpl_propertylist * plist=NULL ;
00663 cpl_frame* first_frame=NULL;
00664 char* ref_file=NULL;
00665
00666 first_frame = cpl_frameset_get_frame(ref, 0) ;
00667 ref_file = cpl_strdup(cpl_frame_get_filename(first_frame)) ;
00668 name_o = cpl_malloc(FILE_NAME_SZ * sizeof(char));
00669 name_p = cpl_malloc(FILE_NAME_SZ * sizeof(char));
00670 sinfo_check_name(out_file, &name_o, CPL_FRAME_TYPE_TABLE, &name_p);
00671 sinfo_msg( "Writing tbl %s pro catg %s" , name_o, pro_catg) ;
00672
00673
00674 if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL))
00675 {
00676 sinfo_msg_error( "getting header from tbl frame %s",ref_file);
00677 cpl_propertylist_delete(plist) ;
00678 cpl_free(ref_file);
00679 cpl_free(name_o);
00680 cpl_free(name_p);
00681 return -1 ;
00682 }
00683 sinfo_clean_header(&plist);
00684
00685
00686 sinfo_log_pro(name_o, pro_catg, CPL_FRAME_TYPE_TABLE,
00687 &set,&plist,parlist,recid);
00688 if(qclog != NULL) {
00689 sinfo_pfits_put_qc(plist, qclog) ;
00690 }
00691
00692 if (cpl_table_save(table, plist, NULL, name_o, 0) != CPL_ERROR_NONE) {
00693 sinfo_msg_error( "Cannot save the product: %s", name_o);
00694 cpl_propertylist_delete(plist) ;
00695 cpl_free(ref_file);
00696 cpl_free(name_o);
00697 cpl_free(name_p);
00698 return -1 ;
00699 }
00700
00701
00702
00703 if (qclog != NULL) {
00704 sinfo_save_paf(name_p,recid,qclog,plist,pro_catg);
00705 }
00706
00707 cpl_propertylist_delete(plist) ;
00708 cpl_msg_indent_less() ;
00709 cpl_free(name_o);
00710 cpl_free(name_p);
00711 cpl_free(ref_file);
00712 return 0 ;
00713 }
00714
00715
00716
00724
00725
00726
00727
00728 int sinfo_pro_save_ims(
00729 cpl_imagelist * ims,
00730 cpl_frameset * ref,
00731 cpl_frameset * set,
00732 const char * out_file,
00733 const char * pro_catg,
00734 cpl_table * qclog,
00735 const char * recid,
00736 cpl_parameterlist* parlist)
00737
00738 {
00739 char * name_o=NULL;
00740 char * name_p=NULL;
00741
00742 cpl_propertylist * plist=NULL ;
00743 cpl_frame* first_frame=NULL;
00744 char* ref_file=NULL;
00745
00746
00747 first_frame = cpl_frameset_get_frame(ref, 0) ;
00748 ref_file = cpl_strdup(cpl_frame_get_filename(first_frame)) ;
00749
00750 name_o = cpl_malloc(FILE_NAME_SZ * sizeof(char));
00751 name_p = cpl_malloc(FILE_NAME_SZ * sizeof(char));
00752 sinfo_check_name(out_file, &name_o, CPL_FRAME_TYPE_IMAGE, &name_p);
00753 sinfo_msg( "Writing ims %s pro catg %s" , name_o, pro_catg) ;
00754
00755 if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL))
00756 {
00757 sinfo_msg_error( "getting header from ims frame %s",ref_file);
00758 cpl_propertylist_delete(plist) ;
00759 cpl_free(ref_file);
00760 cpl_free(name_o);
00761 cpl_free(name_p);
00762 return -1 ;
00763 }
00764 sinfo_clean_header(&plist);
00765 if ( ( strstr(pro_catg,"STD") != NULL ) ||
00766 ( strstr(pro_catg,"PSF") != NULL ) ||
00767 ( strstr(pro_catg,"OBJ") != NULL ) ) {
00768 sinfo_clean_cube_header(&plist);
00769 }
00770
00771
00772 sinfo_log_pro(name_o, pro_catg, CPL_FRAME_TYPE_IMAGE,
00773 &set,&plist,parlist,recid);
00774
00775 if(qclog != NULL) {
00776 sinfo_pfits_put_qc(plist, qclog) ;
00777 }
00778
00779
00780
00781 if (cpl_imagelist_save(ims, name_o, CPL_BPP_DEFAULT,
00782 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00783 sinfo_msg_error( "Cannot save the product %s",name_o);
00784 cpl_propertylist_delete(plist) ;
00785 cpl_free(ref_file);
00786 cpl_free(name_o);
00787 cpl_free(name_p);
00788 return -1 ;
00789 }
00790
00791
00792 if (qclog != NULL) {
00793 sinfo_save_paf(name_p,recid,qclog,plist,pro_catg);
00794 }
00795
00796 cpl_propertylist_delete(plist) ;
00797 cpl_msg_indent_less() ;
00798 cpl_free(name_o);
00799 cpl_free(name_p);
00800 cpl_free(ref_file);
00801 return 0 ;
00802 }
00803
00804
00805
00806 static void
00807 sinfo_log_pro(char* name_o, const char* pro_catg,
00808 int frm_type, cpl_frameset** set,
00809 cpl_propertylist** plist,cpl_parameterlist* parlist,
00810 const char* recid)
00811 {
00812 cpl_frame* product_frame = NULL ;
00813 char * pipe_id=NULL;
00814
00815 pipe_id = cpl_calloc(FILE_NAME_SZ,sizeof(char));
00816 snprintf(pipe_id,MAX_NAME_SIZE-1,"%s%s","sinfo/",PACKAGE_VERSION);
00817 product_frame = cpl_frame_new() ;
00818 cpl_frame_set_filename(product_frame, name_o) ;
00819 cpl_frame_set_tag(product_frame, pro_catg) ;
00820 cpl_frame_set_type(product_frame, frm_type);
00821 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
00822 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
00823
00824 if(cpl_dfs_setup_product_header(*plist,product_frame,*set,parlist,recid,
00825 pipe_id,KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00826 sinfo_msg_warning("Problem in the product DFS-compliance");
00827 sinfo_msg_warning((char* ) cpl_error_get_message());
00828 irplib_error_dump(CPL_MSG_ERROR,CPL_MSG_ERROR);
00829
00830 cpl_error_reset();
00831 }
00832
00833 cpl_frameset_insert(*set, product_frame);
00834 cpl_free(pipe_id);
00835
00836 }
00837
00838 static void
00839 sinfo_check_name(const char* in, char** ou, int type, char** paf) {
00840
00841 char* tmp=NULL;
00842 char name_b[512] ;
00843 if (strstr(in, "." ) != NULL ) {
00844 tmp = sinfo_new_get_rootname(in);
00845 strcpy(name_b,tmp);
00846 } else {
00847 snprintf(name_b, MAX_NAME_SIZE-1,in) ;
00848 }
00849 strcpy(*ou,name_b);
00850 if (type == CPL_FRAME_TYPE_TABLE) {
00851 strcat(*ou,".fits");
00852 } else {
00853 strcat(*ou,".fits");
00854 }
00855 strcpy(*paf,name_b);
00856 strcat(*paf,".paf");
00857
00858 }
00859
00860
00861 static void
00862 sinfo_clean_header(cpl_propertylist** header)
00863 {
00864 cpl_propertylist_erase_regexp(*header, "^ESO PRO .*",0);
00865
00866 }
00867
00868
00869 static void
00870 sinfo_clean_cube_header(cpl_propertylist** header)
00871 {
00872 cpl_propertylist_erase_regexp(*header, "^CRVAL*",0);
00873 cpl_propertylist_erase_regexp(*header, "^CRPIX*",0);
00874 cpl_propertylist_erase_regexp(*header, "^CTYPE*",0);
00875 cpl_propertylist_erase_regexp(*header, "^CUNIT*",0);
00876 cpl_propertylist_erase_regexp(*header, "^CD1_1",0);
00877 cpl_propertylist_erase_regexp(*header, "^CD1_2",0);
00878 cpl_propertylist_erase_regexp(*header, "^CD2_1",0);
00879 cpl_propertylist_erase_regexp(*header, "^CD2_2",0);
00880
00881 }