00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <muse.h>
00023 #include <string.h>
00024
00025
00059
00060
00063 #define PRINT_USAGE(rc) \
00064 fprintf(stderr, "Usage: %s [ -x ] [ -f filter,names ] CUBE FILTER_LIST\n", \
00065 argv[0]); \
00066 cpl_end(); return (rc);
00067
00068 int main(int argc, char **argv)
00069 {
00070 const char *idstring = "muse_cube_filter";
00071 cpl_init(CPL_INIT_DEFAULT);
00072 cpl_msg_set_time_on();
00073 muse_processing_recipeinfo(NULL);
00074 cpl_msg_set_level(CPL_MSG_DEBUG);
00075 cpl_msg_set_component_on();
00076 cpl_errorstate state = cpl_errorstate_get();
00077
00078 if (argc < 3) {
00079
00080 PRINT_USAGE(1);
00081 }
00082
00083 cpl_array *filters = NULL;
00084 char *iname = NULL,
00085 *list = NULL;
00086 cpl_boolean extended = CPL_FALSE;
00087 int i;
00088
00089
00090 for (i = 1; i < argc; i++) {
00091 if (strncmp(argv[i], "-f", 3) == 0) {
00092
00093 i++;
00094 if (i < argc) {
00095 filters = muse_cplarray_new_from_delimited_string(argv[i], ",");
00096 } else {
00097 PRINT_USAGE(2);
00098 }
00099 } else if (strncmp(argv[i], "-x", 3) == 0) {
00100 extended = CPL_TRUE;
00101 } else if (strncmp(argv[i], "-", 1) == 0) {
00102 PRINT_USAGE(9);
00103 } else {
00104 if (iname && list) {
00105 break;
00106 }
00107 if (!iname) {
00108 iname = argv[i];
00109 } else {
00110 list = argv[i];
00111 }
00112 }
00113 }
00114 if (!iname || !list) {
00115 PRINT_USAGE(10);
00116 }
00117 int iext = 0;
00118 if (!filters) {
00119 cpl_msg_info(idstring, "No filter names given, will use all from \"%s\"",
00120 list);
00121
00122 filters = cpl_array_new(0, CPL_TYPE_STRING);
00123 cpl_errorstate es = cpl_errorstate_get();
00124 do {
00125 iext++;
00126 cpl_propertylist *header = cpl_propertylist_load(list, iext);
00127 if (!header) {
00128 break;
00129 }
00130 if (!cpl_propertylist_has(header, "EXTNAME")) {
00131 continue;
00132 }
00133 const char *extname = muse_pfits_get_extname(header);
00134 cpl_array_set_size(filters, cpl_array_get_size(filters) + 1);
00135 cpl_array_set_string(filters, cpl_array_get_size(filters) - 1, extname);
00136 cpl_msg_info(idstring, "Added filter \"%s\"", extname);
00137 cpl_propertylist_delete(header);
00138 } while (cpl_errorstate_is_equal(es));
00139 cpl_errorstate_set(es);
00140 }
00141 #if 0
00142 cpl_array_dump(filters, 0, 10000, stdout);
00143 fflush(stdout);
00144 #endif
00145
00146 cpl_msg_info(idstring, "Loading cube \"%s\"", iname);
00147 muse_datacube *cube = muse_datacube_load(iname);
00148 if (!cube) {
00149 cpl_msg_error(idstring, "MUSE cube could not be loaded from \"%s\"!", iname);
00150 PRINT_USAGE(11);
00151 }
00152
00153
00154 int nx = cpl_image_get_size_x(cpl_imagelist_get(cube->data, 0)),
00155 ny = cpl_image_get_size_y(cpl_imagelist_get(cube->data, 0)),
00156 nl = cpl_imagelist_get_size(cube->data);
00157 cpl_msg_info(idstring, "Loaded cube \"%s\" (%dx%dx%d)", iname, nx, ny, nl);
00158
00159 muse_imagelist *images = muse_imagelist_new();
00160 cpl_array *names = cpl_array_new(0, CPL_TYPE_STRING);
00161 int idx, n = cpl_array_get_size(filters);
00162 for (i = 0, idx = 0; i < n; i++) {
00163 const char *fname = cpl_array_get_string(filters, i);
00164 cpl_table *ftable = NULL;
00165 if (fname && strncmp(fname, "white", 6)) {
00166 iext = cpl_fits_find_extension(list, fname);
00167 if (iext < 1) {
00168 cpl_msg_warning(idstring, "No filter \"%s\" found in table \"%s\"", fname,
00169 list);
00170 continue;
00171 }
00172 cpl_msg_info(idstring, "Integrating over filter \"%s\" (from %s)", fname,
00173 MUSE_TAG_FILTER_LIST);
00174 ftable = cpl_table_load(list, iext, 1);
00175 } else {
00176 cpl_msg_info(idstring, "Integrating over filter \"%s\" (builtin)", fname);
00177 ftable = muse_table_load_filter(NULL, fname);
00178 }
00179 muse_image *fov = muse_datacube_collapse(cube, ftable);
00180 cpl_table_delete(ftable);
00181
00182 muse_image_dq_to_nan(fov);
00183 muse_imagelist_set(images, fov, idx++);
00184 cpl_array_set_size(names, cpl_array_get_size(names) + 1);
00185 cpl_array_set_string(names, cpl_array_get_size(names) - 1, fname);
00186 }
00187 cpl_array_delete(filters);
00188 muse_datacube_delete(cube);
00189
00190 #if 0
00191 cpl_array_dump(names, 0, n, stdout);
00192 fflush(stdout);
00193 #endif
00194
00195
00196 n = muse_imagelist_get_size(images);
00197 if (extended) {
00198 cpl_errorstate es = cpl_errorstate_get();
00199 cpl_error_code rc = muse_datacube_save_recimages(iname, images, names);
00200 if (rc == CPL_ERROR_NONE && cpl_errorstate_is_equal(es)) {
00201 cpl_msg_info(idstring, "Appended %d images into cube \"%s\"", n, iname);
00202 } else {
00203 cpl_msg_error(idstring, "Appending %d images into cube \"%s\" failed: %s",
00204 n, iname, cpl_error_get_message());
00205 }
00206 } else {
00207
00208 char *fn = cpl_sprintf("%s", iname),
00209 *basename = strstr(fn, ".fits");
00210 basename[0] = '\0';
00211 for (i = 0; i < n; i++) {
00212 const char *fname = cpl_array_get_string(names, i);
00213 muse_image *image = muse_imagelist_get(images, i);
00214 char *outname = cpl_sprintf("%s_%s.fits", fn, fname);
00215
00216 muse_image_save(image, outname);
00217 cpl_msg_info(idstring, "Saved image for filter \"%s\" into \"%s\"", fname,
00218 outname);
00219 cpl_free(outname);
00220 }
00221 cpl_free(fn);
00222 }
00223 cpl_array_delete(names);
00224 muse_imagelist_delete(images);
00225
00226 if (!cpl_errorstate_is_equal(state)) {
00227 cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
00228 }
00229 cpl_memory_dump();
00230 cpl_end();
00231 return 0;
00232 }
00233