00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <string.h>
00023 #include <muse.h>
00024
00025
00054
00055
00058 #define PRINT_USAGE(rc) \
00059 fprintf(stderr, "Usage: %s [ -s ] [ -d ] FILENAME\n", argv[0]); \
00060 cpl_end(); return (rc);
00061
00062 int main(int argc, char **argv)
00063 {
00064 cpl_init(CPL_INIT_DEFAULT);
00065 muse_processing_recipeinfo(NULL);
00066
00067 if (argc <= 1) {
00068
00069 PRINT_USAGE(1);
00070 }
00071
00072
00073 cpl_boolean dodelete = CPL_FALSE,
00074 dosign = CPL_FALSE;
00075 char *iname = NULL;
00076 int i;
00077 for (i = 1; i < argc; i++) {
00078 if (strncmp(argv[i], "-s", 3) == 0) {
00079 dosign = CPL_TRUE;
00080 } else if (strncmp(argv[i], "-d", 3) == 0) {
00081 dodelete = CPL_TRUE;
00082 } else if (strncmp(argv[i], "-", 1) == 0) {
00083 PRINT_USAGE(9);
00084 } else {
00085 if (iname) {
00086 break;
00087 }
00088 if (!iname) {
00089 iname = argv[i];
00090 }
00091 }
00092 }
00093
00094 cpl_errorstate state = cpl_errorstate_get();
00095
00096
00097 int next = cpl_fits_count_extensions(iname);
00098 if (next < 2) {
00099 PRINT_USAGE(10);
00100 }
00101 cpl_propertylist *header = cpl_propertylist_load(iname, 0);
00102 const char *tag = muse_pfits_get_pro_catg(header);
00103 printf("Splitting \"%s\" (PRO.CATG %s, %d extensions).\n", iname, tag, next);
00104
00105
00106 cpl_frameset *fset = cpl_frameset_new();
00107
00108
00109 int iext;
00110 for (iext = 1; iext <= next; iext++) {
00111
00112 cpl_propertylist *hext = cpl_propertylist_load(iname, iext);
00113
00114 unsigned char ifu = muse_utils_get_ifu(hext);
00115 const char *xtension = cpl_propertylist_get_string(hext, "XTENSION"),
00116 *extname = muse_pfits_get_extname(hext);
00117 if (!xtension) {
00118 fprintf(stderr, "Broken FITS extension (missing XTENSION, extension "
00119 "%d)\n", iext);
00120 cpl_propertylist_delete(hext);
00121 continue;
00122 }
00123
00124 enum {
00125 IS_BINTABLE,
00126 IS_IMAGE,
00127 IS_CHANNEL_CUBE,
00128 IS_MUSE_IMAGE_DATA,
00129 IS_MUSE_IMAGE_DQ,
00130 IS_MUSE_IMAGE_STAT,
00131 IS_OTHER
00132 } exttype = IS_OTHER;
00133 if (!strncmp(xtension, "BINTABLE", 8)) {
00134 exttype = IS_BINTABLE;
00135 } else if (!strncmp(xtension, "IMAGE", 8)) {
00136 char *chan = cpl_sprintf("CHAN%02hhu", ifu),
00137 *chandata = cpl_sprintf("CHAN%02hhu.DATA", ifu),
00138 *chandq = cpl_sprintf("CHAN%02hhu.DQ", ifu),
00139 *chanstat = cpl_sprintf("CHAN%02hhu.STAT", ifu);
00140 if (extname && !strncmp(extname, chan, strlen(chan) + 1)) {
00141 exttype = IS_IMAGE;
00142 } else if (extname && !strncmp(extname, chandata, strlen(chandata) + 1)) {
00143 exttype = IS_MUSE_IMAGE_DATA;
00144 } else if (extname && !strncmp(extname, chandq, strlen(chandq) + 1)) {
00145 exttype = IS_MUSE_IMAGE_DQ;
00146 } else if (extname && !strncmp(extname, chanstat, strlen(chanstat) + 1)) {
00147 exttype = IS_MUSE_IMAGE_STAT;
00148 } else if (extname &&
00149 !strncmp(extname, chan, strlen(chan)) &&
00150 muse_pfits_get_naxis(hext, 0) == 3) {
00151 exttype = IS_CHANNEL_CUBE;
00152 } else {
00153 exttype = IS_OTHER;
00154 }
00155 cpl_free(chan);
00156 cpl_free(chandata);
00157 cpl_free(chandq);
00158 cpl_free(chanstat);
00159 }
00160
00161
00162 char *basefn = cpl_strdup(iname),
00163 *end = strstr(basefn, ".fits");
00164 if (end) {
00165 *end = '\0';
00166 }
00167 char *outfn = cpl_sprintf("%s-%02hhu.fits", basefn, ifu);
00168 cpl_free(basefn);
00169
00170
00171 switch (exttype) {
00172 case IS_BINTABLE: {
00173 printf("\tbinary table (XTENSION=\'%s\', EXTNAME=\'%s\')\n", xtension,
00174 extname);
00175
00176 cpl_table *table = cpl_table_load(iname, iext, 1);
00177 cpl_propertylist *hout = cpl_propertylist_duplicate(header);
00178 cpl_propertylist_append(hout, hext);
00179 if (strlen(extname) == 6) {
00180
00181 cpl_table_save(table, hout, NULL, outfn, CPL_IO_CREATE);
00182 } else {
00183 cpl_propertylist *hextout = cpl_propertylist_new();
00184
00185 char *extnew = strchr(extname, '.');
00186 if (extnew) {
00187 extnew += 1;
00188 cpl_propertylist_append_string(hextout, "EXTNAME", extnew);
00189 }
00190 cpl_table_save(table, hout, hextout, outfn, CPL_IO_CREATE);
00191 cpl_propertylist_delete(hextout);
00192 }
00193 cpl_table_delete(table);
00194 cpl_propertylist_delete(hout);
00195 break;
00196 }
00197 case IS_IMAGE: {
00198 printf("\tstandard image(XTENSION=\'%s\', EXTNAME=\'%s\')\n", xtension,
00199 extname);
00200
00201 cpl_image *image = cpl_image_load(iname, CPL_TYPE_UNSPECIFIED, 0, iext);
00202 cpl_propertylist *hout = cpl_propertylist_duplicate(header);
00203 cpl_propertylist_append(hout, hext);
00204 cpl_image_save(image, outfn, CPL_TYPE_UNSPECIFIED, hout, CPL_IO_CREATE);
00205 cpl_image_delete(image);
00206 cpl_propertylist_delete(hout);
00207 break;
00208 }
00209 case IS_CHANNEL_CUBE: {
00210 printf("\tchannel cube (XTENSION=\'%s\', EXTNAME=\'%s\')\n", xtension,
00211 extname);
00212
00213 cpl_imagelist *list = cpl_imagelist_load(iname, CPL_TYPE_UNSPECIFIED, iext);
00214 cpl_propertylist *hout = cpl_propertylist_duplicate(header);
00215
00216 unsigned char dummy;
00217 char extnamerest[KEYWORD_LENGTH];
00218 sscanf(extname, "CHAN%02hhu.%s", &dummy, extnamerest);
00219 #if 0
00220 printf("%s -> %02hhu and %s\n", extname, dummy, extnamerest);
00221 #endif
00222 cpl_propertylist_update_string(hext, "EXTNAME", extnamerest);
00223
00224 char *chan = cpl_sprintf("CHAN%02hhu", ifu);
00225 cpl_propertylist_update_string(hout, "EXTNAME", chan);
00226 cpl_free(chan);
00227 cpl_propertylist_copy_property_regexp(hout, hext, "^ESO ", 0);
00228 cpl_propertylist_save(hout, outfn, CPL_IO_CREATE);
00229 cpl_propertylist_erase_regexp(hext, "^ESO ", 0);
00230 cpl_imagelist_save(list, outfn, CPL_TYPE_UNSPECIFIED, hext, CPL_IO_EXTEND);
00231 cpl_imagelist_delete(list);
00232 cpl_propertylist_delete(hout);
00233 break;
00234 }
00235 case IS_MUSE_IMAGE_DATA: {
00236 printf("\tMUSE image (XTENSION=\'%s\', EXTNAME=\'%s\')\n", xtension,
00237 extname);
00238
00239 muse_image *image = muse_image_load_from_extensions(iname, ifu);
00240 muse_image_save(image, outfn);
00241 muse_image_delete(image);
00242 break;
00243 }
00244 case IS_MUSE_IMAGE_DQ:
00245 case IS_MUSE_IMAGE_STAT:
00246 #if 0
00247 printf("MUSE OTHER (%s %s)\n", xtension, extname);
00248 #endif
00249 cpl_propertylist_delete(hext);
00250 cpl_free(outfn);
00251 continue;
00252 default:
00253 fprintf(stderr, "\tUnsupported FITS extension (XTENSION=\'%s\', "
00254 "extension %d)\n", xtension, iext);
00255 cpl_propertylist_delete(hext);
00256 cpl_free(outfn);
00257 continue;
00258 }
00259 cpl_propertylist_delete(hext);
00260
00261
00262 cpl_frame *frame = cpl_frame_new();
00263 cpl_frame_set_filename(frame, outfn);
00264 cpl_free(outfn);
00265 cpl_frame_set_tag(frame, tag);
00266 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
00267
00268 cpl_frame_set_type(frame, CPL_FRAME_TYPE_NONE);
00269 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_NONE);
00270 #if 0
00271 printf("frame:\t");
00272 cpl_frame_dump(frame, stdout);
00273 fflush(stdout);
00274 #endif
00275 cpl_frameset_insert(fset, frame);
00276 }
00277 cpl_propertylist_delete(header);
00278
00279 int nout = cpl_frameset_get_size(fset);
00280 if (dosign) {
00281 printf("Signing %d output product%s...\n", nout, nout == 0 ? "" : "s");
00282 cpl_dfs_sign_products(fset, CPL_DFS_SIGNATURE_DATAMD5 | CPL_DFS_SIGNATURE_CHECKSUM);
00283 }
00284 if (dodelete) {
00285 printf("Deleting input file \"%s\"...\n", iname);
00286 remove(iname);
00287 }
00288
00289 printf("Split output product%s:\n", nout == 0 ? "" : "s");
00290 for (i = 0; i < nout; i++) {
00291 const cpl_frame *frame = cpl_frameset_get_position_const(fset, i);
00292 const char *fn = cpl_frame_get_filename(frame);
00293 next = cpl_fits_count_extensions(fn);
00294 printf("\t\"%s\" (%d FITS extensions)\n", fn, next);
00295 }
00296 cpl_frameset_delete(fset);
00297
00298 int rc = 0;
00299 if (!cpl_errorstate_is_equal(state)) {
00300 cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
00301 rc = 50;
00302 }
00303 cpl_memory_dump();
00304 cpl_end();
00305 return rc;
00306 }
00307