X-shooter Pipeline Reference Manual 3.8.15
test-xsh_detect_continuum.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-04-26 14:10:08 $
23 * $Revision: 1.19 $
24 */
25#ifdef HAVE_CONFIG_H
26# include <config.h>
27#endif
28
29/*--------------------------------------------------------------------------*/
35/*--------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------
39 Includes
40 ---------------------------------------------------------------------------*/
41
42
43#include <cpl.h>
44#include <xsh_data_instrument.h>
45#include <xsh_pfits.h>
46#include <xsh_msg.h>
47#include <xsh_utils.h>
48#include <xsh_data_order.h>
49#include <tests.h>
50#include <math.h>
51#include <xsh_cpl_size.h>
52/*---------------------------------------------------------------------------
53 Defines
54 ---------------------------------------------------------------------------*/
55#define MODULE_ID "XSH_DETECT_CONTINUUM"
56/*---------------------------------------------------------------------------
57 Functions prototypes
58 ---------------------------------------------------------------------------*/
59
60/*--------------------------------------------------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69/*
70 1 polynome (same for all orders)
71 constant coeff incresed by 20 for each order
72*/
73
74static const double poly0_coeff[] = {
75 280., 0.3, -0.0025 } ;
76static const double step = 40. ;
77
78static int nx = 400, ny = 400 ;
79static int starty = 10, endy = 380 ;
80static int dimension = 1, degree = 2, width = 10 ;
81static int norder = 3 ;
82static double exptime = 1. ;
83static const char * img_name = "dtc_img.fits" ;
84static const char * tbl_name = "dtc_tbl.fits" ;
85static const char * dtc_pre_name = "dtc_img_pre.fits" ;
86
87static void verify_order_table( cpl_frame * result )
88{
89 const char *hname ;
90
91 cpl_propertylist * header = NULL ;
92 double residavg = 0. ;
93
94 xsh_msg( " ====== verify_order_tables" ) ;
95
96 /* Get propertylist of new order_table
97 check RESIDAVG, should be < 1 pixel
98 */
99 check( hname = cpl_frame_get_filename( result ) ) ;
100 check( header = cpl_propertylist_load( hname, 0 ) ) ;
102 CPL_TYPE_DOUBLE, &residavg ) ) ;
103
104 assure( residavg < 1., CPL_ERROR_ILLEGAL_INPUT, "Error Too large" ) ;
105 xsh_msg( " Orders Detected OK, RESIDAVG = %lf", residavg ) ;
106
107 cleanup:
108 xsh_free_propertylist(&header);
109 return ;
110}
111
112int main(void)
113{
115 xsh_order_list * list = NULL ;
116 cpl_polynomial * poly0 = NULL ;
117 cpl_polynomial * poly1 = NULL ;
118 cpl_polynomial * poly2 = NULL ;
119 cpl_image *image = NULL ;
120 cpl_image *bias = NULL;
121 cpl_frame * img_frame = NULL, * tbl_frame = NULL, * dtc_frame = NULL,
122 * result_frame = NULL ;
123 xsh_pre * img_pre = NULL ;
124 xsh_clipping_param dcn_clipping ;
125 xsh_detect_continuum_param detect_param;
126
127 XSH_INSTRCONFIG *iconfig ;
128 cpl_propertylist * img_header=NULL ;
129 cpl_frame* resid_frame=NULL;
130
133 xsh_msg("detect_continuum");
138 xsh_instrument_set_recipe_id( instrument, "xsh_orderpos" ) ;
139
140 xsh_msg( " recipe_id: %s", instrument->recipe_id ) ;
141
143 iconfig->orders = norder ;
144
145 xsh_msg( "Create Order List with %d orders", norder ) ;
147
148 xsh_msg( "Create polynomials of degree %d", degree ) ;
149 poly0 = cpl_polynomial_new( dimension ) ;
150 poly1 = cpl_polynomial_new( dimension ) ;
151 poly2 = cpl_polynomial_new( dimension ) ;
152
153 /* Set polynomial coefficients */
154 {
155 cpl_size i ;
156 i = 0 ;
157
158 cpl_polynomial_set_coeff( poly0, &i, poly0_coeff[i] ) ;
159 cpl_polynomial_set_coeff( poly1, &i, poly0_coeff[i] + step ) ;
160 cpl_polynomial_set_coeff( poly2, &i, poly0_coeff[i] + 2*step ) ;
161
162 for( i = 1 ; i<= degree ; i++ ) {
163 cpl_polynomial_set_coeff( poly0, &i, poly0_coeff[i] ) ;
164 cpl_polynomial_set_coeff( poly1, &i, poly0_coeff[i] ) ;
165 cpl_polynomial_set_coeff( poly2, &i, poly0_coeff[i] ) ;
166 }
167 }
168
169 xsh_msg( "Add to order list" ) ;
170 add_to_order_list( list, 0, 1, poly0, width, starty, endy ) ;
171 add_to_order_list( list, 1, 2, poly1, width, starty, endy ) ;
172 add_to_order_list( list, 2, 3, poly2, width, starty, endy ) ;
173
174 xsh_order_list_dump( list, "orders.dmp" ) ;
175
176 /* Save image and create Frame accordingly */
177 img_frame = cpl_frame_new() ;
178 img_header = mkHeader( iconfig, nx, ny, exptime ) ;
179
180 check( image = create_order_image( list, nx, ny ) ) ;
181 cpl_image_save( image, img_name, CPL_BPP_IEEE_DOUBLE, img_header,
182 CPL_IO_DEFAULT);
183
184 cpl_frame_set_filename( img_frame, img_name ) ;
185 cpl_frame_set_tag( img_frame, "ORDERDEF_VIS_D2" );
186 cpl_frame_set_level( img_frame, CPL_FRAME_LEVEL_TEMPORARY);
187 cpl_frame_set_group( img_frame, CPL_FRAME_GROUP_RAW ) ;
188
189 /* Save this frame as a PRE frame */
190 bias = xsh_test_create_bias_image( "BIAS.fits" ,nx, ny, instrument);
191 check( img_pre = xsh_pre_create( img_frame, NULL, bias, instrument,0,CPL_FALSE));
192 xsh_msg( "Saving PRE image \"%s\"", dtc_pre_name ) ;
193 check_msg( dtc_frame = xsh_pre_save( img_pre, dtc_pre_name, "TEST",1 ),
194 "Cant save pre structure" ) ;
195 cpl_frame_set_filename( dtc_frame, dtc_pre_name ) ;
196 cpl_frame_set_tag( dtc_frame, "ORDERDEF_UVB_D2" );
197 cpl_frame_set_level( dtc_frame, CPL_FRAME_LEVEL_TEMPORARY);
198 cpl_frame_set_group( dtc_frame, CPL_FRAME_GROUP_RAW ) ;
199
200 /* Save order table frame */
201 check( tbl_frame = xsh_order_list_save( list, instrument, tbl_name,"ORDERDEF_UVB_D2",ny) ) ;
202
203
204 /* Now detect continuum */
205 dcn_clipping.sigma = 2.5 ;
206 dcn_clipping.niter = 5 ;
207 dcn_clipping.frac = 0.7 ;
208 //The following value is crucial to the success of the test
209 //we use the min allowed to make the test pass
210 dcn_clipping.res_max = 0.4 ;
211
212 detect_param.search_window = 30;
213 detect_param.running_window = 7;
214 detect_param.fit_window = 10;
215 detect_param.poly_degree = 2;
216 detect_param.poly_step = 2;
217 detect_param.fit_threshold = 1.0;
218
220 // cpl_msg_set_level( CPL_MSG_DEBUG ) ;
221 check (result_frame = xsh_detect_continuum( dtc_frame, tbl_frame, NULL,
222 &detect_param,
223 &dcn_clipping,
224 instrument,&resid_frame) ) ;
225
226 /* Now Verify order tables */
227 check( verify_order_table( result_frame ) ) ;
228
229 cleanup:
230 xsh_free_propertylist(&img_header);
231 xsh_free_polynomial(&poly0);
232 xsh_free_polynomial(&poly1);
233 xsh_free_polynomial(&poly2);
234
235 xsh_free_frame(&img_frame);
236 xsh_free_frame(&dtc_frame);
237 xsh_free_frame(&tbl_frame);
238 xsh_free_frame(&result_frame);
239
240 xsh_free_frame(&resid_frame);
241 xsh_order_list_free(&list);
242 xsh_free_image(&image);
243 xsh_free_image(&bias);
244 xsh_pre_free(&img_pre);
247 if (cpl_error_get_code() != CPL_ERROR_NONE) {
248 xsh_error_dump(CPL_MSG_ERROR);
249 return 1;
250 }
251 else {
252 return 0;
253 }
254
255}
256
static int nx
static const char * tbl_name
static double exptime
static int starty
static int dimension
static int width
static int endy
static int degree
static int norder
static void verify_order_table(cpl_frame *result)
int main(void)
static const double step
static int ny
#define MODULE_ID
static const char * dtc_pre_name
static const double poly0_coeff[]
Unit test of XSH_DETECT_CONTINUUM.
static const char * img_name
static xsh_instrument * instrument
xsh_order_list * create_order_list(int norder, xsh_instrument *instrument)
Definition: tests.c:410
void add_to_order_list(xsh_order_list *list, int order, int absorder, cpl_polynomial *poly, int xdelta, int starty, int endy)
Definition: tests.c:432
cpl_image * xsh_test_create_bias_image(const char *name, int nx, int ny, xsh_instrument *instrument)
Definition: tests.c:89
cpl_image * create_order_image(xsh_order_list *list, int nx, int ny)
Definition: tests.c:361
cpl_propertylist * mkHeader(XSH_INSTRCONFIG *iconfig, int nx, int ny, double exptime)
Definition: tests.c:221
cpl_frame * xsh_order_list_save(xsh_order_list *order_list, xsh_instrument *instrument, const char *filename, const char *tag, const int ny)
Save an order list to a frame.
void xsh_order_list_dump(xsh_order_list *list, const char *fname)
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
xsh_pre * xsh_pre_create(cpl_frame *raw, cpl_frame *bpmap, cpl_image *bias_data, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
Create a XSH_PRE from a raw frame.
Definition: xsh_data_pre.c:450
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
cpl_frame * xsh_detect_continuum(cpl_frame *frame, cpl_frame *order_table, cpl_frame *spectral_frame, xsh_detect_continuum_param *detect_param, xsh_clipping_param *dcn_clipping, xsh_instrument *instr, cpl_frame **resid_frame)
Detect order and compute polynomial description of ordermin and order max. Uses a guess order table i...
#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
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_set_recipe_id(xsh_instrument *instrument, const char *recipe_id)
Set the recipe_id into the instrument structure.
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
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
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
Definition: xsh_utils.c:2194
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
cpl_error_code xsh_get_property_value(const cpl_propertylist *plist, const char *keyword, cpl_type keywordtype, void *result)
Read a property value from a property list.
Definition: xsh_utils.c:1600
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
const char * recipe_id
#define TESTS_CLEAN_WORKSPACE(DRL_ID)
Definition: tests.h:139
#define TESTS_INIT_WORKSPACE(DRL_ID)
Definition: tests.h:133
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
@ XSH_LAMP_UNDEFINED
@ XSH_ARM_VIS
@ XSH_MODE_SLIT
#define QC_ORD_ORDERPOS_RESIDAVG
Definition: xsh_pfits_qc.h:176
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137