NACO Pipeline Reference Manual  4.4.0
naco_img_slitpos.c
1 /* $Id: naco_img_slitpos.c,v 1.60 2010-03-02 15:29:12 llundin Exp $
2  *
3  * This file is part of the NACO 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., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: llundin $
23  * $Date: 2010-03-02 15:29:12 $
24  * $Revision: 1.60 $
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 "naco_recipe.h"
37 #include "irplib_slitpos.h"
38 
39 /*-----------------------------------------------------------------------------
40  Recipe defines
41  -----------------------------------------------------------------------------*/
42 
43 #define RECIPE_STRING "naco_img_slitpos"
44 
45 /*-----------------------------------------------------------------------------
46  Private Functions prototypes
47  -----------------------------------------------------------------------------*/
48 
49 static cpl_error_code naco_img_slitpos_qc(cpl_propertylist *,
50  cpl_propertylist *,
51  const irplib_framelist *);
52 
53 static cpl_error_code naco_img_slitpos_save(cpl_frameset *,
54  const cpl_parameterlist *,
55  const cpl_propertylist *,
56  const cpl_propertylist *,
57  const cpl_table *, int,
58  const irplib_framelist *);
59 
60 NACO_RECIPE_DEFINE(naco_img_slitpos, NACO_PARAM_SLIT_W,
61  "Slit Position recipe",
62  RECIPE_STRING " -- NACO imaging slit position recipe.\n"
63  "The Set Of Frames (sof-file) must specify at least one file "
64  "which must be tagged\n"
65  "raw-file.fits " NACO_IMG_SLITPOS_RAW "\n"
66  "\n"
67  "Each image is flipped around the x=y axis before the "
68  "analysis.");
69 
70 /*----------------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------------*/
75 
76 /*-----------------------------------------------------------------------------
77  Functions code
78  -----------------------------------------------------------------------------*/
79 
80 
81 /*----------------------------------------------------------------------------*/
88 /*----------------------------------------------------------------------------*/
89 static int naco_img_slitpos(cpl_frameset * framelist,
90  const cpl_parameterlist * parlist)
91 {
92  irplib_framelist* allframes = NULL;
93  irplib_framelist* rawframes = NULL;
94  irplib_framelist* oneframe = NULL;
95  cpl_frame * frame = NULL;
96  cpl_image * image = NULL;
97  cpl_table * out_table = NULL;
98  cpl_propertylist* qclist = cpl_propertylist_new();
99  cpl_propertylist* paflist = cpl_propertylist_new();
100 
101  int slit_width;
102  int nframes;
103  int i;
104 
105 
106  slit_width = naco_parameterlist_get_int(parlist, RECIPE_STRING,
107  NACO_PARAM_SLIT_W);
108  bug_if(0);
109 
110  /* Identify the RAW and CALIB frames in the input frameset */
111  skip_if (naco_dfs_set_groups(framelist));
112 
113  allframes = irplib_framelist_cast(framelist);
114  bug_if(allframes == NULL);
115 
116  irplib_check(rawframes
117  = irplib_framelist_extract(allframes, NACO_IMG_SLITPOS_RAW),
118  "Could not load the raw frames");
119  irplib_framelist_empty(allframes);
120 
121  nframes = irplib_framelist_get_size(rawframes);
122 
123  oneframe = irplib_framelist_new();
124 
125  /* Reduce each frame separately */
126  for (i=0 ; i < nframes ; i++) {
127  const char * name;
128  int slit_length;
129  double xpos, ypos, angle;
130  double slit_y0, slit_yn;
131  double slit_c0, slit_cn;
132 
133  /* Slit analysis */
134  cpl_msg_info(cpl_func, "Analysing slit of frame %d out of %d", i+1,
135  nframes);
136 
137  frame = irplib_framelist_unset(rawframes, 0, NULL);
138  bug_if(irplib_framelist_set(oneframe, frame, 0));
139 
140  name = cpl_frame_get_filename(frame);
141  frame = NULL;
142  bug_if(name == NULL);
143 
144  irplib_check(image = cpl_image_load(name, CPL_TYPE_FLOAT, 0, 0),
145  "Could not load FITS-image");
146 
147  bug_if(cpl_image_flip(image, 1));
148 
149  out_table = irplib_slitpos_analysis(image, slit_width, NULL);
150  cpl_image_delete(image);
151  image = NULL;
152  skip_if (out_table == NULL);
153 
154  /* THE SLIT LENGTH */
155  /* Find the slit angle in degrees with the horizontal axis */
156  slit_length = cpl_table_get_nrow(out_table);
157 
158  slit_y0 = cpl_table_get(out_table, "SLIT_Y", 0, NULL);
159  slit_yn = cpl_table_get(out_table, "SLIT_Y", slit_length-1, NULL);
160  bug_if(0);
161 
162  slit_c0 = cpl_table_get(out_table, "SLIT_CENTER", 0, NULL);
163  slit_cn = cpl_table_get(out_table, "SLIT_CENTER", slit_length-1, NULL);
164  bug_if(0);
165 
166  /* THE ANGLE: 180 degrees for a perfectly aligned slit */
167  angle = 180.0 + CPL_MATH_DEG_RAD * atan2(slit_cn - slit_c0,
168  slit_yn - slit_y0);
169 
170  /* THE SLIT CENTER POSITION */
171  ypos = (slit_c0 + slit_cn)/2.0;
172  xpos = (slit_y0 + slit_yn)/2.0;
173 
174  /* Print the results */
175  cpl_msg_info(cpl_func, "Slit angle : %g", angle);
176  cpl_msg_info(cpl_func, "Slit length : %d", slit_length);
177  cpl_msg_info(cpl_func, "Slit center : %g %g", xpos, ypos);
178 
179  skip_if(irplib_framelist_load_propertylist_all(oneframe, 0, "^("
180  NACO_PFITS_REGEXP_SLITPOS "|"
181  NACO_PFITS_REGEXP_SLITPOS_PAF
182  ")$", CPL_FALSE));
183 
184  /* Add QC parameters */
185  bug_if(cpl_propertylist_append_double(qclist, "ESO QC SLIT POSANG",
186  angle));
187  bug_if(cpl_propertylist_append_double(qclist, "ESO QC SLIT XPOS",
188  xpos));
189  bug_if(cpl_propertylist_append_double(qclist, "ESO QC SLIT YPOS",
190  ypos));
191 
192  skip_if (naco_img_slitpos_qc(qclist, paflist, oneframe));
193 
194  /* PRO.CATG */
195  bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
196  NACO_IMG_SLITPOS_RES));
197 
198  skip_if (naco_img_slitpos_save(framelist, parlist, qclist, paflist,
199  out_table, i+1, oneframe));
200 
201  cpl_table_delete(out_table);
202  out_table = NULL;
203 
204  cpl_propertylist_empty(qclist);
205  cpl_propertylist_empty(paflist);
206 
207  /* Empty the list - and deallocate the propertylists */
208  irplib_framelist_empty(oneframe);
209 
210  bug_if(irplib_framelist_get_size(oneframe) != 0);
211 
212  }
213 
214  bug_if(irplib_framelist_get_size(rawframes) != 0);
215 
216  end_skip;
217 
218  cpl_image_delete(image);
219  cpl_table_delete(out_table);
220 
221  irplib_framelist_delete(oneframe);
222  irplib_framelist_delete(allframes);
223  irplib_framelist_delete(rawframes);
224  cpl_propertylist_delete(qclist);
225  cpl_propertylist_delete(paflist);
226 
227  cpl_frame_delete(frame);
228 
229  return cpl_error_get_code();
230 }
231 
232 /*----------------------------------------------------------------------------*/
240 /*----------------------------------------------------------------------------*/
241 static cpl_error_code naco_img_slitpos_qc(cpl_propertylist * qclist,
242  cpl_propertylist * paflist,
243  const irplib_framelist * rawframes)
244 {
245 
246  cpl_errorstate cleanstate = cpl_errorstate_get();
247  const cpl_propertylist * reflist
249  const char pafcopy[] = "^(" NACO_PFITS_REGEXP_SLITPOS_PAF ")$";
250  const char * filter;
251 
252 
253  bug_if (0);
254 
255  filter = naco_pfits_get_filter(reflist);
256  if (cpl_error_get_code())
257  naco_error_reset("Could not get FITS key:");
258  else
259  bug_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS",
260  filter));
261 
262  /* THE PAF FILE FOR QC PARAMETERS */
263 
264  /* Get the QC params in qclist and keys for paf in paflist */
265  bug_if(cpl_propertylist_copy_property_regexp(paflist, reflist, pafcopy, 0));
266 
267  bug_if(cpl_propertylist_append(paflist, qclist));
268 
269 
270  end_skip;
271 
272  return cpl_error_get_code();
273 }
274 
275 /*----------------------------------------------------------------------------*/
287 /*----------------------------------------------------------------------------*/
288 static cpl_error_code naco_img_slitpos_save(cpl_frameset * set,
289  const cpl_parameterlist * parlist,
290  const cpl_propertylist * qclist,
291  const cpl_propertylist * paflist,
292  const cpl_table * out_table,
293  int file_nb,
294  const irplib_framelist * oneframe)
295 {
296 
297  cpl_frameset * rawframes = irplib_frameset_cast(oneframe);
298  char * filename = NULL;
299 
300 
301  bug_if (0);
302 
303  /* Write the FITS file */
304  filename = cpl_sprintf(RECIPE_STRING "_%02d" CPL_DFS_FITS, file_nb);
305  skip_if (irplib_dfs_save_table(set, parlist, rawframes, out_table, NULL,
306  RECIPE_STRING, NACO_IMG_SLITPOS_RES, qclist, NULL,
307  naco_pipe_id, filename));
308 
309  cpl_free(filename);
310  filename = cpl_sprintf(RECIPE_STRING "_%02d" CPL_DFS_PAF, file_nb);
311  skip_if (cpl_dfs_save_paf("NACO", RECIPE_STRING, paflist, filename));
312 
313  end_skip;
314 
315  cpl_free(filename);
316  cpl_frameset_delete(rawframes);
317 
318  return cpl_error_get_code();
319 }
cpl_frameset * irplib_frameset_cast(const irplib_framelist *self)
Create a CPL frameset from an irplib_framelist.
cpl_error_code irplib_dfs_save_table(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_table *table, const cpl_propertylist *tablelist, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a table as a DFS-compliant pipeline product.
Definition: irplib_utils.c:351
int naco_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: naco_dfs.c:62
cpl_error_code irplib_framelist_set(irplib_framelist *self, cpl_frame *frame, int pos)
Add a frame to a framelist.
int naco_parameterlist_get_int(const cpl_parameterlist *self, const char *recipe, naco_parameter bitmask)
Retrieve the value of a NACO integer parameter.
const cpl_propertylist * irplib_framelist_get_propertylist_const(const irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
void irplib_framelist_empty(irplib_framelist *self)
Erase all frames from a framelist.
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
cpl_frame * irplib_framelist_unset(irplib_framelist *self, int pos, cpl_propertylist **plist)
Erase a frame from a framelist and return it to the caller.
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
cpl_table * irplib_slitpos_analysis(const cpl_image *imslit, int slit_max_width, double *slit_flux)
Detect the slit position, detect its ends, extract a thin image containing only the slit and find its...
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
irplib_framelist * irplib_framelist_new(void)
Create an empty framelist.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.
const char * naco_pfits_get_filter(const cpl_propertylist *self)
find out the filter
Definition: naco_pfits.c:167