X-shooter Pipeline Reference Manual 3.8.15
xsh_util_ima_rebin.c
Go to the documentation of this file.
1/* $Id: xsh_util_ima_rebin.c,v 1.15 2012-04-26 14:03:50 amodigli Exp $
2 *
3 * This file is part of the XSHOOTER Pipeline
4 * Copyright (C) 2002,2003 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-04-26 14:03:50 $
24 * $Revision: 1.15 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 ----------------------------------------------------------------------------*/
35#include <string.h>
36
37/* cpl */
38#include <cpl.h>
39
40#include <xsh_dfs.h>
41#include <xsh_data_pre.h>
42#include <xsh_utils.h>
43#include <xsh_parameters.h>
44#include <xsh_drl.h>
45#include <xsh_msg.h>
46#include <xsh_pfits.h>
47#include <xsh_error.h>
48//#include <xsh_utils_wrappers.h>
49
50
51/*-----------------------------------------------------------------------------
52 Defines
53 ----------------------------------------------------------------------------*/
54
55#define XSH_UTL_IMA_ARITH_RECIPE_ID "xsh_util_ima_rebin"
56#define XSH_UTL_IMA_ARITH_RECIPE_AUTHOR "A.Modigliani"
57#define XSH_UTL_IMA_ARITH_RECIPE_CONTACT "Andrea.Modigliani@eso.org"
58#define PRO_IMA "PRO_IMA_UVB"
59#define KEY_VALUE_HPRO_DID "PRO-1.15"
60/*-----------------------------------------------------------------------------
61 Functions prototypes
62 ----------------------------------------------------------------------------*/
63
64static int xsh_util_ima_rebin_create(cpl_plugin *) ;
65static int xsh_util_ima_rebin_exec(cpl_plugin *) ;
66static int xsh_util_ima_rebin_destroy(cpl_plugin *) ;
67static int xsh_util_ima_rebin(cpl_parameterlist *, cpl_frameset *) ;
68
69/*-----------------------------------------------------------------------------
70 Static variables
71 ----------------------------------------------------------------------------*/
72
73static char
76"This recipe performs image rebinning (no flux conservation).\n"
77"The input files should be list in an input file (no need to specify a tag)\n"
78"The output are resampled images with filenames\n"
79"binX_binY_filename.fits, \n"
80"where X and Y are the input binx/y parameters values\n"
81"Information on relevant parameters can be found with\n"
82"esorex --params xsh_util_ima_rebin\n"
83"esorex --help xsh_util_ima_rebin\n"
84"\n";
85
86/*-----------------------------------------------------------------------------
87 Functions code
88 ----------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
97/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106int cpl_plugin_get_info(cpl_pluginlist * list)
107{
108 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
109 cpl_plugin * plugin = &recipe->interface ;
110
111 cpl_plugin_init(plugin,
112 CPL_PLUGIN_API,
113 XSH_BINARY_VERSION,
114 CPL_PLUGIN_TYPE_RECIPE,
124
125 cpl_pluginlist_append(list, plugin) ;
126
127 return 0;
128}
129
130/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
140static int xsh_util_ima_rebin_create(cpl_plugin * plugin)
141{
142 cpl_recipe * recipe ;
143 cpl_parameter * p ;
144
145 /* Reset library state */
146 xsh_init();
147
148 /* Get the recipe out of the plugin */
149 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
150 recipe = (cpl_recipe *)plugin ;
151 else return -1 ;
152 cpl_error_reset();
153
154
155 /* Create the parameters list in the cpl_recipe object */
156 recipe->parameters = cpl_parameterlist_new() ;
157
158
159 /* Set generic parameters (common to all recipes) */
161 recipe->parameters ) ) ;
163 /* Fill the parameters list */
164 p = cpl_parameter_new_range("xsh.xsh_util_ima_rebin.binx",
165 CPL_TYPE_INT,
166 "Rebinning X factor: pix_size_o/pix_size_i",
167 "xsh.xsh_util_ima_rebin",1,1,2);
168 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "binx") ;
169 cpl_parameterlist_append(recipe->parameters, p) ;
170
171
172
173 p = cpl_parameter_new_range("xsh.xsh_util_ima_rebin.biny",
174 CPL_TYPE_INT,
175 "Rebinning Y factor: pix_size_o/pix_size_i",
176 "xsh.xsh_util_ima_rebin",1,1,2);
177 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "biny") ;
178 cpl_parameterlist_append(recipe->parameters, p) ;
179
180 cleanup:
181
182 /* Return */
183 return 0;
184}
185
186/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
193static int xsh_util_ima_rebin_exec(cpl_plugin * plugin)
194{
195 cpl_recipe * recipe ;
196 int code=0;
197 cpl_errorstate initial_errorstate = cpl_errorstate_get();
198
199 /* Get the recipe out of the plugin */
200 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
201 recipe = (cpl_recipe *)plugin ;
202 else return -1 ;
203 cpl_error_reset();
204
205 code = xsh_util_ima_rebin(recipe->parameters, recipe->frames) ;
206
207
208 if (!cpl_errorstate_is_equal(initial_errorstate)) {
209 /* Dump the error history since recipe execution start.
210 At this point the recipe cannot recover from the error */
211 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
212 }
213
214 return code ;
215}
216
217/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
224static int xsh_util_ima_rebin_destroy(cpl_plugin * plugin)
225{
226 cpl_recipe * recipe ;
227
228 /* Get the recipe out of the plugin */
229 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
230 recipe = (cpl_recipe *)plugin ;
231 else return -1 ;
232
233 cpl_parameterlist_delete(recipe->parameters) ;
234 return 0 ;
235}
236
237/*---------------------------------------------------------------------------*/
244/*---------------------------------------------------------------------------*/
245static int
246xsh_util_ima_rebin( cpl_parameterlist * parlist,
247 cpl_frameset * frames)
248{
249 cpl_parameter * param= NULL ;
250 cpl_frame* frm=NULL;
251 cpl_image* ima_tot=NULL;
252 cpl_image* ima_pre=NULL;
253 cpl_image* ima_ove=NULL;
254 cpl_image* ima_dat=NULL;
255
256 cpl_image* ima_datr=NULL;
257 cpl_image* ima_prer=NULL;
258 cpl_image* ima_over=NULL;
259
260 cpl_image* ima_new=NULL;
261 cpl_propertylist* plist=NULL;
262 cpl_propertylist* hext=NULL;
263
264 int binx=1;
265 int biny=1;
266 int nfrm=0;
267 const char* name=NULL;
268 char new_name[256];
269 int prscx=0;
270 int prscy=0;
271 int ovscx=0;
272 int ovscy=0;
273 int nx=0;
274 int ny=0;
275
276 int nxpr=0;
277 int nypr=0;
278 int nxdr=0;
279 int nydr=0;
280 int nxor=0;
281 int nyor=0;
282 int next=0;
283 int k=0;
284 double cdelt1=0;
285 double cdelt2=0;
286 const char* base_name=NULL;
287 int i=0;
288
289 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
290 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
291
292 /* HOW TO RETRIEVE INPUT PARAMETERS */
293 /* --stropt */
294 check(param=cpl_parameterlist_find(parlist,
295 "xsh.xsh_util_ima_rebin.binx"));
296 check(binx=cpl_parameter_get_int(param));
297
298
299 check(param=cpl_parameterlist_find(parlist,
300 "xsh.xsh_util_ima_rebin.biny"));
301 check(biny=cpl_parameter_get_int(param));
302 nfrm=cpl_frameset_get_size(frames);
303
304
305 for (i = 0; i < nfrm; i++) {
306 frm=cpl_frameset_get_frame(frames,i);
307
308 check(name=cpl_frame_get_filename(frm));
309 next=cpl_frame_get_nextensions(frm);
310 check(plist=cpl_propertylist_load(name,0));
311
312 check(prscx=xsh_pfits_get_prscx(plist));
313 check(prscy=xsh_pfits_get_prscy(plist));
314 check(ovscx=xsh_pfits_get_ovscx(plist));
315 check(ovscy=xsh_pfits_get_ovscy(plist));
316
317 xsh_msg("Prescan: %d,%d Overscan: %d,%d",prscx,prscy,ovscx,ovscy);
318
319 if(cpl_propertylist_has(plist,XSH_WIN_BINX) > 0) {
320 cpl_propertylist_set_int(plist,XSH_WIN_BINX,binx);
321 } else {
322 cpl_propertylist_append_int(plist,XSH_WIN_BINX,binx);
323 }
324
325 if(cpl_propertylist_has(plist,XSH_WIN_BINY) > 0) {
326 cpl_propertylist_set_int(plist,XSH_WIN_BINY,biny);
327 } else {
328 cpl_propertylist_append_int(plist,XSH_WIN_BINY,biny);
329 }
330
331 xsh_msg("Prescan: %d,%d Overscan: %d,%d",prscx,prscy,ovscx,ovscy);
332
333 if(cpl_propertylist_has(plist,XSH_CDELT1) > 0) {
334 check(cdelt1=xsh_pfits_get_cdelt1(plist));
335 if(cdelt1==1) {
336 check(cpl_propertylist_set_double(plist,XSH_CDELT1,cdelt1*binx));
337 }
338 } else {
339 if(cdelt1>1) {
340 cpl_propertylist_append_double(plist,XSH_CDELT1,2);
341 }
342 }
343
344
345
346
347 if(cpl_propertylist_has(plist,XSH_CDELT2) > 0) {
348 check(cdelt2=xsh_pfits_get_cdelt2(plist));
349 if(cdelt2==1) {
350 check(cpl_propertylist_set_double(plist,XSH_CDELT2,cdelt2*binx));
351 }
352 } else {
353 if(cdelt2>1) {
354 cpl_propertylist_append_double(plist,XSH_CDELT2,2);
355 }
356 }
357
358
359 for(k=0;k<=next;k++) {
360 check(ima_tot=cpl_image_load(name,CPL_TYPE_FLOAT,0,k));
361 check(hext=cpl_propertylist_load(name,k));
362
363 check(nx=cpl_image_get_size_x(ima_tot));
364 check(ny=cpl_image_get_size_y(ima_tot));
365
366 check(ima_dat=cpl_image_extract(ima_tot,prscx+1,prscy+1,nx-ovscx,ny-ovscy));
367 check(ima_pre=cpl_image_extract(ima_tot,1,prscy+1,prscx,ny));
368 check(ima_ove=cpl_image_extract(ima_tot,nx-ovscx+1,prscy+1,nx,ny));
369 base_name=xsh_get_basename(name);
370 sprintf(new_name,"pre_binx%d_biny%d_%s",binx,biny,base_name);
371 //check(cpl_image_save(ima_pre,new_name,CPL_BPP_IEEE_FLOAT,plist,
372 // CPL_IO_DEFAULT));
373
374 sprintf(new_name,"ove_binx%d_biny%d_%s",binx,biny,base_name);
375 //check(cpl_image_save(ima_ove,new_name,CPL_BPP_IEEE_FLOAT,plist,
376 // CPL_IO_DEFAULT));
377
378 sprintf(new_name,"dat_binx%d_biny%d_%s",binx,biny,base_name);
379 //check(cpl_image_save(ima_dat,new_name,CPL_BPP_IEEE_FLOAT,plist,
380 // CPL_IO_DEFAULT));
381
382
383 check(ima_datr=cpl_image_extract_subsample(ima_dat,binx,biny));
384 check(ima_prer=cpl_image_extract_subsample(ima_pre,binx,biny));
385 check(ima_over=cpl_image_extract_subsample(ima_ove,binx,biny));
386
387 check(nxpr=cpl_image_get_size_x(ima_prer));
388 check(nypr=cpl_image_get_size_y(ima_prer));
389 check(nxor=cpl_image_get_size_x(ima_over));
390 check(nyor=cpl_image_get_size_y(ima_over));
391 check(nxdr=cpl_image_get_size_x(ima_datr));
392 check(nydr=cpl_image_get_size_y(ima_datr));
393
394 check(ima_new=cpl_image_new(nxpr+nxdr+nxor,ny/2,
395 CPL_TYPE_FLOAT));
396 check(cpl_image_copy(ima_new,ima_prer,1,1));
397 check(cpl_image_copy(ima_new,ima_datr,1+nxpr,1));
398 check(cpl_image_copy(ima_new,ima_over,1+nxpr+nxdr,1));
399
400 sprintf(new_name,"datr_binx%d_biny%d_%s",binx,biny,base_name);
401 //check(cpl_image_save(ima_datr,new_name,CPL_BPP_IEEE_FLOAT,plist,
402 // CPL_IO_DEFAULT));
403
404 sprintf(new_name,"binx%d_biny%d_%s",binx,biny,base_name);
405 xsh_msg("new_name=%s",new_name);
406
407 //check(cpl_image_save(ima_new,new_name,CPL_BPP_16_SIGNED,plist,
408 // CPL_IO_DEFAULT));
409 if(k==0) {
410 check(cpl_image_save(ima_new,new_name,XSH_PRE_DATA_BPP,plist,
411 CPL_IO_DEFAULT));
412 } else if (k==1) {
413 check(cpl_image_save(ima_new,new_name,XSH_PRE_ERRS_BPP,hext,
414 CPL_IO_EXTEND));
415 } else if (k==2) {
416 check(cpl_image_save(ima_new,new_name,XSH_PRE_QUAL_BPP,hext,
417 CPL_IO_EXTEND));
418 }
419 xsh_free_image(&ima_tot);
420 xsh_free_image(&ima_dat);
421 xsh_free_image(&ima_pre);
422 xsh_free_image(&ima_ove);
423 xsh_free_image(&ima_new);
424 xsh_free_propertylist(&plist);
426 }
427 }
428
429 cleanup:
430 xsh_free_image(&ima_tot);
431 xsh_free_image(&ima_dat);
432 xsh_free_image(&ima_pre);
433 xsh_free_image(&ima_ove);
434 xsh_free_image(&ima_new);
435
436
437 if (cpl_error_get_code()) {
438 return -1 ;
439 } else {
440 return 0 ;
441 }
442
443}
int binx
int biny
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
int xsh_pfits_get_ovscx(const cpl_propertylist *plist)
find out the OVSCX value
Definition: xsh_pfits.c:391
double xsh_pfits_get_cdelt2(const cpl_propertylist *plist)
find out the cdelt2
Definition: xsh_pfits.c:2216
int xsh_pfits_get_ovscy(const cpl_propertylist *plist)
find out the OVSCY value
Definition: xsh_pfits.c:408
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
Definition: xsh_pfits.c:2196
int xsh_pfits_get_prscx(const cpl_propertylist *plist)
find out the PRSCX value
Definition: xsh_pfits.c:425
int xsh_pfits_get_prscy(const cpl_propertylist *plist)
find out the PRSCY value
Definition: xsh_pfits.c:442
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static int xsh_util_ima_rebin(cpl_parameterlist *, cpl_frameset *)
Get the command line options and execute the data reduction.
static int xsh_util_ima_rebin_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static int xsh_util_ima_rebin_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
static int xsh_util_ima_rebin_create(cpl_plugin *)
Setup the recipe options
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
char * xsh_get_basename(const char *filename)
Return base filename.
Definition: xsh_utils.c:1175
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
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
#define XSH_PRE_ERRS_BPP
Definition: xsh_data_pre.h:45
#define XSH_PRE_QUAL_BPP
Definition: xsh_data_pre.h:47
#define XSH_PRE_DATA_BPP
Definition: xsh_data_pre.h:43
int nx
int ny
cpl_error_code xsh_parameters_decode_bp(const char *recipe_id, cpl_parameterlist *plist, const int ival)
void xsh_parameters_generic(const char *recipe_id, cpl_parameterlist *plist)
#define XSH_CDELT2
Definition: xsh_pfits.h:97
#define XSH_CDELT1
Definition: xsh_pfits.h:96
#define XSH_WIN_BINY
Definition: xsh_pfits.h:156
#define XSH_WIN_BINX
Definition: xsh_pfits.h:155
static char xsh_util_ima_rebin_description_short[]
#define XSH_UTL_IMA_ARITH_RECIPE_AUTHOR
static char xsh_util_ima_rebin_description[]
#define XSH_UTL_IMA_ARITH_RECIPE_ID
#define XSH_UTL_IMA_ARITH_RECIPE_CONTACT