KMOS Pipeline Reference Manual  1.3.11
kmo_fits_check.c
00001 /* 
00002  * This file is part of the KMOS Pipeline
00003  * Copyright (C) 2002,2003 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 #include <string.h>
00025 
00026 #include <cpl.h>
00027 
00028 #include "kmo_utils.h"
00029 #include "kmo_dfs.h"
00030 #include "kmo_error.h"
00031 #include "kmo_priv_fits_check.h"
00032 #include "kmo_constants.h"
00033 #include "kmo_debug.h"
00034 
00035 static int kmo_fits_check_create(cpl_plugin *);
00036 static int kmo_fits_check_exec(cpl_plugin *);
00037 static int kmo_fits_check_destroy(cpl_plugin *);
00038 static int kmo_fits_check(cpl_parameterlist *, cpl_frameset *);
00039 
00040 
00041 static char kmo_fits_check_description[] =
00042 "Recipe to print information on FITS files, contained data/noise values or\n"
00043 "header keywords of all extensions of a fits file, preferably a KMOS fits-file\n"
00044 "(RAW, F1I, F2I, F3I, F2D etc.). This recipe is intended for debugging purposes\n"
00045 "only.\n"
00046 "By default a short summary is printed.\n"
00047 "The following data types of keywords are recognized: bool, char, double, float,\n"
00048 "int, long, string\n"
00049 "As input one fits-file is accepted, no output frame is generated.\n"
00050 "\n"
00051 "BASIC PARAMETERS:\n"
00052 "-----------------\n"
00053 "--h\n"
00054 "With this parameter just the header keywords are printed:\n"
00055 "  –1  prints the primary header and the headers of all the extensions\n"
00056 "   0  prints just the primary header\n"
00057 "   1  prints the header of the first extension etc.\n"
00058 "\n"
00059 "--d\n"
00060 "With this parameter just the data (or depending on the extension: noise) is\n"
00061 "printed:\n"
00062 "  –1 prints the primary header and the headers of all the extensions\n"
00063 "   0 prints data of the primary header which is empty for KMOS FITS frames\n"
00064 "   1 prints the data/noise of the first extension etc.\n"
00065 "This parameter should only be used with very small datasets, otherwise the\n"
00066 "screen will be flooded with numbers.\n"
00067 "\n"
00068 "-------------------------------------------------------------------------------\n"
00069 "  Input files:\n"
00070 "\n"
00071 "   DO                    KMOS                                                  \n"
00072 "   category              Type   Explanation                    Required #Frames\n"
00073 "   --------              -----  -----------                    -------- -------\n"
00074 "   <none or any>         <any>  Any FITS file                      Y       1   \n"
00075 "\n"
00076 "  Output files:\n"
00077 "\n"
00078 "   DO                    KMOS\n"
00079 "   category              Type   Explanation\n"
00080 "   --------              -----  -----------\n"
00081 "   <none>                <none> No output frames generated, only text output   \n"
00082 "-------------------------------------------------------------------------------\n"
00083 "\n";
00084 
00099 int cpl_plugin_get_info(cpl_pluginlist *list)
00100 {
00101     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00102     cpl_plugin *plugin = &recipe->interface;
00103 
00104     cpl_plugin_init(plugin,
00105                         CPL_PLUGIN_API,
00106                         KMOS_BINARY_VERSION,
00107                         CPL_PLUGIN_TYPE_RECIPE,
00108                         "kmo_fits_check",
00109                         "Check contents of a KMOS fits-file",
00110                         kmo_fits_check_description,
00111                         "Alex Agudo Berbel",
00112                         "usd-help@eso.org",
00113                         kmos_get_license(),
00114                         kmo_fits_check_create,
00115                         kmo_fits_check_exec,
00116                         kmo_fits_check_destroy);
00117 
00118     cpl_pluginlist_append(list, plugin);
00119 
00120     return 0;
00121 }
00122 
00130 static int kmo_fits_check_create(cpl_plugin *plugin)
00131 {
00132     cpl_recipe *recipe;
00133     cpl_parameter *p;
00134 
00135     /* Check that the plugin is part of a valid recipe */
00136     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00137         recipe = (cpl_recipe *)plugin;
00138     else
00139         return -1;
00140 
00141     /* Create the parameters list in the cpl_recipe object */
00142     recipe->parameters = cpl_parameterlist_new();
00143 
00144     /* Fill the parameters list */
00145     /* --data */
00146     p = cpl_parameter_new_value("kmos.fits_check.data",
00147                                 CPL_TYPE_INT,
00148                                 "The extension whose data to print. "
00149                                 "(0 is the empty primary extension, "
00150                                 "-1 prints just all data extensions, "
00151                                 "by default all headers and data of all "
00152                                 "extensions are printed.",
00153                                 "kmos.fits_check",
00154                                 -2);
00155     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "data");
00156     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00157     cpl_parameterlist_append(recipe->parameters, p);
00158 
00159     /* --header */
00160     p = cpl_parameter_new_value("kmos.fits_check.header",
00161                                 CPL_TYPE_INT,
00162                                 "The extension whose header to print"
00163                                 "(0 is the empty primary extension, "
00164                                 "-1 prints just all headers, "
00165                                 "by default all headers and data of all "
00166                                 "extensions are printed.",
00167                                 "kmos.fits_check",
00168                                 -2);
00169     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "header");
00170     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00171     cpl_parameterlist_append(recipe->parameters, p);
00172 
00173     return 0;
00174 }
00175 
00181 static int kmo_fits_check_exec(cpl_plugin *plugin)
00182 {
00183     cpl_recipe  *recipe;
00184 
00185     /* Get the recipe out of the plugin */
00186     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00187         recipe = (cpl_recipe *)plugin;
00188     else return -1 ;
00189 
00190     return kmo_fits_check(recipe->parameters, recipe->frames);
00191 }
00192 
00198 static int kmo_fits_check_destroy(cpl_plugin *plugin)
00199 {
00200     cpl_recipe *recipe;
00201 
00202     /* Get the recipe out of the plugin */
00203     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00204         recipe = (cpl_recipe *)plugin;
00205     else return -1 ;
00206 
00207     cpl_parameterlist_delete(recipe->parameters);
00208     return 0 ;
00209 }
00210 
00211 
00212 
00221 static int kmo_fits_check(cpl_parameterlist *parlist, cpl_frameset *frameset)
00222 {
00223     int                 i                   = 0,
00224                         ret_val             = 0,
00225                         info                = FALSE,
00226                         valid               = TRUE,
00227                         h                   = 0,
00228                         d                   = 0;
00229     cpl_imagelist       *imglist            = NULL;
00230     cpl_image           *img                = NULL;
00231     kmclipm_vector      *vec                = NULL;
00232     cpl_frame           *frame              = NULL;
00233     cpl_propertylist    *pl                 = NULL;
00234     cpl_table           *tbl                = NULL;
00235     main_fits_desc      desc;
00236     const char          *xtension = NULL;
00237 
00238     KMO_TRY
00239     {
00240         kmo_init_fits_desc(&desc);
00241 
00242         /* --- check input --- */
00243         KMO_TRY_ASSURE((parlist != NULL) &&
00244                        (frameset != NULL),
00245                        CPL_ERROR_NULL_INPUT,
00246                        "Not all input data is provided!");
00247 
00248         if (cpl_frameset_get_size(frameset) == 0) {
00249             KMO_TRY_ASSURE(cpl_frameset_get_size(frameset) == 1,
00250                            CPL_ERROR_ILLEGAL_INPUT,
00251                            "Recipe needs a FITS file to inspect!");
00252         } else {
00253             KMO_TRY_ASSURE(cpl_frameset_get_size(frameset) == 1,
00254                            CPL_ERROR_ILLEGAL_INPUT,
00255                            "Recipe can check just one frame!");
00256         }
00257 
00258         KMO_TRY_EXIT_IF_NULL(
00259             frame = kmo_dfs_get_frame(frameset, "0"));
00260 
00261         cpl_msg_info("", "--- Parameter setup for kmo_fits_check ----");
00262 
00263         h = kmo_dfs_get_parameter_int(parlist, "kmos.fits_check.header");
00264         KMO_TRY_CHECK_ERROR_STATE();
00265         KMO_TRY_EXIT_IF_ERROR(
00266             kmo_dfs_print_parameter_help(parlist, "kmos.fits_check.header"));
00267 
00268         d = kmo_dfs_get_parameter_int(parlist, "kmos.fits_check.data");
00269         KMO_TRY_CHECK_ERROR_STATE();
00270         KMO_TRY_EXIT_IF_ERROR(
00271             kmo_dfs_print_parameter_help(parlist, "kmos.fits_check.data"));
00272 
00273         cpl_msg_info("", "-------------------------------------------");
00274 
00275         cpl_msg_severity msg_level = cpl_msg_get_level();
00276         cpl_msg_set_level(CPL_MSG_OFF);
00277         desc = kmo_identify_fits_header(cpl_frame_get_filename(frame));
00278         cpl_msg_set_level(msg_level);
00279 
00280         if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
00281             // This doesn't seem to be a valid KMOS FITS frame
00282             valid = FALSE;
00283             cpl_error_reset();
00284         } else {
00285             valid = TRUE;
00286 
00287             if (desc.frame_type == illegal_frame) {
00288                 valid = FALSE;
00289                 cpl_error_reset();
00290             }
00291         }
00292 
00293         if ((h == -2) && (d == -2)) {
00294             // default case, print info
00295             info = TRUE;
00296         }
00297 
00298         if (info) {
00299             /* print info */
00300             KMO_TRY_EXIT_IF_ERROR(
00301                 kmo_fits_check_print_info(valid, &desc, frame));
00302         } else if (((h == -1) && (d > cpl_frame_get_nextensions(frame)+1)) ||
00303                    ((d == -1) && (h > cpl_frame_get_nextensions(frame)+1))) {
00304             printf("++++++++++++++++++++++++++++++++++++++++"
00305                    "++++++++++++++++++++++++++++++++++++++++\n");
00306 
00307             printf("ERROR: Supplied fits file has only %d extensions!\n",
00308                    cpl_frame_get_nextensions(frame)+1);
00309 
00310             printf("++++++++++++++++++++++++++++++++++++++++"
00311                    "++++++++++++++++++++++++++++++++++++++++\n");
00312         } else {
00313             /* print requested header or data section (-1 prints all of them) */
00314             int nr_ext = 0;
00315             if (valid) {
00316                 nr_ext = desc.nr_ext;
00317             } else {
00318                 nr_ext = cpl_frame_get_nextensions(frame);
00319             }
00320 
00321             if ((h < -2) || (d < -2)) {
00322                 printf(">>> The specified extension number must be "
00323                        "equal or greater than 0 (-1 to display all headers)!\n");
00324             } else if ((h > nr_ext) || (d > nr_ext)) {
00325                 if (nr_ext == 0) {
00326                     printf(">>> The file has only a primary extension!\n");
00327                 } else if (nr_ext == 1) {
00328                     printf(">>> The file has only 1 extension!\n");
00329                 } else {
00330                     printf(">>> The file has only %d extensions!\n", nr_ext);
00331                 }
00332             } else {
00333                 for (i = 0; i <= nr_ext; i++) {
00334                     KMO_TRY_EXIT_IF_NULL(
00335                         pl = kmclipm_propertylist_load(cpl_frame_get_filename(frame),
00336                                                   i));
00337 
00338                     // print label
00339                     if (((h == -1) || (d == -1)) ||
00340                         ((h == i) && (d == -2)) ||
00341                         ((h == -1) && (d == -2)) ||
00342                         ((h == -2) && (d == i)) ||
00343                         ((h == -2) && (d == -1)) ||
00344                         ((h == d) && (h == i))) {
00345                         if (i == 0) {
00346                             printf("++++++++++++++++++++++++++++++++++++++++"
00347                                    "++++++++++++++++++++++++++++++++++++++++\n");
00348                             if (cpl_propertylist_get_int(pl, NAXIS) == 0) {
00349                                 printf("Extension 0, primary\n");
00350                                 printf("--------------------\n");
00351                             } else {
00352                                 printf("Extension 0\n");
00353                                 printf("-----------\n");
00354                             }
00355                         } else {
00356                             printf("++++++++++++++++++++++++++++++++++++++++"
00357                                    "++++++++++++++++++++++++++++++++++++++++\n");
00358                             printf("Extension %d\n", i);
00359                             printf("------------\n");
00360                         }
00361                     }
00362 
00363                     // print header
00364                     if (((h == -1) && (d == -1)) ||
00365                         ((d == -2) && (h == i)) ||
00366                         ((d == -2) && (h == -1)) ||
00367                         ((h == d) && (h == i)))
00368                     {
00369                         KMO_TRY_EXIT_IF_ERROR(
00370                             kmo_fits_check_print_header(pl));
00371                     }
00372 
00373                     // print separator between header and data
00374                     if (d == h) {
00375                         printf("---------------------------\n");
00376                     }
00377 
00378                     // print data
00379                     if (((h == -1) && (d == -1)) ||
00380                         ((h == -2) && (d == i)) ||
00381                         ((h == -2) && (d == -1)) ||
00382                         ((h == d) && (h == i))) {
00383 
00384                         if (cpl_propertylist_get_int(pl, NAXIS) == 0) {
00385                             printf("empty\n");
00386                         } else {
00387                             if (cpl_propertylist_has(pl, XTENSION)) {
00388                                 xtension = cpl_propertylist_get_string(pl,
00389                                                                     XTENSION);
00390                             } else {
00391                                 xtension = "";
00392                             }
00393 
00394                             if ((desc.fits_type == f3i_fits) ||
00395                                 ((cpl_propertylist_get_int(pl, NAXIS) == 3) &&
00396                                         (desc.fits_type == illegal_fits))) {
00397                                 KMO_TRY_EXIT_IF_NULL(
00398                                     imglist = kmclipm_imagelist_load(
00399                                                   cpl_frame_get_filename(frame),
00400                                                   CPL_TYPE_FLOAT, i));
00401 
00402                                 KMO_TRY_EXIT_IF_ERROR(
00403                                     kmo_fits_check_print_imagelist(imglist));
00404 
00405                                 cpl_imagelist_delete(imglist); imglist = NULL;
00406                             } else if ((desc.fits_type == f2d_fits) ||
00407                                        (desc.fits_type == b2d_fits) ||
00408                                        (desc.fits_type == f2i_fits) ||
00409                                        (desc.fits_type == raw_fits) ||
00410                                        ((cpl_propertylist_get_int(pl, NAXIS) == 2) &&
00411                                         (desc.fits_type == illegal_fits) &&
00412                                         (strcmp(xtension, "BINTABLE") != 0)))
00413                             {
00414                                 KMO_TRY_EXIT_IF_NULL(
00415                                     img = kmclipm_image_load(
00416                                                   cpl_frame_get_filename(frame),
00417                                                   CPL_TYPE_FLOAT, 0, i));
00418 
00419                                 KMO_TRY_EXIT_IF_ERROR(
00420                                     kmo_fits_check_print_image(img));
00421 
00422                                 cpl_image_delete(img); img = NULL;
00423                             } else if ((desc.fits_type == f1i_fits) ||
00424                                        (desc.fits_type == f1d_fits) ||
00425                                        (desc.fits_type == f1s_fits) ||
00426                                        ((cpl_propertylist_get_int(pl, NAXIS) == 1) &&
00427                                         (desc.fits_type == illegal_fits)))
00428                             {
00429                                 KMO_TRY_EXIT_IF_NULL(
00430                                     vec = kmclipm_vector_load(
00431                                                   cpl_frame_get_filename(frame),
00432                                                   i));
00433 
00434                                 KMO_TRY_EXIT_IF_ERROR(
00435                                     kmo_fits_check_print_vector(vec));
00436 
00437                                 kmclipm_vector_delete(vec); vec = NULL;
00438                             } else if ((desc.fits_type == f1l_fits) ||
00439                                        (desc.fits_type == f2l_fits) ||
00440                                        (strcmp(xtension, "BINTABLE") == 0))
00441                             {
00442                                 KMO_TRY_EXIT_IF_NULL(
00443                                     tbl = kmclipm_table_load(
00444                                                   cpl_frame_get_filename(frame),
00445                                                   i, 0));
00446 
00447                                 KMO_TRY_EXIT_IF_ERROR(
00448                                     kmo_fits_check_print_table(tbl));
00449 
00450                                 cpl_table_delete(tbl); tbl = NULL;
00451                             }
00452                         }
00453                     }
00454                     cpl_propertylist_delete(pl); pl = NULL;
00455                 }
00456                 printf("++++++++++++++++++++++++++++++++++++++++"
00457                        "++++++++++++++++++++++++++++++++++++++++\n");
00458 
00459                 if (desc.fits_type == raw_fits) {
00460                     printf(">>> Attention: It is assumed that the file\n");
00461                     printf(">>>            is a RAW-fits with 4 extensions\n");
00462                 }
00463             }
00464         }
00465     }
00466     KMO_CATCH
00467     {
00468         KMO_CATCH_MSG();
00469 
00470         ret_val = -1;
00471     }
00472 
00473     kmo_free_fits_desc(&desc);
00474 
00475     return ret_val;
00476 }
00477