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