File: fors_subtract_sky_lss.c

package info (click to toggle)
cpl-plugin-fors 5.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,664 kB
  • ctags: 3,906
  • sloc: ansic: 68,347; cpp: 16,320; sh: 11,458; python: 1,123; makefile: 823
file content (440 lines) | stat: -rw-r--r-- 13,552 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* $Id: fors_subtract_sky_lss.c,v 1.6 2013-10-09 15:59:38 cgarcia Exp $
 *
 * This file is part of the FORS Data Reduction Pipeline
 * Copyright (C) 2002-2010 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * $Author: cgarcia $
 * $Date: 2013-10-09 15:59:38 $
 * $Revision: 1.6 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <math.h>
#include <cpl.h>
#include <moses.h>
#include <fors_dfs.h>

static int fors_subtract_sky_lss_create(cpl_plugin *);
static int fors_subtract_sky_lss_exec(cpl_plugin *);
static int fors_subtract_sky_lss_destroy(cpl_plugin *);
static int fors_subtract_sky_lss(cpl_parameterlist *, cpl_frameset *);

static char fors_subtract_sky_lss_description[] =
"This recipe is used to subtract the sky from wavelength calibrated\n"
"scientific spectra produced by the recipe fors_resample. A simple median\n"
"signal level is subtracted from each image column.\n"
"In the table below the MXU acronym can be read alternatively as MOS\n"
"and LSS, depending on the instrument mode of the input data. The acronym\n"
"SCI may be read STD in case of standard stars observations.\n"
"Note that only LSS or LSS-like MOS/MXU data are to be processed by this\n"
"recipe.\n\n"
"Input files:\n\n"
"  DO category:               Type:       Explanation:         Required:\n"
"  MAPPED_ALL_SCI_MXU         Raw         Scientific exposure     Y\n\n"
"Output files:\n\n"
"  DO category:               Data type:  Explanation:\n"
"  MAPPED_SCI_MXU             FITS image  Rectified scientific spectra\n"
"  MAPPED_SKY_SCI_MXU         FITS image  Rectified sky spectra\n\n";

#define fors_subtract_sky_lss_exit(message)            \
{                                             \
if ((const char *)message != NULL) cpl_msg_error(recipe, message);  \
cpl_image_delete(skymap);                     \
cpl_image_delete(sky);                        \
cpl_image_delete(spectra);                    \
cpl_propertylist_delete(header);              \
cpl_msg_indent_less();                        \
return -1;                                    \
}


#define fors_subtract_sky_lss_exit_memcheck(message)   \
{                                             \
if ((const char *)message != NULL) cpl_msg_info(recipe, message);   \
cpl_image_delete(skymap);                     \
cpl_image_delete(sky);                        \
cpl_image_delete(spectra);                    \
cpl_propertylist_delete(header);              \
cpl_msg_indent_less();                        \
return 0;                                     \
}


/**
 * @brief    Build the list of available plugins, for this module. 
 *
 * @param    list    The plugin list
 *
 * @return   0 if everything is ok, -1 otherwise
 *
 * Create the recipe instance and make it available to the application 
 * using the interface. This function is exported.
 */

int cpl_plugin_get_info(cpl_pluginlist *list)
{
    cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe );
    cpl_plugin *plugin = &recipe->interface;

    cpl_plugin_init(plugin,
                    CPL_PLUGIN_API,
                    FORS_BINARY_VERSION,
                    CPL_PLUGIN_TYPE_RECIPE,
                    "fors_subtract_sky_lss",
                    "Subtract sky from calibrated long slit exposure",
                    fors_subtract_sky_lss_description,
                    "Carlo Izzo",
                    PACKAGE_BUGREPORT,
    "This file is currently part of the FORS Instrument Pipeline\n"
    "Copyright (C) 2002-2010 European Southern Observatory\n\n"
    "This program is free software; you can redistribute it and/or modify\n"
    "it under the terms of the GNU General Public License as published by\n"
    "the Free Software Foundation; either version 2 of the License, or\n"
    "(at your option) any later version.\n\n"
    "This program is distributed in the hope that it will be useful,\n"
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
    "GNU General Public License for more details.\n\n"
    "You should have received a copy of the GNU General Public License\n"
    "along with this program; if not, write to the Free Software Foundation,\n"
    "Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n",
                    fors_subtract_sky_lss_create,
                    fors_subtract_sky_lss_exec,
                    fors_subtract_sky_lss_destroy);

    cpl_pluginlist_append(list, plugin);
    
    return 0;
}


