MUSE Pipeline Reference Manual  2.1.1
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);
75 
76  if (argc <= 2) {
77  /* filenames are needed at least */
78  PRINT_USAGE(1);
79  }
80 
81  char *iname = NULL, /* input image name */
82  *oname = NULL; /* output image name */
83  float datavalue = 1.,
84  statvalue = 0.;
85  /* make the dqvalue variable signed to be able to check for wrong inputs */
86  cpl_size dqvalue = -1;
87 
88  /* argument processing */
89  int i;
90  for (i = 1; i < argc; i++) {
91  if (strncmp(argv[i], "-d", 3) == 0) {
92  /* skip to next arg to get datavalue */
93  i++;
94  if (i < argc) {
95  datavalue = atof(argv[i]);
96  } else {
97  PRINT_USAGE(2);
98  }
99  } else if (strncmp(argv[i], "-q", 3) == 0) {
100  /* skip to next arg to get dqvalue */
101  i++;
102  if (i < argc) {
103  dqvalue = atoi(argv[i]);
104  if (dqvalue < 0 || dqvalue > UINT_MAX) {
105  PRINT_USAGE(4);
106  }
107  } else {
108  PRINT_USAGE(3);
109  }
110  } else if (strncmp(argv[i], "-s", 3) == 0) {
111  /* skip to next arg to get statvalue */
112  i++;
113  if (i < argc) {
114  statvalue = atof(argv[i]);
115  if (statvalue < 0) {
116  PRINT_USAGE(6);
117  }
118  } else {
119  PRINT_USAGE(5);
120  }
121  } else if (strncmp(argv[i], "-", 1) == 0) { /* unallowed options */
122  PRINT_USAGE(9);
123  } else {
124  if (iname && oname) {
125  break; /* we have the possible names, skip the rest */
126  }
127  if (!iname) {
128  iname = argv[i] /* set the name for the input image */;
129  } else {
130  oname = argv[i] /* set the name for the output image */;
131  }
132  }
133  } /* for i (all arguments) */
134 
135  muse_image *image = muse_image_load(iname);
136  if (!image) {
137  PRINT_USAGE(10);
138  }
139  printf("Loaded \"%s\".\n", iname);
140 
141  /* Fill the three image extensions, save the result */
142  cpl_error_code rc = CPL_ERROR_NONE;
143  cpl_size nx = cpl_image_get_size_x(image->data),
144  ny = cpl_image_get_size_y(image->data);
145  rc = cpl_image_fill_window(image->data, 1, 1, nx, ny, datavalue);
146  printf("Filled DATA image with %g (rc = %d)\n", datavalue, rc);
147  rc = cpl_image_fill_window(image->stat, 1, 1, nx, ny, statvalue);
148  printf("Filled STAT image with %g (rc = %d)\n", statvalue, rc);
149  if (dqvalue >= 0) {
150  rc = cpl_image_fill_window(image->dq, 1, 1, nx, ny, dqvalue);
151  printf("Filled DQ image with %u (rc = %d)\n", (unsigned int)dqvalue, rc);
152  }
153  if (!cpl_propertylist_has(image->header, "BUNIT")) {
154  cpl_propertylist_append_string(image->header, "BUNIT", "count");
155  }
156  rc = muse_image_save(image, oname);
157 
158  if (rc != CPL_ERROR_NONE) {
159  fprintf(stderr, "Some error occurred while saving to \"%s\" "
160  "(rc = %d, %s)\n", oname, rc, cpl_error_get_message());
161  rc = 20;
162  } else {
163  printf("Saved to \"%s\".\n", oname);
164  }
165  muse_image_delete(image);
166  cpl_end();
167  return rc;
168 }
169 
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:405
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:228
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.