MOONS Pipeline Reference Manual 0.13.2
moo_create_s1d.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include <math.h>
28#include <string.h>
29#include <cpl.h>
30
31#include "moo_create_s1d.h"
32#include "moo_fits.h"
33#include "moo_pfits.h"
34#include "moo_badpix.h"
35#include "moo_utils.h"
36#include "moo_dfs.h"
37
38/*----------------------------------------------------------------------------*/
43/*----------------------------------------------------------------------------*/
46/*-----------------------------------------------------------------------------
47 Function codes
48 -----------------------------------------------------------------------------*/
49
50static moo_s1d *
51_moo_create_s1d_single(moo_sci_single *sci_single,
52 int indextarg,
53 const char *targname,
54 double talpha,
55 double tdelta,
56 double exptime,
57 cpl_propertylist *sci_header)
58{
59 cpl_propertylist *pheader = NULL;
60 cpl_propertylist *header = NULL;
61 cpl_table *data = NULL;
62 cpl_image *iflux = NULL;
63 cpl_image *ierr = NULL;
64 cpl_image *isky = NULL;
65 moo_s1d *result = NULL;
66
67 cpl_ensure(sci_single != NULL, CPL_ERROR_NULL_INPUT, NULL);
68
69 result = moo_s1d_new();
70
71 char *filename = cpl_strdup(sci_single->filename);
72
73 char *basename = strrchr(filename, '/');
74 if (basename == NULL) {
75 basename = filename;
76 }
77 else {
78 basename = basename + 1;
79 }
80 int len = strlen(basename);
81 basename[len - 5] = '\0';
82 result->filename =
83 cpl_sprintf("%s_%s_%s.fits", basename, sci_single->extname, targname);
84 pheader = cpl_propertylist_duplicate(sci_header);
85 moo_pfits_update_ra(pheader, talpha);
86 moo_pfits_update_dec(pheader, tdelta);
87 cpl_propertylist_set_double(pheader, MOO_PFITS_EXPTIME, exptime);
88
89 result->primary_header = pheader;
90 header = cpl_propertylist_duplicate(sci_single->header);
91 result->header = header;
92 double crpix1 = moo_pfits_get_crpix1(header);
93 double crval1 = moo_pfits_get_crval1(header);
94 double cd1_1 = moo_pfits_get_cd1_1(header);
95 int naxis1 = moo_pfits_get_naxis1(header);
96 double wmin = crval1 + (1 - crpix1) * cd1_1;
97 double wmax = crval1 + (naxis1 - crpix1) * cd1_1;
98 cpl_propertylist_set_string(header, MOO_PFITS_EXTNAME,
99 MOO_S1D_SPETCRUM_EXTNAME);
100 cpl_propertylist_append_string(header, "VOCLASS", "SPECTRUM v2.0");
101 cpl_propertylist_append_double(header, "TDMIN", wmin);
102 cpl_propertylist_append_double(header, "TDMAX", wmax);
103 cpl_propertylist_append_string(header, "TUTYP1",
104 "spec:Data.SpectralAxis.Value");
105 cpl_propertylist_append_string(header, "TUCD1", "em.wl;obs.atmos");
106 cpl_propertylist_append_string(header, "TUTYP2",
107 "spec:Data.FluxAxis.Value");
108 cpl_propertylist_append_string(header, "TUCD2",
109 "phot.flux.density;em.wl;src.net;meta.main");
110 cpl_propertylist_append_string(header, "TUTYP3",
111 "Spectrum.Data.FluxAxis.Accuracy.StatError");
112 cpl_propertylist_append_string(header, "TUCD3",
113 "phot.flux.density;em.wl;src.net;meta.main");
114 cpl_propertylist_append_string(header, "TUTYP4",
115 "meta.code.qual;meta.main");
116 cpl_propertylist_append_string(header, "TUCD4",
117 "spec:Data.FluxAxis.Accuracy.QualityStatus");
118 cpl_propertylist_append_string(header, "TUTYP5",
119 "spec:Data.FluxAxis.Value");
120 cpl_propertylist_append_string(header, "TUCD5",
121 "phot.flux.density;em.wl;src.net;meta.main");
122 data = cpl_table_new(naxis1);
123 result->data = data;
124 moo_try_check(cpl_table_new_column(data, MOO_S1D_WAVE, CPL_TYPE_DOUBLE),
125 " ");
126 moo_try_check(cpl_table_new_column(data, MOO_S1D_FLUX, CPL_TYPE_DOUBLE),
127 " ");
128 moo_try_check(cpl_table_new_column(data, MOO_S1D_ERR, CPL_TYPE_DOUBLE),
129 " ");
130 moo_try_check(cpl_table_new_column(data, MOO_S1D_QUAL, CPL_TYPE_INT), " ");
131 hdrl_image *himg = moo_sci_single_get_image(sci_single);
132 moo_try_check(cpl_table_new_column(data, MOO_S1D_BGFLUX, CPL_TYPE_DOUBLE),
133 " ");
134
135 iflux = hdrl_image_get_image(himg);
136 ierr = hdrl_image_get_error(himg);
137 isky = moo_sci_single_get_sky(sci_single);
138 cpl_image *iqual = moo_sci_single_get_qual(sci_single);
139
140 for (int i = 1; i <= naxis1; i++) {
141 int rej;
142 double flux = cpl_image_get(iflux, i, indextarg, &rej);
143 double err = cpl_image_get(ierr, i, indextarg, &rej);
144 int qual = (int)cpl_image_get(iqual, i, indextarg, &rej);
145 double sky = cpl_image_get(isky, i, indextarg, &rej);
146 cpl_table_set_double(data, MOO_S1D_WAVE, i - 1,
147 crval1 + (i - crpix1) * cd1_1);
148 cpl_table_set_double(data, MOO_S1D_FLUX, i - 1, flux);
149 cpl_table_set_double(data, MOO_S1D_ERR, i - 1, err);
150 cpl_table_set_int(data, MOO_S1D_QUAL, i - 1, qual);
151 cpl_table_set_double(data, MOO_S1D_BGFLUX, i - 1, sky);
152 }
153
154moo_try_cleanup:
155 cpl_free(filename);
156 return result;
157}
158/*----------------------------------------------------------------------------*/
175/*----------------------------------------------------------------------------*/
176cpl_error_code
177moo_create_s1d(cpl_frame *sci_frame,
178 moo_create_s1d_params *params,
179 const char *tag,
180 moo_products *products)
181{
182 moo_sci *sci = NULL;
183 cpl_ensure_code(sci_frame != NULL, CPL_ERROR_NULL_INPUT);
184 cpl_ensure_code(params != NULL, CPL_ERROR_NULL_INPUT);
185
186 sci = moo_sci_create(sci_frame);
187
188 cpl_errorstate prestate = cpl_errorstate_get();
189 cpl_msg_info(__func__, "Create S1D");
190
191 if (params->do_s1d) {
193 cpl_table *table = ttable->table;
194 int nrow = cpl_table_get_nrow(table);
195
196 for (int t = 0; t < nrow; t++) {
197 int indextarg =
198 cpl_table_get_int(table, MOO_TARGET_TABLE_INDEXTARG, t, NULL);
199 const char *targname =
200 cpl_table_get_string(table, MOO_TARGET_TABLE_TARGNAME, t);
201 double talpha =
202 cpl_table_get_double(table, MOO_TARGET_TABLE_TARGALPHA, t,
203 NULL);
204 double tdelta =
205 cpl_table_get_double(table, MOO_TARGET_TABLE_TARGDELTA, t,
206 NULL);
207 for (int j = 0; j < 3; j++) {
208 moo_sci_single *sci_single = moo_sci_get_single(sci, j);
209 double exptime = moo_target_table_get_exptime(ttable, t, j);
210 if (sci_single != NULL) {
211 moo_s1d *s1d = NULL;
213
214 s1d = _moo_create_s1d_single(sci_single, indextarg,
215 targname, talpha, tdelta,
216 exptime, sci->primary_header);
217
218 if (products != NULL) {
219 moo_products_add_s1d(products, s1d, tag, sci_frame);
220 }
221 else {
222 moo_s1d_save(s1d);
223 }
224 moo_s1d_delete(s1d);
225 }
226 }
227 }
228 }
229
230 if (!cpl_errorstate_is_equal(prestate)) {
231 cpl_msg_error(__func__, "Error in create_s1d");
232 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
233 }
234 moo_sci_delete(sci);
235 return CPL_ERROR_NONE;
236}
#define MOO_BADPIX_GOOD
Definition: moo_badpix.h:36
moo_s1d * moo_s1d_new(void)
Create a new moo_cube.
Definition: moo_s1d.c:70
void moo_s1d_save(moo_s1d *self)
Save a moo_s1d to a FITS file.
Definition: moo_s1d.c:117
void moo_s1d_delete(moo_s1d *self)
Delete a moo_s1d.
Definition: moo_s1d.c:88
cpl_image * moo_sci_single_get_sky(moo_sci_single *self)
Get sky of sci single.
hdrl_image * moo_sci_single_get_image(moo_sci_single *self)
Get image of SCI_SINGLE.
cpl_image * moo_sci_single_get_qual(moo_sci_single *self)
Get image of qual.
moo_sci_single * moo_sci_load_single(moo_sci *self, moo_detector_type type, int level)
Load the type part in SCI and return it.
Definition: moo_sci.c:323
void moo_sci_delete(moo_sci *self)
Delete a moo_sci.
Definition: moo_sci.c:84
moo_sci * moo_sci_create(const cpl_frame *frame)
Create a new empty SCI filename.
Definition: moo_sci.c:249
moo_sci_single * moo_sci_get_single(moo_sci *self, moo_detector_type type)
Get the type part in SCI and return it.
Definition: moo_sci.c:351
moo_target_table * moo_sci_get_target_table(moo_sci *self)
Get the target table of SCI file.
Definition: moo_sci.c:284
double moo_target_table_get_exptime(moo_target_table *self, int i, int j)
rget the exptime of a target
cpl_error_code moo_create_s1d(cpl_frame *sci_frame, moo_create_s1d_params *params, const char *tag, moo_products *products)
Apply the telluric correction to SCI.
double moo_pfits_get_cd1_1(const cpl_propertylist *plist)
find out the CD1_1 value
Definition: moo_pfits.c:1256
int moo_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
Definition: moo_pfits.c:1000
cpl_error_code moo_pfits_update_dec(cpl_propertylist *plist, double value)
Set the DEC value.
Definition: moo_pfits.c:1094
double moo_pfits_get_crval1(const cpl_propertylist *plist)
find out the CRVAL1 value
Definition: moo_pfits.c:1176
cpl_error_code moo_pfits_update_ra(cpl_propertylist *plist, double value)
Set the RA value.
Definition: moo_pfits.c:1077
double moo_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
Definition: moo_pfits.c:1196
cpl_frame * moo_products_add_s1d(moo_products *self, moo_s1d *s1d, const char *tag, const cpl_frame *inherit_frame)
create a product from a MOLECTABLE object
the different type of detectors