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
00061
00062
00065 #define PRINT_USAGE(rc) \
00066 fprintf(stderr, "Usage: %s [ -f ] [ -s SOFFILE ] OFFSET_LIST " \
00067 "[ FILE [ FILE [ ... ] ] ] \n", argv[0]); \
00068 cpl_end(); return (rc);
00069
00070 int main(int argc, char **argv)
00071 {
00072 const char *idstring = "muse_offset_list_create";
00073 cpl_init(CPL_INIT_DEFAULT);
00074 cpl_msg_set_time_on();
00075 muse_processing_recipeinfo(NULL);
00076 cpl_msg_set_level(CPL_MSG_DEBUG);
00077 cpl_msg_set_component_on();
00078 cpl_errorstate state = cpl_errorstate_get();
00079
00080 if (argc < 2) {
00081
00082 PRINT_USAGE(1);
00083 }
00084
00085 char *oname = NULL;
00086 cpl_array *files = NULL,
00087 *soffiles = NULL;
00088 cpl_boolean overwrite = CPL_FALSE;
00089 int i;
00090
00091
00092 for (i = 1; i < argc; i++) {
00093 if (strncmp(argv[i], "-s", 3) == 0) {
00094
00095 i++;
00096 if (i < argc) {
00097 FILE *fp = fopen(argv[i], "r");
00098 if (!fp) {
00099 PRINT_USAGE(3);
00100 }
00101 if (!soffiles) {
00102 soffiles = cpl_array_new(0, CPL_TYPE_STRING);
00103 }
00104 char fn[4096],
00105 tag[50];
00106 while (fscanf(fp, "%4095s %49s", fn, tag) != EOF) {
00107 if (!strncmp(tag, "PIXTABLE_", 9)) {
00108 #if 0
00109 cpl_msg_debug(idstring, "IS a pixel table: \"%s\" is %s", fn, tag);
00110 #endif
00111
00112 cpl_array_set_size(soffiles, cpl_array_get_size(soffiles) + 1);
00113
00114 cpl_array_set_string(soffiles, cpl_array_get_size(soffiles) - 1, fn);
00115 } else {
00116 #if 0
00117 cpl_msg_debug(idstring, "not a pixel table: \"%s\" is %s", fn, tag);
00118 #endif
00119 }
00120 }
00121 fclose(fp);
00122 } else {
00123 PRINT_USAGE(2);
00124 }
00125 } else if (strncmp(argv[i], "-f", 3) == 0) {
00126 overwrite = CPL_TRUE;
00127 } else if (strncmp(argv[i], "-", 1) == 0) {
00128 PRINT_USAGE(9);
00129 } else {
00130 if (!oname) {
00131 oname = argv[i];
00132 } else if (!files) {
00133 files = cpl_array_new(1, CPL_TYPE_STRING);
00134
00135 cpl_array_set_string(files, 0, argv[i]);
00136 } else {
00137 cpl_array_set_size(files, cpl_array_get_size(files) + 1);
00138
00139 cpl_array_set_string(files, cpl_array_get_size(files) - 1, argv[i]);
00140 }
00141 }
00142 }
00143
00144 #if 0
00145 printf("files:\n");
00146 cpl_array_dump(files, 0, cpl_array_get_size(files), stdout);
00147 printf("from .sof:\n");
00148 cpl_array_dump(soffiles, 0, cpl_array_get_size(soffiles), stdout);
00149 fflush(stdout);
00150 #endif
00151
00152
00153 if (!files) {
00154 files = soffiles;
00155 } else {
00156 if (cpl_array_get_size(soffiles) > 0) {
00157 cpl_array_insert(files, soffiles, cpl_array_get_size(files));
00158 }
00159 cpl_array_delete(soffiles);
00160 soffiles = NULL;
00161 }
00162
00163
00164
00165 if (!oname) {
00166 cpl_array_delete(files);
00167 PRINT_USAGE(10);
00168 }
00169 if (!access(oname, F_OK) && !overwrite) {
00170 cpl_array_delete(files);
00171 PRINT_USAGE(11);
00172 }
00173
00174
00175 cpl_propertylist *pheader = cpl_propertylist_new();
00176 cpl_propertylist_append_string(pheader, "INSTRUME", "MUSE");
00177 cpl_propertylist_append_string(pheader, "ESO PRO CATG", MUSE_TAG_OFFSET_LIST);
00178 cpl_table *olist = NULL;
00179 int nfiles = 0;
00180 if (!files) {
00181 cpl_msg_debug(idstring, "No MUSE exposures given on input, creating empty "
00182 "table with 10 rows");
00183 olist = muse_cpltable_new(muse_offset_list_def, 10);
00184 cpl_propertylist_append_string(pheader, "FILES", "no files given");
00185 } else {
00186 nfiles = cpl_array_get_size(files);
00187 cpl_msg_debug(idstring, "%d MUSE exposures given on input, creating table "
00188 "for these", nfiles);
00189 olist = muse_cpltable_new(muse_offset_list_def, nfiles);
00190 }
00191
00192 #if 0
00193
00194
00195 cpl_recipe *recipe = NULL;
00196 muse_processing *proc = muse_processing_new("muse_offset_list_create", recipe);
00197 muse_processing_sort_exposures(proc);
00198 muse_processing_delete(proc);
00199 #endif
00200
00201
00202 for (i = 0; i < nfiles; i++) {
00203 const char *fn = cpl_array_get_string(files, i);
00204 cpl_propertylist *header = cpl_propertylist_load(fn, 0);
00205 if (!header) {
00206 cpl_msg_warning(idstring, "Could not load primary header from \"%s\"!",
00207 fn);
00208 continue;
00209 }
00210 cpl_table_set_string(olist, MUSE_OFFSETS_DATEOBS, i,
00211 muse_pfits_get_dateobs(header));
00212 cpl_table_set_double(olist, MUSE_OFFSETS_MJDOBS, i,
00213 muse_pfits_get_mjdobs(header));
00214 cpl_propertylist_delete(header);
00215 char *kw = cpl_sprintf("FILE%d", i+1),
00216 *p = strrchr(fn, '/');
00217 if (p) {
00218 p++;
00219 } else {
00220 p = (char *)fn;
00221 }
00222 cpl_propertylist_append_string(pheader, kw, p);
00223 cpl_free(kw);
00224 }
00225
00226
00227 cpl_error_code rc = cpl_table_save(olist, pheader, NULL, oname, CPL_IO_CREATE);
00228 cpl_array_delete(files);
00229 cpl_table_delete(olist);
00230 cpl_propertylist_delete(pheader);
00231 switch (rc) {
00232 case CPL_ERROR_NONE:
00233 rc = 0;
00234 cpl_msg_info(idstring, "Saved %s as \"%s\"", MUSE_TAG_OFFSET_LIST, oname);
00235 break;
00236 case CPL_ERROR_FILE_NOT_CREATED:
00237 cpl_msg_error(idstring, "Could not create output file \"%s\"", oname);
00238 rc = 12;
00239 break;
00240 default:
00241 rc = 50;
00242 cpl_msg_error(idstring, "An unknown error occurred: %s",
00243 cpl_error_get_message());
00244 }
00245
00246 if (!cpl_errorstate_is_equal(state)) {
00247 cpl_errorstate_dump(state, CPL_FALSE, muse_cplerrorstate_dump_some);
00248 }
00249 cpl_memory_dump();
00250 cpl_end();
00251 return rc;
00252 }
00253