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
00056
00057
00060 #define PRINT_USAGE(rc) \
00061 fprintf(stderr, "Usage: %s CUBE_OUT CUBE_IN_1 CUBE_IN_2 [ CUBE_IN_3 ... ]\n",\
00062 argv[0]); \
00063 cpl_end(); return (rc);
00064
00065 int main(int argc, char **argv)
00066 {
00067 const char *idstring = "muse_cube_concatenate";
00068 cpl_init(CPL_INIT_DEFAULT);
00069 cpl_msg_set_time_on();
00070 muse_processing_recipeinfo(NULL);
00071 cpl_msg_set_level(CPL_MSG_DEBUG);
00072 cpl_msg_set_component_on();
00073
00074 if (argc < 4) {
00075
00076 PRINT_USAGE(1);
00077 }
00078
00079 char *oname = NULL;
00080
00081 int i;
00082 for (i = 1; i < argc; i++) {
00083 if (strncmp(argv[i], "-", 1) == 0) {
00084 PRINT_USAGE(9);
00085 } else {
00086 if (oname) {
00087 break;
00088 }
00089 oname = argv[i];
00090 }
00091 }
00092 if (!oname) {
00093 PRINT_USAGE(10);
00094 }
00095 FILE *fp = fopen(oname, "r");
00096 if (fp) {
00097 cpl_msg_error(idstring, "Output cube \"%s\" is already present!", oname);
00098 cpl_msg_error(idstring, "Please specify another output name or rename the "
00099 "existing file with this name.");
00100 fclose(fp);
00101 PRINT_USAGE(11);
00102 }
00103
00104 int ncubes = argc - i;
00105 cpl_msg_info(idstring, "Will write concatenation of %d cubes to \"%s\".",
00106 ncubes, oname);
00107
00108 cpl_errorstate state = cpl_errorstate_get();
00109
00110
00111
00112 cpl_msg_info(idstring, "Loading cube 1 from \"%s\"", argv[2]);
00113 muse_datacube *cube1 = muse_datacube_load(argv[2]);
00114 int icube = 3;
00115 for (icube = 3; icube < ncubes + 2; icube++) {
00116 cpl_msg_info(idstring, "Loading cube %d from \"%s\"", icube - 1, argv[icube]);
00117 muse_datacube *cube2 = muse_datacube_load(argv[icube]);
00118 if (!cube2) {
00119 cpl_msg_warning(idstring, "Could not load MUSE cube from \"%s\": %s",
00120 argv[icube], cpl_error_get_message());
00121 continue;
00122 }
00123 cpl_error_code rc = muse_datacube_concat(cube1, cube2);
00124 if (rc != CPL_ERROR_NONE) {
00125 cpl_msg_warning(idstring, "Error while concatenating \"%s\": %s",
00126 argv[icube], cpl_error_get_message());
00127 }
00128 muse_datacube_delete(cube2);
00129 }
00130
00131 int rc = 0;
00132 if (!cpl_errorstate_is_equal(state)) {
00133 cpl_msg_error(idstring, "Some errors occurred while concatenating the "
00134 "cubes (not saving an output cube):");
00135 cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
00136 rc = 50;
00137 } else {
00138 cpl_msg_info(idstring, "Saving concatenated cube as \"%s\".", oname);
00139 muse_datacube_save(cube1, oname);
00140 cpl_msg_info(idstring, "...done");
00141 }
00142 muse_datacube_delete(cube1);
00143
00144 cpl_memory_dump();
00145 cpl_end();
00146 return rc;
00147 }
00148