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>
133 const cpl_table *atm_extinction,
134 bool correct_binning,
135 cpl_image **scaled_error)
137 cpl_image *scaled = NULL;
138 double exptime, gain;
143 assure_nomsg( scaled_error == NULL || spectrum_error != NULL, CPL_ERROR_NULL_INPUT);
144 assure_nomsg( spectrum_header != NULL, CPL_ERROR_NULL_INPUT);
146 nx = cpl_image_get_size_x(spectrum);
147 ny = cpl_image_get_size_y(spectrum);
149 if (spectrum_error != NULL)
151 assure( nx == cpl_image_get_size_x(spectrum_error) &&
152 ny == cpl_image_get_size_y(spectrum_error), CPL_ERROR_INCOMPATIBLE_INPUT,
153 "Error spectrum geometry differs from spectrum: %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" vs. %d x %d",
154 cpl_image_get_size_x(spectrum_error),
155 cpl_image_get_size_y(spectrum_error),
159 assure( ny % n_traces == 0, CPL_ERROR_INCOMPATIBLE_INPUT,
160 "Spectrum image height (%d) is not a multiple of "
161 "the number of traces (%d). Confused, bailing out",
164 norders = ny / n_traces;
170 "Could not read exposure time");
173 "Could not read gain factor");
179 "Could not read binning");
183 uves_msg(
"Spectrum will not be normalized to unit binning");
187 assure( exptime > 0, CPL_ERROR_ILLEGAL_INPUT,
"Non-positive exposure time: %f s", exptime);
188 assure( gain > 0, CPL_ERROR_ILLEGAL_INPUT,
"Non-positive gain: %f", gain);
189 assure( binx > 0, CPL_ERROR_ILLEGAL_INPUT,
"Illegal binning: %d", binx);
191 uves_msg(
"Correcting for exposure time = %f s, gain = %f, binx = %d", exptime, gain, binx);
193 check( scaled = cpl_image_divide_scalar_create(spectrum, exptime * gain * binx),
194 "Error correcting spectrum for gain, exposure time, binning");
196 if (scaled_error != NULL)
198 check( *scaled_error = cpl_image_divide_scalar_create(spectrum_error,
199 exptime * gain * binx),
200 "Error correcting rebinned spectrum for gain, exposure time, binning");
208 double dlambda, lambda_start;
212 double airmass_start, airmass_end;
215 "Error reading airmass start");
218 "Error reading airmass end");
221 airmass = (airmass_start + airmass_end) / 2;
225 uves_msg(
"Correcting for extinction through airmass %f", airmass);
228 "Error reading bin width from header");
230 for (order = 1; order <= norders; order++)
241 "Error reading start wavelength from header");
246 "Error reading start wavelength from header");
249 for (trace = 1; trace <= n_traces; trace++)
251 int spectrum_row = (order - 1)*n_traces + trace;
254 for (x = 1; x <= nx; x++)
263 lambda = lambda_start + (x-1) * dlambda;
265 flux = cpl_image_get(scaled, x, spectrum_row, &pis_rejected1);
266 if (scaled_error != NULL)
268 dflux = cpl_image_get(*scaled_error, x,
269 spectrum_row, &pis_rejected2);
272 if (!pis_rejected1 && (scaled_error == NULL || !pis_rejected2))
279 lambda, atm_extinction,
280 "LAMBDA",
"LA_SILLA", &istart),
281 "Error interpolating extinction coefficient");
294 scaled, x, spectrum_row,
295 flux * pow(10, 0.4 * extinction * airmass));
296 if (scaled_error != NULL)
299 *scaled_error, x, spectrum_row,
300 dflux * pow(10, 0.4 * extinction * airmass));
305 cpl_image_reject(scaled, x, spectrum_row);
306 if (scaled_error != NULL)
308 cpl_image_reject(*scaled_error, x, spectrum_row);
319 if (cpl_error_get_code() != CPL_ERROR_NONE)
321 uves_free_image(&scaled);
322 if (scaled_error != NULL)
324 uves_free_image(scaled_error);