86 #include <uves_reduce_utils.h>
88 #include <uves_pfits.h>
89 #include <uves_utils.h>
90 #include <uves_utils_wrappers.h>
91 #include <uves_error.h>
104 uves_get_airmass(
const double z) {
108 double x=z/180.*CPL_MATH_PI;
110 double sec=1./cos(x);
111 return ( sec * ( 1.-0.0012 * ( sec*sec-1. ) ) );
151 const cpl_table *atm_extinction,
152 bool correct_binning,
153 cpl_image **scaled_error)
155 cpl_image *scaled = NULL;
156 double exptime, gain;
161 assure_nomsg( scaled_error == NULL || spectrum_error != NULL, CPL_ERROR_NULL_INPUT);
162 assure_nomsg( spectrum_header != NULL, CPL_ERROR_NULL_INPUT);
164 nx = cpl_image_get_size_x(spectrum);
165 ny = cpl_image_get_size_y(spectrum);
167 if (spectrum_error != NULL)
169 assure( nx == cpl_image_get_size_x(spectrum_error) &&
170 ny == cpl_image_get_size_y(spectrum_error), CPL_ERROR_INCOMPATIBLE_INPUT,
171 "Error spectrum geometry differs from spectrum: %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" vs. %d x %d",
172 cpl_image_get_size_x(spectrum_error),
173 cpl_image_get_size_y(spectrum_error),
177 assure( ny % n_traces == 0, CPL_ERROR_INCOMPATIBLE_INPUT,
178 "Spectrum image height (%d) is not a multiple of "
179 "the number of traces (%d). Confused, bailing out",
182 norders = ny / n_traces;
188 "Could not read exposure time");
191 "Could not read gain factor");
197 "Could not read binning");
201 uves_msg(
"Spectrum will not be normalized to unit binning");
205 assure( exptime > 0, CPL_ERROR_ILLEGAL_INPUT,
"Non-positive exposure time: %f s", exptime);
206 assure( gain > 0, CPL_ERROR_ILLEGAL_INPUT,
"Non-positive gain: %f", gain);
207 assure( binx > 0, CPL_ERROR_ILLEGAL_INPUT,
"Illegal binning: %d", binx);
209 uves_msg(
"Correcting for exposure time = %f s, gain = %f, binx = %d", exptime, gain, binx);
211 check( scaled = cpl_image_divide_scalar_create(spectrum, exptime * gain * binx),
212 "Error correcting spectrum for gain, exposure time, binning");
214 if (scaled_error != NULL)
216 check( *scaled_error = cpl_image_divide_scalar_create(spectrum_error,
217 exptime * gain * binx),
218 "Error correcting rebinned spectrum for gain, exposure time, binning");
226 double dlambda, lambda_start;
228 int miss_airm_start=0;
231 double airmass_start, airmass_end;
232 if(cpl_propertylist_has(raw_header,UVES_AIRMASS_START)) {
234 "Error reading airmass start");
240 airmass_start=uves_get_airmass(z);
244 if(cpl_propertylist_has(raw_header,UVES_AIRMASS_END)) {
246 "Error reading airmass end");
251 airmass_end=uves_get_airmass(z);
255 if(miss_airm_start && miss_airm_end) {
257 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
260 airmass = (airmass_start + airmass_end) / 2;
265 uves_msg(
"Correcting for extinction through airmass %f", airmass);
268 "Error reading bin width from header");
270 for (order = 1; order <= norders; order++)
281 "Error reading start wavelength from header");
286 "Error reading start wavelength from header");
289 for (trace = 1; trace <= n_traces; trace++)
291 int spectrum_row = (order - 1)*n_traces + trace;
294 for (x = 1; x <= nx; x++)
303 lambda = lambda_start + (x-1) * dlambda;
305 flux = cpl_image_get(scaled, x, spectrum_row, &pis_rejected1);
306 if (scaled_error != NULL)
308 dflux = cpl_image_get(*scaled_error, x,
309 spectrum_row, &pis_rejected2);
312 if (!pis_rejected1 && (scaled_error == NULL || !pis_rejected2))
319 lambda, atm_extinction,
320 "LAMBDA",
"LA_SILLA", &istart),
321 "Error interpolating extinction coefficient");
334 scaled, x, spectrum_row,
335 flux * pow(10, 0.4 * extinction * airmass));
336 if (scaled_error != NULL)
339 *scaled_error, x, spectrum_row,
340 dflux * pow(10, 0.4 * extinction * airmass));
345 cpl_image_reject(scaled, x, spectrum_row);
346 if (scaled_error != NULL)
348 cpl_image_reject(*scaled_error, x, spectrum_row);
359 if (cpl_error_get_code() != CPL_ERROR_NONE)
361 uves_free_image(&scaled);
362 if (scaled_error != NULL)
364 uves_free_image(scaled_error);
#define uves_msg_error(...)
Print an error message.
#define uves_msg_warning(...)
Print an warning message.
double uves_pfits_get_tel_alt_start(const uves_propertylist *plist)
Find out the start telescope altitude.
double uves_pfits_get_gain(const uves_propertylist *plist, enum uves_chip chip)
Find out the gain.
double uves_pfits_get_wstart(const uves_propertylist *plist, int order)
Read the wstart keyword.
double uves_spline_hermite_table(double xp, const cpl_table *t, const char *column_x, const char *column_y, int *istart)
Spline interpolation based on Hermite polynomials.
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
int uves_pfits_get_biny(const uves_propertylist *plist)
Find out the y binning factor.
double uves_pfits_get_exptime(const uves_propertylist *plist)
Find out the exposure time in seconds.
cpl_image * uves_normalize_spectrum(const cpl_image *spectrum, const cpl_image *spectrum_error, const uves_propertylist *spectrum_header, const uves_propertylist *raw_header, int n_traces, enum uves_chip chip, const cpl_table *atm_extinction, bool correct_binning, cpl_image **scaled_error)
Normalize a spectrum.
double uves_pfits_get_airmass_end(const uves_propertylist *plist)
Find out the end airmass.
double uves_pfits_get_cdelt1(const uves_propertylist *plist)
Find out the cdelt1.
double uves_pfits_get_crval1(const uves_propertylist *plist)
Find out the crval1.
#define assure_nomsg(BOOL, CODE)
double uves_pfits_get_airmass_start(const uves_propertylist *plist)
Find out the start airmass.