00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <xsh_model_utils.h>
00029 #include <xsh_model_kernel.h>
00030 #include <xsh_model_io.h>
00031 #include <xsh_pfits.h>
00032 #include <xsh_dfs.h>
00033 #include <xsh_utils.h>
00034 #include <xsh_msg.h>
00035 #include <xsh_error.h>
00036 #include <xsh_data_instrument.h>
00037
00054
00055
00056
00057 cpl_frame*
00058 xsh_util_physmod_model_THE_create(cpl_frame* config_frame,
00059 xsh_instrument* instrument,
00060 cpl_frame* wave_list,
00061 const int binx, const int biny,
00062 const int num_ph, const int sky)
00063
00064 {
00065
00066
00067 struct xs_3* p_xs_3_config=NULL;
00068 struct xs_3 xs_3_config;
00069 const char* line_list=NULL;
00070 cpl_frame* THE_frm=NULL;
00071
00072 char THE_filename[256];
00073
00074 const char* pro_catg=NULL;
00075 cpl_propertylist* plist=NULL;
00076 cpl_table* tab=NULL;
00077 const char* seq_arm=NULL;
00078 int i=0;
00079 int* pslit_index=NULL;
00080
00081
00082
00083 p_xs_3_config=&xs_3_config;
00084
00085 if (xsh_model_config_load_best(config_frame,p_xs_3_config)!=CPL_ERROR_NONE) {
00086 xsh_msg_error("Cannot load %s as a config",
00087 cpl_frame_get_filename(config_frame)) ;
00088 return NULL ;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 check(line_list=cpl_frame_get_filename(wave_list));
00111
00112 check(xsh_model_binxy(p_xs_3_config,binx,biny));
00113
00114 if(num_ph == 1) {
00115 if(sky == 0) {
00116 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_THEO_TAB_SING,instrument));
00117 } else {
00118 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_SKY_TAB_SING,instrument));
00119 }
00120 } else {
00121 if(sky == 0) {
00122 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_THEO_TAB_MULT,instrument));
00123 } else {
00124 check(pro_catg=XSH_GET_TAG_FROM_ARM(XSH_SKY_TAB_MULT,instrument));
00125 }
00126 }
00127 sprintf(THE_filename,"%s%s",pro_catg,".fits");
00128
00129
00130 check(THE_frm=xsh_model_THE_create(p_xs_3_config,instrument,line_list,
00131 num_ph,-1,THE_filename));
00132
00133 check(plist=cpl_propertylist_load(THE_filename,0));
00134 check(tab=cpl_table_load(THE_filename,1,0));
00135
00136 if(!cpl_propertylist_has(plist,XSH_WIN_BINX)) {
00137 cpl_propertylist_append_int(plist,XSH_WIN_BINX,binx);
00138 }
00139
00140
00141 if(!cpl_propertylist_has(plist,XSH_WIN_BINY)) {
00142 cpl_propertylist_append_int(plist,XSH_WIN_BINY,biny);
00143 }
00144
00145 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_UVB){
00146 seq_arm="UVB ";
00147 } else if ( xsh_instrument_get_arm(instrument) == XSH_ARM_VIS){
00148 seq_arm="VIS ";
00149 } else if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR){
00150 seq_arm="NIR ";
00151 }
00152
00153 if(!cpl_propertylist_has(plist,XSH_SEQ_ARM)) {
00154 cpl_propertylist_append_string(plist,XSH_SEQ_ARM,seq_arm);
00155 }
00156
00157
00158 check(xsh_pfits_set_pcatg(plist,pro_catg ) ) ;
00159
00160 check(cpl_table_save(tab,plist,NULL,THE_filename,CPL_IO_DEFAULT));
00161 xsh_free_table(&tab);
00162 xsh_free_propertylist(&plist);
00163
00164 check(cpl_frame_set_filename(THE_frm,THE_filename));
00165
00166 check(xsh_frame_config(THE_filename,pro_catg,CPL_FRAME_TYPE_TABLE,
00167 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
00168 &THE_frm));
00169
00170 if(num_ph == 1) {
00171 check(tab=cpl_table_load(THE_filename,1,0));
00172 check(plist=cpl_propertylist_load(THE_filename,0));
00173 pslit_index=cpl_table_get_data_int(tab,"slit_index");
00174 for(i=0;i<cpl_table_get_nrow(tab);i++){
00175 pslit_index[i]=4;
00176 }
00177 check(cpl_table_save(tab,plist,NULL,THE_filename,CPL_IO_DEFAULT));
00178 xsh_free_table(&tab);
00179 xsh_free_propertylist(&plist);
00180 }
00181 xsh_msg("model THE table corresponding to optimized configuration %s %s",
00182 cpl_frame_get_filename(THE_frm),
00183 cpl_frame_get_tag(THE_frm));
00184
00185
00186 cleanup:
00187
00188 xsh_free_propertylist(&plist);
00189 xsh_free_table(&tab);
00190
00191 if(cpl_error_get_code() == CPL_ERROR_NONE) {
00192 return THE_frm;
00193 } else {
00194 xsh_free_frame(&THE_frm);
00195 xsh_print_rec_status(0);
00196 return NULL;
00197 }
00198
00199
00200 }
00201
00202
00210
00211 cpl_error_code
00212 xsh_model_temperature_update_structure(struct xs_3* p_xs_3_config,
00213 cpl_frame* ref_frame,
00214 xsh_instrument* instrument)
00215 {
00216 double zeroK=-273.15;
00217 const char* ref_name=NULL;
00218 cpl_propertylist* ref_head=NULL;
00219 check(ref_name=cpl_frame_get_filename(ref_frame));
00220 check(ref_head=cpl_propertylist_load(ref_name,0));
00221
00222
00223
00224
00225
00226
00227 if (xsh_instrument_get_arm(instrument)==XSH_ARM_VIS) {
00228 p_xs_3_config->temper=xsh_pfits_get_temp5(ref_head)-zeroK;
00229 }
00230 else if (xsh_instrument_get_arm(instrument)==XSH_ARM_UVB) {
00231 p_xs_3_config->temper=xsh_pfits_get_temp2(ref_head)-zeroK;
00232 p_xs_3_config->fdet=252.56094+(float)(xsh_pfits_get_FOCU1ENC(ref_head))*0.00034065216;
00233 }
00234 else if (xsh_instrument_get_arm(instrument)==XSH_ARM_NIR) {
00235 p_xs_3_config->temper=xsh_pfits_get_temp82(ref_head);
00236 p_xs_3_config->t_ir_p2=xsh_pfits_get_temp82(ref_head);
00237 p_xs_3_config->t_ir_p3=xsh_pfits_get_temp82(ref_head);
00238 }
00239
00240 cleanup:
00241 xsh_free_propertylist(&ref_head);
00242 return cpl_error_get_code();
00243 }
00244
00252
00253 cpl_error_code
00254 xsh_model_temperature_update_frame(cpl_frame** model_config_frame,
00255 cpl_frame* ref_frame,
00256 xsh_instrument* instrument,
00257 int* found_temp)
00258 {
00259 double zeroK=-273.15;
00260 const char* cfg_name=NULL;
00261 const char* ref_name=NULL;
00262 cpl_table* cfg_tab=NULL;
00263 cpl_propertylist* cfg_head=NULL;
00264 cpl_propertylist* ref_head=NULL;
00265 double focu1enc=0.0;
00266 double old_focu1enc=0.0;
00267 double old_fdet=0.0;
00268 double temp2=0;
00269 double temp5=0;
00270 double temp82=0;
00271 char local_cfg_name[256];
00272
00273
00274
00275
00276
00277
00278 check(cfg_name=cpl_frame_get_filename(*model_config_frame));
00279 check(ref_name=cpl_frame_get_filename(ref_frame));
00280 check(ref_head=cpl_propertylist_load(ref_name,0));
00281
00282 check(cfg_tab=cpl_table_load(cfg_name,1,0));
00283 check(cfg_head=cpl_propertylist_load(cfg_name,0));
00284 sprintf(local_cfg_name,"local_cfg_name_%s.fits",
00285 xsh_instrument_arm_tostring( instrument ));
00286
00287 if (xsh_instrument_get_arm(instrument) == XSH_ARM_UVB) {
00288 check(old_fdet=cpl_table_get_double(cfg_tab,"Best_Guess",36,NULL));
00289 focu1enc=(float)(xsh_pfits_get_FOCU1ENC(ref_head));
00290 old_focu1enc=(float)(xsh_pfits_get_FOCU1ENC(cfg_head));
00291 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00292 xsh_msg_warning("FITS key %s not found",XSH_FOCU1ENC_VAL);
00293 xsh_msg_warning("We cannot update the UVB camera focal length");
00294 *found_temp=false;
00295 cpl_error_reset();
00296 } else {
00297
00298 xsh_msg("XSH_FOCU1ENC_VAL =%f ( %f )",focu1enc,old_fdet+(focu1enc-old_focu1enc)*0.00034065216);
00299 }
00300
00301 check(cpl_table_set_double(cfg_tab,"Best_Guess",36,old_fdet+(focu1enc-old_focu1enc)*0.00034065216));
00302 }
00303
00304 if (xsh_instrument_get_arm(instrument) == XSH_ARM_VIS) {
00305 temp5=xsh_pfits_get_temp5(ref_head);
00306 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00307 xsh_msg_warning("FITS key %s not found or outside of expected range",
00308 XSH_TEMP5_VAL);
00309 xsh_msg_warning("We cannot update the prism temperatures");
00310 *found_temp=false;
00311 cpl_error_reset();
00312 } else {
00313 xsh_msg("XSH_TEMP5_VAL=%f [C]",temp5);
00314 }
00315 check(cpl_table_set_double(cfg_tab,"Best_Guess",1,temp5-zeroK));
00316 }
00317 else if (xsh_instrument_get_arm(instrument) == XSH_ARM_UVB) {
00318 temp2=xsh_pfits_get_temp2(ref_head);
00319 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00320 xsh_msg_warning("FITS key %s not found or outside of expected range",
00321 XSH_TEMP2_VAL);
00322 xsh_msg_warning("We cannot update the prism temperatures");
00323 *found_temp=false;
00324 cpl_error_reset();
00325 } else {
00326 xsh_msg("XSH_TEMP2_VAL=%f [C]",temp2);
00327 }
00328 check(cpl_table_set_double(cfg_tab,"Best_Guess",1,temp2-zeroK));
00329 }
00330 else if (xsh_instrument_get_arm(instrument) == XSH_ARM_NIR) {
00331 temp82=xsh_pfits_get_temp82(ref_head);
00332
00333 if(cpl_error_get_code() != CPL_ERROR_NONE || temp82 >200.0) {
00334 xsh_msg_warning("FITS key %s not found or outside of expected range",
00335 XSH_TEMP82_VAL);
00336 xsh_msg_warning("We cannot update the prism temperatures");
00337 *found_temp=false;
00338 cpl_error_reset();
00339 temp82=105.0;
00340 } else {
00341 xsh_msg("XSH_TEMP82_VAL=%f [K]",temp82);
00342 }
00343 check(cpl_table_set_double(cfg_tab,"Best_Guess",1,temp82));
00344 check(cpl_table_set_double(cfg_tab,"Best_Guess",2,temp82));
00345 check(cpl_table_set_double(cfg_tab,"Best_Guess",3,temp82));
00346 }
00347 check(cpl_table_save(cfg_tab, cfg_head, NULL, local_cfg_name, CPL_IO_DEFAULT));
00348 cpl_frame_set_filename(*model_config_frame,local_cfg_name);
00349 xsh_add_temporary_file(local_cfg_name);
00350
00351 cleanup:
00352 xsh_free_table(&cfg_tab);
00353 xsh_free_propertylist(&cfg_head);
00354 xsh_free_propertylist(&ref_head);
00355
00356 return cpl_error_get_code();
00357
00358 }