/**
 * @brief    Setup the recipe options    
 *
 * @param    plugin  The plugin
 *
 * @return   0 if everything is ok
 *
 * Defining the command-line/configuration parameters for the recipe.
 */

static int fors_subtract_sky_lss_create(cpl_plugin *plugin)
{
    cpl_recipe    *recipe;
/* Uncomment in case parameters are defined
    cpl_parameter *p;
*/

    /* 
     * Check that the plugin is part of a valid recipe 
     */

    if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
        recipe = (cpl_recipe *)plugin;
    else 
        return -1;

    /* 
     * Create the parameters list in the cpl_recipe object 
     */

    recipe->parameters = cpl_parameterlist_new(); 

    return 0;
}


/**
 * @brief    Execute the plugin instance given by the interface
 *
 * @param    plugin  the plugin
 *
 * @return   0 if everything is ok
 */

static int fors_subtract_sky_lss_exec(cpl_plugin *plugin)
{
    cpl_recipe *recipe;
    
    if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
        recipe = (cpl_recipe *)plugin;
    else 
        return -1;

    return fors_subtract_sky_lss(recipe->parameters, recipe->frames);
}


/**
 * @brief    Destroy what has been created by the 'create' function
 *
 * @param    plugin  The plugin
 *
 * @return   0 if everything is ok
 */

static int fors_subtract_sky_lss_destroy(cpl_plugin *plugin)
{
    cpl_recipe *recipe;
    
    if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
        recipe = (cpl_recipe *)plugin;
    else 
        return -1;

    cpl_parameterlist_delete(recipe->parameters); 

    return 0;
}


/**
 * @brief    Interpret the command line options and execute the data processing
 *
 * @param    parlist     The parameters list
 * @param    frameset    The set-of-frames
 *
 * @return   0 if everything is ok
 */

