X-shooter Pipeline Reference Manual 3.8.15
xsh_molecfit_model.c
Go to the documentation of this file.
1/*
2 * This file is part of the ESO X-Shooter Pipeline
3 * Copyright (C) 2001-2018 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*----------------------------------------------------------------------------*/
24/*----------------------------------------------------------------------------*/
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28/* xshooter headers */
29#include <xsh_error.h>
30#include <xsh_utils.h>
31
32/* Molecfit Model */
33#include "xsh_molecfit_model.h"
34#include <mf_wrap_config.h>
35#include <telluriccorr.h>
36//#include <mf_spectrum.h>
37//#include <mf_wrap.h>
38/*----------------------------------------------------------------------------*/
42/*----------------------------------------------------------------------------*/
43
44/*---------------------------------------------------------------------------
45 Recipe Defines
46 ---------------------------------------------------------------------------*/
47
48#define MODEL_RECIPE_ID "xsh_molecfit_model"
49#define MODEL_RECIPE_AUTHOR "N. Fernando, B. Miszalski"
50#define MODEL_RECIPE_CONTACT "nuwanthika.fernando@partner.eso.org"
51
52/*---------------------------------------------------------------------------
53 Functions prototypes
54 ---------------------------------------------------------------------------*/
55
56/*
57 * Plugin initalization, execute and cleanup handlers
58 */
59
60int xsh_molecfit_model_create(cpl_plugin *);
61int xsh_molecfit_model_exec(cpl_plugin *);
62int xsh_molecfit_model_destroy(cpl_plugin *);
63
64/* The actual executor function */
65int xsh_molecfit_model(cpl_frameset *frameset, const cpl_parameterlist *parlist);
66
67/*----------------------------------------------------------------------------*/
71/*----------------------------------------------------------------------------*/
72
74"Applies molecfit_model to input spectra";
75
77"Applies molecfit_model to input spectra";
78
79/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84
85/*----------------------------------------------------------------------------*/
89/*----------------------------------------------------------------------------*/
90
91
92/*----------------------------------------------------------------------------*/
96/*----------------------------------------------------------------------------*/
97
98/*----------------------------------------------------------------------------*/
102/*----------------------------------------------------------------------------*/
103
106/*----------------------------------------------------------------------------*/
110/*----------------------------------------------------------------------------*/
111
112/*--------------------------------------------------------------------------*/
121/*--------------------------------------------------------------------------*/
122
123int cpl_plugin_get_info(cpl_pluginlist *list) {
124 cpl_recipe *recipe = NULL;
125 cpl_plugin *plugin = NULL;
126
127 recipe = cpl_calloc(1, sizeof(*recipe));
128 if ( recipe == NULL ){
129 return -1;
130 }
131
132 plugin = &recipe->interface ;
133
134 cpl_plugin_init(plugin,
135 CPL_PLUGIN_API, /* Plugin API */
136 XSH_BINARY_VERSION, /* Plugin version */
137 CPL_PLUGIN_TYPE_RECIPE, /* Plugin type */
138 MODEL_RECIPE_ID, /* Plugin name */
140 xsh_molecfit_model_description, /* Detailed help */
141 MODEL_RECIPE_AUTHOR, /* Author name */
142 MODEL_RECIPE_CONTACT, /* Contact address */
143 xsh_get_license(), /* Copyright */
147
148 cpl_pluginlist_append(list, plugin);
149
150 return (cpl_error_get_code() != CPL_ERROR_NONE);
151 }
152
153/*--------------------------------------------------------------------------*/
163/*--------------------------------------------------------------------------*/
164
165int xsh_molecfit_model_create(cpl_plugin *plugin){
166 cpl_recipe *recipe = NULL;
167 /*xsh_clipping_param detarc_clip_param = {2.0, 0, 0.7, 0, 0.3};
168 xsh_detect_arclines_param detarc_param =
169 {6, 3, 0, 5, 4, 1, 5, 5.0,
170 XSH_GAUSSIAN_METHOD, FALSE};
171 xsh_dispersol_param dispsol_param = { 4, 5 } ;
172 char paramname[256];
173 cpl_parameter* p=NULL;
174 int ival=DECODE_BP_FLAG_DEF;
175 */
176
177 /* Reset library state */
178 xsh_init();
179
180 /* Check input */
181 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
182
183 /* Get the recipe out of the plugin */
184 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
185 CPL_ERROR_TYPE_MISMATCH,
186 "Plugin is not a recipe");
187
188 recipe = (cpl_recipe *)plugin;
189
190 /* Create the parameter list in the cpl_recipe object */
191 recipe->parameters = cpl_parameterlist_new();
192 assure( recipe->parameters != NULL,
193 CPL_ERROR_ILLEGAL_OUTPUT,
194 "Memory allocation failed!");
195
196 //xsh_molecfit_model parameters
197
198 //MOLECFIT_PARAMETER_LIST
199 //list_molec
201 MOLECFIT_PARAMETER_LIST,"NULL",
203
204 //MOLECFIT_PARAMETER_FIT
205 //fit_molec
207 MOLECFIT_PARAMETER_FIT,"NULL",
209
210 //MOLECFIT_PARAMETER_RELATIVE_VALUE
211 //rel_col
213 MOLECFIT_PARAMETER_RELATIVE_VALUE,"NULL",
215
216 //MOLECFIT_PARAMETER_WAVE_RANGE_INCLUDE
217 //wave_include
219 MOLECFIT_PARAMETER_WAVE_RANGE_INCLUDE,"NULL",
221
222 //MOLECFIT_PARAMETER_WAVE_RANGE_EXCLUDE
223 //wave_exclude
225 MOLECFIT_PARAMETER_WAVE_RANGE_EXCLUDE,"NULL",
227
228 //MOLECFIT_PARAMETER_PIXEL_RANGE_EXCLUDE
229 //pixel_exclude
231 MOLECFIT_PARAMETER_PIXEL_RANGE_EXCLUDE,"NULL",
233
234 //MF_PARAMETERS_COLUMN_LAMBDA
235 //column_lambda
237 MF_PARAMETERS_COLUMN_LAMBDA,"WAVE",
239
240 //MF_PARAMETERS_COLUMN_FLUX
241 //column_flux
243 MF_PARAMETERS_COLUMN_FLUX,"FLUX",
245
246 //MF_PARAMETERS_COLUMN_DFLUX
247 //column_dflux
249 MF_PARAMETERS_COLUMN_DFLUX,"ERR",
251
252 //MF_PARAMETERS_DEFAULT_ERROR
253 //default_error
255 MF_PARAMETERS_DEFAULT_ERROR,0.01,
257
258 //MF_PARAMETERS_FTOL
259 //ftol
261 MF_PARAMETERS_FTOL,0.001,
263
264 //MF_PARAMETERS_XTOL
265 //xtol
267 MF_PARAMETERS_XTOL,0.001,
269
270 //MF_PARAMETERS_FIT_CONTINUUM
271 //fit_continuum
273 MF_PARAMETERS_FIT_CONTINUUM,"1",
275
276 //MF_PARAMETERS_CONTINUUM_N
277 //continuum_n
279 MF_PARAMETERS_CONTINUUM_N,"1",
281
282 //MF_PARAMETERS_FIT_WLC
283 //fit_wlc
285 MF_PARAMETERS_FIT_WLC,"1",
287 //MF_PARAMETERS_WLC_N
288 //wlc_n
290 MF_PARAMETERS_WLC_N,2,
292
293 //MF_PARAMETERS_WLC_CONST
294 //wlc_const
296 MF_PARAMETERS_WLC_CONST,0.0,
298
299 //MF_PARAMETERS_FIT_RES_BOX
300 //fit_res_box
302 MF_PARAMETERS_FIT_RES_BOX,false,
304
305 //MF_PARAMETERS_RES_BOX
306 //res_box
308 MF_PARAMETERS_RES_BOX,1.0,
310
311 //MF_PARAMETERS_FIT_GAUSS
312 //fit_res_gauss
314 MF_PARAMETERS_FIT_GAUSS,true,
316
317 //MF_PARAMETERS_RES_GAUSS
318 //res_gauss
320 MF_PARAMETERS_RES_GAUSS,1.0,
322
323 //MF_PARAMETERS_FIT_LORENTZ
324 //fit_res_lorentz
326 MF_PARAMETERS_FIT_LORENTZ,false,
328
329 //MF_PARAMETERS_RES_LORENTZ
330 //res_lorentz
332 MF_PARAMETERS_RES_LORENTZ,2.0,
334
335 //MF_PARAMETERS_KERN_MODE
336 //kernmode
338 MF_PARAMETERS_KERN_MODE,false,
340
341 //MF_PARAMETERS_KERN_FAC
342 //kernfac
344 MF_PARAMETERS_KERN_FAC,3.0,
346
347 //MF_PARAMETERS_VAR_KERN
348 //varkern
350 MF_PARAMETERS_VAR_KERN,true,
352
353 //MOLECFIT_PARAMETER_USE_INPUT_KERNEL
354 //use_input_kernel
356 MOLECFIT_PARAMETER_USE_INPUT_KERNEL,true,
358
359 //MF_PARAMETERS_FIT_TELESCOPE_BACK
360 //fit_telescope_background
362 MF_PARAMETERS_FIT_TELESCOPE_BACK,false,
364
365 //MF_PARAMETERS_TELESCOPE_BACK_CONST
366 //telescope_background_const
368 MF_PARAMETERS_TELESCOPE_BACK_CONST,0.1,
370
371 //MF_PARAMETERS_PWV
372 //pwv
374 MF_PARAMETERS_PWV,-1.0,
376
377 //MF_PARAMETERS_SLIT_WIDTH_KEYWORD
378 //slit_width_keyword
380 MF_PARAMETERS_SLIT_WIDTH_KEYWORD,"HEADER",
381 MF_PARAMETERS_SLIT_WIDTH_DESC ));
382
383 //MF_PARAMETERS_SLIT_WIDTH_VALUE
384 //slit_width_value
386 MF_PARAMETERS_SLIT_WIDTH_VALUE,MF_PARAMETERS_SLIT_WIDTH_VALUE_INIT,
387 MF_PARAMETERS_SLIT_WIDTH_VALUE_DESC));
388
389 //MF_LNFL_LINE_DB
390 //lnfl_line_db
392 MF_LNFL_LINE_DB,MF_LNFL_LINE_DB_AER_VERSION,
394
395
396 cleanup:
397 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
398 xsh_error_dump(CPL_MSG_ERROR);
399 return 1;
400 }
401 else {
402 return 0;
403 }
404}
405
406
407/*--------------------------------------------------------------------------*/
413/*--------------------------------------------------------------------------*/
414
415int xsh_molecfit_model_exec(cpl_plugin *plugin) {
416 cpl_recipe *recipe = NULL;
417
418 /* Check parameter */
419 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
420
421 /* Get the recipe out of the plugin */
422 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
423 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
424
425 recipe = (cpl_recipe *)plugin;
426 /* Check recipe */
427 xsh_molecfit_model( recipe->frames, recipe->parameters);
428
429 cleanup:
430 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
431 xsh_error_dump(CPL_MSG_ERROR);
432 cpl_error_reset();
433 return 1;
434 }
435 else {
436 return 0;
437 }
438}
439
440/*--------------------------------------------------------------------------*/
446/*--------------------------------------------------------------------------*/
447int xsh_molecfit_model_destroy(cpl_plugin *plugin)
448{
449 cpl_recipe *recipe = NULL;
450
451 /* Check parameter */
452 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
453
454 /* Get the recipe out of the plugin */
455 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
456 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
457
458 recipe = (cpl_recipe *)plugin;
459
460 xsh_free_parameterlist(&recipe->parameters);
461
462 cleanup:
463 if (cpl_error_get_code() != CPL_ERROR_NONE)
464 {
465 return 1;
466 }
467 else
468 {
469 return 0;
470 }
471}
472
473cpl_error_code xsh_molecfit_setup_frameset(cpl_frameset* frameset,cpl_parameterlist* list,const char* arm,const char* input_name){
474
475 const char* tag;
476 //check the input frame (SCIENCE); we know the tag already thanks to the input_name parameter
477 //so no need to search frameset for it
478 //input_name may be one of the following:
479 //SCI_SLIT_FLUX_IDP_XXX
480 //SCI_SLIT_FLUX_MERGE1D_XXX
481 //TELL_SLIT_MERGE1D_XXX
482 //TELL_SLIT_FLUX_MERGE1D_XXX
483 //STD_SLIT_FLUX_IDP_YYY_XXX
484 //where XXX is the arm and YYY is NOD, STARE or OFFSET
485
486 tag = input_name;
487 cpl_frame* f = cpl_frameset_find(frameset,input_name);
488 cpl_frame_set_group(f,CPL_FRAME_GROUP_RAW);
489
490 //cpl_frame_set_type(f,CPL_FRAME_TYPE_NONE);
491 //cpl_frame_set_tag(f,"SCIENCE");
492 //cpl_frame_set_level(f,CPL_FRAME_LEVEL_NONE);
493
494 //other inputs
495
496 //KERNEL_LIBRARY_XXX
497 tag = cpl_sprintf("%s_%s",MOLECFIT_KERNEL_LIBRARY,arm);
498 f = cpl_frameset_find(frameset,tag);
499 if(f){
500 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
501 }
502 //GDAS_XXX
503 tag = cpl_sprintf("%s_%s",MOLECFIT_GDAS,arm);
504 //tag = MOLECFIT_GDAS;
505 f = cpl_frameset_find(frameset,tag);
506 if(f){
507 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
508 }
509 //WAVE_INCLUDE_XXX
510 tag = cpl_sprintf("%s_%s",MOLECFIT_WAVE_INCLUDE,arm);
511 f = cpl_frameset_find(frameset,tag);
512 if(f){
513 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
514 }
515 //WAVE_EXCLUDE_XXX
516 tag = cpl_sprintf("%s_%s",MOLECFIT_WAVE_EXCLUDE,arm);
517 f = cpl_frameset_find(frameset,tag);
518 if(f){
519 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
520 }
521 //PIX_EXCLUDE_XXX
522 tag = cpl_sprintf("%s_%s",MOLECFIT_PIXEL_EXCLUDE,arm);
523 f = cpl_frameset_find(frameset,tag);
524 if(f){
525 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
526 }
527 //MOLECULES_XXX This uses MF_INPUT_MOLECULES_XXX tag, telluriccorr expects MOLECFIT_MODEL_MOLECULES_XXX
528 tag = cpl_sprintf("%s_%s",MOLECFIT_MOLECULES,arm);
529 f = cpl_frameset_find(frameset,tag);
530 if(f){
531 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
532 }
533 //ATM_PROFILE_STANDARD_XXX
534 tag = cpl_sprintf("%s_%s",MOLECFIT_ATM_PROFILE_STANDARD,arm);
535 f = cpl_frameset_find(frameset,tag);
536 if(f){
537 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
538 }
539 //ATM_PROFILE_COMBINED_XXX
540 tag = cpl_sprintf("%s_%s",MOLECFIT_ATM_PROFILE_COMBINED,arm);
541 f = cpl_frameset_find(frameset,tag);
542 if(f){
543 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
544 }
545 return CPL_ERROR_NONE;
546}
547
548void xsh_molecfit_model_qc(cpl_propertylist* pl,cpl_table* tab){
549 if(tab){
550 for(cpl_size row=0;row<cpl_table_get_nrow(tab);row++){
551 const char* fld = cpl_sprintf("%s",cpl_table_get_string(tab,"parameter",row));
552 double val = cpl_table_get_double(tab,"value",row,NULL);
553 if(fld){
554 if(!strcmp(fld,"initial_chi2")){
555 cpl_propertylist_update_double(pl,"ESO QC INITIAL CHI2",val);
556 }
557 if(!strcmp(fld,"best_chi2")){
558 cpl_propertylist_update_double(pl,"ESO QC BEST CHI2",val);
559 }
560 if(!strcmp(fld,"reduced_chi2")){
561 cpl_propertylist_update_double(pl,"ESO QC REDUCED CHI2",val);
562 }
563 if(!strcmp(fld,"rms_rel_to_err")){
564 cpl_propertylist_update_double(pl,"ESO QC RMS TO ERR",val);
565 }
566 if(!strcmp(fld,"rms_rel_to_mean")){
567 cpl_propertylist_update_double(pl,"ESO QC RMS TO MEAN",val);
568 }
569 if(!strcmp(fld,"chip 1, coef 0")){
570 cpl_propertylist_update_double(pl,"ESO QC LAMBDA 0",val);
571 }
572 if(!strcmp(fld,"chip 1, coef 1")){
573 cpl_propertylist_update_double(pl,"ESO QC LAMBDA 1",val);
574 }
575 if(!strcmp(fld,"chip 1, coef 2")){
576 cpl_propertylist_update_double(pl,"ESO QC LAMBDA 2",val);
577 }
578 if(!strcmp(fld,"h2o_col_mm")){
579 cpl_propertylist_update_double(pl,"ESO QC H2O COL MM",val);
580 }
581 if(strstr(fld,"rel_mol_col_")){
582 char *string[4];
583 string[0] = strtok(fld,"_");
584 int i=0;
585 while(string[i]!= NULL){
586 if(string[i] != NULL && strcmp(string[i],"rel") && strcmp(string[i],"mol") && strcmp(string[i],"col")){
587 const char* kw = cpl_sprintf("ESO QC REL MOL COL %s",xsh_string_toupper(string[i]));
588 cpl_propertylist_update_double(pl,kw,val);
589 }
590 i=i+1;
591 string[i]= strtok(NULL,"_");
592 }
593 }
594 if(strstr(fld,"ppmv_")){
595 char *string[3];
596 string[0] = strtok(fld,"_");
597 int i=0;
598 while(string[i]!= NULL){
599 if(string[i]!= NULL && strcmp(string[i],"ppmv") && strcmp(string[i],"mol") && strcmp(string[i],"col")){
600 const char* kw = cpl_sprintf("ESO QC PPMV %s",xsh_string_toupper(string[i]));
601 cpl_propertylist_update_double(pl,kw,val);
602 }
603 i=i+1;
604 string[i]= strtok(NULL,"_");
605 }
606 }
607
608
609 }
610 }
611 }
612
613}
614
615
616/*----------------------------------------------------------------------------*/
626/*----------------------------------------------------------------------------*/
627
628/*Instrument sid- example instrument - XSHOOTER?*/
629int xsh_molecfit_model(cpl_frameset *frameset, const cpl_parameterlist *parlist)
630{
631 //TODO: cpl_errorstate_ensure NOT DEFINED???
632
633 //cpl_errorstate_ensure(frameset && parlist, CPL_ERROR_NULL_INPUT, return, CPL_ERROR_NULL_INPUT, 'NULL input: framset and/or parlist');
634 cpl_error_code err= CPL_ERROR_NONE;
635 cpl_errorstate initial_errorstate = cpl_errorstate_get();
636
637 //INPUT FRAMES WILL BE NAMED DIFFERENT TO MOLECFIT_SCIENCE, MOLECFIT_STD* etc.
638 //TODO: implement xsh_check_and_set_input_tags (if needed?)
639 //err = xsh_check_and_set_input_tags(frameset); //INSTRUMENT_SPECIFIC
640 //cpl_boolean* science_data_is_table;
641
642 //err = mf_wrap_preconfig_check(frameset, parlist,science_data_is_table);/*Check parameters and frameset for correct inputs - returns CPL_ERROR*/
643
644 cpl_parameterlist* ilist = cpl_parameterlist_new();
645 cpl_parameterlist* iframelist = cpl_parameterlist_new();
646 // get instrument specific parameter defaults
647 err = xsh_molecfit_model_config(frameset,parlist,ilist,iframelist);
648 if(err){
649 return err;
650 }
651 //return err;
652 //
653
654 cpl_msg_info(cpl_func,"completed xsh_molecfit_model_config; %d : %s",err,cpl_error_get_message());
655 //these are in iframelist, so they don't end up as input parameters for molecfit via
656 const char* input_name = cpl_parameter_get_string(cpl_parameterlist_find(iframelist,"INPUTNAME"));
657 const char* arm = cpl_parameter_get_string(cpl_parameterlist_find(iframelist,"ARM"));
658 //const char* obsmode = cpl_parameter_get_string(cpl_parameterlist_find(iframelist,"OBSMODE"));
659 const char* is_idp = cpl_parameter_get_string(cpl_parameterlist_find(iframelist,"IDP"));
660 const char* fname = cpl_parameter_get_string(cpl_parameterlist_find(iframelist,"INPUTFILENAME"));
661
662 cpl_msg_info(cpl_func,"iframelist details: INPUTNAME: %s; ARM: %s; IDP: %s; INPUTFILENAME: %s;",input_name,arm,is_idp,fname);
663
664 //setup some output filename suffixes
665
666 //suffix == 'XXX' in xsh_molecfit_model specs
667 const char* suffix = arm;
668 //type_suffix == 'YYY' in xsh_molecfit_model specs (note that definition of YYY here is different to YYY of input frames)
669 const char* type_suffix = "STD";
670 if(strstr(input_name,"SCI")){
671 type_suffix = "SCI";
672 }
673
674
675 //cpl_frame* input_frame = cpl_frameset_find(frameset,input_name);
676
677 //BEFORE WE LOAD THE DATA (SPECTRUM), first load some params that we need to convert spectrum from mf_wrap_fits format into proper format (cpl_table)
678
679 //parlist2 is NOW called mergedlist
680 cpl_parameterlist* mergedlist = cpl_parameterlist_new();
681 //TODO: Check merge function - does not currently work?
682 // cpl_msg_info(cpl_func,"Calling mf_wrap_merge_parameterlists");
683
684 //ilist has instrument defaults...these should be ignored if param is provided...
685
686 err = mf_wrap_merge_parameterlists(ilist, parlist,mergedlist);
687/* cpl_msg_info(cpl_func,"AFTER MERGE PARAM LISTS");
688 cpl_msg_info(cpl_func,"ILIST");
689 cpl_parameterlist_dump(ilist,stdout);
690 cpl_msg_info(cpl_func,"PARLIST");
691 cpl_parameterlist_dump(parlist,stdout);
692 cpl_msg_info(cpl_func,"MERGEDLIST");
693 cpl_parameterlist_dump(mergedlist,stdout);
694 */
695 //cpl_msg_info(cpl_func,"Finished calling mf_wrap_merge_parameterlists %d: %s",err,cpl_error_get_message());
696
697
698 err = cpl_error_get_code();
699 //cpl_msg_info(cpl_func,"ERR after mf_wrap_merge_parameterlists %d: %s",err,cpl_error_get_message());
700
701 //setup framset tags
702 //essential function to allow mf_wrap_save() to work!
703 //could also sort out all input/output frameset issues in this function
704 err = xsh_molecfit_setup_frameset(frameset,mergedlist,arm,input_name);
705 //3. SETUP INPUT FILE NAME TAGS
706 //cpl_msg_info(cpl_func,"calling mf_wrap_config_model_input_filenames");
707 //err = mf_wrap_config_model_input_filenames(frameset,mergedlist,arm);
708 //4. SETUP OUTPUT FILE NAME TAGS
709 //cpl_msg_info(cpl_func,"calling mf_wrap_config_model_output_filenames");
710 //N.B. This is not needed!
711 //err = mf_wrap_config_model_output_filenames(frameset,mergedlist,arm,input_name);
712
713
714
715/*
716 cpl_msg_info(cpl_func,"PARLIST");
717 cpl_parameterlist_dump(parlist,stdout);
718 cpl_msg_info(cpl_func,"ILIST");
719 cpl_parameterlist_dump(ilist,stdout);
720 cpl_msg_info(cpl_func,"MERGEMF_DLIST");
721 cpl_parameterlist_dump(mergedlist,stdout);
722*/
723
724 mf_wrap_fits* data;//= mf_wrap_fits_load(fname, CPL_FALSE);
725 cpl_msg_info(cpl_func,"calling mf_wrap_fits_load");
726 data = mf_wrap_fits_load(fname, CPL_FALSE);
727 /*if(data != NULL){
728 cpl_msg_info(cpl_func,"n_ext %d",data->n_ext);
729 }*/
730
731
732 //err = set_output_filename_suffix(arm, parlist); // This could be moved to instlist create function mf_inst_config();
733
734
735 cpl_msg_info(cpl_func,"calling mf_wrap_config_model_init");
736 cpl_msg_info(cpl_func,"property list of header size");
737 cpl_msg_info(cpl_func,"size: %lld",cpl_propertylist_get_size(data->v_ext[0].header));
738 mf_wrap_model_parameter* parameters = mf_wrap_config_model_init(mergedlist,data->v_ext[0].header);
739
740
741 /*if(data == NULL){
742 cpl_msg_info(cpl_func,"pre xsh_molecfit_model_spec_data_calcs: mf_wrap_fits* data == NULL");
743 }*/
744
745
746 //cpl_msg_info(cpl_func,"xsh_molecfit_model_spec_data_calcs");
747
748 // - once data is loaded we need to prep_data for mf_model -- //
749 // -- include the data checks in here --- //
750
751
752 // DO we extract the mf_wrap_data_convert_to_table() from mf_wrap_data() ?
753 //NOTE - Are the parameters required to be already updated with the header values? //
754
755 /*merging with header data */
756 //cpl_error_code e = mf_parameters_config_update_with_header_keywords(parameters->mf_config->parameters, data->v_ext[0].header);
757 //BRENT NOTES: mf_config->parameters does not compile
758 //cpl_error_code e = mf_parameters_config_update_with_header_keywords(mf_config->parameters, data->v_ext[0].header);
759 //NOTE- how do we hanlde CHIPS and RANGES as a part of this??
760
761 // ---- move this into its own function later- OR - potentially at the end of mf_wrap_data ----//
762 if (!err) {
763 err = mf_wrap_data_convert_to_table( data,
764 parameters->chip_extensions,
765 parameters->use_only_input_pri_ext,
766 parameters->dflux_extension_data,
767 parameters->mask_extension_data,
768 parameters->mf_config->parameters->inputs.column_lambda,
769 parameters->mf_config->parameters->inputs.column_flux,
770 parameters->mf_config->parameters->inputs.column_dflux,
771 parameters->mf_config->parameters->inputs.column_mask);
772 //cpl_msg_info(cpl_func,"after 'mf_wrap_data_convert_to_table': %s",cpl_error_get_message());
773
774 //Calculate CONTINUUM_CONST
775 err = xsh_molecfit_model_spec_data_calcs(data,is_idp,mergedlist,parameters);
776 //cpl_msg_info(cpl_func,"after 'xsh_molecfit_model_spec_data_calcs': %s",cpl_error_get_message());
777 //cpl_parameterlist_dump(mergedlist,stdout);
778
779
780 //return err;
781
782 /* Save input DATA --> Combined (molecfit_spectrum in BINTABLE format) */
783 //Not relevant for xshooter
784 /*if (parameters->chip_extensions) {
785
786 cpl_size index_combined = data->v_ext[0].spectrum_data ? 0 : 1;
787 cpl_msg_info(cpl_func, "Save combined multi-extension molecfit_spectrum input FITS file ('%s') in ext=%lld ...", MOLECFIT_MODEL_CHIPS_COMBINED, index_combined);
788
789 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, MOLECFIT_MODEL_CHIPS_COMBINED, NULL);
790 if (!err) err = mf_wrap_save_mf_results(data->v_ext[index_combined].spectrum_head, MOLECFIT_MODEL_CHIPS_COMBINED, CPL_TRUE, NULL, data->v_ext[index_combined].spectrum_data, NULL);
791 }*/
792 }
793 //cpl_msg_info(cpl_func,"before 'mf_wrap_molecules': %s",cpl_error_get_message());
794
795 // Load MOLECULES
796 cpl_table* molecules = NULL;
797
798 //cpl_msg_info(cpl_func,"calling mf_wrap_molecules");
799 //Data not found originating from mf_wrap_molecules!
800 // Telluriccor is expecting the tag to be MODEL_MOLECULES_XXX at mf_wrap_utils:369
801 molecules = mf_wrap_molecules(frameset, data, mergedlist, parameters,suffix);
802 //cpl_msg_info(cpl_func,"DUMP MOLECULES TABLE AFTER mf_wrap_molecules");
803 //cpl_table_dump(molecules,0,cpl_table_get_nrow(molecules),stdout);
804
805 //cpl_msg_info(cpl_func,"after 'mf_wrap_molecules': %s",cpl_error_get_message());
806 /* Load TAG = WAVE_INCLUDE, WAVE_EXCLUDE, PIXEL_EXCLUDE */
807 cpl_table* inc_wranges = NULL;
808 cpl_table* exc_wranges = NULL;
809 cpl_table* exc_pranges = NULL;
810
811 //cpl_msg_info(cpl_func,"calling mf_wrap_wranges");
812 err = mf_wrap_wranges(frameset, data, mergedlist, parameters, &inc_wranges, &exc_wranges, &exc_pranges,suffix);
813 //cpl_msg_info(cpl_func,"nrows: inc_wranges %lld exc_wranges %lld exc_pranges %lld",cpl_table_get_nrow(inc_wranges),cpl_table_get_nrow(exc_wranges),cpl_table_get_nrow(exc_pranges));
814 //when we are done with the tables, should probably cleanup like this (but later on)...
815 //cpl_table_delete(inc_wranges);
816 //cpl_table_delete(exc_wranges);
817 //cpl_table_delete(exc_pranges);
818
819
820 /* Load TAG : MODEL_KERNEL_LIBRARY/KERNEL_LIBRARY */
821 mf_wrap_fits *kernel_data = NULL;
822 cpl_table *mapping_kernel = NULL;
823//
824 err = mf_wrap_kernel(frameset, parameters, kernel_data, mapping_kernel,suffix);
825//
826// /* Load TAG = GDAS */
827 cpl_boolean gdas_write = CPL_FALSE;
828 cpl_table *gdas_user = NULL;
829 //cpl_msg_error(cpl_func,"before mf_wrap_gdas: %d %s", err, cpl_error_get_message());
830//
831 err = mf_wrap_gdas(frameset, parameters, gdas_user,suffix);
832 //cpl_msg_error(cpl_func,"after mf_wrap_gdas: %d %s", err, cpl_error_get_message());
833
834 /* Load TAG = ATM_PROFILE_STANDARD */
835 cpl_boolean atm_profile_standard_write = CPL_FALSE;
836 cpl_table *atm_profile_standard = NULL;
837 cpl_table *atm_profile_combined = NULL;
838 //cpl_msg_error(cpl_func,"before mf_wrap_atm : %d %s", err, cpl_error_get_message());
839 const char* atm_suffix = cpl_sprintf("%s_%s",type_suffix,suffix);
840
841 err = mf_wrap_atm(frameset, parameters, &atm_profile_standard,atm_suffix);//, &atm_profile_combined);
842
843
844 //cpl_msg_error(cpl_func,"after mf_wrap_atm : %d %s", err, cpl_error_get_message());
845 /*** Save generic output files */
846 if (!err) {
847
848 cpl_msg_info(cpl_func, "Save generic multi-extension output FITS file ('%s','%s') ...", MOLECFIT_ATM_PARAMETERS, MOLECFIT_BEST_FIT_MODEL);
849 //cpl_msg_info(cpl_func, "Save generic multi-extension output FITS file ('%s','%s','%s') ...", MOLECFIT_ATM_PARAMETERS, MOLECFIT_BEST_FIT_MODEL, MOLECFIT_BEST_FIT_PARAMETERS);
850
851 const char* combined_suffix = cpl_sprintf("%s_%s",type_suffix,suffix);
852
853 const char* output_fname = mf_wrap_tag_suffix(MOLECFIT_ATM_PARAMETERS,combined_suffix,CPL_TRUE);
854 const char* tag_name = mf_wrap_tag_suffix(MOLECFIT_ATM_PARAMETERS,combined_suffix,CPL_FALSE);
855 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, tag_name, output_fname);
856
857 //output_fname = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_TRUE);
858 //tag_name = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_FALSE);
859 //err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, tag_name, output_fname);
860
861 output_fname = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_MODEL,combined_suffix,CPL_TRUE);
862 tag_name = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_MODEL,combined_suffix,CPL_FALSE);
863 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, tag_name,output_fname);
864
865 output_fname = mf_wrap_tag_suffix("MOLECFIT_DATA",combined_suffix,CPL_TRUE);
866 tag_name = mf_wrap_tag_suffix("MOLECFIT_DATA",combined_suffix,CPL_FALSE);
867 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, tag_name,output_fname);
868 // cpl_msg_error(cpl_func,"after mf_wrap_save MOLECFIT_BEST_FIT_PARAMETERS: %d %s", err, cpl_error_get_message());
869 //cpl_msg_error(cpl_func,"after mf_wrap_save MOLECFIT_BEST_FIT_MODEL: %d %s", err, cpl_error_get_message());
870 //cpl_msg_error(cpl_func,"after mf_wrap_save MOLECFIT_DATA: %d %s", err, cpl_error_get_message());
871
872
873 if (kernel_data) {
874 output_fname = mf_wrap_tag_suffix(MOLECFIT_MODEL_KERNEL_LIBRARY,suffix,CPL_TRUE);
875 tag_name = mf_wrap_tag_suffix(MOLECFIT_MODEL_KERNEL_LIBRARY,suffix,CPL_FALSE);
876 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, tag_name,output_fname);
877 }
878 }
879
880
881 int null;
882 //cpl_msg_error(cpl_func,"PRE-RECIPE EXECUTION STEP: %d %s", err, cpl_error_get_message());
883
884 /*** Recipe execution ***/
885 if (!err) {
886 //cpl_msg_error(cpl_func,"RECIPE EXECUTION STEP: %d %s", err, cpl_error_get_message());
887
888 /* Execution extensions with spectral data in table data->v_ext[?].spectrum data */
889 /* NOTE: If chip_extensions = TRUE then all spectra data from extensions with */
890 /* index > 0 have been merged into the table data->v_ext[1].spectrum data */
891
892 cpl_size n_ext;
893 if ( parameters->use_only_input_pri_ext) n_ext = 1;
894 else if (parameters->chip_extensions ) n_ext = data->v_ext[0].spectrum_data ? 1 : 2;
895 else n_ext = data->n_ext;
896
897 //cpl_msg_error(cpl_func,"RECIPE n_ext: %lld %s", n_ext, cpl_error_get_message());
898
899 if (!parameters->use_only_input_pri_ext && n_ext == 1){
900 cpl_msg_warning(cpl_func,"No data extensions found. USE_ONLY_INPUT_PRIMARY_DATA=FALSE and number of extensions == 1");
901 }
902
903
904 /* --------------- */
905 /* Execution Loops */
906 /* --------------- */
907 /* Iterate over all extensions in the data object that contain spectrum_data tables.*/
908 for (cpl_size ext = 0; ext < n_ext && !err; ext++) {
909
910 mf_model_results *results = NULL;
911
912 /* Create input molecfit spec format */
913 if (data->v_ext[ext].spectrum_data) {
914
915 /* Get kernel */
916 cpl_propertylist *header_kernel = NULL;
917 cpl_matrix *kernel = NULL;
918 if (kernel_data) {
919
920 if (mapping_kernel) {
921 //TODO: check whether MOLECFIT_MAPPING_KERNEL_EXT needs suffix or not - suspect not
922 cpl_size index_kernel_ext = cpl_table_get(mapping_kernel, MOLECFIT_MAPPING_KERNEL_EXT, ext, &null);
923 header_kernel = kernel_data->v_ext[index_kernel_ext].header;
924 kernel = kernel_data->v_ext[index_kernel_ext].matrix;
925 } else {
926 header_kernel = kernel_data->v_ext[ext].header;
927 kernel = kernel_data->v_ext[ext].matrix;
928 }
929 }
930
931 /* Get STD_MODEL/SCIENCE_CALCTRANS/SCIENCE extension header/table data */
932 cpl_msg_info(cpl_func, "Load spectrum, ext = %lld ...", ext);
933 cpl_table *molecfit_data = mf_spectrum_create(parameters->mf_config->parameters, data->v_ext[ext].spectrum_data);
934
935
936
937 /* If the chip_extension flag is false then molecfit_data is taken from a single extension as we need to test */
938 /* that all ranges assigned to this extension no have wavlength overlap with the data in this extension */
939 if (!parameters->chip_extensions) {
940 double max_wave = cpl_table_get_column_max(molecfit_data,MF_COL_IN_LAMBDA);
941 double min_wave = cpl_table_get_column_min(molecfit_data,MF_COL_IN_LAMBDA);
942 // err= molecfit_model_check_extensions_and_ranges(ext,min_wave,max_wave,inc_wranges);
943 err = xsh_molecfit_model_check_extensions_and_ranges(ext,min_wave,max_wave,inc_wranges);
944 if(!err){
945 inc_wranges = cpl_table_extract_selected(inc_wranges);
946 }
947
948 //------- ^ NOT IMPLEMENTED --------------------//
949 }
950
951
952 //cpl_msg_error(cpl_func,"pre mf_model %s", cpl_error_get_message());
953 //cpl_table_dump(molecfit_data,0,cpl_table_get_nrow(molecfit_data),stdout);
954
955 if (!err) {
956
957 /* CALL MOLECFIT */
958 results = mf_model( parameters->mf_config, /* mf_configuration *config */
959 molecules, /* cpl_table *molecules */
960 data->v_ext[ext].spectrum_head, /* const cpl_propertylist *header_spec */
961 molecfit_data, /* const cpl_table *spec */
962 inc_wranges, /* cpl_table *inc_wranges */
963 exc_wranges, /* cpl_table *exc_wranges */
964 exc_pranges, /* cpl_table *exc_pranges */
965 header_kernel, /* const cpl_propertylist *header_kernel */
966 kernel, /* const cpl_matrix *kernel */
967 gdas_user, /* const cpl_table *gdas_user */
968 atm_profile_standard, /* const cpl_table *atm_profile_standard */
969 atm_profile_combined); /* const cpl_table *atm_profile_combined */
970
971
972 /* Cleanup */
973 cpl_table_delete(molecfit_data);
974 err = cpl_error_get_code();
975
976 }
977
978
979 if (!err) {
980
981 /* Write GDAS files on disk */
982 if (results && !gdas_write) {
983 mf_gdas_write(results, mergedlist, gdas_user, frameset, parameters, data,suffix);
984 }
985
986
987 /* Write ATM_PROFILE files on disk */
988 if (!err && results && !atm_profile_standard_write) {
989 mf_atm_write(results, mergedlist, frameset, parameters, data, atm_profile_standard,suffix);
990 }
991
992 /* Save output kernel */
993 if (!err && kernel_data) {
994
995 cpl_matrix *kernel_matrix = NULL;
996 if (results) {
997 if (results->kernel) kernel_matrix = results->kernel;
998 }
999 const char* output_fname = mf_wrap_tag_suffix(MOLECFIT_MODEL_KERNEL_LIBRARY,suffix,CPL_TRUE);
1000 err += mf_wrap_save_mf_results(header_kernel, output_fname, CPL_FALSE, kernel_matrix, NULL, NULL);
1001 }
1002 }
1003
1004 /* Save outputs : extensions with/without execute molecfit */
1005 if (!err && (parameters->use_only_input_pri_ext || ext > 0) ) {
1006
1007
1008 cpl_table *atm_profile_fitted = results ? results->atm_profile_fitted : NULL;
1009 cpl_table *best_fit_params = results ? results->res : NULL;
1010 cpl_table *best_fit_model = results ? results->spec : NULL;
1011
1012 const char* combined_suffix = cpl_sprintf("%s_%s",type_suffix,suffix);
1013 const char* output_fname = mf_wrap_tag_suffix(MOLECFIT_ATM_PARAMETERS,combined_suffix,CPL_TRUE);
1014 err += mf_wrap_save_mf_results(data->v_ext[ext].header, output_fname,CPL_FALSE, NULL, atm_profile_fitted, NULL);
1015 /*Handle BEST FIT PARAMS - have to write both placeholder and full results here, in order to get qc params into primary header */
1016 if(best_fit_params){
1017 output_fname = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_TRUE);
1018 const char* tag_name = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_FALSE);
1019
1020 /* Setup different header for BFP and then add qc keywords */
1021 cpl_propertylist* bfp_hdr = cpl_propertylist_duplicate(parameters->pl);
1022 xsh_molecfit_model_qc(bfp_hdr,best_fit_params);
1023
1024 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, bfp_hdr, tag_name, output_fname);
1025 err += mf_wrap_save_mf_results(data->v_ext[ext].header, output_fname, CPL_FALSE, NULL, best_fit_params, NULL);
1026 cpl_propertylist_delete(bfp_hdr);
1027 } else {
1028 const char* output_fname = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_TRUE);
1029 const char* tag_name = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_FALSE);
1030 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, tag_name, output_fname);
1031 }
1032 output_fname = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_MODEL,combined_suffix,CPL_TRUE);
1033 err += mf_wrap_save_mf_results(data->v_ext[ext].header, output_fname,CPL_FALSE, NULL, best_fit_model, NULL);
1034 if (results) {
1035 output_fname = mf_wrap_tag_suffix("MOLECFIT_DATA",combined_suffix,CPL_TRUE);
1036 err += mf_wrap_save_mf_results(data->v_ext[ext].header,output_fname,CPL_FALSE, NULL, results->spec, NULL);
1037 }
1038
1039
1040 } else {
1041 //else write placeholder for BEST FIT PARAMS...
1042 if(!err){
1043 const char* combined_suffix = cpl_sprintf("%s_%s",type_suffix,suffix);
1044 const char *output_fname = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_TRUE);
1045 const char* tag_name = mf_wrap_tag_suffix(MOLECFIT_BEST_FIT_PARAMETERS,combined_suffix,CPL_FALSE);
1046 err += mf_wrap_save(frameset, frameset, mergedlist, MODEL_RECIPE_ID, parameters->pl, tag_name, output_fname);
1047 }
1048 }
1049
1050 /* Cleanup mf_model(...) results */
1051 if (results) mf_model_results_delete(results);
1052
1053 }
1054 /* ----------------------- */
1055 /* END of Execution Loops */
1056 /* ---------------------- */
1057
1058 }/* End if (!err) */
1059 }
1060
1061 /* Cleanup */
1062 if (parameters ) mf_wrap_model_parameter_delete( parameters );
1063 if (data ) mf_wrap_fits_delete( data );
1064 if (molecules ) cpl_table_delete( molecules );
1065 if (inc_wranges ) cpl_table_delete( inc_wranges );
1066 if (exc_wranges ) cpl_table_delete( exc_wranges );
1067 if (exc_pranges ) cpl_table_delete( exc_pranges );
1068 if (kernel_data ) mf_wrap_fits_delete( kernel_data );
1069 if (mapping_kernel ) cpl_table_delete( mapping_kernel );
1070 if (gdas_user ) cpl_table_delete( gdas_user );
1071 if (atm_profile_standard) cpl_table_delete( atm_profile_standard );
1072 if (atm_profile_combined) cpl_table_delete( atm_profile_combined );
1073
1074
1075 /* Check Recipe status and end */
1076 if (!err && cpl_errorstate_is_equal(initial_errorstate)) {
1077 cpl_msg_info(cpl_func,"Recipe successfully!");
1078 } else {
1079 /* Dump the error history */
1080 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
1081 cpl_msg_error(cpl_func,"Recipe failed!, error(%d)=%s", err, cpl_error_get_message());
1082 }
1083
1084 return err;
1085
1086}
1087
1088
1089cpl_error_code xsh_molecfit_model_spec_data_calcs(mf_wrap_fits* data, const char* is_idp, cpl_parameterlist* ilist,mf_wrap_model_parameter* parameters){
1090 //work out which extension to access for the spectrum
1091
1092 cpl_error_code err = CPL_ERROR_NONE;
1093 cpl_boolean USE_ONLY_INPUT_PRIMARY_DATA = CPL_TRUE;
1094 if(!strcmp(is_idp,"TRUE")){
1095 USE_ONLY_INPUT_PRIMARY_DATA = CPL_FALSE;
1096 }
1097
1098
1099 //code from molecfit_model - to work out which is which...
1100 //unused //cpl_size n_ext;
1101 int spec_ext = 0;
1102 //TO LATER TEST/CUSTOMISE PER XSHOOTER DATA FORMATS...
1103 if(USE_ONLY_INPUT_PRIMARY_DATA){
1104 spec_ext = 0;
1105 } else {
1106 spec_ext = 1;
1107 }
1108 cpl_msg_info(cpl_func,"spec_ext: %d",spec_ext);
1109 //cpl_msg_info(cpl_func,"flux column: %s",MF_PARAMETERS_COLUMN_FLUX);
1110 //cpl_table* spectrum = data->v_ext[spec_ext].spectrum_data;
1111 //algorithm to work out the median continuum flux (perhaps RETURN LATER to do some sigma clipping)
1112 //median of flux - XSHOOTER_INPUT_DATA_FLUX_COLUMN is defined in xsh_molecfit_model.h
1113 //TODO: check input data and see what it uses
1114 //if(data == NULL){
1115 // cpl_msg_info(cpl_func,"mf_wrap_fits* data == NULL");
1116 // }
1117
1118 cpl_size nrows = cpl_table_get_nrow(data->v_ext[spec_ext].spectrum_data);
1119 cpl_msg_info(cpl_func,"nrows: %lld",nrows);
1120 //cpl_table_dump(data->v_ext[spec_ext].spectrum_data,0,nrows,stdout);
1121
1122 const char* FLUX_COLUMN = cpl_parameter_get_string(cpl_parameterlist_find(ilist,MF_PARAMETERS_COLUMN_FLUX));
1123 cpl_msg_info(cpl_func,"flux column: %s",FLUX_COLUMN);
1124
1125 //TODO: MUST REMOVE NANs HERE...HOW TO DO THAT?
1126
1127 double median = cpl_table_get_column_median(data->v_ext[spec_ext].spectrum_data,FLUX_COLUMN);
1128 cpl_msg_info(cpl_func,"spectrum median: %.4e",median);
1129
1130 //cpl_propertylist_update_string(ilist,"CONTINUUM_CONST",cpl_sprintf("%.4e",median));
1131 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MF_PARAMETERS_CONTINUUM_CONST,CPL_TYPE_DOUBLE,NULL,NULL,median));
1132 //because we are setting up CONTINUUM_CONST **AFTER** mf_wrap_config_model_init, it will be missing from the parameters...
1133 //update that here...(could also be done in xsh_molecfit_model(), but it's neater and more self-contained to have it here
1134 mf_parameters_fitting *fitting = &(parameters->mf_config->parameters->fitting);
1135 fitting->fit_continuum.const_val = median;
1136 cpl_propertylist_update_double(parameters->pl, MF_PARAMETERS_CONTEX_DEFAULT" "MF_PARAMETERS_CONTINUUM_CONST, fitting->fit_continuum.const_val);
1137 parameters->mf_config->parameters->fitting = *fitting;
1138
1139 return err;
1140}
1141
1142
1143cpl_error_code xsh_molecfit_model_spec_header_calcs(const char* fname,const char* arm, cpl_parameterlist* ilist){
1144
1145 cpl_msg_info(cpl_func,"xsh_molecfit_model_spec_header_calcs");
1146 //const char* fname = cpl_frame_get_filename(f);
1147 cpl_msg_info(cpl_func,"reading in %s",fname);
1148 //assume header info is in first extension
1149 cpl_size ext = 0;
1150 //cpl_size n_ext = cpl_frame_get_nextensions(f);
1151 //Is this always in the first extension????
1152 //for (cpl_size ext=0;ext< n_ext;ext++){
1153 cpl_propertylist* hdr= cpl_propertylist_load(fname, ext);
1154 if(hdr){
1155 //SLIT_WIDTH_VALUE
1156 if (cpl_parameterlist_find(ilist, "SLIT_WIDTH_KEYWORD")==NULL){
1157 const char* slit_width_kw = "NONE";
1158 if(!strcmp(arm,"UVB")){
1159 slit_width_kw = "ESO INS OPTI3 NAME";
1160 } else if(!strcmp(arm,"VIS")){
1161 slit_width_kw = "ESO INS OPTI4 NAME";
1162 } else if(!strcmp(arm,"NIR")){
1163 slit_width_kw = "ESO INS OPTI5 NAME";
1164 }
1165 cpl_msg_info(cpl_func,"to read in %s value",slit_width_kw);
1166 cpl_property *prop_hval = cpl_propertylist_get_property(hdr, slit_width_kw);
1167 if(prop_hval){
1168 const char* hval = cpl_property_get_string(prop_hval);
1169 if(hval){
1170 cpl_msg_info(cpl_func,"SLIT_WIDTH_VALUE: %s (read from %s)",strtok(hval,"x"),slit_width_kw);
1171 //if the keyword value is 0.5x11, return 0.5 as slit width
1172 //write to SLIT_WIDTH_VALUE parameter
1173 //set to NONE to ensure the SLIT_WIDTH_VALUE parameter value is utilised
1174 //cpl_propertylist_update_string(ilist,"SLIT_WIDTH_KEYWORD","NONE");
1175 //cpl_propertylist_update_string(ilist,"SLIT_WIDTH_VALUE",strtok(hval,"x"));
1176 cpl_parameterlist_append(ilist,cpl_parameter_new_value("SLIT_WIDTH_KEYWORD",CPL_TYPE_STRING,NULL,NULL,"NONE"));
1177 cpl_parameterlist_append(ilist,cpl_parameter_new_value("SLIT_WIDTH_VALUE",CPL_TYPE_DOUBLE,NULL,NULL,atof(strtok(hval,"x"))));
1178 }
1179 }
1180
1181 }
1182
1183 //WAVELENGTH_FRAME *AND* OBS_ERF_RV_KEY
1184 const char* WAVELENGTH_FRAME = "AIR";
1185 const char* OBS_ERF_RV_KEY = "NONE";
1186
1187 cpl_property *prop_tu = cpl_propertylist_get_property(hdr,"TUCD1");
1188 cpl_property *prop_ss = cpl_propertylist_get_property(hdr,"SPECSYS");
1189 cpl_property *prop_hb = cpl_propertylist_get_property(hdr,"ESO QC VRAD BARYCOR");
1190 cpl_property *prop_hh = cpl_propertylist_get_property(hdr,"ESO QC VRAD HELICOR");
1191 //cpl_msg_info(cpl_func,"HDR values: TUCD1: %s; SPECSYS: %s; ESO QC VRAD BARYCOR: %s; ESO QC VRAD HELICOR: %s;",
1192 //cpl_property_get_string(prop_tu),cpl_property_get_string(prop_ss),cpl_property_get_string(prop_hb),cpl_property_get_string(prop_hh));
1193
1194
1195 if(prop_tu){
1196 const char* val = cpl_property_get_string(prop_tu);
1197 cpl_msg_info(cpl_func,"TUCD1 found: %s",val);
1198 if(!strcmp(val,"em.wl;obs.atmos")){
1199 WAVELENGTH_FRAME="AIR";
1200 } else if(!strcmp(val,"em.wl")){
1201 WAVELENGTH_FRAME="VAC";
1202 }
1203 } else {
1204 //if TUCD1 not found, set to AIR (i.e. same as TUCD1 = em.wl;obs.atmos)
1205 WAVELENGTH_FRAME="AIR";
1206 }
1207
1208 if(prop_ss){
1209 const char* val = cpl_property_get_string(prop_ss);
1210 cpl_msg_info(cpl_func,"SPECSYS found: %s",val);
1211 if(!strcmp(val,"TOPOCENT")){
1212 OBS_ERF_RV_KEY = "NONE";
1213 } else if(!strcmp(val,"BARYCENT")){
1214 OBS_ERF_RV_KEY = "ESO QC VRAD BARYCOR";
1215 WAVELENGTH_FRAME = cpl_sprintf("%s_RV",WAVELENGTH_FRAME);
1216 } else if(!strcmp(val,"HELIOCENT")){
1217 OBS_ERF_RV_KEY = "ESO QC VRAD HELICOR";
1218 WAVELENGTH_FRAME = cpl_sprintf("%s_RV",WAVELENGTH_FRAME);
1219 }
1220 } //else {
1221 //if SPECSYS not found, set to NONE (i.e. same as SPECSYS = TOPOCENT)
1222 // OBS_ERF_RV_KEY = "NONE";
1223 // }
1224 /*if(prop_hb){
1225 double val = cpl_property_get_double(prop_hb);
1226 cpl_msg_info(cpl_func,"ESO QC VRAD BARYCOR found: %.2f",val);
1227 }
1228 if(prop_hh){
1229 double val = cpl_property_get_double(prop_hh);
1230 cpl_msg_info(cpl_func,"ESO QC VRAD HELICOR found: %.2f",val);
1231 }*/
1232
1233/* if(!prop_hb || !prop_hh){
1234 OBS_ERF_RV_KEY = "NONE";
1235 }
1236*/
1237 if(WAVELENGTH_FRAME){
1238 //cpl_propertylist_update_string(ilist,"WAVELENGTH_FRAME",WAVELENGTH_FRAME);
1239 cpl_msg_info(cpl_func,"WAVELENGTH_FRAME: %s",WAVELENGTH_FRAME);
1240 cpl_parameterlist_append(ilist,cpl_parameter_new_value("WAVELENGTH_FRAME",CPL_TYPE_STRING,NULL,NULL,WAVELENGTH_FRAME));
1241
1242 }
1243 if(OBS_ERF_RV_KEY){
1244 cpl_msg_info(cpl_func,"OBS_ERF_RV_KEY: %s",OBS_ERF_RV_KEY);
1245 //cpl_propertylist_update_string(ilist,"OBS_ERF_RV_KEY",OBS_ERF_RV_KEY);
1246 cpl_parameterlist_append(ilist,cpl_parameter_new_value("OBS_ERF_RV_KEY",CPL_TYPE_STRING,NULL,NULL,OBS_ERF_RV_KEY));
1247 }
1248 }
1249
1250
1251 //CONTINUUM_CONST
1252 //calculate median of spectrum FLUX values...
1253 //this is handled in xsh_molecfit_model_spec_data_calcs()
1254
1255 //cleanup - cleanup cpl_property variables too?
1256 if (hdr) {
1257 cpl_propertylist_delete(hdr);
1258 }
1259
1260
1261 return CPL_ERROR_NONE;
1262
1263}
1264
1265
1266
1267/* -----------------------------------------------------------------------------------------------------/
1268 *
1269 * OVERVIEW OF STEPS
1270 * 1. DO SOME ERROR CHECKING ON FRAMESET? (may be done later in setup config, but just being thorough)
1271 * 1.5 GET *TAG* OF INPUT SCIENCE from FRAMSET using either MOLECFIT_STD_MODEL, MOLECFIT_SCIENCE_CALCTRANS or MOLECFIT_SCIENCE
1272 * 1.55 (Check for YYY in this FILENAME, in case we need it later...)
1273 * 2. DETERMINE IF SUFFIX NEEDED FROM DATASET (e.g. UVB/VIS/NIR for xshooter)
1274 * 3. SETUP INPUT FILE NAME TAGS
1275 * 4. SETUP OUTPUT FILE NAME TAGS
1276 * 5. SETUP PARAMETERS (e.g. --list_molec, etc.)
1277 * N.B. We expect to be provided a FITS binary table of WAVE_INCLUDE & MOLECULES params,
1278 * but FOR NOW, we setup these values internally via the string parameter versions.
1279 * 6. SETUP *EXTRA* PARAMETERS
1280 *
1281 *
1282-------------------------------------------------------------------------------------------------------*/
1283
1284cpl_error_code xsh_molecfit_model_config(cpl_frameset *frameset, const cpl_parameterlist *parlist,
1285 cpl_parameterlist* ilist, cpl_parameterlist* iframelist){
1286
1287 cpl_msg_info(cpl_func,"xsh_molecfit_model_config");
1288 //cpl_frameset_dump(frameset,stdout);
1289 //cpl_parameterlist_dump(parlist,stdout);
1290 //cpl_msg_info(cpl_func,"");
1291
1292 cpl_parameterlist* iframe = cpl_parameterlist_new();
1293 //INPUTNAME == input tag name
1294 //ARM == UVB, VIS, NIR
1295 //OBSMODE == NOD, STARE or OFFSET; If none of these, it is set to DEFAULT
1296 //IDP (bool) == TRUE or FALSE
1297 //1. Get all the input params into the iframe parameterlist (INPUTNAME,ARM,OBSMODE,IDP)
1298 cpl_error_code err= CPL_ERROR_NONE;
1299 //cpl_msg_info(cpl_func,"pre xsh_molecfit_utils_find_input_frame; %d : %s",err,cpl_error_get_message());
1300 err=xsh_molecfit_utils_find_input_frame(frameset, iframe);
1301
1302 //cpl_msg_info(cpl_func,"post xsh_molecfit_utils_find_input_frame; %d : %s",err,cpl_error_get_message());
1303 if(err){
1304 return err;
1305 }
1306 //read in the user given params
1307 //const char* val = cpl_parameter_get_string(cpl_parameterlist_find(parlist,"xsh.xsh_molecfit_model.list_molec"));
1308 //convert command line args to molecfit parameters
1309 //cpl_parameter *p;
1310 //const char* prefix = "xsh.xsh_molecfit_model.";
1311 //p = cpl_parameterlist_find(cpl_sprintf("%s%s",prefix,"list_molec"));
1312 //if(p){
1313 // LIST_MOLEC
1314
1315
1316
1317 const char* input_name = cpl_parameter_get_string(cpl_parameterlist_find(iframe,"INPUTNAME"));
1318 const char* arm = cpl_parameter_get_string(cpl_parameterlist_find(iframe,"ARM"));
1319 const char* obsmode = cpl_parameter_get_string(cpl_parameterlist_find(iframe,"OBSMODE"));
1320 const char* is_idp = cpl_parameter_get_string(cpl_parameterlist_find(iframe,"IDP"));
1321 const char* fname = cpl_parameter_get_string(cpl_parameterlist_find(iframe,"INPUTFILENAME"));
1322
1323 cpl_msg_info(cpl_func,"iframe details; INPUTNAME: %s; ARM: %s; IDP: %s; OBSMODE: %s; INPUTFILENAME: %s",input_name,arm,is_idp,obsmode,fname);
1324
1325 //add iframe parameters (INPUTNAME,ARM,OBSMODE,IDP) to iframelist so that we can access them from xsh_molecfit_model
1326 //these are not added to ilist, as they are only meant to be instrument dependent molecfit parameters
1327 err = cpl_parameterlist_append(iframelist,cpl_parameterlist_find(iframe,"INPUTNAME"));
1328 err = cpl_parameterlist_append(iframelist,cpl_parameterlist_find(iframe,"ARM"));
1329 err = cpl_parameterlist_append(iframelist,cpl_parameterlist_find(iframe,"OBSMODE"));
1330 err = cpl_parameterlist_append(iframelist,cpl_parameterlist_find(iframe,"IDP"));
1331 err = cpl_parameterlist_append(iframelist,cpl_parameterlist_find(iframe,"INPUTFILENAME"));
1332
1333 //cpl_msg_info(cpl_func,"finishing up xsh_molecfit_model list_molec: %s",val);
1334
1335
1336 //return err;
1337
1338 //5. SETUP PARAMETERS (e.g. --list_molec, etc.)
1340 //(We may define specific values for its parameters later)
1341 /*By default, these are all NULL -- just to spell it out here, so we remind ourselves that we are satisfying the requirements*/
1342
1343 const char* LIST_MOLEC;
1344 const char* FIT_MOLEC;
1345 const char* REL_COL;
1346 const char* WAVE_INCLUDE;
1347 const char* MAP_REGIONS_TO_CHIP;
1348 //const char* FIT_CONTINUUM;
1349 //const char* FIT_WLC;
1350 //const char* CONTINUUM_N;
1351 //int WLC_N;
1352 cpl_boolean USE_ONLY_INPUT_PRIMARY_DATA;
1353 int USE_DATA_EXTENSION_AS_DFLUX;
1354 int USE_DATA_EXTENSION_AS_MASK;
1355 double WLG_TO_MICRON;
1356 cpl_boolean TRANSMISSION;
1357 //const char* WAVE_EXCLUDE;
1358 //const char* PIXEL_EXCLUDE;
1359
1360 //need to add mapping kernel here... + any other (internal ones) as placeholders
1361 //place to setup INTERNAL PARAMETERS...
1362 //basically - any parameters we want configurable via input rc file, we need to define in xsh_molecfit_model_create()
1363 //else - define here...
1364 //if this is NOT added here for MOLECFIT_MODEL_MAPPING_KERNEL, then in mf_wrap_config_model_init() the cpl_parameter_get_string()
1365 //function fails and segfaults
1366 const char* input_tag;
1367 input_tag = mf_wrap_tag_suffix(MOLECFIT_KERNEL_LIBRARY,arm,CPL_FALSE);
1368 if(!cpl_frameset_find(frameset,input_tag)){
1369 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MOLECFIT_MODEL_MAPPING_KERNEL,CPL_TYPE_STRING,NULL,NULL,"NULL"));
1370 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MOLECFIT_PARAMETER_USE_INPUT_KERNEL,CPL_TYPE_BOOL,NULL,NULL,false));
1371 }
1372 //if we have an actual input kernel, we will have to set MOLECFIT_MODEL_MAPPING_KERNEL!
1373
1374
1375 //if the sof does not have WAVE_EXCLUDE_XXX, add this default param
1376 input_tag = mf_wrap_tag_suffix(MOLECFIT_PARAMETER_WAVE_RANGE_EXCLUDE,arm,CPL_FALSE);
1377 if(!cpl_frameset_find(frameset,input_tag)){
1378 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MOLECFIT_PARAMETER_WAVE_RANGE_EXCLUDE,CPL_TYPE_STRING,NULL,NULL,"NULL"));
1379 }
1380 //if the sof does not have PIX_EXCLUDE_XXX, add this default param
1381 input_tag = mf_wrap_tag_suffix(MOLECFIT_PARAMETER_PIXEL_RANGE_EXCLUDE,arm,CPL_FALSE);
1382 if(!cpl_frameset_find(frameset,input_tag)){
1383 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MOLECFIT_PARAMETER_PIXEL_RANGE_EXCLUDE,CPL_TYPE_STRING,NULL,NULL,"NULL"));
1384 }
1385 //need to add this so parameters->chip_extensions works
1386 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MOLECFIT_PARAMETER_CHIP_EXTENSIONS,CPL_TYPE_BOOL,NULL,NULL,false));
1387
1388 //if the sof does not have GDAS_XXX, add this default param
1389 input_tag = mf_wrap_tag_suffix(MOLECFIT_GDAS,arm,CPL_FALSE);
1390 if(!cpl_frameset_find(frameset,input_tag)){
1391 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MF_PARAMETERS_GDAS_PROFILE,CPL_TYPE_STRING,NULL,NULL,"auto"));
1392 }
1393
1394 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MF_PARAMETERS_EXPERT_MODE,CPL_TYPE_BOOL,NULL,NULL,CPL_FALSE));
1395
1396 TRANSMISSION = CPL_TRUE;
1397 cpl_parameterlist_append(ilist,cpl_parameter_new_value(MF_PARAMETERS_TRANSMISSION,CPL_TYPE_BOOL,NULL,NULL,TRANSMISSION));
1398
1399
1400 if(!strcmp(arm,"NIR")){
1401 LIST_MOLEC = "H2O,CO2,CO,CH4,O2";
1402 FIT_MOLEC = "1,1,0,0,0";
1403 REL_COL = " 1,1.06,1,1,1";
1404 WAVE_INCLUDE = "1.16,1.20,1.47,1.48,1.77,1.78,2.06,2.07,2.35,2.36";
1405 //MAPPED_TO_CHIP
1406 MAP_REGIONS_TO_CHIP = "1";
1407 //CONT_FIT_FLAG
1408 //FIT_CONTINUUM = "1";
1409 //FIT_WLC="1";
1410 //CONT_POLY_ORDER
1411 //CONTINUUM_N = "1";
1412 }
1413 if(!strcmp(arm,"VIS")){
1414 LIST_MOLEC = "H2O,O2";
1415 FIT_MOLEC = "1,1";
1416 REL_COL = " 1,1";
1417 WAVE_INCLUDE = "0.686,0.694,0.725,0.730,0.970,0.980";
1418 //MAPPED_TO_CHIP
1419 MAP_REGIONS_TO_CHIP = "1";
1420 //CONT_FIT_FLAG
1421 //FIT_CONTINUUM = "1";
1422 //FIT_WLC= "1";
1423 //CONT_POLY_ORDER
1424 //CONTINUUM_N = "1";
1425 }
1426 const char* default_list_molec = cpl_parameter_get_string(cpl_parameterlist_find(parlist,"LIST_MOLEC"));
1427 const char* default_fit_molec = cpl_parameter_get_string(cpl_parameterlist_find(parlist,"FIT_MOLEC"));
1428 const char* default_rel_col = cpl_parameter_get_string(cpl_parameterlist_find(parlist,"REL_COL"));
1429 const char* default_wave_include = cpl_parameter_get_string(cpl_parameterlist_find(parlist,"WAVE_INCLUDE"));
1430 const char* SLIT_WIDTH_KEYWORD = cpl_parameter_get_string(cpl_parameterlist_find(parlist,"SLIT_WIDTH_KEYWORD"));
1431 double SLIT_WIDTH_VALUE = cpl_parameter_get_double(cpl_parameterlist_find(parlist,"SLIT_WIDTH_VALUE"));
1432
1433 /* Set arm dependent parameters */
1434 cpl_msg_info(cpl_func,"setting arm dependent parameters");
1435 if(!strcmp(arm,"VIS") || !strcmp(arm,"NIR")){
1436 //if MOLECULES_XXX is specified in sof, don't set these params...
1437 //#FIXME the tag shouldn't be hard coded, and is probably
1438 input_tag = mf_wrap_tag_suffix("MOLECULES",arm,CPL_FALSE);
1439 if(!cpl_frameset_find(frameset,input_tag) && (!strcmp(default_list_molec,"NULL") && !strcmp(default_fit_molec,"NULL") && !strcmp(default_rel_col,"NULL") )){
1440 cpl_parameterlist_append(ilist,cpl_parameter_new_value("LIST_MOLEC",CPL_TYPE_STRING,NULL,NULL,LIST_MOLEC));
1441 cpl_parameterlist_append(ilist,cpl_parameter_new_value("FIT_MOLEC",CPL_TYPE_STRING,NULL,NULL,FIT_MOLEC));
1442 cpl_parameterlist_append(ilist,cpl_parameter_new_value("REL_COL",CPL_TYPE_STRING,NULL,NULL,REL_COL));
1443 }
1444 input_tag = mf_wrap_tag_suffix("WAVE_INCLUDE",arm,CPL_FALSE);
1445 //to set
1446 //set default if
1447 //1. param == NULL and tag not in frameset (i.e. user has not set param)
1448 //do not set if
1449 //2. param == NULL and tag in framset
1450 //3. param != NULL (and tag missing from framset) - the user has set some regions
1451 if(!cpl_frameset_find(frameset,input_tag) && !strcmp(default_wave_include,"NULL")){
1452 cpl_parameterlist_append(ilist,cpl_parameter_new_value("WAVE_INCLUDE",CPL_TYPE_STRING,NULL,NULL,WAVE_INCLUDE));
1453 }
1454
1455 cpl_parameterlist_append(ilist,cpl_parameter_new_value("MAP_REGIONS_TO_CHIP",CPL_TYPE_STRING,NULL,NULL,MAP_REGIONS_TO_CHIP));
1456 //cpl_parameterlist_append(ilist,cpl_parameter_new_value("FIT_CONTINUUM",CPL_TYPE_STRING,NULL,NULL,FIT_CONTINUUM));
1457 //cpl_parameterlist_append(ilist,cpl_parameter_new_value("FIT_WLC",CPL_TYPE_STRING,NULL,NULL,FIT_WLC));
1458 //cpl_parameterlist_append(ilist,cpl_parameter_new_value("CONTINUUM_N",CPL_TYPE_STRING,NULL,NULL,CONTINUUM_N));
1459
1460 /*
1461 cpl_propertylist_update_string(ilist,"LIST_MOLEC",LIST_MOLEC);
1462 cpl_propertylist_update_string(ilist,"FIT_MOLEC",FIT_MOLEC);
1463 //MOLECFIT_PARAMETER_RELATIVE_VALUE => MF_COL_REL_COL
1464 cpl_propertylist_update_string(ilist,"REL_COL",REL_COL);
1465 cpl_propertylist_update_string(ilist,"WAVE_INCLUDE",WAVE_INCLUDE);
1466 cpl_propertylist_update_string(ilist,"MAP_REGIONS_TO_CHIP",MAP_REGIONS_TO_CHIP);
1467 cpl_propertylist_update_string(ilist,"FIT_CONTINUUM",FIT_CONTINUUM);
1468 cpl_propertylist_update_string(ilist,"CONTINUUM_N",CONTINUUM_N);
1469 */
1470
1471 }
1472
1473 const char* PIX_SCALE_KEYWORD = "NONE";
1474 double PIX_SCALE_VALUE;
1475 //0.16 was in xshooter molecfit params doc/requirements
1476 //using actual values from https://www.eso.org/sci/facilities/paranal/instruments/xshooter/inst.html
1477 if(!strcmp(arm,"UVB")){
1478 PIX_SCALE_VALUE = 0.161;
1479 }
1480 if(!strcmp(arm,"VIS")){
1481 PIX_SCALE_VALUE = 0.158;
1482 }
1483 if(!strcmp(arm,"NIR")){
1484 //0.20 was in xshooter molecfit params doc/requirements
1485 //PIX_SCALE_VALUE = "0.20";
1486 //actual value from https://www.eso.org/sci/facilities/paranal/instruments/xshooter/inst.html
1487 PIX_SCALE_VALUE = 0.248;
1488 }
1489 //cpl_propertylist_update_string(ilist,"PIX_SCALE_KEYWORD",PIX_SCALE_KEYWORD);
1490 //cpl_propertylist_update_string(ilist,"PIX_SCALE_VALUE",PIX_SCALE_VALUE);
1491 cpl_parameterlist_append(ilist,cpl_parameter_new_value("PIX_SCALE_KEYWORD",CPL_TYPE_STRING,NULL,NULL,PIX_SCALE_KEYWORD));
1492 cpl_parameterlist_append(ilist,cpl_parameter_new_value("PIX_SCALE_VALUE",CPL_TYPE_DOUBLE,NULL,NULL,PIX_SCALE_VALUE));
1493 if (!strcmp(SLIT_WIDTH_KEYWORD,"NONE")){
1494 cpl_parameterlist_append(ilist,cpl_parameter_new_value("SLIT_WIDTH_KEYWORD",CPL_TYPE_STRING,NULL,NULL,SLIT_WIDTH_KEYWORD));
1495 cpl_parameterlist_append(ilist,cpl_parameter_new_value("SLIT_WIDTH_VALUE",CPL_TYPE_DOUBLE,NULL,NULL, SLIT_WIDTH_VALUE));
1496 }
1497
1498 /* Set arm independent parameters */
1499 //WLC_N = 2;
1500 //cpl_propertylist_update_string(ilist,"WLC_N",WLC_N);
1501 //cpl_parameterlist_append(ilist,cpl_parameter_new_value("WLC_N",CPL_TYPE_INT,NULL,NULL,WLC_N));
1502
1503 //if not IDP, this is TRUE
1504 USE_ONLY_INPUT_PRIMARY_DATA = CPL_TRUE;
1505 if(!strcmp(is_idp,"TRUE")){
1506 USE_ONLY_INPUT_PRIMARY_DATA = CPL_FALSE;
1507 }
1508 //cpl_propertylist_update_string(ilist,"USE_ONLY_INPUT_PRIMARY_DATA",USE_ONLY_INPUT_PRIMARY_DATA);
1509 cpl_parameterlist_append(ilist,cpl_parameter_new_value("USE_ONLY_INPUT_PRIMARY_DATA",CPL_TYPE_BOOL,NULL,NULL,USE_ONLY_INPUT_PRIMARY_DATA));
1510
1511
1512
1513 //FOR ALL CASES, even if IDP or not
1514 USE_DATA_EXTENSION_AS_DFLUX = 1;
1515 //cpl_propertylist_update_string(ilist,"USE_DATA_EXTENSION_AS_DFLUX",USE_DATA_EXTENSION_AS_DFLUX);
1516 cpl_parameterlist_append(ilist,cpl_parameter_new_value("USE_DATA_EXTENSION_AS_DFLUX",CPL_TYPE_INT,NULL,NULL,USE_DATA_EXTENSION_AS_DFLUX));
1517
1518 //default is 0 - do not need to set?
1519 USE_DATA_EXTENSION_AS_MASK = 0;
1520 cpl_parameterlist_append(ilist,cpl_parameter_new_value("USE_DATA_EXTENSION_AS_MASK",CPL_TYPE_INT,NULL,NULL,USE_DATA_EXTENSION_AS_MASK));
1521
1522 WLG_TO_MICRON = 1e-3;//e-3;//0.001;
1523 //cpl_propertylist_update_string(ilist,"WLG_TO_MICRON",WLG_TO_MICRON);
1524 cpl_parameterlist_append(ilist,cpl_parameter_new_value("WLG_TO_MICRON",CPL_TYPE_DOUBLE,NULL,NULL,WLG_TO_MICRON));
1525
1526
1527
1528 //deal with parameters that require access to input file header....
1529 //cpl_frame* input_frame = cpl_frameset_find(frameset,input_name);
1530 //const char* fname = cpl_frame_get_filename(input_frame);
1531 //cpl_msg_info(cpl_func,"fname: %s",fname);
1532 //maybe the pointer is stale or something; just try passing the framset and tag - retrieve inside the function.
1533 cpl_msg_info(cpl_func,"calling xsh_molecfit_model_spec_header_calcs with fname = %s",fname);
1534 err = xsh_molecfit_model_spec_header_calcs(fname,arm,ilist);
1535
1536 //CONTINUUM_CONST
1537 //this is handled in xsh_molecfit_model_spec_data_calcs()
1538
1539 //free up iframe
1540 //have to do this at the end, otherwise strings (e.g. fname) get deleted
1541 //cpl_parameterlist_delete(iframe);
1542
1543 return err;
1544 }
1545
1546
1547//cpl_error xsh_check_and_set_input_tags(frameset){
1548 //map the XSHOOTER input frame tags to MOLECFIT SCIENCE etc.
1549
1550 //check the frameset for the following tags =>
1551 //SCI_SLIT_FLUX_IDP_XXX, SCI_SLIT_FLUX_MERGE1D_XXX,
1552 //TELL_SLIT_FLUX_MERGE_XXX, TELL_SLIT_MERGE1D_XXX,
1553 //STD_SLIT_FLUX_IDP_YYY_XXX
1554 //where XXX = VIS/NIR/ UBB, and YYY= NOD, STARE, OFFSET
1555
1556 //call a function in Telluriccorr that maps out the input frame names to molecfit defaults();
1557
1558
1559//}
1560
1561
cpl_error_code xsh_molecfit_model_spec_header_calcs(const char *fname, const char *arm, cpl_parameterlist *ilist)
int xsh_molecfit_model(cpl_frameset *frameset, const cpl_parameterlist *parlist)
Interpret the command line options and execute the data processing.
int xsh_molecfit_model_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
int xsh_molecfit_model_create(cpl_plugin *)
Setup the recipe options.
int xsh_molecfit_model_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
cpl_error_code xsh_molecfit_model_spec_data_calcs(mf_wrap_fits *data, const char *is_idp, cpl_parameterlist *ilist, mf_wrap_model_parameter *parameters)
void xsh_molecfit_model_qc(cpl_propertylist *pl, cpl_table *tab)
cpl_error_code xsh_molecfit_setup_frameset(cpl_frameset *frameset, cpl_parameterlist *list, const char *arm, const char *input_name)
cpl_error_code xsh_molecfit_model_config(cpl_frameset *frameset, const cpl_parameterlist *parlist, cpl_parameterlist *ilist, cpl_parameterlist *iframelist)
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: xsh_2dmap.c:151
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
void xsh_free_parameterlist(cpl_parameterlist **p)
Deallocate a parameter list and set the pointer to NULL.
Definition: xsh_utils.c:2224
const char * xsh_string_toupper(char *s)
Convert all lowercase characters in a string into uppercase characters.
Definition: xsh_utils.c:4542
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
void xsh_init(void)
Reset library state.
Definition: xsh_utils.c:1160
#define XSH_MOLECFIT_PARAMETER_USE_INPUT_KERNEL_DESC
#define MODEL_RECIPE_ID
char xsh_molecfit_model_description[]
char xsh_molecfit_model_description_short[]
#define MODEL_RECIPE_AUTHOR
#define MODEL_RECIPE_CONTACT
#define XSH_MF_PARAMETERS_RES_BOX_DESC
#define XSH_MF_PARAMETERS_FIT_RES_BOX_DESC
#define XSH_MF_PARAMETERS_COLUMN_LAMBDA_DESC
#define XSH_MF_PARAMETERS_COLUMN_FLUX_DESC
#define XSH_MF_PARAMETERS_CONTINUUM_N_DESC
#define XSH_MF_PARAMETERS_FIT_GAUSS_DESC
#define XSH_MOLECFIT_PARAMETER_RELATIVE_VALUE_DESC
#define XSH_MOLECFIT_PARAMETER_WAVE_RANGE_INCLUDE_DESC
#define XSH_MF_PARAMETERS_TELESCOPE_BACK_CONST_DESC
#define XSH_MF_PARAMETERS_FIT_WLC_DESC
#define XSH_MF_PARAMETERS_COLUMN_DFLUX_DESC
#define XSH_MF_LNFL_LINE_DB_DESC
#define XSH_MF_PARAMETERS_RES_LORENTZ_DESC
#define XSH_MF_PARAMETERS_RES_GAUSS_DESC
#define XSH_MOLECFIT_PARAMETER_LIST_DESC
#define XSH_MOLECFIT_PARAMETER_PIXEL_RANGE_EXCLUDE_DESC
#define XSH_MF_PARAMETERS_KERN_MODE_DESC
#define XSH_MF_PARAMETERS_FIT_CONTINUUM_DESC
#define XSH_MF_PARAMETERS_KERN_FAC_DESC
#define XSH_MF_PARAMETERS_FTOL_DESC
#define XSH_MF_PARAMETERS_WLC_N_DESC
#define XSH_MF_PARAMETERS_DEFAULT_ERROR_DESC
#define XSH_MF_PARAMETERS_VAR_KERN_DESC
#define XSH_MF_PARAMETERS_PWV_DESC
#define XSH_MF_PARAMETERS_FIT_LORENTZ_DESC
#define XSH_MOLECFIT_PARAMETER_WAVE_RANGE_EXCLUDE_DESC
#define XSH_MF_PARAMETERS_WLC_CONST_DESC
#define XSH_MF_PARAMETERS_XTOL_DESC
#define XSH_MOLECFIT_PARAMETER_FIT_DESC
#define XSH_MF_PARAMETERS_FIT_TELESCOPE_BACK_DESC
cpl_error_code xsh_molecfit_utils_find_input_frame(cpl_frameset *frameset, cpl_parameterlist *iframe)
cpl_error_code xsh_molecfit_model_check_extensions_and_ranges(cpl_size extension, double min_wav, double max_wav, cpl_table *range)
void xsh_parameters_new_boolean(cpl_parameterlist *list, const char *recipe_id, const char *name, int value, const char *comment)
void xsh_parameters_new_double(cpl_parameterlist *list, const char *recipe_id, const char *name, double value, const char *comment)
void xsh_parameters_new_string(cpl_parameterlist *list, const char *recipe_id, const char *name, const char *value, const char *comment)
void xsh_parameters_new_int(cpl_parameterlist *list, const char *recipe_id, const char *name, int value, const char *comment)