00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <cpl.h>
00037
00038 #include <string.h>
00039 #include <strings.h>
00040 #include <sys/types.h>
00041 #include <regex.h>
00042 #include <assert.h>
00043
00044 #include "xsh_data_instrument.h"
00045 #include "xsh_qc_handling.h"
00046 #include "xsh_msg.h"
00047 #include "xsh_paf_save.h"
00048 #include "xsh_error.h"
00049
00050
00051
00052
00053
00054
00055
00056 #define PAF_KEY_LEN 21
00057
00058 #define PAF_KEY_FORMAT "%-21s "
00059
00060
00066
00067
00070
00071
00072
00073 static FILE * irplib_paf_init(const xsh_instrument * instrume,
00074 const char * recipe,
00075 const char * filename,
00076 const cpl_propertylist* primary_header) ;
00077 static cpl_error_code irplib_paf_dump( qc_description *pqc,
00078 const char * kwnamw, const cpl_propertylist * paflist,
00079 FILE *paf ) ;
00080 static cpl_error_code irplib_paf_dump_int( char *kw, int value,
00081 const char *comment, FILE *paf ) ;
00082 static cpl_error_code irplib_paf_dump_double( char *kw, double value,
00083 const char *comment, FILE *paf ) ;
00084 static cpl_error_code irplib_paf_dump_string( char *kw, const char * value,
00085 const char *comment, FILE *paf ) ;
00086
00087
00103
00104 cpl_error_code xsh_paf_save( const xsh_instrument * instrument,
00105 const char *recipe,
00106 const cpl_propertylist * paflist,
00107 const char * filename,
00108 const char * pro_catg )
00109 {
00110 qc_description * pqc ;
00111 FILE *paf = NULL ;
00112 const char * arm ;
00113
00114 XSH_ASSURE_NOT_NULL( instrument ) ;
00115 XSH_ASSURE_NOT_NULL( recipe ) ;
00116 XSH_ASSURE_NOT_NULL( paflist ) ;
00117 XSH_ASSURE_NOT_NULL( filename ) ;
00118
00119 arm = xsh_instrument_arm_tostring( (xsh_instrument *)instrument ) ;
00120 xsh_msg_dbg_low( "=====>>>>> paf_save (%s, %s, %s)", recipe,
00121 arm, pro_catg ) ;
00122
00123 paf = irplib_paf_init( instrument, recipe, filename, paflist ) ;
00124
00125 pqc = NULL ;
00126
00127
00128
00129
00130 while ( (pqc = xsh_get_qc_desc_by_recipe( recipe, pqc )) != NULL ) {
00131 xsh_msg_dbg_low( " Found KW: %s\n", pqc->kw_name ) ;
00132
00133
00134
00135 #if 1
00136 if ( xsh_is_qc_for_pro_catg( pro_catg, pqc ) == 0 ) {
00137
00138 xsh_msg_dbg_low( "QC '%s' not for PRO.CATG '%s'", pqc->kw_name,
00139 pro_catg ) ;
00140 continue ;
00141 }
00142 #endif
00143
00144
00145
00146 if ( xsh_is_qc_for_arm( arm, pqc ) == 0 ) {
00147
00148 xsh_msg_dbg_low( "QC '%s' not for arm '%s'", pqc->kw_name, arm ) ;
00149 continue ;
00150 }
00151
00152
00153
00154
00155
00156 if ( strchr( pqc->kw_name, 'i' ) != NULL ) {
00157
00158 char kformat[256];
00159 const char *pk;
00160 char* pm;
00161 int i ;
00162 xsh_msg_dbg_low( " ++++ Multiple KW '%s'", pqc->kw_name ) ;
00163
00164 for( i = 0, pk = pqc->kw_name, pm = kformat ; *pk != '\0' ; i++, pk++ )
00165 if ( *pk == 'i' ) {
00166 strcpy( pm, "%d" ) ;
00167 pm += 2 ;
00168 }
00169 else *pm++ = *pk ;
00170 *pm = '\0' ;
00171 for( i = 0 ; i<10 ; i++ ) {
00172 char curname[256] ;
00173 sprintf( curname, kformat, i ) ;
00174 xsh_msg_dbg_low( " %d --> '%s'", i, curname ) ;
00175 if ( cpl_propertylist_has( paflist, curname ) ) {
00176 xsh_msg_dbg_low( "QC Parameter \"%s\" is in propertylist",
00177 curname ) ;
00178
00179 irplib_paf_dump( pqc, curname, paflist, paf ) ;
00180 }
00181 else break ;
00182 }
00183 if ( i == 0 ) xsh_msg( "QC Parameter \"%s\" NOT in propertylist",
00184 pqc->kw_name ) ;
00185 }
00186 else if ( cpl_propertylist_has( paflist, pqc->kw_name ) ) {
00187 xsh_msg_dbg_low( "QC Parameter \"%s\" is in propertylist",
00188 pqc->kw_name ) ;
00189
00190 irplib_paf_dump( pqc, pqc->kw_name, paflist, paf ) ;
00191 }
00192 else xsh_msg_debug( "QC Parameter \"%s\" NOT in propertylist",
00193 pqc->kw_name ) ;
00194
00195 }
00196
00197
00198 fclose( paf ) ;
00199
00200 cleanup:
00201 return cpl_error_get_code() ;
00202
00203 }
00204
00225
00226 static FILE * irplib_paf_init(const xsh_instrument * instrument,
00227 const char * recipe,
00228 const char * filename,
00229 const cpl_propertylist* primary_header)
00230 {
00231 FILE * paf = NULL;
00232 char * paf_id = NULL;
00233 const char paf_desc[] = "QC file";
00234 int nlen;
00235 cpl_error_code error;
00236 const char* key_value = NULL;
00237
00238
00239 cpl_ensure(instrument != NULL, CPL_ERROR_NULL_INPUT, NULL);
00240 cpl_ensure(recipe != NULL, CPL_ERROR_NULL_INPUT, NULL);
00241 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
00242
00243 #if 0
00244 paf_id = cpl_malloc(sizeof(char) *
00245 (strlen(instrument) + strlen("/") + strlen(recipe) + 1));
00246 assert( paf_id != NULL);
00247 strcpy(paf_id, instrument);
00248 strcat(paf_id, "/");
00249 strcat(paf_id, recipe);
00250 #else
00251 paf_id = xsh_stringcat_any( recipe, NULL ) ;
00252 #endif
00253 paf = fopen(filename, "w");
00254
00255 if (paf == NULL) {
00256 cpl_free(paf_id);
00257 cpl_ensure(0, CPL_ERROR_FILE_IO, NULL);
00258 return NULL;
00259 }
00260
00261
00262
00263
00264 error = CPL_ERROR_NONE;
00265
00266 if (!error) {
00267 nlen = fprintf(paf, "PAF.HDR.START ;# start of header\n");
00268 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00269 }
00270
00271 if (!error) {
00272 nlen = fprintf(paf, "PAF.TYPE \"pipeline product\" ;\n");
00273 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00274 }
00275
00276 if (!error) {
00277 nlen = fprintf(paf, "PAF.ID \"%s\"\n", paf_id);
00278 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00279 }
00280
00281 if (!error) {
00282 nlen = fprintf(paf, "PAF.NAME \"%s\"\n", filename);
00283 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00284 }
00285
00286 if (!error) {
00287 nlen = fprintf(paf, "PAF.DESC \"%s\"\n", paf_desc);
00288 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00289 }
00290
00291 if (!error) {
00292 nlen = fprintf(paf, "PAF.CHCK.CHECKSUM \"\"\n");
00293 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00294 }
00295
00296 if (!error) {
00297 nlen = fprintf(paf, "PAF.HDR.END ;# end of header\n");
00298 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00299 }
00300
00301
00302 if(cpl_propertylist_has(primary_header,"ARCFILE")) {
00303 key_value=cpl_propertylist_get_string(primary_header,"ARCFILE");
00304 } else {
00305 key_value="ARCFILE_NOT_FOUND";
00306 }
00307 if (!error) {
00308 nlen = fprintf(paf, "ARCFILE \"%s\";# archive file name\n",key_value);
00309 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00310 }
00311
00312 if(cpl_propertylist_has(primary_header,"PIPEFILE")) {
00313 key_value=cpl_propertylist_get_string(primary_header,"PIPEFILE");
00314 } else {
00315 key_value="PIPEFILE_NOT_FOUND";
00316 }
00317 if (!error) {
00318 nlen = fprintf(paf, "PIPEFILE \"%s\";# File name of data product\n",key_value);
00319 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00320 }
00321
00322
00323 if(cpl_propertylist_has(primary_header,"PRO.REC1.PIPE.ID")) {
00324 key_value=cpl_propertylist_get_string(primary_header,"PRO.REC1.PIPE.ID");
00325 } else {
00326 key_value="PRO_REC1_PIPE_ID_NOT_FOUND";
00327 }
00328 if (!error) {
00329 nlen = fprintf(paf, "PRO.REC1.PIPE.ID \"%s\";# Pipeline (unique) identifier\n",key_value);
00330 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00331 }
00332
00333
00334
00335
00336 if (!error) {
00337 nlen = fprintf(paf, "PRO.TYPE \"REDUCED\";# Product type\n");
00338 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00339 }
00340
00341
00342
00343 if (!error) {
00344 nlen = fprintf(paf, "\n");
00345 if (nlen != 1) error = CPL_ERROR_FILE_IO;
00346 }
00347
00348 cpl_free(paf_id);
00349
00350 if (error) {
00351 cpl_msg_error(cpl_func, "Could not write PAF: %s", filename);
00352 cpl_ensure(0, error, NULL);
00353 }
00354 fflush( paf ) ;
00355
00356 if(paf != NULL) {
00357 return paf;
00358 }
00359 return NULL;
00360 }
00361
00362 static void no_space( char * str )
00363 {
00364 for( ; *str != '\0' ; str++ )
00365 if ( *str == ' ' ) *str = '.' ;
00366 }
00367
00397 static cpl_error_code irplib_paf_dump( qc_description *pqc, const char *kwname,
00398 const cpl_propertylist * paflist,
00399 FILE *paf )
00400 {
00401 cpl_error_code err ;
00402 char * keydot = NULL, *pk ;
00403
00404 keydot = cpl_strdup( kwname ) ;
00405 pk = keydot ;
00406
00407 no_space( keydot ) ;
00408 if ( strncmp( keydot, "ESO.", 4 ) == 0 ) pk += 4 ;
00409 xsh_msg_dbg_low( "irplib_dump: '%s'", pk ) ;
00410
00411 switch( cpl_propertylist_get_type( paflist, kwname ) ) {
00412 case CPL_TYPE_CHAR:
00413 err = irplib_paf_dump_int(pk,
00414 cpl_propertylist_get_char( paflist, kwname),
00415 pqc->kw_help, paf);
00416 break ;
00417 case CPL_TYPE_INT:
00418 err = irplib_paf_dump_int(pk,
00419 cpl_propertylist_get_int( paflist, kwname),
00420 pqc->kw_help, paf);
00421 break;
00422 case CPL_TYPE_LONG:
00423 if (sizeof(long) == sizeof(int))
00424 err = irplib_paf_dump_int(pk,
00425 cpl_propertylist_get_long( paflist,
00426 kwname),
00427 pqc->kw_help, paf);
00428 break;
00429 case CPL_TYPE_FLOAT:
00430 err = irplib_paf_dump_double(pk,
00431 cpl_propertylist_get_float( paflist,
00432 kwname),
00433 pqc->kw_help, paf);
00434 break;
00435 case CPL_TYPE_DOUBLE:
00436 err = irplib_paf_dump_double(pk,
00437 cpl_propertylist_get_double( paflist,
00438 kwname),
00439 pqc->kw_help, paf);
00440 break;
00441 case CPL_TYPE_STRING:
00442 err = irplib_paf_dump_string(pk,
00443 cpl_propertylist_get_string( paflist,
00444 kwname),
00445 pqc->kw_help, paf);
00446 break;
00447 default:
00448 err = CPL_ERROR_UNSUPPORTED_MODE;
00449 }
00450 cpl_free(keydot);
00451 return err ;
00452 }
00453
00464 static cpl_error_code irplib_paf_dump_int( char *key, int value,
00465 const char *comment, FILE *paf )
00466 {
00467 cpl_ensure_code(paf, CPL_ERROR_NULL_INPUT);
00468 cpl_ensure_code(key, CPL_ERROR_NULL_INPUT);
00469
00470 if (comment == NULL)
00471 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%d\n",
00472 key, value) > PAF_KEY_LEN,
00473 CPL_ERROR_FILE_IO);
00474 else
00475 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%d ; # %s\n",
00476 key, value, comment) > PAF_KEY_LEN,
00477 CPL_ERROR_FILE_IO);
00478
00479 return CPL_ERROR_NONE;
00480 }
00481
00492 static cpl_error_code irplib_paf_dump_double( char *key, double value,
00493 const char *comment, FILE *paf )
00494 {
00495 cpl_ensure_code(paf, CPL_ERROR_NULL_INPUT);
00496 cpl_ensure_code(key, CPL_ERROR_NULL_INPUT);
00497
00498 if (comment == NULL)
00499 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%.10g\n",
00500 key, value) > PAF_KEY_LEN,
00501 CPL_ERROR_FILE_IO);
00502 else
00503 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%.10g ; # %s\n",
00504 key, value, comment) > PAF_KEY_LEN,
00505 CPL_ERROR_FILE_IO);
00506
00507 return CPL_ERROR_NONE;
00508 }
00509
00522 static cpl_error_code irplib_paf_dump_string( char *key, const char * value,
00523 const char *comment, FILE *paf )
00524 {
00525 cpl_ensure_code(paf, CPL_ERROR_NULL_INPUT);
00526 cpl_ensure_code(key, CPL_ERROR_NULL_INPUT);
00527 cpl_ensure_code(value, CPL_ERROR_NULL_INPUT);
00528
00529 if (comment == NULL)
00530 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "\"%s\"\n",
00531 key, value) > PAF_KEY_LEN,
00532 CPL_ERROR_FILE_IO);
00533 else
00534 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "\"%s\" ; # %s\n",
00535 key, value, comment) > PAF_KEY_LEN,
00536 CPL_ERROR_FILE_IO);
00537
00538 return CPL_ERROR_NONE;
00539 }
00540
00541