68 #define PRINT_USAGE(rc) \ 69 fprintf(stderr, "Usage: %s [ -x ] [ -f filter,names ] [ -l <lambda1> <lambda"\ 70 "2> ] CUBE FILTER_LIST\n", argv[0]); \ 71 cpl_end(); return (rc); 73 int main(
int argc,
char **argv)
75 const char *idstring =
"muse_cube_filter";
76 cpl_init(CPL_INIT_DEFAULT);
77 cpl_msg_set_time_on();
79 cpl_msg_set_level(CPL_MSG_DEBUG);
80 cpl_msg_set_component_on();
81 cpl_errorstate state = cpl_errorstate_get();
88 cpl_array *filters = NULL;
93 cpl_boolean extended = CPL_FALSE;
97 for (i = 1; i < argc; i++) {
98 if (strncmp(argv[i],
"-f", 3) == 0) {
106 }
else if (strncmp(argv[i],
"-x", 3) == 0) {
108 }
else if (strncmp(argv[i],
"-l", 3) == 0) {
111 lambda1 = atof(argv[++i]);
112 lambda2 = atof(argv[++i]);
116 }
else if (strncmp(argv[i],
"-", 1) == 0) {
129 if (!iname || !list) {
134 cpl_msg_info(idstring,
"No filter names given, will use all from \"%s\"",
137 filters = cpl_array_new(0, CPL_TYPE_STRING);
138 cpl_errorstate es = cpl_errorstate_get();
141 cpl_propertylist *header = cpl_propertylist_load(list, iext);
145 if (!cpl_propertylist_has(header,
"EXTNAME")) {
149 cpl_array_set_size(filters, cpl_array_get_size(filters) + 1);
150 cpl_array_set_string(filters, cpl_array_get_size(filters) - 1, extname);
151 cpl_msg_info(idstring,
"Added filter \"%s\"", extname);
152 cpl_propertylist_delete(header);
153 }
while (cpl_errorstate_is_equal(es));
154 cpl_errorstate_set(es);
158 if (isnan(lambda1) && isnan(lambda2)) {
160 }
else if (!isfinite(lambda1) || !isfinite(lambda2) || lambda1 >= lambda2) {
161 cpl_msg_error(idstring,
"Bad wavelength range given %f ... %f Angstrom!",
163 cpl_array_delete(filters);
167 char *fname = cpl_sprintf(
"CUSTOM_%.3f_%.3f", lambda1, lambda2);
168 cpl_array_set_size(filters, cpl_array_get_size(filters) + 1);
169 cpl_array_set_string(filters, cpl_array_get_size(filters) - 1, fname);
173 cpl_table_set(ftable,
"lambda", 0, lambda1 - 1e-5);
174 cpl_table_set(ftable,
"lambda", 1, lambda1);
175 cpl_table_set(ftable,
"lambda", 2, lambda2);
176 cpl_table_set(ftable,
"lambda", 3, lambda2 + 1e-5);
177 cpl_table_set(ftable,
"throughput", 0, 0.);
178 cpl_table_set(ftable,
"throughput", 1, 1.);
179 cpl_table_set(ftable,
"throughput", 2, 1.);
180 cpl_table_set(ftable,
"throughput", 3, 0.);
183 fcustom->
table = ftable;
184 fcustom->
header = cpl_propertylist_new();
185 cpl_propertylist_append_string(fcustom->
header,
"EXTNAME", fname);
189 cpl_array_dump(filters, 0, 10000, stdout);
193 cpl_msg_info(idstring,
"Loading cube \"%s\"", iname);
196 cpl_msg_error(idstring,
"MUSE cube could not be loaded from \"%s\"!", iname);
201 int nx = cpl_image_get_size_x(cpl_imagelist_get(cube->
data, 0)),
202 ny = cpl_image_get_size_y(cpl_imagelist_get(cube->
data, 0)),
203 nl = cpl_imagelist_get_size(cube->
data);
204 cpl_msg_info(idstring,
"Loaded cube \"%s\" (%dx%dx%d)", iname, nx, ny, nl);
207 cpl_array *names = cpl_array_new(0, CPL_TYPE_STRING);
208 int idx, n = cpl_array_get_size(filters);
209 for (i = 0, idx = 0; i < n; i++) {
210 const char *fname = cpl_array_get_string(filters, i);
212 if (fname && strncmp(fname,
"white", 6) && strncmp(fname,
"CUSTOM_", 7)) {
213 iext = cpl_fits_find_extension(list, fname);
215 cpl_msg_warning(idstring,
"No filter \"%s\" found in table \"%s\"", fname,
219 cpl_msg_info(idstring,
"Integrating over filter \"%s\" (from %s)", fname,
220 MUSE_TAG_FILTER_LIST);
222 ftable->
table = cpl_table_load(list, iext, 1);
223 ftable->
header = cpl_propertylist_load(list, 0);
224 cpl_propertylist *hext = cpl_propertylist_load(list, iext);
225 cpl_propertylist_copy_property_regexp(ftable->
header, hext,
226 "^EXTNAME$|^Z|^COMMENT", 0);
227 cpl_propertylist_delete(hext);
228 }
else if (fname && !strncmp(fname,
"CUSTOM_", 7)) {
229 cpl_msg_info(idstring,
"Integrating over \"%s\" filter (wavelength range " 230 "%.3f ... %.3f Angstrom)", fname, lambda1, lambda2);
233 cpl_msg_info(idstring,
"Integrating over filter \"%s\" (builtin)", fname);
241 cpl_array_set_size(names, cpl_array_get_size(names) + 1);
242 cpl_array_set_string(names, cpl_array_get_size(names) - 1, fname);
244 cpl_array_delete(filters);
248 cpl_array_dump(names, 0, n, stdout);
255 cpl_errorstate es = cpl_errorstate_get();
257 if (rc == CPL_ERROR_NONE && cpl_errorstate_is_equal(es)) {
258 cpl_msg_info(idstring,
"Appended %d images into cube \"%s\"", n, iname);
260 cpl_msg_error(idstring,
"Appending %d images into cube \"%s\" failed: %s",
261 n, iname, cpl_error_get_message());
265 char *fn = cpl_sprintf(
"%s", iname),
266 *basename = strstr(fn,
".fits");
268 for (i = 0; i < n; i++) {
269 const char *fname = cpl_array_get_string(names, i);
271 char *outname = cpl_sprintf(
"%s_%s.fits", fn, fname);
274 cpl_msg_info(idstring,
"Saved image for filter \"%s\" into \"%s\"", fname,
280 cpl_array_delete(names);
283 if (!cpl_errorstate_is_equal(state)) {
Structure definition of a MUSE datacube.
const muse_cpltable_def muse_filtertable_def[]
MUSE filter table definition.
Structure definition for a collection of muse_images.
const char * muse_pfits_get_extname(const cpl_propertylist *aHeaders)
find out the extension name
muse_datacube * muse_datacube_load(const char *aFilename)
Load header, DATA and optionally STAT and DQ extensions as well as the reconstructed images of a MUSE...
void muse_datacube_delete(muse_datacube *aCube)
Deallocate memory associated to a muse_datacube object.
void muse_imagelist_delete(muse_imagelist *aList)
Free the memory of the MUSE image list.
muse_table * muse_table_new(void)
Allocate memory for a new muse_table object.
Structure definition of MUSE three extension FITS file.
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
cpl_table * muse_cpltable_new(const muse_cpltable_def *aDef, cpl_size aLength)
Create an empty table according to the specified definition.
cpl_array * muse_cplarray_new_from_delimited_string(const char *aString, const char *aDelim)
Convert a delimited string into an array of strings.
muse_image * muse_imagelist_get(muse_imagelist *aList, unsigned int aIdx)
Get the muse_image of given list index.
cpl_table * table
The table.
cpl_error_code muse_datacube_save_recimages(const char *aFilename, muse_imagelist *aImages, cpl_array *aNames)
Save reconstructed images of a cube in extra extensions.
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
muse_table * muse_table_load_filter(muse_processing *aProcessing, const char *aFilterName)
Load a table for a given filter name.
cpl_imagelist * data
the cube containing the actual data values
Structure to store a table together with a property list.
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
cpl_propertylist * header
the header
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.
muse_imagelist * muse_imagelist_new(void)
Create a new (empty) MUSE image list.
Definition of a cpl table structure.
muse_image * muse_datacube_collapse(muse_datacube *aCube, const muse_table *aFilter)
Integrate a FITS NAXIS=3 datacube along the wavelength direction.
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.
cpl_error_code muse_imagelist_set(muse_imagelist *aList, muse_image *aImage, unsigned int aIdx)
Set the muse_image of given list index.
cpl_error_code muse_image_dq_to_nan(muse_image *aImage)
Convert pixels flagged in the DQ extension to NANs in DATA (and STAT, if present).