X-shooter Pipeline Reference Manual 3.8.15
xsh_model_utils.c
Go to the documentation of this file.
1/* $Id: xsh_model_utils.c,v 1.13 2012-12-18 14:15:45 amodigli Exp $
2 *
3 * This file is part of the ESO X-shooter Pipeline
4 * Copyright (C) 2006 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: amodigli $
23 * $Date: 2012-12-18 14:15:45 $
24 * $Revision: 1.13 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#include <xsh_model_utils.h>
29#include <xsh_model_kernel.h>
30#include <xsh_model_io.h>
31#include <xsh_pfits.h>
32#include <xsh_dfs.h>
33#include <xsh_utils.h>
34#include <xsh_msg.h>
35#include <xsh_error.h>
36#include <xsh_data_instrument.h>
37/*--------------------------------------------------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56
57cpl_frame*
58xsh_util_physmod_model_THE_create(cpl_frame* config_frame,
60 cpl_frame* wave_list,
61 const int binx, const int biny,
62 const int num_ph, const int sky)
63
64{
65
66
67 struct xs_3* p_xs_3_config=NULL;
68 struct xs_3 xs_3_config;
69 const char* line_list=NULL;
70 cpl_frame* THE_frm=NULL;
71
72 char THE_filename[256];
73
74 const char* pro_catg=NULL;
75 cpl_propertylist* plist=NULL;
76 cpl_table* tab=NULL;
77 const char* seq_arm=NULL;
78 int i=0;
79 int* pslit_index=NULL;
80 //xsh_pre * pre_sci = NULL ;
81
82 //Load xsh model configuration
83 p_xs_3_config=&xs_3_config;
84
85 if (xsh_model_config_load_best(config_frame,p_xs_3_config)!=CPL_ERROR_NONE) {
86 xsh_msg_error("Cannot load %s as a config",
87 cpl_frame_get_filename(config_frame)) ;
88 return NULL ;
89 }
90 /*Update prism temperature(s) before call to phys mod so that predicted
91 positions are for sci exposure prism temperature(s)*/
92/* if ( sci_frame != NULL) { */
93/* check( pre_sci = xsh_pre_load (sci_frame, instrument)); */
94/* if (xsh_instrument_get_arm(instrument)==XSH_ARM_VIS) { */
95/* p_xs_3_config->temper=xsh_pfits_get_temp5(pre_sci->data_header); */
96/* } */
97/* else if (xsh_instrument_get_arm(instrument)==XSH_ARM_UVB) { */
98/* p_xs_3_config->temper=xsh_pfits_get_temp2(pre_sci->data_header); */
99/* } */
100/* else if (xsh_instrument_get_arm(instrument)==XSH_ARM_NIR) { */
101/* p_xs_3_config->temper=xsh_pfits_get_temp82(pre_sci->data_header); */
102/* p_xs_3_config->t_ir_p2=xsh_pfits_get_temp82(pre_sci->data_header); */
103/* p_xs_3_config->t_ir_p3=xsh_pfits_get_temp82(pre_sci->data_header); */
104/* } */
105/* } */
106/* else { */
107/* xsh_msg("Using physical model CFG file prism temperature, no update to sci exposure prism temperature available"); */
108/* } */
109
110 check(line_list=cpl_frame_get_filename(wave_list));
111
112 check(xsh_model_binxy(p_xs_3_config,binx,biny));
113
114 if(num_ph == 1) {
115 if(sky == 0) {
117 } else {
119 }
120 } else {
121 if(sky == 0) {
123 } else {
125 }
126 }
127 sprintf(THE_filename,"%s%s",pro_catg,".fits");
128
129
130 check(THE_frm=xsh_model_THE_create(p_xs_3_config,instrument,line_list,
131 num_ph,-1,THE_filename));
132
133 check(plist=cpl_propertylist_load(THE_filename,0));
134 check(tab=cpl_table_load(THE_filename,1,0));
135
136 if(!cpl_propertylist_has(plist,XSH_WIN_BINX)) {
137 cpl_propertylist_append_int(plist,XSH_WIN_BINX,binx);
138 }
139
140
141 if(!cpl_propertylist_has(plist,XSH_WIN_BINY)) {
142 cpl_propertylist_append_int(plist,XSH_WIN_BINY,biny);
143 }
144
146 seq_arm="UVB ";
148 seq_arm="VIS ";
150 seq_arm="NIR ";
151 }
152
153 if(!cpl_propertylist_has(plist,XSH_SEQ_ARM)) {
154 cpl_propertylist_append_string(plist,XSH_SEQ_ARM,seq_arm);
155 }
156
157
158 check(xsh_pfits_set_pcatg(plist,pro_catg ) ) ;
159
160 check(cpl_table_save(tab,plist,NULL,THE_filename,CPL_IO_DEFAULT));
161 xsh_free_table(&tab);
162 xsh_free_propertylist(&plist);
163
164 check(cpl_frame_set_filename(THE_frm,THE_filename));
165
166 check(xsh_frame_config(THE_filename,pro_catg,CPL_FRAME_TYPE_TABLE,
167 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL,
168 &THE_frm));
169
170 if(num_ph == 1) {
171 check(tab=cpl_table_load(THE_filename,1,0));
172 check(plist=cpl_propertylist_load(THE_filename,0));
173 pslit_index=cpl_table_get_data_int(tab,"slit_index");
174 for(i=0;i<cpl_table_get_nrow(tab);i++){
175 pslit_index[i]=4;
176 }
177 check(cpl_table_save(tab,plist,NULL,THE_filename,CPL_IO_DEFAULT));
178 xsh_free_table(&tab);
179 xsh_free_propertylist(&plist);
180 }
181 xsh_msg("model THE table corresponding to optimized configuration %s %s",
182 cpl_frame_get_filename(THE_frm),
183 cpl_frame_get_tag(THE_frm));
184
185
186 cleanup:
187
188 xsh_free_propertylist(&plist);
189 xsh_free_table(&tab);
190 // xsh_pre_free( &pre_sci);
191 if(cpl_error_get_code() == CPL_ERROR_NONE) {
192 return THE_frm;
193 } else {
194 xsh_free_frame(&THE_frm);
196 return NULL;
197 }
198
199
200}
201
202/*--------------------------------------------------------------------------*/
210/*--------------------------------------------------------------------------*/
211cpl_error_code
213 cpl_frame* ref_frame,
215{
216 double zeroK=-273.15;
217 const char* ref_name=NULL;
218 cpl_propertylist* ref_head=NULL;
219 check(ref_name=cpl_frame_get_filename(ref_frame));
220 check(ref_head=cpl_propertylist_load(ref_name,0));
221
222 /*Update prism temperature(s) before call to phys mod so that predicted
223 positions are for actual frame exposure prism temperature(s)
224 UVB,VIS temperatures are in C, NIR ones are in K
225 */
226
228 p_xs_3_config->temper=xsh_pfits_get_temp5(ref_head)-zeroK;
229 }
231 p_xs_3_config->temper=xsh_pfits_get_temp2(ref_head)-zeroK;
232 p_xs_3_config->fdet=252.56094+(float)(xsh_pfits_get_FOCU1ENC(ref_head))*0.00034065216;
233 }
235 p_xs_3_config->temper=xsh_pfits_get_temp82(ref_head);
236 p_xs_3_config->t_ir_p2=xsh_pfits_get_temp82(ref_head);
237 p_xs_3_config->t_ir_p3=xsh_pfits_get_temp82(ref_head);
238 }
239
240 cleanup:
241 xsh_free_propertylist(&ref_head);
242 return cpl_error_get_code();
243}
244/*--------------------------------------------------------------------------*/
252/*--------------------------------------------------------------------------*/
253cpl_error_code
254xsh_model_temperature_update_frame(cpl_frame** model_config_frame,
255 cpl_frame* ref_frame,
257 int* found_temp)
258{
259 double zeroK=-273.15;
260 const char* cfg_name=NULL;
261 const char* ref_name=NULL;
262 cpl_table* cfg_tab=NULL;
263 cpl_propertylist* cfg_head=NULL;
264 cpl_propertylist* ref_head=NULL;
265 double focu1enc=0.0;
266 double old_focu1enc=0.0;
267 double old_fdet=0.0;
268 double temp2=0;
269 double temp5=0;
270 double temp82=0;
271 char local_cfg_name[256];
272
273 /*Update prism temperature(s) before call to phys mod so that predicted
274 positions are for actual frame exposure prism temperature(s)
275 UVB,VIS temperatures are in C, NIR ones are in K
276 */
277
278 check(cfg_name=cpl_frame_get_filename(*model_config_frame));
279 check(ref_name=cpl_frame_get_filename(ref_frame));
280 check(ref_head=cpl_propertylist_load(ref_name,0));
281 //xsh_msg("name=%s",cfg_name);
282 check(cfg_tab=cpl_table_load(cfg_name,1,0));
283 check(cfg_head=cpl_propertylist_load(cfg_name,0));
284 sprintf(local_cfg_name,"local_cfg_name_%s.fits",
286 /*update config with fdet from data header */
288 check(old_fdet=cpl_table_get_double(cfg_tab,"Best_Guess",36,NULL));
289 focu1enc=(float)(xsh_pfits_get_FOCU1ENC(ref_head));
290 old_focu1enc=(float)(xsh_pfits_get_FOCU1ENC(cfg_head));
291 if(cpl_error_get_code() != CPL_ERROR_NONE) {
292 xsh_msg_warning("FITS key %s not found",XSH_FOCU1ENC_VAL);
293 xsh_msg_warning("We cannot update the UVB camera focal length");
294 *found_temp=false;
295 cpl_error_reset();
296 } else {
297 // xsh_msg("XSH_FOCU1ENC_VAL =%f ( %f )",focu1enc,252.56094+focu1enc*0.00034065216);
298 xsh_msg("XSH_FOCU1ENC_VAL =%f ( %f )",focu1enc,old_fdet+(focu1enc-old_focu1enc)*0.00034065216);
299 }
300 // check(cpl_table_set_double(cfg_tab,"Best_Guess",36,252.56094+focu1enc*0.00034065216));
301 check(cpl_table_set_double(cfg_tab,"Best_Guess",36,old_fdet+(focu1enc-old_focu1enc)*0.00034065216));
302 }
303 /*update config with temperatures from data header */
305 temp5=xsh_pfits_get_temp5(ref_head);
306 if(cpl_error_get_code() != CPL_ERROR_NONE) {
307 xsh_msg_warning("FITS key %s not found or outside of expected range",
309 xsh_msg_warning("We cannot update the prism temperatures");
310 *found_temp=false;
311 cpl_error_reset();
312 } else {
313 xsh_msg("XSH_TEMP5_VAL=%f [C]",temp5);
314 }
315 check(cpl_table_set_double(cfg_tab,"Best_Guess",1,temp5-zeroK));
316 }
318 temp2=xsh_pfits_get_temp2(ref_head);
319 if(cpl_error_get_code() != CPL_ERROR_NONE) {
320 xsh_msg_warning("FITS key %s not found or outside of expected range",
322 xsh_msg_warning("We cannot update the prism temperatures");
323 *found_temp=false;
324 cpl_error_reset();
325 } else {
326 xsh_msg("XSH_TEMP2_VAL=%f [C]",temp2);
327 }
328 check(cpl_table_set_double(cfg_tab,"Best_Guess",1,temp2-zeroK));
329 }
331 temp82=xsh_pfits_get_temp82(ref_head);
332 /* extra check: some com data miss or have wrong temperature */
333 if(cpl_error_get_code() != CPL_ERROR_NONE || temp82 >200.0) {
334 xsh_msg_warning("FITS key %s not found or outside of expected range",
336 xsh_msg_warning("We cannot update the prism temperatures");
337 *found_temp=false;
338 cpl_error_reset();
339 temp82=105.0;
340 } else {
341 xsh_msg("XSH_TEMP82_VAL=%f [K]",temp82);
342 }
343 check(cpl_table_set_double(cfg_tab,"Best_Guess",1,temp82));
344 check(cpl_table_set_double(cfg_tab,"Best_Guess",2,temp82));
345 check(cpl_table_set_double(cfg_tab,"Best_Guess",3,temp82));
346 }
347 check(cpl_table_save(cfg_tab, cfg_head, NULL, local_cfg_name, CPL_IO_DEFAULT));
348 cpl_frame_set_filename(*model_config_frame,local_cfg_name);
349 xsh_add_temporary_file(local_cfg_name);
350
351 cleanup:
352 xsh_free_table(&cfg_tab);
353 xsh_free_propertylist(&cfg_head);
354 xsh_free_propertylist(&ref_head);
355
356 return cpl_error_get_code();
357
358}
static xsh_instrument * instrument
int binx
int biny
#define check(COMMAND)
Definition: xsh_error.h:71
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
cpl_error_code xsh_model_config_load_best(cpl_frame *config_frame, xsh_xs_3 *p_xs_3)
Load the config model table and fill the struct.
Definition: xsh_model_io.c:174
void xsh_model_binxy(xsh_xs_3 *p_xs_3, int bin_X, int bin_Y)
corrects model for detector's binning
cpl_frame * xsh_model_THE_create(xsh_xs_3 *p_xs_3, xsh_instrument *instr, const char *line_list, int num_ph, double sep_ph, const char *THE_filename)
Compute the THE table (centroid for each feature in lamp spectrum)
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
double xsh_pfits_get_temp5(const cpl_propertylist *plist)
find out the temp5 (VIS prism)
Definition: xsh_pfits.c:1845
double xsh_pfits_get_temp2(const cpl_propertylist *plist)
find out the temp2 (UVB prism)
Definition: xsh_pfits.c:1866
int xsh_pfits_get_FOCU1ENC(const cpl_propertylist *plist)
find out the FOCU1 POS (UVB prism auto-focus temperature)
Definition: xsh_pfits.c:1824
double xsh_pfits_get_temp82(const cpl_propertylist *plist)
find out the temp82 (NIR prisms)
Definition: xsh_pfits.c:1887
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
DOUBLE t_ir_p3
DOUBLE fdet
DOUBLE t_ir_p2
DOUBLE temper
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
int xsh_print_rec_status(const int val)
Check if an error has happened and returns error kind and location.
Definition: xsh_dfs.c:877
void xsh_frame_config(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level, cpl_frame **frame)
Define a frame characteristics.
Definition: xsh_dfs.c:900
#define XSH_THEO_TAB_SING
Definition: xsh_dfs.h:1084
#define XSH_SKY_TAB_MULT
Definition: xsh_dfs.h:1099
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_SKY_TAB_SING
Definition: xsh_dfs.h:1105
#define XSH_THEO_TAB_MULT
Definition: xsh_dfs.h:1089
cpl_error_code xsh_model_temperature_update_frame(cpl_frame **model_config_frame, cpl_frame *ref_frame, xsh_instrument *instrument, int *found_temp)
cpl_error_code xsh_model_temperature_update_structure(struct xs_3 *p_xs_3_config, cpl_frame *ref_frame, xsh_instrument *instrument)
cpl_frame * xsh_util_physmod_model_THE_create(cpl_frame *config_frame, xsh_instrument *instrument, cpl_frame *wave_list, const int binx, const int biny, const int num_ph, const int sky)
#define XSH_SEQ_ARM
Definition: xsh_pfits.h:173
#define XSH_TEMP82_VAL
Definition: xsh_pfits.h:142
#define XSH_TEMP5_VAL
Definition: xsh_pfits.h:144
#define XSH_WIN_BINY
Definition: xsh_pfits.h:156
#define XSH_WIN_BINX
Definition: xsh_pfits.h:155
#define XSH_FOCU1ENC_VAL
Definition: xsh_pfits.h:140
#define XSH_TEMP2_VAL
Definition: xsh_pfits.h:143