34#include "moo_scilist.h"
68 moo_resp *res = cpl_calloc(1,
sizeof(moo_resp));
87 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
89 cpl_error_code status = CPL_ERROR_NONE;
90 cpl_errorstate prev_state = cpl_errorstate_get();
105 if (!cpl_errorstate_is_equal(prev_state)) {
106 status = cpl_error_get_code();
107 cpl_errorstate_set(prev_state);
126 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
160 moo_resp *result = NULL;
162 cpl_ensure(scilist != NULL, CPL_ERROR_NULL_INPUT, NULL);
167 result->primary_header = cpl_propertylist_new();
168 result->response_table = cpl_table_new(size);
170 cpl_table_new_column(result->response_table, MOO_RESP_TABLE_INDEXRBN,
172 cpl_table_new_column(result->response_table, MOO_RESP_TABLE_SPECTRO,
174 cpl_table_new_column(result->response_table, MOO_RESP_TABLE_RESPONSE,
177 for (
int il = 0; il < size; il++) {
178 cpl_array *sel = NULL;
179 char *response_colname = NULL;
182 if (target_table->table != NULL) {
183 const char *targname = moo_target_table_get_std_name(target_table);
186 cpl_table_select_all(sci->target_table->table);
187 cpl_table_and_selected_int(sci->target_table->table,
188 MOO_TARGET_TABLE_INDEXTARG, CPL_EQUAL_TO,
190 sel = cpl_table_where_selected(sci->target_table->table);
191 int idx = cpl_array_get_cplsize(sel, 0, NULL);
192 int idrbn = cpl_table_get_int(sci->target_table->table,
193 MOO_TARGET_TABLE_INDEXRBN, idx, NULL);
195 cpl_table_get_int(sci->target_table->table,
196 MOO_TARGET_TABLE_SPECTRO, idx, NULL);
197 cpl_table_set_int(result->response_table, MOO_RESP_TABLE_INDEXRBN,
199 response_colname = cpl_sprintf(
"%s_%d", MOO_RESP_RESPONSE, il);
200 cpl_table_set_int(result->response_table, MOO_RESP_TABLE_SPECTRO,
202 cpl_table_set_string(result->response_table,
203 MOO_RESP_TABLE_RESPONSE, il, response_colname);
204 cpl_array_delete(sel);
205 cpl_free(response_colname);
209 for (
int i = 0; i < 3; i++) {
210 cpl_table *table = NULL;
212 for (
int il = 0; il < size; il++) {
213 const char *response_colname =
214 cpl_table_get_string(result->response_table,
215 MOO_RESP_TABLE_RESPONSE, il);
218 if (target_table->table != NULL) {
219 const char *targname =
220 moo_target_table_get_std_name(target_table);
224 moo_sci_single *sci_single =
227 if (sci_single != NULL) {
228 cpl_propertylist *header =
236 cpl_image *img = hdrl_image_get_image(himg);
237 int nx = cpl_image_get_size_x(img);
240 table = cpl_table_new(nx);
241 cpl_table_new_column(table, MOO_RESP_WAVE,
243 for (
int j = 1; j <= nx; j++) {
244 double w = (j - crpix1) * cd1_1 + crval1;
245 cpl_table_set_double(table, MOO_RESP_WAVE, j - 1,
249 cpl_table_new_column(table, response_colname,
254 cpl_sprintf(
"%s_%s", response_colname,
256 char *fluxfilter_colname =
257 cpl_sprintf(
"%s_%s", response_colname,
258 MOO_RESP_SPECFLUXFILTER);
260 cpl_sprintf(
"%s_%s", response_colname,
263 cpl_sprintf(
"%s_%s", response_colname,
267 cpl_table_new_column(table, flux_colname,
269 cpl_table_new_column(table, fluxfilter_colname,
271 cpl_table_new_column(table, flx_colname,
273 cpl_table_new_column(table, atm_colname,
276 cpl_free(flux_colname);
277 cpl_free(fluxfilter_colname);
278 cpl_free(flx_colname);
279 cpl_free(atm_colname);
282 for (
int j = 1; j <= nx; j++) {
284 double f = cpl_image_get(img, j, idtarget, &rej);
289 cpl_table_set_double(table, response_colname, j - 1, f);
293 cpl_sprintf(
"%s_%s", response_colname,
295 cpl_table_set_double(table, flux_colname, j - 1, f);
296 cpl_free(flux_colname);
323 if (self->primary_header != NULL) {
324 cpl_propertylist_delete(self->primary_header);
326 if (self->ri != NULL) {
327 cpl_table_delete(self->ri);
329 if (self->yj != NULL) {
330 cpl_table_delete(self->yj);
332 if (self->h != NULL) {
333 cpl_table_delete(self->h);
335 if (self->response_table != NULL) {
336 cpl_table_delete(self->response_table);
357 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
358 if (self->ri != NULL) {
359 cpl_propertylist *h = cpl_propertylist_new();
360 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
362 cpl_table_save(self->ri, h, h, filename, CPL_IO_EXTEND);
363 cpl_propertylist_delete(h);
365 if (self->yj != NULL) {
366 cpl_propertylist *h = cpl_propertylist_new();
367 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
369 cpl_table_save(self->yj, h, h, filename, CPL_IO_EXTEND);
370 cpl_propertylist_delete(h);
372 if (self->h != NULL) {
373 cpl_propertylist *h = cpl_propertylist_new();
374 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
376 cpl_table_save(self->h, h, h, filename, CPL_IO_EXTEND);
377 cpl_propertylist_delete(h);
380 if (self->response_table != NULL) {
381 cpl_propertylist *h = cpl_propertylist_new();
382 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
383 MOO_RESP_EXT_RESPONSE_TABLE);
384 cpl_table_save(self->response_table, h, h, filename, CPL_IO_EXTEND);
385 cpl_propertylist_delete(h);
403 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
405 const char *filename = cpl_frame_get_filename(frame);
406 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
407 cpl_errorstate prev_state = cpl_errorstate_get();
411 res->primary_header = cpl_propertylist_load(filename, 0);
415 res->ri = cpl_table_load(filename, qnum, 0);
417 qnum = cpl_fits_find_extension(filename, MOO_RESP_EXT_YJ);
419 res->yj = cpl_table_load(filename, qnum, 0);
422 qnum = cpl_fits_find_extension(filename, MOO_RESP_EXT_H);
424 res->h = cpl_table_load(filename, qnum, 0);
427 qnum = cpl_fits_find_extension(filename, MOO_RESP_EXT_RESPONSE_TABLE);
429 res->response_table = cpl_table_load(filename, qnum, 0);
432 if (!cpl_errorstate_is_equal(prev_state)) {
433 cpl_msg_debug(__func__,
"Error in loading response : %d",
434 cpl_error_get_code());
437 cpl_errorstate_set(prev_state);
456 const char *colname = NULL;
457 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
458 cpl_ensure(self->response_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
459 int nrow = cpl_table_get_nrow(self->response_table);
462 for (
int i = 0; i < nrow; i++) {
463 int n = cpl_table_get_int(self->response_table, MOO_RESP_TABLE_INDEXRBN,
465 const char *ncolname = cpl_table_get_string(self->response_table,
466 MOO_RESP_TABLE_RESPONSE, i);
467 const int spectro = cpl_table_get_int(self->response_table,
468 MOO_RESP_TABLE_SPECTRO, i, NULL);
469 if (spectro == fspectro) {
470 int diff = (int)fabs(idrbn - n);
476 else if (min_diff > diff) {
enum _moo_detector_type_ moo_detector_type
The type code type.
#define MOO_RESP_EXT_RI
RESPONSE format.
void moo_resp_delete(moo_resp *self)
Delete a moo_resp.
cpl_error_code moo_resp_set_table(moo_resp *self, moo_detector_type type, cpl_table *t)
assign table in moo_resp
void moo_resp_save(moo_resp *self, const char *filename)
Save a moo_resp to a FITS file.
cpl_table * moo_resp_get_table(moo_resp *self, moo_detector_type type)
Get a reponse table from RESP.
moo_resp * moo_resp_new(void)
Create a new moo_resp.
moo_resp * moo_resp_create(moo_scilist *scilist, int badpix_level)
Prepare RESP for SCI frame.
moo_resp * moo_resp_load(const cpl_frame *frame)
Load a RESP frame and create a moo_resp.
const char * moo_resp_get_colname(moo_resp *self, int idrbn, int fspectro)
Get the name of the response colname for a given indexrbn.
hdrl_image * moo_sci_single_get_image(moo_sci_single *self)
Get image of SCI_SINGLE.
cpl_propertylist * moo_sci_single_get_header(moo_sci_single *self)
Get header of sci single.
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.
moo_target_table * moo_sci_get_target_table(moo_sci *self)
Get the target table of SCI file.
moo_sci * moo_scilist_get(moo_scilist *self, int i)
Get the SCI at the position i in the list.
cpl_size moo_scilist_get_size(const moo_scilist *self)
Get the number of SCI in the scilist.
int moo_target_table_find_target(moo_target_table *self, const char *targname)
find target targindex in TARGET_TABLE
double moo_pfits_get_cd1_1(const cpl_propertylist *plist)
find out the CD1_1 value
double moo_pfits_get_crval1(const cpl_propertylist *plist)
find out the CRVAL1 value
double moo_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
the different type of detectors