MUSE Pipeline Reference Manual  2.1.1
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
20  * 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 /*----------------------------------------------------------------------------*
28  * Includes *
29  *----------------------------------------------------------------------------*/
30 #include <cpl.h>
31 #include "muse_mask.h"
32 
33 #include "muse_utils.h"
34 
35 /*----------------------------------------------------------------------------*/
43 /*----------------------------------------------------------------------------*/
46 /*----------------------------------------------------------------------------*/
59 /*----------------------------------------------------------------------------*/
60 muse_mask *
62 {
63  return (muse_mask *)cpl_calloc(1, sizeof(muse_mask));
64 } /* muse_mask_new() */
65 
66 /*----------------------------------------------------------------------------*/
76 /*----------------------------------------------------------------------------*/
77 void
79 {
80  if (!aMask) {
81  return;
82  }
83  cpl_mask_delete(aMask->mask);
84  cpl_propertylist_delete(aMask->header);
85  cpl_free(aMask);
86 } /* muse_mask_delete() */
87 
88 /*----------------------------------------------------------------------------*/
101 /*----------------------------------------------------------------------------*/
102 muse_mask *
103 muse_mask_load(const char *aFilename)
104 {
105  muse_mask *mask = muse_mask_new();
106  if (mask == NULL) {
107  return NULL;
108  }
109 
110  mask->header = cpl_propertylist_load(aFilename, 0);
111  if (!mask->header) {
112  cpl_msg_error(__func__, "Loading \"%s\" failed: %s", aFilename,
113  cpl_error_get_message());
114  muse_mask_delete(mask);
115  return NULL;
116  }
117 
118  mask->mask = cpl_mask_load(aFilename, 0, 0);
119  if (mask->mask == NULL) {
120  cpl_msg_error(__func__, "Could not load mask from \"%s\": %s",
121  aFilename, cpl_error_get_message());
122  muse_mask_delete(mask);
123  return NULL;
124  }
125 
126  return mask;
127 } /* muse_mask_load() */
128 
129 /*----------------------------------------------------------------------------*/
141 /*----------------------------------------------------------------------------*/
142 cpl_error_code
143 muse_mask_save(muse_mask *aMask, const char *aFilename)
144 {
145  cpl_ensure_code(aMask && aFilename, CPL_ERROR_NULL_INPUT);
146 
147  cpl_image *image = cpl_image_new_from_mask(aMask->mask);
148  cpl_error_code err = cpl_image_save(image, aFilename, CPL_TYPE_UNSPECIFIED,
149  aMask->header, CPL_IO_CREATE);
150  cpl_image_delete(image);
151 
152  if (err != CPL_ERROR_NONE) {
153  cpl_msg_error(__func__, "Could not save mask %s: %s",
154  aFilename, cpl_error_get_message());
155  return err;
156  }
157 
158  return CPL_ERROR_NONE;
159 } /* muse_mask_save() */
160 
void muse_mask_delete(muse_mask *aMask)
Deallocate memory associated to a muse_mask object.
Definition: muse_mask.c:78
muse_mask * muse_mask_load(const char *aFilename)
Load a mask file and its FITS header.
Definition: muse_mask.c:103
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:143
muse_mask * muse_mask_new(void)
Allocate memory for a new muse object.
Definition: muse_mask.c:61
Handling of "mask" files.
Definition: muse_mask.h:43
cpl_propertylist * header
the FITS header
Definition: muse_mask.h:56
cpl_mask * mask
The mask data.
Definition: muse_mask.h:49