IIINSTRUMENT Pipeline Reference Manual 0.1.15
rrrecipe_calib.c
1/*
2 * This file is part of the IIINSTRUMENT Pipeline
3 * Copyright (C) 2002-2017 European Southern Observatory
4 *
5 * This program 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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*----------------------------------------------------------------------------*/
28/*----------------------------------------------------------------------------*/
29
30#include "iiinstrument_utils.h"
31#include "iiinstrument_pfits.h"
32#include "iiinstrument_dfs.h"
33
34#include <cpl.h>
35#include <string.h>
36
37/*----------------------------------------------------------------------------*/
41/*----------------------------------------------------------------------------*/
42
43#define RECIPE_NAME "rrrecipe_calib"
44#define CONTEXT "iiinstrument."RECIPE_NAME
45
46/*----------------------------------------------------------------------------*/
50/*----------------------------------------------------------------------------*/
51
52/*----------------------------------------------------------------------------*/
56/*----------------------------------------------------------------------------*/
57
58/*----------------------------------------------------------------------------*/
62/*----------------------------------------------------------------------------*/
63
64static char rrrecipe_calib_description[] =
65 "This example text is used to describe the recipe.\n"
66 "The description should include the required FITS-files and\n"
67 "their associated tags, e.g.\n"
68 "IIINSTRUMENT-RRRECIPE-CALIB-raw-file.fits " RRRECIPE_CALIB_RAW "\n"
69 "\n"
70 "Additionally, it should describe functionality of the expected output."
71 "\n";
72
73/* Standard CPL recipe definition */
74cpl_recipe_define( rrrecipe_calib,
75 IIINSTRUMENT_BINARY_VERSION,
76 "Firstname Lastname",
77 PACKAGE_BUGREPORT,
78 "2021",
79 "An example recipe.",
80 rrrecipe_calib_description);
81
82/*----------------------------------------------------------------------------*/
86/*----------------------------------------------------------------------------*/
87
90/*----------------------------------------------------------------------------*/
94/*----------------------------------------------------------------------------*/
95
96/*----------------------------------------------------------------------------*/
106/*----------------------------------------------------------------------------*/
107static int rrrecipe_calib(
108 cpl_frameset * frameset,
109 const cpl_parameterlist * parlist)
110{
111 const cpl_parameter *param;
112 const char *str_option;
113 int bool_option;
114 cpl_frameset *rawframes;
115 const cpl_frame *firstframe;
116 double qc_param;
117 cpl_propertylist *plist;
118 cpl_propertylist *applist;
119 cpl_image *image;
120 int nraw;
121 int i;
122
123
124 if (iiinstrument_check_and_set_groups(frameset) != CPL_ERROR_NONE) {
125 return cpl_error_get_code();
126 }
127
128
129 /* Use the errorstate to detect an error in a function that does not
130 return an error code. */
131 cpl_errorstate prestate = cpl_errorstate_get();
132
133
134 /* HOW TO RETRIEVE INPUT PARAMETERS */
135
136 /* --stropt */
137 param = cpl_parameterlist_find_const(parlist, CONTEXT".str_option");
138 str_option = cpl_parameter_get_string(param);
139
140 /* --boolopt */
141 param = cpl_parameterlist_find_const(parlist, CONTEXT".bool_option");
142 bool_option = cpl_parameter_get_bool(param);
143
144 if (!cpl_errorstate_is_equal(prestate)) {
145 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
146 "Could not retrieve the input parameters");
147 }
148
149 /* HOW TO ACCESS INPUT DATA */
150
151 /* - A required file */
152 rawframes = cpl_frameset_new();
153 nraw = 0;
154 for (i = 0; i<cpl_frameset_get_size(frameset); i++) {
155 const cpl_frame * current_frame;
156 current_frame = cpl_frameset_get_position_const(frameset, i);
157 if(!strcmp(cpl_frame_get_tag(current_frame), RRRECIPE_CALIB_RAW)) {
158 cpl_frame * new_frame = cpl_frame_duplicate(current_frame);
159 cpl_frameset_insert(rawframes, new_frame);
160 nraw++;
161 }
162 }
163 if (nraw == 0) {
164 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
165 "SOF does not have any file tagged "
166 "with %s", RRRECIPE_CALIB_RAW);
167 }
168
169
170 /* HOW TO GET THE FIRST FRAME OF A FRAME */
171 firstframe = cpl_frameset_get_position_const(rawframes, 0);
172
173
174 /* HOW TO GET THE VALUE OF A FITS KEYWORD */
175 /* - Load only DETector related keys */
176 plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(firstframe),
177 0, "ESO DET ", 0);
178 if (plist == NULL) {
179 /* In this case an error message is added to the error propagation */
180 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
181 "Could not read the FITS header");
182 }
183
184 if (bool_option == CPL_FALSE) {
185 cpl_msg_info(cpl_func, "Bool option unset: String: %s", str_option);
186 }
187
188 qc_param = iiinstrument_pfits_get_dit(plist);
189 cpl_propertylist_delete(plist);
190
191
192 /* Check for a change in the CPL error state */
193 /* - if it did change then propagate the error and return */
194 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
195
196
197 /* NOW PERFORMING THE DATA REDUCTION */
198
199 /* Let's just load an image for the example */
200 image = cpl_image_load(cpl_frame_get_filename(firstframe), CPL_TYPE_FLOAT, 0,
201 0);
202 if (image == NULL) {
203 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
204 "Could not load the image");
205 }
206
207 applist = cpl_propertylist_new();
208
209 /* Add the product category */
210 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
211 RRRECIPE_OUT_CALIB_PROCATG);
212
213 /* Add a QC parameter */
214 cpl_propertylist_append_double(applist, "ESO QC QCPARAM", qc_param);
215
216 /* HOW TO SAVE A DFS-COMPLIANT PRODUCT TO DISK */
217 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
218 CPL_BPP_IEEE_FLOAT, RECIPE_NAME, applist,
219 NULL, PACKAGE "/" PACKAGE_VERSION,
220 "rrrecipe_calib.fits")) {
221 /* Propagate the error */
222 (void)cpl_error_set_where(cpl_func);
223 }
224
225
226 /* Cleanup */
227 cpl_image_delete(image);
228 cpl_propertylist_delete(applist);
229
230
231 return cpl_error_get_code();
232}
233
236/*----------------------------------------------------------------------------*/
245/*----------------------------------------------------------------------------*/
246static cpl_error_code rrrecipe_calib_fill_parameterlist(
247 cpl_parameterlist *self)
248{
249 /* Add the different default parameters to the recipe */
250 cpl_errorstate prestate = cpl_errorstate_get();
251
252 /* Fill the parameters list */
253 cpl_parameter *par;
254
255 /* --stropt */
256 par = cpl_parameter_new_value(CONTEXT".str_option",
257 CPL_TYPE_STRING, "the string option",
258 CONTEXT, "NONE");
259 cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI, "stropt");
260 cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
261 cpl_parameterlist_append(self, par);
262
263 /* --boolopt */
264 par = cpl_parameter_new_value(CONTEXT".bool_option",
265 CPL_TYPE_BOOL, "a flag",
266 CONTEXT, TRUE);
267 cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI, "boolopt");
268 cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
269 cpl_parameterlist_append(self, par);
270
271
272 /* Check possible errors */
273 if (!cpl_errorstate_is_equal(prestate)) {
274 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
275 "rrrecipe_calib_fill_parameterlist failed!");
276 }
277
278 return CPL_ERROR_NONE;
279}
double iiinstrument_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
cpl_error_code iiinstrument_check_and_set_groups(cpl_frameset *frameset)
check the entries in the recipe and classify the frameset with the tags