29#include "moo_detector.h"
34#include "moo_fibres_table.h"
35#include "moo_line_table.h"
70 moo_map *res = cpl_calloc(1,
sizeof(moo_map));
86 cpl_table *result = NULL;
87 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
89 if (self->line_table != NULL) {
90 result = self->line_table;
93 if (self->filename != NULL) {
95 cpl_fits_find_extension(self->filename, MOO_LINE_TABLE_EXTNAME);
97 self->line_table = cpl_table_load(self->filename, qnum, 0);
100 result = self->line_table;
107moo_map_load(
const cpl_frame *mapframe)
109 cpl_ensure(mapframe != NULL, CPL_ERROR_NULL_INPUT, NULL);
110 const char *filename = cpl_frame_get_filename(mapframe);
111 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
113 cpl_errorstate prev_state = cpl_errorstate_get();
115 res->filename = cpl_strdup(filename);
116 cpl_size qnum = cpl_fits_find_extension(filename, MOO_FIBRES_TABLE_EXTNAME);
118 res->fibre_table = cpl_table_load(filename, qnum, 0);
120 cpl_errorstate_set(prev_state);
122 res->primary_header = cpl_propertylist_load(filename, 0);
124 for (
int i = 0; i < 2; i++) {
125 for (
int j = 0; j < 3; j++) {
128 cpl_propertylist *header = NULL;
129 header = moo_fits_load_extension_header(filename, NULL, extname);
130 res->data_header[i * 3 + j] = header;
132 if (header != NULL) {
135 res->data[i * 3 + j] =
142 if (!cpl_errorstate_is_equal(prev_state)) {
143 cpl_errorstate_set(prev_state);
144 cpl_msg_error(
"moo_map",
"Error in loading WAVE MAP %s", filename);
168 cpl_propertylist *header)
170 cpl_ensure_code(ntas >= 1 && ntas <= 2, CPL_ERROR_ILLEGAL_INPUT);
171 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
172 cpl_ensure_code(header != NULL, CPL_ERROR_NULL_INPUT);
175 int nx = cpl_image_get_size_x(data);
176 int ny = cpl_image_get_size_y(data);
177 self->data[(ntas - 1) * 3 + type] = data;
178 cpl_array *wmintab = cpl_array_new(ny, CPL_TYPE_DOUBLE);
179 cpl_array *wmaxtab = cpl_array_new(ny, CPL_TYPE_DOUBLE);
180 for (
int j = 1; j <= ny; j++) {
181 cpl_image *data_copy = cpl_image_extract(data, 1, j, nx, j);
182 cpl_image_reject_value(data_copy, CPL_VALUE_NAN);
183 int nbrej = cpl_image_count_rejected(data_copy);
185 double lmax = cpl_image_get_max(data_copy);
186 double lmin = cpl_image_get_min(data_copy);
187 cpl_array_set(wmintab, j - 1, lmin);
188 cpl_array_set(wmaxtab, j - 1, lmax);
190 cpl_image_delete(data_copy);
192 double wmin = cpl_array_get_median(wmintab);
193 double wmax = cpl_array_get_median(wmaxtab);
196 cpl_array_delete(wmaxtab);
197 cpl_array_delete(wmintab);
199 self->data_header[(ntas - 1) * 3 + type] = header;
200 return CPL_ERROR_NONE;
204moo_map_set_fibre_table(moo_map *self, cpl_table *fibre_table)
206 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
207 cpl_ensure_code(fibre_table != NULL, CPL_ERROR_NULL_INPUT);
209 self->fibre_table = fibre_table;
211 return CPL_ERROR_NONE;
215moo_map_set_line_table(moo_map *self, cpl_table *line_table)
217 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
218 cpl_ensure_code(line_table != NULL, CPL_ERROR_NULL_INPUT);
220 self->line_table = line_table;
222 return CPL_ERROR_NONE;
240 if (self->filename != NULL) {
241 cpl_free(self->filename);
243 if (self->primary_header != NULL) {
244 cpl_propertylist_delete(self->primary_header);
246 for (i = 0; i < 6; i++) {
247 if (self->data[i] != NULL) {
248 cpl_image_delete(self->data[i]);
250 if (self->data_header[i] != NULL) {
251 cpl_propertylist_delete(self->data_header[i]);
254 if (self->fibre_table != NULL) {
255 cpl_table_delete(self->fibre_table);
257 if (self->line_table != NULL) {
258 cpl_table_delete(self->line_table);
278 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
280 for (
int i = 0; i < 2; i++) {
281 for (
int j = 0; j < 3; j++) {
284 NULL, extname, CPL_TYPE_FLOAT,
285 self->data_header[i * 3 + j]);
289 if (self->fibre_table != NULL) {
290 cpl_propertylist *h = cpl_propertylist_new();
291 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
292 MOO_FIBRES_TABLE_EXTNAME);
294 cpl_table_save(self->fibre_table, h, h, filename, CPL_IO_EXTEND);
295 cpl_propertylist_delete(h);
297 if (self->line_table != NULL) {
298 cpl_propertylist *h = cpl_propertylist_new();
299 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
300 MOO_LINE_TABLE_EXTNAME);
302 cpl_table_save(self->line_table, h, h, filename, CPL_IO_EXTEND);
303 cpl_propertylist_delete(h);
322 cpl_error_code status = CPL_ERROR_NONE;
323 const char *monotonous_colnames[] = { MOO_FIBRES_TABLE_MONOTONOUS_RI,
324 MOO_FIBRES_TABLE_MONOTONOUS_YJ,
325 MOO_FIBRES_TABLE_MONOTONOUS_H };
327 cpl_table *fibre_table = self->fibre_table;
328 cpl_propertylist *primary_header = self->primary_header;
330 cpl_ensure_code(sformat != NULL, CPL_ERROR_NULL_INPUT);
331 cpl_msg_info(__func__,
"Check WAVEMAP");
333 cpl_msg_indent_more();
334 for (
int ntas = 1; ntas <= 2; ntas++) {
335 cpl_table_select_all(fibre_table);
337 cpl_table_and_selected_int(fibre_table, MOO_FIBRES_TABLE_HEALTH,
339 size = cpl_table_and_selected_int(fibre_table,
340 MOO_FIBRES_TABLE_SPECTRO,
342 cpl_array *sel = cpl_table_where_selected(fibre_table);
343 for (
int type = 0; type < 3; type++) {
344 cpl_image *orig_data = self->data[(ntas - 1) * 3 + type];
345 cpl_propertylist *header =
346 self->data_header[(ntas - 1) * 3 + type];
347 const char *monotonous_colname = monotonous_colnames[type];
348 if (orig_data != NULL) {
349 cpl_image *data = cpl_image_cast(orig_data, CPL_TYPE_FLOAT);
350 int nx = cpl_image_get_size_x(data);
351 const float *tdata = cpl_image_get_data_const(data);
352 moo_spectral_format_info *info =
357 for (
int j = 0; j < size; j++) {
358 int index = cpl_array_get_cplsize(sel, j, NULL);
360 cpl_table_get_int(fibre_table,
361 MOO_FIBRES_TABLE_INDEXEXT, index,
364 const char *fibname =
365 cpl_table_get_string(fibre_table,
366 MOO_FIBRES_TABLE_FIBRE, index);
369 isnan(tdata[first + (indexext - 1) * nx])) {
373 if (first < (nx - 2)) {
374 double w1 = tdata[first + (indexext - 1) * nx];
375 double w2 = tdata[first + 1 + (indexext - 1) * nx];
378 for (
int i = first + 1; i < nx - 1; i++) {
379 w1 = tdata[i + (indexext - 1) * nx];
380 w2 = tdata[i + 1 + (indexext - 1) * nx];
386 "Not monotonous solution "
387 "for fibre %s band %s spectro %d",
388 fibname, band, ntas);
395 cpl_table_set_int(fibre_table, monotonous_colname,
403 cpl_image_delete(data);
406 cpl_array_delete(sel);
409 cpl_msg_indent_less();
430 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
431 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
433 fprintf(stream,
"---MOO_MAP\n");
436 for (i = 0; i < 6; i++) {
437 if (self->data[i] != NULL) {
438 fprintf(stream,
"data[%d] %p\n", i, self->data[i]);
439 if (self->data[i] != NULL) {
440 cpl_image_dump_structure(self->data[i], stream);
444 if (self->fibre_table) {
445 fprintf(stream,
"fibre table %p\n", self->fibre_table);
448 return CPL_ERROR_NONE;
452moo_map_update_linetable(moo_map *self, moo_rbn *rbn)
454 cpl_table *line_table = self->line_table;
455 int nrow = cpl_table_get_nrow(line_table);
456 cpl_table *fibre_table = rbn->fibre_table;
458 for (
int i = 0; i < nrow; i++) {
460 cpl_table_get_int(line_table, MOO_LINE_TABLE_SPECTRO, i, NULL);
462 cpl_table_get_int(line_table, MOO_LINE_TABLE_INDEXEXT, i, NULL);
464 cpl_table_select_all(fibre_table);
465 cpl_table_and_selected_int(fibre_table, MOO_FIBRES_TABLE_SPECTRO,
466 CPL_EQUAL_TO, spectro);
467 cpl_table_and_selected_int(fibre_table, MOO_FIBRES_TABLE_INDEXEXT,
468 CPL_EQUAL_TO, indexext);
469 cpl_array *sel = cpl_table_where_selected(fibre_table);
470 int idx = cpl_array_get_cplsize(sel, 0, NULL);
472 cpl_table_get_int(fibre_table, MOO_FIBRES_TABLE_INDEXRBN, idx,
474 cpl_table_set_int(line_table, MOO_LINE_TABLE_INDEXRBN, i, indexrbn);
475 cpl_array_delete(sel);
const char * moo_detector_get_name(moo_detector_type type)
Get the extension name of a detector.
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
enum _moo_detector_type_ moo_detector_type
The type code type.
cpl_error_code moo_fits_write_extension_image(cpl_image *image, const char *filename, const char *name, const char *detectorname, cpl_type type, cpl_propertylist *header)
Write an image as extension in FITS file.
cpl_image * moo_fits_load_extension_image(const char *filename, const char *name, const char *detectorname, cpl_type type)
Load an image from FITS file.
cpl_error_code moo_map_check(moo_map *self, moo_spectral_format *sformat)
Save a moo_map to a FITS file.
cpl_error_code moo_map_dump(const moo_map *self, FILE *stream)
Dump structural information of MAP.
void moo_map_save(moo_map *self, const char *filename)
Save a moo_map to a FITS file.
moo_map * moo_map_new(void)
Create a new moo_map.
cpl_table * moo_map_get_line_table(moo_map *self)
Get the LINE TABLE extension of a map.
cpl_error_code moo_map_set_data(moo_map *self, moo_detector_type type, int ntas, cpl_image *data, cpl_propertylist *header)
set map data for relevant extension
void moo_map_delete(moo_map *self)
Delete a moo_map.
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
cpl_error_code moo_qc_set_wavecal_monotonous_solution(cpl_propertylist *plist, int val)
Set the QC.WAVECAL.MONOTONOUS.SOLUTION value.
cpl_error_code moo_qc_set_wave_min(cpl_propertylist *plist, double val)
Set the QC.WAVE.MIN value.
cpl_error_code moo_qc_set_wave_max(cpl_propertylist *plist, double val)
Set the QC.WAVE.MAX value.