MUSE Pipeline Reference Manual  1.0.2
muse_fill_image.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) 2007-2014 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 #include <muse.h>
23 #include <string.h>
24 
25 /*----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------*/
63 
66 #define PRINT_USAGE(rc) \
67  fprintf(stderr, "Usage: %s [ -d datavalue ] [ -q dqvalue] [ -s statvalue ] " \
68  "IMAGE_IN IMAGE_OUT\n", argv[0]); \
69  cpl_end(); return (rc);
70 
71 int main(int argc, char **argv)
72 {
73  cpl_init(CPL_INIT_DEFAULT);
74 
75  if (argc <= 2) {
76  /* filenames are needed at least */
77  PRINT_USAGE(1);
78  }
79 
80  char *iname = NULL, /* input image name */
81  *oname = NULL; /* output image name */
82  float datavalue = 1.,
83  statvalue = 0.;
84  /* make the dqvalue variable signed to be able to check for wrong inputs */
85  cpl_size dqvalue = -1;
86 
87  /* argument processing */
88  int i;
89  for (i = 1; i < argc; i++) {
90  if (strncmp(argv[i], "-d", 3) == 0) {
91  /* skip to next arg to get datavalue */
92  i++;
93  if (i < argc) {
94  datavalue = atof(argv[i]);
95  } else {
96  PRINT_USAGE(2);
97  }
98  } else if (strncmp(argv[i], "-q", 3) == 0) {
99  /* skip to next arg to get dqvalue */
100  i++;
101  if (i < argc) {
102  dqvalue = atoi(argv[i]);
103  if (dqvalue < 0 || dqvalue > UINT_MAX) {
104  PRINT_USAGE(4);
105  }
106  } else {
107  PRINT_USAGE(3);
108  }
109  } else if (strncmp(argv[i], "-s", 3) == 0) {
110  /* skip to next arg to get statvalue */
111  i++;
112  if (i < argc) {
113  statvalue = atof(argv[i]);
114  if (statvalue < 0) {
115  PRINT_USAGE(6);
116  }
117  } else {
118  PRINT_USAGE(5);
119  }
120  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
121  PRINT_USAGE(9);
122  } else {
123  if (iname && oname) {
124  break; /* we have the possible names, skip the rest */
125  }
126  if (!iname) {
127  iname = argv[i] /* set the name for the input image */;
128  } else {
129  oname = argv[i] /* set the name for the output image */;
130  }
131  }
132  } /* for i (all arguments) */
133 
134  muse_image *image = muse_image_load(iname);
135  if (!image) {
136  PRINT_USAGE(10);
137  }
138  printf("Loaded \"%s\".\n", iname);
139 
140  /* Fill the three image extensions, save the result */
141  cpl_error_code rc = CPL_ERROR_NONE;
142  cpl_size nx = cpl_image_get_size_x(image->data),
143  ny = cpl_image_get_size_y(image->data);
144  rc = cpl_image_fill_window(image->data, 1, 1, nx, ny, datavalue);
145  printf("Filled DATA image with %g (rc = %d)\n", datavalue, rc);
146  rc = cpl_image_fill_window(image->stat, 1, 1, nx, ny, statvalue);
147  printf("Filled STAT image with %g (rc = %d)\n", statvalue, rc);
148  if (dqvalue >= 0) {
149  rc = cpl_image_fill_window(image->dq, 1, 1, nx, ny, dqvalue);
150  printf("Filled DQ image with %u (rc = %d)\n", (unsigned int)dqvalue, rc);
151  }
152  if (!cpl_propertylist_has(image->header, "BUNIT")) {
153  cpl_propertylist_append_string(image->header, "BUNIT", "count");
154  }
155  rc = muse_image_save(image, oname);
156 
157  if (rc != CPL_ERROR_NONE) {
158  fprintf(stderr, "Some error occurred while saving to \"%s\" "
159  "(rc = %d, %s)\n", oname, rc, cpl_error_get_message());
160  rc = 20;
161  } else {
162  printf("Saved to \"%s\".\n", oname);
163  }
164  muse_image_delete(image);
165  cpl_end();
166  return rc;
167 }
168 
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
Definition: muse_image.c:85
cpl_image * data
the data extension
Definition: muse_image.h:46
cpl_image * stat
the statistics extension
Definition: muse_image.h:64
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
cpl_propertylist * header
the FITS header
Definition: muse_image.h:72
cpl_image * dq
the data quality extension
Definition: muse_image.h:56
cpl_error_code muse_image_save(muse_image *aImage, const char *aFilename)
Save the three image extensions and the FITS headers of a MUSE image to a file.
Definition: muse_image.c:399
muse_image * muse_image_load(const char *aFilename)
Load the three extensions and the FITS headers of a MUSE image from a file.
Definition: muse_image.c:223