CRIRES Pipeline Reference Manual 2.3.19
crires_util_profile.c
1/* $Id: crires_util_profile.c,v 1.35 2012-09-19 14:10:27 yjung Exp $
2 *
3 * This file is part of the crires 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: yjung $
23 * $Date: 2012-09-19 14:10:27 $
24 * $Revision: 1.35 $
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
36#include "crires_recipe.h"
37
38#include "crires_extract.h"
39
40/*-----------------------------------------------------------------------------
41 Define
42 -----------------------------------------------------------------------------*/
43
44#define RECIPE_STRING "crires_util_profile"
45
46/*-----------------------------------------------------------------------------
47 Functions prototypes
48 -----------------------------------------------------------------------------*/
49
50static int crires_util_profile_save(const cpl_table **,
51 const cpl_parameterlist *, cpl_frameset *) ;
52
53static char crires_util_profile_description[] =
54"This recipe accepts 1 parameter:\n"
55"First parameter: the profile produced by the spectrum extraction.\n"
56" (PRO TYPE = "CRIRES_PROTYPE_PROFILE")\n"
57"\n"
58"This recipe produces 1 file:\n"
59"First product: the table with the FWHMs values of the profile.\n"
60" (PRO TYPE = "CRIRES_PROTYPE_FWHM")\n" ;
61
62CRIRES_RECIPE_DEFINE(crires_util_profile,
63 CRIRES_PARAM_DISPLAY,
64 "Compute the FWHM of the spectrum along lambda",
65 crires_util_profile_description) ;
66
67/*-----------------------------------------------------------------------------
68 Static variables
69 -----------------------------------------------------------------------------*/
70
71static struct {
72 /* Inputs */
73 int display ;
74 /* Outputs */
75 int win_mode ;
76 crires_illum_period period ;
77} crires_util_profile_config ;
78
79/*-----------------------------------------------------------------------------
80 Functions code
81 -----------------------------------------------------------------------------*/
82
83/*----------------------------------------------------------------------------*/
90/*----------------------------------------------------------------------------*/
91static int crires_util_profile(
92 cpl_frameset * frameset,
93 const cpl_parameterlist * parlist)
94{
95 cpl_frame * fr ;
96 const char * fname ;
97 cpl_imagelist * profile ;
98 cpl_propertylist * plist ;
99 const char * sval ;
100 cpl_table * tab[CRIRES_NB_DETECTORS] ;
101 cpl_bivector * centers_fwhms ;
102 int nbrows, ly ;
103 int i, j ;
104
105 /* Initialise */
106 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
107 tab[i] = NULL ;
108 }
109 ly = -1 ;
110
111 /* Retrieve input parameters */
112 crires_util_profile_config.display = crires_parameterlist_get_int(parlist,
113 RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
114
115 /* Identify the RAW and CALIB frames in the input frameset */
116 if (crires_dfs_set_groups(frameset, NULL)) {
117 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
118 return -1 ;
119 }
120
121 /* Get the detector illumination period */
122 fr = cpl_frameset_get_position(frameset, 0);
123 crires_util_profile_config.period =
124 crires_get_detector_illum_period(cpl_frame_get_filename(fr)) ;
125 if (crires_util_profile_config.period == CRIRES_ILLUM_UNKNOWN) {
126 cpl_msg_error(__func__,
127 "Cannot determine the detector illumination period") ;
128 return -1 ;
129 }
130
131 /* Windowing mode ? */
132 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
133 return -1 ;
134 sval = crires_pfits_get_ncorrs(plist) ;
135 if (!strcmp(sval, "FowlerNsampGRstWin")) {
136 crires_util_profile_config.period = CRIRES_ILLUM_FULL_DETECTOR ;
137 crires_util_profile_config.win_mode = 1 ;
138 } else {
139 crires_util_profile_config.win_mode = 0 ;
140 }
141 cpl_propertylist_delete(plist) ;
142
143 /* Display the Detector illumination */
144 crires_display_detector_illum(crires_util_profile_config.period) ;
145
146 /* The first frame must be the profile images */
147 cpl_msg_info(__func__, "Input image loading") ;
148 fname = cpl_frame_get_filename(fr) ;
149 profile = crires_load_file(fname, crires_util_profile_config.period,
150 CPL_TYPE_FLOAT) ;
151
152 /* Get the FWHMs */
153 cpl_msg_info(__func__, "FWHM computation") ;
154 cpl_msg_indent_more() ;
155 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
156 /* Skip some detectors in windowing mode */
157 if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
158 (crires_util_profile_config.win_mode == 1)) {
159 continue ;
160 }
161 cpl_msg_info(__func__, "Chip number %d", i+1) ;
162 cpl_msg_indent_more() ;
163 if ((centers_fwhms = crires_extract_spatial_fwhm(
164 cpl_imagelist_get(profile, i))) == NULL) {
165 cpl_msg_error(__func__,
166 "Cannot get the FWHM for chip number %d", i+1) ;
167 continue ;
168 }
169 cpl_msg_indent_less() ;
170
171 /* Correct for the position */
172 if (i+1 == 1)
173 ly = crires_get_detector_ly1(crires_util_profile_config.period) ;
174 if (i+1 == 2)
175 ly = crires_get_detector_ly2(crires_util_profile_config.period) ;
176 if (i+1 == 3)
177 ly = crires_get_detector_ly3(crires_util_profile_config.period) ;
178 if (i+1 == 4)
179 ly = crires_get_detector_ly4(crires_util_profile_config.period) ;
180 /* If ly not found, use the bottom of the image */
181 if (ly < 0) ly = 1 ;
182 cpl_vector_add_scalar(cpl_bivector_get_x(centers_fwhms),(double)(ly-1));
183
184 /* Plot if requested */
185 if(crires_util_profile_config.display == i+1) {
186 cpl_plot_vector(
187"set grid;set xlabel 'Position (pixels)';set ylabel 'Y positions (pixels)';",
188 "t 'Y positions' w lines", "", cpl_bivector_get_x(centers_fwhms));
189 cpl_plot_vector(
190 "set grid;set xlabel 'Position (pixels)';set ylabel 'FWHM (pixels)';",
191 "t 'FWHMs' w lines", "", cpl_bivector_get_y(centers_fwhms));
192 }
193
194 /* Create a table from the vector */
195 nbrows = cpl_image_get_size_x(cpl_imagelist_get(profile, i)) ;
196 tab[i] = cpl_table_new(nbrows) ;
197 cpl_table_new_column(tab[i], CRIRES_COL_WL_DETPOSX, CPL_TYPE_INT) ;
198 cpl_table_new_column(tab[i], CRIRES_COL_DETPOSY, CPL_TYPE_DOUBLE) ;
199 cpl_table_new_column(tab[i], CRIRES_COL_FWHM, CPL_TYPE_DOUBLE) ;
200 for (j=0 ; j<nbrows ; j++) {
201 cpl_table_set_int(tab[i], CRIRES_COL_WL_DETPOSX, j, j+1) ;
202 cpl_table_set_double(tab[i], CRIRES_COL_DETPOSY, j,
203 cpl_vector_get(cpl_bivector_get_x(centers_fwhms), j));
204 cpl_table_set_double(tab[i], CRIRES_COL_FWHM, j,
205 cpl_vector_get(cpl_bivector_get_y(centers_fwhms), j));
206 }
207 cpl_bivector_delete(centers_fwhms) ;
208 }
209 cpl_msg_indent_less() ;
210 cpl_imagelist_delete(profile) ;
211
212 /* Reconstruct chips in windowing mode using detector 2 */
213 if (crires_util_profile_config.win_mode == 1) {
214 if (tab[1] != NULL) {
215 tab[0] = cpl_table_duplicate(tab[1]) ;
216 cpl_table_set_size(tab[0], 0) ;
217 tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(tab[0]) ;
218 }
219 }
220
221 /* Save the product */
222 cpl_msg_info(__func__, "Save the product") ;
223 cpl_msg_indent_more() ;
224 if (crires_util_profile_save((const cpl_table **)tab, parlist, frameset)) {
225 cpl_msg_error(__func__, "Cannot save the product") ;
226 cpl_msg_indent_less() ;
227 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) cpl_table_delete(tab[i]) ;
228 return -1 ;
229 }
230 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) cpl_table_delete(tab[i]) ;
231 cpl_msg_indent_less() ;
232
233 /* Return */
234 if (cpl_error_get_code()) return -1 ;
235 else return 0 ;
236}
237
238/*----------------------------------------------------------------------------*/
246/*----------------------------------------------------------------------------*/
247static int crires_util_profile_save(
248 const cpl_table ** out,
249 const cpl_parameterlist * parlist,
250 cpl_frameset * set)
251{
252 const char * recipe_name = "crires_util_profile" ;
253
254 /* Write the table */
255 crires_table_save(set,
256 parlist,
257 set,
258 out,
259 recipe_name,
260 CRIRES_FWHM_TAB,
261 CRIRES_PROTYPE_FWHM,
262 NULL,
263 NULL,
264 PACKAGE "/" PACKAGE_VERSION,
265 "crires_util_profile.fits") ;
266
267 return 0;
268}