static int fors_subtract_sky_lss(cpl_parameterlist *parlist, 
                                 cpl_frameset *frameset)
{

    const char *recipe = "fors_subtract_sky_lss";


    /*
     * Input parameters (none)
     */

    /*
     * CPL objects
     */

    cpl_image        *spectra   = NULL;
    cpl_image        *skymap    = NULL;
    cpl_image        *sky       = NULL;
    cpl_table        *maskslits = NULL;

    cpl_propertylist *header    = NULL;

    /*
     * Auxiliary variables
     */

    char        version[80];
    char       *instrume = NULL;
    const char *mapped_science_tag;
    const char *mapped_science_sky_tag;
    const char *mapped_sky_tag;
    int         mxu, mos, lss;
    int         treat_as_lss = 0;
    int         nscience;
    double      mxpos;
    int         nx, ny;
    int         standard;
    float      *data;
    float      *sdata;
    int         i, j;


    snprintf(version, 80, "%s-%s", PACKAGE, PACKAGE_VERSION);

    cpl_msg_set_indentation(2);


    /* 
     * Get configuration parameters (none)
     */

    /* 
     * Check input set-of-frames
     */

    cpl_msg_indent_less();
    cpl_msg_info(recipe, "Check input set-of-frames:");
    cpl_msg_indent_more();

    mxu = cpl_frameset_count_tags(frameset, "MAPPED_ALL_SCI_MXU");
    mos = cpl_frameset_count_tags(frameset, "MAPPED_ALL_SCI_MOS");
    lss = cpl_frameset_count_tags(frameset, "MAPPED_ALL_SCI_LSS");
    standard = 0;

    if (mxu + mos + lss == 0) {
        mxu = cpl_frameset_count_tags(frameset, "MAPPED_ALL_STD_MXU");
        mos = cpl_frameset_count_tags(frameset, "MAPPED_ALL_STD_MOS");
        lss = cpl_frameset_count_tags(frameset, "MAPPED_ALL_STD_LSS");
        standard = 1;
    }

    if (mxu + mos + lss == 0)
        fors_subtract_sky_lss_exit("Missing input scientific frame");

    nscience = mxu + mos + lss;

    if (nscience > 1)
        fors_subtract_sky_lss_exit("More than one scientific frame in input"); 

    if (mxu) {
        if (standard) {
            cpl_msg_info(recipe, "MXU data found");
            mapped_science_tag     = "MAPPED_STD_MXU";
            mapped_science_sky_tag = "MAPPED_ALL_STD_MXU";
            mapped_sky_tag         = "MAPPED_SKY_STD_MXU";
        }
        else {
            cpl_msg_info(recipe, "MXU data found");
            mapped_science_tag     = "MAPPED_SCI_MXU";
            mapped_science_sky_tag = "MAPPED_ALL_SCI_MXU";
            mapped_sky_tag         = "MAPPED_SKY_SCI_MXU";
        }
    }

    if (lss) {
        if (standard) {
            cpl_msg_info(recipe, "LSS data found");
            mapped_science_tag      = "MAPPED_STD_LSS";
            mapped_science_sky_tag  = "MAPPED_ALL_STD_LSS";
            mapped_sky_tag          = "MAPPED_SKY_STD_LSS";
        }
        else {
            cpl_msg_info(recipe, "LSS data found");
            mapped_science_tag      = "MAPPED_SCI_LSS";
            mapped_science_sky_tag  = "MAPPED_ALL_SCI_LSS";
            mapped_sky_tag          = "MAPPED_SKY_SCI_LSS";
        }
    }

    if (mos) {
        if (standard) {
            cpl_msg_info(recipe, "MOS data found");
            mapped_science_tag      = "MAPPED_STD_MOS";
            mapped_science_sky_tag  = "MAPPED_ALL_STD_MOS";
            mapped_sky_tag          = "MAPPED_SKY_STD_MOS";
        }
        else {
            cpl_msg_info(recipe, "MOS data found");
            mapped_science_tag      = "MAPPED_SCI_MOS";
            mapped_science_sky_tag  = "MAPPED_ALL_SCI_MOS";
            mapped_sky_tag          = "MAPPED_SKY_SCI_MOS";
        }
    }

    /*
     * Loading input data
     */

    cpl_msg_info(recipe, "Load mapped scientific exposure...");
    cpl_msg_indent_more();

    spectra = dfs_load_image(frameset, mapped_science_sky_tag, 
                             CPL_TYPE_FLOAT, 0, 0);

    if (spectra == NULL)
        fors_subtract_sky_lss_exit("Cannot load input frame");

    header = dfs_load_header(frameset, mapped_science_sky_tag, 0);

    if (header == NULL)
        fors_subtract_sky_lss_exit("Cannot load input frame header");

    instrume = (char *)cpl_propertylist_get_string(header, "INSTRUME");
    if (instrume == NULL)
        fors_subtract_sky_lss_exit("Missing keyword INSTRUME in scientific header");
    instrume = cpl_strdup(instrume);

    if (instrume[4] == '1')
        snprintf(version, 80, "%s/%s", "fors1", VERSION);
    if (instrume[4] == '2')
        snprintf(version, 80, "%s/%s", "fors2", VERSION);

    cpl_free(instrume); instrume = NULL;

    cpl_msg_indent_less();

    if (mos || mxu) {
        int nslits_out_det = 0;

        if (mos)
            maskslits = mos_load_slits_fors_mos(header, &nslits_out_det);
        else
            maskslits = mos_load_slits_fors_mxu(header);

        /*
         * Check if all slits have the same X offset: in such case,
         * treat the observation as a long-slit one!
         */

        treat_as_lss = fors_mos_is_lss_like(maskslits, nslits_out_det);

        cpl_table_delete(maskslits); maskslits = NULL;

        if (treat_as_lss)
            cpl_msg_info(recipe, "All MOS slits have the same offset: %.2f\n"
                         "The LSS data reduction strategy is applied.",
                         mxpos);
        else
            fors_subtract_sky_lss_exit("This recipe can only be used "
                                       "with LSS-like data");
    }

    nx = cpl_image_get_size_x(spectra);
    ny = cpl_image_get_size_y(spectra);

    skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
    sky    = cpl_image_collapse_median_create(spectra, 0, 0, 1);

    data   = cpl_image_get_data(skymap);

    for (i = 0; i < ny; i++) {
        sdata  = cpl_image_get_data(sky);
        for (j = 0; j < nx; j++) {
            *data++ = *sdata++;
        }
    }

    cpl_image_delete(sky); sky = NULL;
    cpl_image_subtract(spectra, skymap);

    if (dfs_save_image(frameset, skymap, mapped_sky_tag, header,
                       parlist, recipe, version))
        fors_subtract_sky_lss_exit(NULL);

    cpl_image_delete(skymap); skymap = NULL;

    if (dfs_save_image(frameset, spectra, mapped_science_tag, header,
                       parlist, recipe, version))
        fors_subtract_sky_lss_exit(NULL);

    cpl_image_delete(spectra); spectra = NULL;

    cpl_propertylist_delete(header); header = NULL;

    if (cpl_error_get_code()) {
        cpl_msg_error(cpl_error_get_where(), "%s", cpl_error_get_message());
        fors_subtract_sky_lss_exit(NULL);
    }
    else 
        return 0;
}