IIINSTRUMENT Pipeline Reference Manual 0.1.16
rrrecipe_postproc.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_postproc"
44#define CONTEXT "iiinstrument."RECIPE_NAME
45
46/*----------------------------------------------------------------------------*/
50/*----------------------------------------------------------------------------*/
51
52/*----------------------------------------------------------------------------*/
56/*----------------------------------------------------------------------------*/
57
58/*----------------------------------------------------------------------------*/
62/*----------------------------------------------------------------------------*/
63
64static char rrrecipe_postproc_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-SCIENCE.fits " RRRECIPE_OUT_PROCATG "\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_postproc,
75 IIINSTRUMENT_BINARY_VERSION,
76 "Firstname Lastname",
77 PACKAGE_BUGREPORT,
78 "2021",
79 "An example recipe.",
80 rrrecipe_postproc_description);
81
82/*----------------------------------------------------------------------------*/
86/*----------------------------------------------------------------------------*/
87
90/*----------------------------------------------------------------------------*/
94/*----------------------------------------------------------------------------*/
95
96/*----------------------------------------------------------------------------*/
106/*----------------------------------------------------------------------------*/
107static int rrrecipe_postproc(
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_OUT_PROCATG)) {
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_OUT_PROCATG);
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 /* Do some operations */
208 if (bool_option == CPL_TRUE) {
209 cpl_msg_info(cpl_func, "Performing algorithm" );
210 }
211
212 applist = cpl_propertylist_new();
213
214 /* Add the product category */
215 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
216 RRRECIPE_OUT_POSTPROC_PROCATG);
217
218 /* Add a QC parameter */
219 cpl_propertylist_append_double(applist, "ESO QC QCPARAM", qc_param);
220
221 /* HOW TO SAVE A DFS-COMPLIANT PRODUCT TO DISK */
222 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
223 CPL_BPP_IEEE_FLOAT, RECIPE_NAME, applist,
224 NULL, PACKAGE "/" PACKAGE_VERSION,
225 "rrrecipe_postproc.fits")) {
226 /* Propagate the error */
227 (void)cpl_error_set_where(cpl_func);
228 }
229
230
231 /* Cleanup */
232 cpl_image_delete(image);
233 cpl_propertylist_delete(applist);
234
235
236 return cpl_error_get_code();
237}
238
241/*----------------------------------------------------------------------------*/
250/*----------------------------------------------------------------------------*/
251static cpl_error_code rrrecipe_postproc_fill_parameterlist(
252 cpl_parameterlist *self)
253{
254 /* Add the different default parameters to the recipe */
255 cpl_errorstate prestate = cpl_errorstate_get();
256
257 /* Fill the parameters list */
258 cpl_parameter *par;
259
260 /* --stropt */
261 par = cpl_parameter_new_value(CONTEXT".str_option",
262 CPL_TYPE_STRING, "the string option",
263 CONTEXT, "NONE");
264 cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI, "stropt");
265 cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
266 cpl_parameterlist_append(self, par);
267
268 /* --boolopt */
269 par = cpl_parameter_new_value(CONTEXT".bool_option",
270 CPL_TYPE_BOOL, "a flag",
271 CONTEXT, TRUE);
272 cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI, "boolopt");
273 cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
274 cpl_parameterlist_append(self, par);
275
276
277 /* Check possible errors */
278 if (!cpl_errorstate_is_equal(prestate)) {
279 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
280 "rrrecipe_postproc_fill_parameterlist failed!");
281 }
282
283 return CPL_ERROR_NONE;
284}
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