MOONS Pipeline Reference Manual 0.13.2
moo_fits.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 <cpl.h>
29#include <hdrl.h>
30#include "moo_utils.h"
31#include "moo_pfits.h"
32/*----------------------------------------------------------------------------*/
37/*----------------------------------------------------------------------------*/
38
41/*-----------------------------------------------------------------------------
42 Function codes
43 -----------------------------------------------------------------------------*/
44/*----------------------------------------------------------------------------*/
59/*----------------------------------------------------------------------------*/
60cpl_error_code
62 const char *filename,
63 const char *name,
64 const char *detectorname,
65 cpl_propertylist *header)
66{
67 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
68 cpl_ensure_code(detectorname != NULL, CPL_ERROR_NULL_INPUT);
69
70 char *extname;
71 if (name == NULL) {
72 extname = cpl_sprintf("%s", detectorname);
73 }
74 else {
75 extname = cpl_sprintf("%s_%s", name, detectorname);
76 }
77
78 cpl_propertylist *h = NULL;
79
80 if (header != NULL) {
81 h = cpl_propertylist_duplicate(header);
82 }
83 else {
84 h = cpl_propertylist_new();
85 }
86
87 if (cpl_propertylist_has(h, MOO_PFITS_EXTNAME) == 0) {
88 moo_try_check(cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
89 extname),
90 " ");
91 }
92
93 if (mask != NULL) {
94 moo_try_check(cpl_mask_save(mask, filename, h, CPL_IO_EXTEND), " ");
95 }
96 else {
97 moo_try_check(cpl_propertylist_save(h, filename, CPL_IO_EXTEND), " ");
98 }
99moo_try_cleanup:
100 cpl_propertylist_delete(h);
101 cpl_free(extname);
102 return cpl_error_get_code();
103}
104
105/*----------------------------------------------------------------------------*/
121/*----------------------------------------------------------------------------*/
122cpl_error_code
124 const char *filename,
125 const char *name,
126 const char *detectorname,
127 cpl_type type,
128 cpl_propertylist *header)
129{
130 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
131 cpl_ensure_code(detectorname != NULL, CPL_ERROR_NULL_INPUT);
132
133 char *extname;
134 if (name == NULL) {
135 extname = cpl_sprintf("%s", detectorname);
136 }
137 else {
138 extname = cpl_sprintf("%s_%s", name, detectorname);
139 }
140 cpl_propertylist *h = NULL;
141
142 if (header != NULL) {
143 h = cpl_propertylist_duplicate(header);
144 }
145 else {
146 h = cpl_propertylist_new();
147 }
148
149 if (cpl_propertylist_has(h, MOO_PFITS_EXTNAME) == 0) {
150 moo_try_check(cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
151 extname),
152 " ");
153 }
154
155 if (image != NULL) {
156 moo_try_check(cpl_image_save(image, filename, type, h, CPL_IO_EXTEND),
157 " ");
158 }
159 else {
160 moo_try_check(cpl_propertylist_save(h, filename, CPL_IO_EXTEND), " ");
161 }
162
163moo_try_cleanup:
164
165 cpl_propertylist_delete(h);
166 cpl_free(extname);
167 return cpl_error_get_code();
168}
169
170/*----------------------------------------------------------------------------*/
185/*----------------------------------------------------------------------------*/
186cpl_error_code
188 const char *filename,
189 const char *name,
190 const char *detectorname,
191 cpl_propertylist *header)
192{
193 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
194 cpl_ensure_code(detectorname != NULL, CPL_ERROR_NULL_INPUT);
195
196 char *extname;
197 if (name == NULL) {
198 extname = cpl_sprintf("%s", detectorname);
199 }
200 else {
201 extname = cpl_sprintf("%s_%s", name, detectorname);
202 }
203
204 cpl_propertylist *h = NULL;
205
206 if (header != NULL) {
207 h = cpl_propertylist_duplicate(header);
208 }
209 else {
210 h = cpl_propertylist_new();
211 }
212
213 if (cpl_propertylist_has(h, MOO_PFITS_EXTNAME) == 0) {
214 moo_try_check(cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
215 extname),
216 " ");
217 }
218
219 if (table != NULL) {
220 moo_try_check(cpl_table_save(table, NULL, h, filename, CPL_IO_EXTEND),
221 " ");
222 }
223 else {
224 moo_try_check(cpl_propertylist_save(h, filename, CPL_IO_EXTEND), " ");
225 }
226moo_try_cleanup:
227 cpl_propertylist_delete(h);
228 cpl_free(extname);
229 return cpl_error_get_code();
230}
231
232/*----------------------------------------------------------------------------*/
248/*----------------------------------------------------------------------------*/
249cpl_error_code
251 const char *filename,
252 const char *name,
253 const char *detectorname,
254 cpl_type type,
255 cpl_propertylist *header)
256{
257 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
258 cpl_ensure_code(detectorname != NULL, CPL_ERROR_NULL_INPUT);
259
260 char *extname;
261 if (name == NULL) {
262 extname = cpl_sprintf("%s", detectorname);
263 }
264 else {
265 extname = cpl_sprintf("%s_%s", name, detectorname);
266 }
267 cpl_propertylist *h = NULL;
268
269 if (header != NULL) {
270 h = cpl_propertylist_duplicate(header);
271 }
272 else {
273 h = cpl_propertylist_new();
274 }
275
276 if (cpl_propertylist_has(h, MOO_PFITS_EXTNAME) == 0) {
277 moo_try_check(cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
278 extname),
279 " ");
280 }
281
282 if (cube != NULL) {
283 moo_try_check(cpl_imagelist_save(cube, filename, type, h,
284 CPL_IO_EXTEND),
285 " ");
286 }
287 else {
288 moo_try_check(cpl_propertylist_save(h, filename, CPL_IO_EXTEND), " ");
289 }
290moo_try_cleanup:
291 cpl_propertylist_delete(h);
292 cpl_free(extname);
293 return cpl_error_get_code();
294}
295/*----------------------------------------------------------------------------*/
309/*----------------------------------------------------------------------------*/
310cpl_image *
311moo_fits_load_extension_image(const char *filename,
312 const char *name,
313 const char *detectorname,
314 cpl_type type)
315{
316 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
317 cpl_ensure(detectorname != NULL, CPL_ERROR_NULL_INPUT, NULL);
318
319 cpl_image *res = NULL;
320
321 char *extname;
322 if (name == NULL) {
323 extname = cpl_sprintf("%s", detectorname);
324 }
325 else {
326 extname = cpl_sprintf("%s_%s", name, detectorname);
327 }
328 cpl_size extnum = cpl_fits_find_extension(filename, extname);
329 if (extnum > 0) {
330 moo_try_check(res = cpl_image_load(filename, type, 0, extnum),
331 "Can't load image extension %" CPL_SIZE_FORMAT
332 " in file %s",
333 extnum, filename);
334 }
335 else {
336 cpl_msg_debug("moo_fits", "Couldn't find extension %s in %s", extname,
337 filename);
338 }
339moo_try_cleanup:
340 cpl_free(extname);
341 return res;
342}
343
344/*----------------------------------------------------------------------------*/
357/*----------------------------------------------------------------------------*/
358cpl_table *
359moo_fits_load_extension_table(const char *filename,
360 const char *name,
361 const char *detectorname)
362{
363 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
364 cpl_ensure(detectorname != NULL, CPL_ERROR_NULL_INPUT, NULL);
365
366 cpl_table *res = NULL;
367
368 char *extname;
369 if (name == NULL) {
370 extname = cpl_sprintf("%s", detectorname);
371 }
372 else {
373 extname = cpl_sprintf("%s_%s", name, detectorname);
374 }
375 cpl_size extnum = cpl_fits_find_extension(filename, extname);
376 if (extnum > 0) {
377 moo_try_check(res = cpl_table_load(filename, extnum, 0),
378 "Can't load table extension %" CPL_SIZE_FORMAT
379 " in file %s",
380 extnum, filename);
381 }
382 else {
383 cpl_msg_debug("moo_fits", "Couldn't find extension %s in %s", extname,
384 filename);
385 }
386moo_try_cleanup:
387 cpl_free(extname);
388 return res;
389}
390
391/*----------------------------------------------------------------------------*/
404/*----------------------------------------------------------------------------*/
405cpl_mask *
406moo_fits_load_extension_mask(const char *filename,
407 const char *name,
408 const char *detectorname)
409{
410 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
411 cpl_ensure(detectorname != NULL, CPL_ERROR_NULL_INPUT, NULL);
412
413 cpl_mask *res = NULL;
414
415 char *extname;
416 if (name == NULL) {
417 extname = cpl_sprintf("%s", detectorname);
418 }
419 else {
420 extname = cpl_sprintf("%s_%s", name, detectorname);
421 }
422 cpl_size extnum = cpl_fits_find_extension(filename, extname);
423 if (extnum > 0) {
424 moo_try_check(res = cpl_mask_load(filename, 0, extnum),
425 "Can't load mask extension %" CPL_SIZE_FORMAT
426 " in file %s",
427 extnum, filename);
428 }
429 else {
430 cpl_msg_debug("moo_fits", "Couldn't find extension %s in %s", extname,
431 filename);
432 }
433
434moo_try_cleanup:
435 cpl_free(extname);
436 return res;
437}
438
439/*----------------------------------------------------------------------------*/
453/*----------------------------------------------------------------------------*/
454cpl_imagelist *
455moo_fits_load_extension_cube(const char *filename,
456 const char *name,
457 const char *detectorname,
458 cpl_type type)
459{
460 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
461 cpl_ensure(detectorname != NULL, CPL_ERROR_NULL_INPUT, NULL);
462
463 cpl_imagelist *res = NULL;
464
465 char *extname;
466 if (name == NULL) {
467 extname = cpl_sprintf("%s", detectorname);
468 }
469 else {
470 extname = cpl_sprintf("%s_%s", name, detectorname);
471 }
472 cpl_size extnum = cpl_fits_find_extension(filename, extname);
473 if (extnum > 0) {
474 moo_try_check(res = cpl_imagelist_load(filename, type, extnum),
475 "Can't load image extension %" CPL_SIZE_FORMAT
476 " in file %s",
477 extnum, filename);
478 }
479 else {
480 cpl_msg_debug("moo_fits", "Couldn't find extension %s in %s", extname,
481 filename);
482 }
483moo_try_cleanup:
484 cpl_free(extname);
485 return res;
486}
487cpl_propertylist *
488moo_fits_load_extension_header(const char *filename,
489 const char *name,
490 const char *detectorname)
491{
492 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
493 cpl_ensure(detectorname != NULL, CPL_ERROR_NULL_INPUT, NULL);
494
495 cpl_propertylist *res = NULL;
496
497 char *extname;
498 if (name == NULL) {
499 extname = cpl_sprintf("%s", detectorname);
500 }
501 else {
502 extname = cpl_sprintf("%s_%s", name, detectorname);
503 }
504 cpl_size extnum = cpl_fits_find_extension(filename, extname);
505 if (extnum > 0) {
506 moo_try_check(res = cpl_propertylist_load(filename, extnum),
507 "Can't load header extension %" CPL_SIZE_FORMAT
508 " in file %s",
509 extnum, filename);
510 }
511 else {
512 cpl_msg_debug("moo_fits", "Couldn't find extension %s in %s", extname,
513 filename);
514 }
515moo_try_cleanup:
516 cpl_free(extname);
517 return res;
518}
519
520/*----------------------------------------------------------------------------*/
531/*----------------------------------------------------------------------------*/
532cpl_error_code
533moo_fits_create(const char *filename)
534{
535 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
536 cpl_propertylist *h = cpl_propertylist_new();
537 moo_try_check(cpl_propertylist_save(h, filename, CPL_IO_CREATE), " ");
538
539moo_try_cleanup:
540 cpl_propertylist_delete(h);
541 return cpl_error_get_code();
542}
cpl_error_code moo_fits_write_extension_mask(cpl_mask *mask, const char *filename, const char *name, const char *detectorname, cpl_propertylist *header)
Write a mask as extension in FITS file.
Definition: moo_fits.c:61
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.
Definition: moo_fits.c:123
cpl_error_code moo_fits_create(const char *filename)
Create a new fits file with empty propertylist.
Definition: moo_fits.c:533
cpl_mask * moo_fits_load_extension_mask(const char *filename, const char *name, const char *detectorname)
Load a mask from FITS file.
Definition: moo_fits.c:406
cpl_table * moo_fits_load_extension_table(const char *filename, const char *name, const char *detectorname)
Load a table from FITS file.
Definition: moo_fits.c:359
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.
Definition: moo_fits.c:311
cpl_imagelist * moo_fits_load_extension_cube(const char *filename, const char *name, const char *detectorname, cpl_type type)
Load a cube from FITS file.
Definition: moo_fits.c:455
cpl_error_code moo_fits_write_extension_table(cpl_table *table, const char *filename, const char *name, const char *detectorname, cpl_propertylist *header)
Write an image as extension in FITS file.
Definition: moo_fits.c:187
cpl_error_code moo_fits_write_extension_cube(cpl_imagelist *cube, const char *filename, const char *name, const char *detectorname, cpl_type type, cpl_propertylist *header)
Write a cube as extension in FITS file.
Definition: moo_fits.c:250