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
00071
00072
00075 #define PRINT_USAGE(rc) \
00076 fprintf(stderr, "Usage: %s [ -l1 lambda1 ] [ -l2 lambda2 ] [ -x1 xpos1 ] " \
00077 "[ -x2 xpos2 ] [ -y1 ypos1 ] [ -y2 ypos2 ] [ -i IFU ] [ -s slice ] " \
00078 "PIXTABLE_IN PIXTABLE_OUT\n", argv[0]); \
00079 cpl_end(); return (rc);
00080
00081 int main(int argc, char **argv)
00082 {
00083 cpl_init(CPL_INIT_DEFAULT);
00084 muse_processing_recipeinfo(NULL);
00085
00086 if (argc <= 1) {
00087
00088 PRINT_USAGE(1);
00089 }
00090
00091 char *tiname = NULL,
00092 *toname = NULL;
00093
00094 double x1 = -FLT_MAX, x2 = FLT_MAX,
00095 y1 = -FLT_MAX, y2 = FLT_MAX,
00096 l1 = -FLT_MAX, l2 = FLT_MAX;
00097 unsigned char ifu = 0;
00098 unsigned short slice = 0;
00099 int i;
00100
00101
00102 for (i = 1; i < argc; i++) {
00103 if (strncmp(argv[i], "-x1", 4) == 0) {
00104
00105 i++;
00106 if (i < argc) {
00107 x1 = atof(argv[i]);
00108 } else {
00109 PRINT_USAGE(2);
00110 }
00111 } else if (strncmp(argv[i], "-x2", 4) == 0) {
00112 i++;
00113 if (i < argc) {
00114 x2 = atof(argv[i]);
00115 } else {
00116 PRINT_USAGE(2);
00117 }
00118 } else if (strncmp(argv[i], "-y1", 4) == 0) {
00119 i++;
00120 if (i < argc) {
00121 y1 = atof(argv[i]);
00122 } else {
00123 PRINT_USAGE(3);
00124 }
00125 } else if (strncmp(argv[i], "-y2", 4) == 0) {
00126 i++;
00127 if (i < argc) {
00128 y2 = atof(argv[i]);
00129 } else {
00130 PRINT_USAGE(3);
00131 }
00132 } else if (strncmp(argv[i], "-l1", 4) == 0) {
00133
00134 i++;
00135 if (i < argc) {
00136 l1 = atof(argv[i]);
00137 } else {
00138 PRINT_USAGE(4);
00139 }
00140 } else if (strncmp(argv[i], "-l2", 4) == 0) {
00141 i++;
00142 if (i < argc) {
00143 l2 = atof(argv[i]);
00144 } else {
00145 PRINT_USAGE(4);
00146 }
00147 } else if (strncmp(argv[i], "-i", 3) == 0) {
00148 i++;
00149 if (i < argc) {
00150 ifu = atoi(argv[i]);
00151 } else {
00152 PRINT_USAGE(5);
00153 }
00154 } else if (strncmp(argv[i], "-s", 3) == 0) {
00155 i++;
00156 if (i < argc) {
00157 slice = atoi(argv[i]);
00158 } else {
00159 PRINT_USAGE(6);
00160 }
00161 } else if (strncmp(argv[i], "-", 1) == 0) {
00162 PRINT_USAGE(9);
00163 } else {
00164 if (tiname && toname) {
00165 break;
00166 }
00167 if (!tiname) {
00168 tiname = argv[i];
00169 } else {
00170 toname = argv[i];
00171 }
00172 }
00173 }
00174 if (!tiname || !toname) {
00175 PRINT_USAGE(1);
00176 }
00177
00178 cpl_msg_set_level(CPL_MSG_WARNING);
00179 muse_pixtable *table = muse_pixtable_load(tiname);
00180 if (!table) {
00181 PRINT_USAGE(10);
00182 }
00183
00184
00185
00186
00187
00188 double ll = fmax(l1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_LLO)),
00189 lu = fmin(l2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_LHI)),
00190 xl = fmax(x1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_XLO)),
00191 xu = fmin(x2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_XHI)),
00192 yl = fmax(y1, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_YLO)),
00193 yu = fmin(y2, cpl_propertylist_get_float(table->header, MUSE_HDR_PT_YHI));
00194
00195 printf("MUSE pixel table \"%s\" (%"CPL_SIZE_FORMAT" rows)\n"
00196 " cropping to lambda = %.2f..%.2f Angstrom\n"
00197 " xpos = %.3e..%.3e %s\n"
00198 " ypos = %.3e..%.3e %s\n",
00199 tiname, muse_pixtable_get_nrow(table), ll, lu,
00200 xl, xu, cpl_table_get_column_unit(table->table, MUSE_PIXTABLE_XPOS),
00201 yl, yu, cpl_table_get_column_unit(table->table, MUSE_PIXTABLE_YPOS));
00202
00203 muse_pixtable_restrict_wavelength(table, l1, l2);
00204 muse_pixtable_restrict_xpos(table, x1, x2);
00205 muse_pixtable_restrict_ypos(table, y1, y2);
00206
00207
00208 if (ifu >= 1 && ifu <= kMuseNumIFUs) {
00209 printf(" selecting pixels from IFU %02hhu ", ifu);
00210 cpl_table_unselect_all(table->table);
00211 const int *origin = cpl_table_get_data_int_const(table->table,
00212 MUSE_PIXTABLE_ORIGIN);
00213 cpl_size irow, nrow = muse_pixtable_get_nrow(table);
00214 for (irow = 0; irow < nrow; irow++) {
00215 if (muse_pixtable_origin_get_ifu(origin[irow]) != ifu) {
00216 cpl_table_select_row(table->table, irow);
00217 }
00218 }
00219 printf("(%d of %d)\n", (int)cpl_table_count_selected(table->table), (int)nrow);
00220 cpl_table_erase_selected(table->table);
00221 }
00222 if (slice >= 1 && slice <= kMuseSlicesPerCCD) {
00223 printf(" selecting pixels from slice %02hu ", slice);
00224 cpl_table_unselect_all(table->table);
00225 const int *origin = cpl_table_get_data_int_const(table->table,
00226 MUSE_PIXTABLE_ORIGIN);
00227 cpl_size irow, nrow = muse_pixtable_get_nrow(table);
00228 for (irow = 0; irow < nrow; irow++) {
00229 if (muse_pixtable_origin_get_slice(origin[irow]) != slice) {
00230 cpl_table_select_row(table->table, irow);
00231 }
00232 }
00233 printf("(%d of %d)\n", (int)cpl_table_count_selected(table->table), (int)nrow);
00234 cpl_table_erase_selected(table->table);
00235 }
00236
00237
00238 cpl_error_code rc = muse_pixtable_save(table, toname);
00239 switch (rc) {
00240 case CPL_ERROR_NONE:
00241 rc = 0;
00242 break;
00243 default:
00244 rc = 50;
00245 }
00246 printf("MUSE pixel table \"%s\" (%"CPL_SIZE_FORMAT" rows) saved\n",
00247 toname, muse_pixtable_get_nrow(table));
00248
00249 muse_pixtable_delete(table);
00250 cpl_end();
00251 return rc;
00252 }
00253