X-shooter Pipeline Reference Manual 3.8.15
test-xsh_extract.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library 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, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-01-16 21:09:42 $
23 * $Revision: 1.17 $
24 */
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*-------------------------------------------------------------------------*/
36/*-------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------
40 Includes
41 --------------------------------------------------------------------------*/
42
43#include <tests.h>
44
45#include <xsh_data_pre.h>
46#include <xsh_error.h>
47#include <xsh_msg.h>
48#include <xsh_data_instrument.h>
49#include <xsh_data_rec.h>
51#include <xsh_drl.h>
52#include <xsh_pfits.h>
53
54#include <xsh_badpixelmap.h>
55
56#include <cpl.h>
57#include <math.h>
58
59#include <getopt.h>
60
61/*--------------------------------------------------------------------------
62 Defines
63 --------------------------------------------------------------------------*/
64
65#define MODULE_ID "XSH_EXTRACT"
66
67enum {
69} ;
70
71static struct option long_options[] = {
72 {"method", required_argument, 0, METHOD_OPT},
73 {"debug", required_argument, 0, DEBUG_OPT},
74 {"help", 0, 0, HELP_OPT},
75 {0, 0, 0, 0}
76};
77
78static void Help( void )
79{
80 puts( "Unitary test of xsh_extract");
81 puts( "Usage: test_xsh_extract [options] <input_files>");
82
83 puts( "Options" ) ;
84 puts( " --method=<n> : method for extraction LOCALIZATION | FULL | NOD");
85 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
86 puts( " --help : What you see" ) ;
87 puts( "\nInput Files" ) ;
88 puts( "The input files argument MUST be in this order:" ) ;
89 puts( " 1. Rectified frame 2D" ) ;
90 puts( " 2. Localization table");
91 TEST_END();
92 exit(0);
93}
94
95static void HandleOptions( int argc, char **argv,
96 xsh_extract_param *extract_par)
97{
98 int opt ;
99 int option_index = 0;
100
101 while (( opt = getopt_long (argc, argv, "slit_position:slit_height:method",
102 long_options, &option_index)) != EOF ){
103
104 switch ( opt ) {
105 case METHOD_OPT:
106 if ( strcmp(optarg, EXTRACT_METHOD_PRINT( LOCALIZATION_METHOD)) == 0){
107 extract_par->method = LOCALIZATION_METHOD;
108 }
109 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( FULL_METHOD)) == 0){
110 extract_par->method = FULL_METHOD;
111 }
112 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( NOD_METHOD)) == 0){
113 extract_par->method = NOD_METHOD;
114 }
115 else{
116 xsh_msg("WRONG method %s", optarg);
117 exit(-1);
118 }
119 break ;
120 case DEBUG_OPT:
121 if ( strcmp( optarg, "LOW")==0){
123 }
124 else if ( strcmp( optarg, "HIGH")==0){
126 }
127 break;
128 case HELP_OPT:
129 Help();
130 break;
131 default:
132 Help();
133 break;
134 }
135 }
136 return;
137}
138
139
140static void analyse_extraction( cpl_frame* rec_frame, xsh_instrument* instr);
141
142/*--------------------------------------------------------------------------
143 Implementation
144 --------------------------------------------------------------------------*/
145static void analyse_extraction( cpl_frame* rec_frame, xsh_instrument* instr)
146{
147 const char* rec_name = NULL;
148 xsh_rec_list* rec_list = NULL;
149 int iorder;
150
151 XSH_ASSURE_NOT_NULL( rec_frame);
152
153 check( rec_name = cpl_frame_get_filename( rec_frame));
154
155 printf("RECTIFY frame : %s\n", rec_name);
156 check( rec_list = xsh_rec_list_load( rec_frame, instr));
157
158 for(iorder=0; iorder< rec_list->size; iorder++){
159 int order = 0, ilambda = 0;
160 int nlambda = 0;
161 float *flux = NULL;
162 float *err = NULL;
163 double *lambda = NULL;
164 char name[256];
165 FILE* datfile = NULL;
166
167 check( order = xsh_rec_list_get_order(rec_list, iorder));
168 check( nlambda = xsh_rec_list_get_nlambda(rec_list, iorder));
169 check( flux = xsh_rec_list_get_data1( rec_list, iorder));
170 check( err = xsh_rec_list_get_errs1( rec_list, iorder));
171 check( lambda = xsh_rec_list_get_lambda( rec_list, iorder));
172
173 sprintf( name, "extract_order%d.dat",order);
174 xsh_msg("Save file %s",name);
175 datfile = fopen( name, "w");
176
177 for(ilambda=0; ilambda < nlambda; ilambda++){
178 fprintf( datfile,"%f %f\n",lambda[ilambda],flux[ilambda]);
179 }
180 fclose(datfile);
181
182 sprintf( name, "extract_err_order%d.dat",order);
183 xsh_msg("Save file %s",name);
184 datfile = fopen( name, "w");
185
186 for(ilambda=0; ilambda < nlambda; ilambda++){
187 fprintf( datfile,"%f %f\n",lambda[ilambda],err[ilambda]);
188 }
189 fclose(datfile);
190 }
191 cleanup:
192 xsh_rec_list_free( &rec_list);
193 return;
194}
195
203int main( int argc, char **argv)
204{
205 /* Declarations */
206 int ret = 0 ;
208 xsh_extract_param extract_obj = { LOCALIZATION_METHOD};
209 cpl_propertylist *header= NULL;
210 const char *tag = NULL;
211 cpl_frame* result = NULL;
212 cpl_frame* result_eso = NULL;
213
214 char* rec_name = NULL;
215 cpl_frame* rec_frame = NULL;
216 char* loc_name = NULL;
217 cpl_frame* loc_frame = NULL;
218 XSH_ARM arm;
219
220 /* Initialize libraries */
222
223 cpl_msg_set_level(CPL_MSG_DEBUG);
225
226
227 /* Analyse parameters */
228 HandleOptions( argc, argv, &extract_obj );
229 if ( (argc - optind) > 0 ) {
230 rec_name = argv[optind];
231 if ( (argc - optind) > 1){
232 loc_name = argv[optind+1];
233 }
234 }
235 else{
236 Help();
237 }
238 rec_frame = cpl_frame_new();
239 XSH_ASSURE_NOT_NULL (rec_frame);
240 cpl_frame_set_filename( rec_frame, rec_name) ;
241
242 check( header = cpl_propertylist_load( rec_name, 0));
243 check( tag = xsh_pfits_get_pcatg( header));
244 check( arm = xsh_pfits_get_arm( header))
245;
246 cpl_frame_set_level( rec_frame, CPL_FRAME_LEVEL_TEMPORARY);
247 cpl_frame_set_group( rec_frame, CPL_FRAME_GROUP_RAW ) ;
248 cpl_frame_set_tag( rec_frame, tag);
249
250 /* Create instrument structure and fill */
255
256 if ( loc_name != NULL){
257 loc_frame = cpl_frame_new();
258 cpl_frame_set_filename( loc_frame, loc_name);
259 cpl_frame_set_level( rec_frame, CPL_FRAME_LEVEL_TEMPORARY);
260 cpl_frame_set_group( rec_frame, CPL_FRAME_GROUP_RAW );
261 }
262 /* Create REGION FILES for rectify */
263 xsh_msg("Extract Parameters");
264 xsh_msg(" method : %d",extract_obj.method);
265 xsh_msg("Rectified frame : %s", rec_name);
266 if (loc_name != NULL){
267 xsh_msg("Localization table : %s", loc_name);
268 }
269
270 check(result = xsh_extract(rec_frame, loc_frame, instrument,
271 &extract_obj,&result_eso,"test"));
272
274
275 cleanup:
276
278 xsh_free_frame( &rec_frame);
279 xsh_free_frame( &result);
280 xsh_free_frame( &result_eso);
281 xsh_free_frame( &loc_frame);
282 xsh_free_frame( &rec_frame);
283 xsh_free_propertylist( &header);
284
285 if (cpl_error_get_code() != CPL_ERROR_NONE) {
286 xsh_error_dump(CPL_MSG_ERROR);
287 ret= 1;
288 }
289 TEST_END();
290 return ret ;
291}
292
int main()
Unit test of xsh_bspline_interpol.
static void analyse_extraction(cpl_frame *rec_frame, xsh_instrument *instr)
static void HandleOptions(int argc, char **argv, xsh_extract_param *extract_par)
static void Help(void)
#define MODULE_ID
static struct option long_options[]
@ METHOD_OPT
@ HELP_OPT
@ DEBUG_OPT
static xsh_instrument * instrument
double * xsh_rec_list_get_lambda(xsh_rec_list *list, int idx)
xsh_rec_list * xsh_rec_list_load(cpl_frame *frame, xsh_instrument *instrument)
load an rec list from a frame
Definition: xsh_data_rec.c:289
float * xsh_rec_list_get_data1(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_errs1(xsh_rec_list *list, int idx)
int xsh_rec_list_get_order(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:846
int xsh_rec_list_get_nlambda(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:865
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
Definition: xsh_data_rec.c:760
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
cpl_frame * xsh_extract(cpl_frame *rec_frame, cpl_frame *loc_frame, xsh_instrument *instrument, xsh_extract_param *extract_par, cpl_frame **res_frame_ext, const char *rec_prefix)
simple 1D extraction of point source like object
Definition: xsh_extract.c:808
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
void xsh_instrument_set_lamp(xsh_instrument *i, XSH_LAMP lamp)
Set a lamp on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
xsh_instrument * xsh_instrument_new(void)
create new instrument structure
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
XSH_ARM xsh_pfits_get_arm(const cpl_propertylist *plist)
Definition: xsh_pfits.c:270
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
Definition: xsh_pfits.c:1627
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
enum extract_method method
#define TEST_END()
Definition: tests.h:111
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
@ XSH_LAMP_QTH
@ XSH_MODE_IFU
int order
Definition: xsh_detmon_lg.c:80
#define EXTRACT_METHOD_PRINT(method)
@ FULL_METHOD
@ NOD_METHOD
@ LOCALIZATION_METHOD
@ XSH_DEBUG_LEVEL_HIGH
Definition: xsh_utils.h:138
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138