MUSE Pipeline Reference Manual  1.0.2
muse_mask.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2013 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 /*----------------------------------------------------------------------------*
27  * Includes *
28  *----------------------------------------------------------------------------*/
29 #include <cpl.h>
30 #include "muse_mask.h"
31 #include "muse_utils.h"
32 
36 /*---------------------------------------------------------------------------*/
49 /*---------------------------------------------------------------------------*/
50 muse_mask *
52 {
53  return (muse_mask *)cpl_calloc(1, sizeof(muse_mask));
54 } /* muse_mask_new() */
55 
56 /*---------------------------------------------------------------------------*/
66 /*---------------------------------------------------------------------------*/
67 void
69 {
70  if (aMask != NULL) {
71  cpl_mask_delete(aMask->mask);
72  cpl_propertylist_delete(aMask->header);
73  cpl_free(aMask);
74  }
75 } /* muse_mask_delete() */
76 
77 /*---------------------------------------------------------------------------*/
89 /*---------------------------------------------------------------------------*/
90 muse_mask *muse_mask_load(const char *aFilename)
91 {
92  muse_mask *mask = muse_mask_new();
93  if (mask == NULL) {
94  return NULL;
95  }
96 
97  mask->header = cpl_propertylist_load(aFilename, 0);
98  if (!mask->header) {
99  cpl_msg_error(__func__, "Loading \"%s\" failed: %s", aFilename,
100  cpl_error_get_message());
101  muse_mask_delete(mask);
102  return NULL;
103  }
104 
105  mask->mask = cpl_mask_load(aFilename, 0, 0);
106  if (mask->mask == NULL) {
107  cpl_msg_error(__func__, "Could not load mask from %s: %s",
108  aFilename, cpl_error_get_message());
109  muse_mask_delete(mask);
110  cpl_error_set(__func__, MUSE_ERROR_READ_DATA);
111  return NULL;
112  }
113 
114  return mask;
115 } /* muse_mask_load() */
116 
117 /*----------------------------------------------------------------------------*/
129 /*----------------------------------------------------------------------------*/
130 
131 cpl_error_code muse_mask_save(muse_mask *aMask, const char *aFilename)
132 {
133  cpl_ensure_code(aMask && aFilename, CPL_ERROR_NULL_INPUT);
134 
135  cpl_image *image = cpl_image_new_from_mask(aMask->mask);
136  cpl_error_code err = cpl_image_save(image, aFilename, CPL_TYPE_UNSPECIFIED,
137  aMask->header, CPL_IO_CREATE);
138  cpl_image_delete(image);
139 
140  if (err != CPL_ERROR_NONE) {
141  cpl_msg_error(__func__, "Could not save mask %s: %s",
142  aFilename, cpl_error_get_message());
143  return err;
144  }
145 
146  return CPL_ERROR_NONE;
147 } /* muse_mask_save */
148 
149 
muse_mask * muse_mask_new(void)
Allocate memory for a new muse object.
Definition: muse_mask.c:51
muse_mask * muse_mask_load(const char *aFilename)
Load a mask file and its FITS header.
Definition: muse_mask.c:90
cpl_error_code muse_mask_save(muse_mask *aMask, const char *aFilename)
Save the data and the FITS headers of a MUSE mask to a file.
Definition: muse_mask.c:131
Handling of "mask" files.
Definition: muse_mask.h:42
cpl_propertylist * header
the FITS header
Definition: muse_mask.h:55
void muse_mask_delete(muse_mask *aMask)
Deallocate memory associated to a muse_mask object.
Definition: muse_mask.c:68
cpl_mask * mask
The mask data.
Definition: muse_mask.h:48