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[80];
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[80] ;
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 }
00259
00260
00261
00262
00263 error = CPL_ERROR_NONE;
00264
00265 if (!error) {
00266 nlen = fprintf(paf, "PAF.HDR.START ;# start of header\n");
00267 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00268 }
00269
00270 if (!error) {
00271 nlen = fprintf(paf, "PAF.TYPE \"pipeline product\" ;\n");
00272 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00273 }
00274
00275 if (!error) {
00276 nlen = fprintf(paf, "PAF.ID \"%s\"\n", paf_id);
00277 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00278 }
00279
00280 if (!error) {
00281 nlen = fprintf(paf, "PAF.NAME \"%s\"\n", filename);
00282 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00283 }
00284
00285 if (!error) {
00286 nlen = fprintf(paf, "PAF.DESC \"%s\"\n", paf_desc);
00287 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00288 }
00289
00290 if (!error) {
00291 nlen = fprintf(paf, "PAF.CHCK.CHECKSUM \"\"\n");
00292 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00293 }
00294
00295 if (!error) {
00296 nlen = fprintf(paf, "PAF.HDR.END ;# end of header\n");
00297 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00298 }
00299
00300
00301 if(cpl_propertylist_has(primary_header,"ARCFILE")) {
00302 key_value=cpl_propertylist_get_string(primary_header,"ARCFILE");
00303 } else {
00304 key_value="ARCFILE_NOT_FOUND";
00305 }
00306 if (!error) {
00307 nlen = fprintf(paf, "ARCFILE \"%s\";# archive file name\n",key_value);
00308 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00309 }
00310
00311 if(cpl_propertylist_has(primary_header,"PIPEFILE")) {
00312 key_value=cpl_propertylist_get_string(primary_header,"PIPEFILE");
00313 } else {
00314 key_value="PIPEFILE_NOT_FOUND";
00315 }
00316 if (!error) {
00317 nlen = fprintf(paf, "PIPEFILE \"%s\";# File name of data product\n",key_value);
00318 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00319 }
00320
00321
00322 if(cpl_propertylist_has(primary_header,"PRO.REC1.PIPE.ID")) {
00323 key_value=cpl_propertylist_get_string(primary_header,"PRO.REC1.PIPE.ID");
00324 } else {
00325 key_value="PRO_REC1_PIPE_ID_NOT_FOUND";
00326 }
00327 if (!error) {
00328 nlen = fprintf(paf, "PRO.REC1.PIPE.ID \"%s\";# Pipeline (unique) identifier\n",key_value);
00329 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00330 }
00331
00332
00333
00334
00335 if (!error) {
00336 nlen = fprintf(paf, "PRO.TYPE \"REDUCED\";# Product type\n");
00337 if (nlen <= PAF_KEY_LEN) error = CPL_ERROR_FILE_IO;
00338 }
00339
00340
00341
00342 if (!error) {
00343 nlen = fprintf(paf, "\n");
00344 if (nlen != 1) error = CPL_ERROR_FILE_IO;
00345 }
00346
00347 cpl_free(paf_id);
00348
00349 if (error) {
00350 (void)fclose(paf);
00351 cpl_msg_error(cpl_func, "Could not write PAF: %s", filename);
00352 cpl_ensure(0, error, NULL);
00353 }
00354 fflush( paf ) ;
00355
00356 return paf;
00357
00358 }
00359
00360 static void no_space( char * str )
00361 {
00362 for( ; *str != '\0' ; str++ )
00363 if ( *str == ' ' ) *str = '.' ;
00364 }
00365
00395 static cpl_error_code irplib_paf_dump( qc_description *pqc, const char *kwname,
00396 const cpl_propertylist * paflist,
00397 FILE *paf )
00398 {
00399 cpl_error_code err ;
00400 char * keydot = NULL, *pk ;
00401
00402 keydot = cpl_strdup( kwname ) ;
00403 pk = keydot ;
00404
00405 no_space( keydot ) ;
00406 if ( strncmp( keydot, "ESO.", 4 ) == 0 ) pk += 4 ;
00407 xsh_msg_dbg_low( "irplib_dump: '%s'", pk ) ;
00408
00409 switch( cpl_propertylist_get_type( paflist, kwname ) ) {
00410 case CPL_TYPE_CHAR:
00411 err = irplib_paf_dump_int(pk,
00412 cpl_propertylist_get_char( paflist, kwname),
00413 pqc->kw_help, paf);
00414 break ;
00415 case CPL_TYPE_INT:
00416 err = irplib_paf_dump_int(pk,
00417 cpl_propertylist_get_int( paflist, kwname),
00418 pqc->kw_help, paf);
00419 break;
00420 case CPL_TYPE_LONG:
00421 if (sizeof(long) == sizeof(int))
00422 err = irplib_paf_dump_int(pk,
00423 cpl_propertylist_get_long( paflist,
00424 kwname),
00425 pqc->kw_help, paf);
00426 break;
00427 case CPL_TYPE_FLOAT:
00428 err = irplib_paf_dump_double(pk,
00429 cpl_propertylist_get_float( paflist,
00430 kwname),
00431 pqc->kw_help, paf);
00432 break;
00433 case CPL_TYPE_DOUBLE:
00434 err = irplib_paf_dump_double(pk,
00435 cpl_propertylist_get_double( paflist,
00436 kwname),
00437 pqc->kw_help, paf);
00438 break;
00439 case CPL_TYPE_STRING:
00440 err = irplib_paf_dump_string(pk,
00441 cpl_propertylist_get_string( paflist,
00442 kwname),
00443 pqc->kw_help, paf);
00444 break;
00445 default:
00446 err = CPL_ERROR_UNSUPPORTED_MODE;
00447 }
00448 cpl_free(keydot);
00449 return err ;
00450 }
00451
00462 static cpl_error_code irplib_paf_dump_int( char *key, int value,
00463 const char *comment, FILE *paf )
00464 {
00465 cpl_ensure_code(paf, CPL_ERROR_NULL_INPUT);
00466 cpl_ensure_code(key, CPL_ERROR_NULL_INPUT);
00467
00468 if (comment == NULL)
00469 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%d\n",
00470 key, value) > PAF_KEY_LEN,
00471 CPL_ERROR_FILE_IO);
00472 else
00473 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%d ; # %s\n",
00474 key, value, comment) > PAF_KEY_LEN,
00475 CPL_ERROR_FILE_IO);
00476
00477 return CPL_ERROR_NONE;
00478 }
00479
00490 static cpl_error_code irplib_paf_dump_double( char *key, double value,
00491 const char *comment, FILE *paf )
00492 {
00493 cpl_ensure_code(paf, CPL_ERROR_NULL_INPUT);
00494 cpl_ensure_code(key, CPL_ERROR_NULL_INPUT);
00495
00496 if (comment == NULL)
00497 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%.10g\n",
00498 key, value) > PAF_KEY_LEN,
00499 CPL_ERROR_FILE_IO);
00500 else
00501 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "%.10g ; # %s\n",
00502 key, value, comment) > PAF_KEY_LEN,
00503 CPL_ERROR_FILE_IO);
00504
00505 return CPL_ERROR_NONE;
00506 }
00507
00520 static cpl_error_code irplib_paf_dump_string( char *key, const char * value,
00521 const char *comment, FILE *paf )
00522 {
00523 cpl_ensure_code(paf, CPL_ERROR_NULL_INPUT);
00524 cpl_ensure_code(key, CPL_ERROR_NULL_INPUT);
00525 cpl_ensure_code(value, CPL_ERROR_NULL_INPUT);
00526
00527 if (comment == NULL)
00528 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "\"%s\"\n",
00529 key, value) > PAF_KEY_LEN,
00530 CPL_ERROR_FILE_IO);
00531 else
00532 cpl_ensure_code(fprintf(paf, PAF_KEY_FORMAT "\"%s\" ; # %s\n",
00533 key, value, comment) > PAF_KEY_LEN,
00534 CPL_ERROR_FILE_IO);
00535
00536 return CPL_ERROR_NONE;
00537 }
00538
00539