File: uves_qclog.c

package info (click to toggle)
cpl-plugin-uves 6.1.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 23,128 kB
  • sloc: ansic: 171,056; sh: 4,359; python: 3,002; makefile: 1,322
file content (1214 lines) | stat: -rw-r--r-- 38,700 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
/* $Id: uves_qclog.c,v 1.53 2012-11-19 09:12:54 jtaylor Exp $
 *
 * This file is part of the UVES Pipeline
 * Copyright (C) 2002,2003 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Author: jtaylor $
 * $Date: 2012-11-19 09:12:54 $
 * $Revision: 1.53 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <uves_time.h>
#include <uves_globals.h>
/*----------------------------------------------------------------------------*/
/**
 * @defgroup uves_qclog   Interface for reading/writing QC LOG keywords. 
 */
/*----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
                Defines
 -----------------------------------------------------------------------------*/
#define FILE_NAME_SZ 1024  /* fixme: should not use fixed size buffers */

/**@{*/

/*-----------------------------------------------------------------------------
                Includes
 -----------------------------------------------------------------------------*/

#include "uves_qclog.h"

#include <uves_baryvel.h>
#include <uves_utils_wrappers.h>
#include <uves_pfits.h>
#include <uves_error.h>
#include <uves_msg.h>

#include <irplib_utils.h>
#include <string.h>
/*-----------------------------------------------------------------------------
                Forward declarations
 -----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
                Function codes
 -----------------------------------------------------------------------------*/

static FILE * uves_paf_print_header(
        const char    *   filename,
        const char    *   paf_id,
        const char    *   paf_desc,
        const char    *   login_name,
        const char    *   datetime) ;

/*----------------------------------------------------------------------------*/
/**
  @brief    Open a new PAF file, output a default header.
  @param    filename    Name of the file to create.
  @param    paf_id        PAF identificator.
  @param    paf_desc    PAF description.
  @param    login_name  Login name
  @param    datetime    Date
  @return    Opened file pointer.

  This function creates a new PAF file with the requested file name.
  If another file already exists with the same name, it will be
  overwritten (if the file access rights allow it).

  A default header is produced according to the VLT DICB standard. You
  need to provide an identificator (paf_id) of the producer of the
  file. Typically, something like "ISAAC/zero_point".

  The PAF description (paf_desc) is meant for humans. Typically,
  something like "Zero point computation results".

  This function returns an opened file pointer, ready to receive more
  data through fprintf's. The caller is responsible for fclose()ing
  the file.
 */
/*----------------------------------------------------------------------------*/
static FILE * uves_paf_print_header(
    const char    *   filename,
    const char    *      paf_id,
    const char    *    paf_desc,
    const char    *   login_name,
    const char    *   datetime)
{
    FILE * paf ;
    
    if ((paf=fopen(filename, "w"))==NULL) {
        uves_msg_error("cannot create PAF file [%s]", filename);
        return NULL ;
    }
    fprintf(paf, "PAF.HDR.START         ; # start of header\n");
    fprintf(paf, "PAF.TYPE              \"pipeline product\" ;\n");
    fprintf(paf, "PAF.ID                \"%s\"\n", paf_id);
    fprintf(paf, "PAF.NAME              \"%s\"\n", filename);
    fprintf(paf, "PAF.DESC              \"%s\"\n", paf_desc);
    fprintf(paf, "PAF.CRTE.NAME         \"%s\"\n", login_name) ;
    fprintf(paf, "PAF.CRTE.DAYTIM       \"%s\"\n", datetime) ;
    fprintf(paf, "PAF.LCHG.NAME         \"%s\"\n", login_name) ;
    fprintf(paf, "PAF.LCHG.DAYTIM       \"%s\"\n", datetime) ;
    fprintf(paf, "PAF.CHCK.CHECKSUM     \"\"\n");
    fprintf(paf, "PAF.HDR.END           ; # end of header\n");
    fprintf(paf, "\n");
    return paf ;
}


/**
@brief computes rootname
@param filename name of file
@return rootname of filename
*/

char *
uves_get_rootname(const char * filename)
{
    static char path[MAX_NAME_SIZE+1];
    char * lastdot ;

    if (strlen(filename)>MAX_NAME_SIZE) return NULL ;
    memset(path, 0, MAX_NAME_SIZE);
    strcpy(path, filename);
    lastdot = strrchr(path, '.');
    if (lastdot == NULL) return path ;
    if ((!strcmp(lastdot, ".fits")) || (!strcmp(lastdot, ".FITS")) ||
        (!strcmp(lastdot, ".paf")) || (!strcmp(lastdot, ".PAF")) ||
        (!strcmp(lastdot, ".dat")) || (!strcmp(lastdot, ".DAT")) ||
        (!strcmp(lastdot, ".tfits")) || (!strcmp(lastdot, ".TFITS")) ||
        (!strcmp(lastdot, ".ascii")) || (!strcmp(lastdot, ".ASCII")))
    {
        lastdot[0] = (char)0;
    }
    return path ;
}
/**
@brief get paf file name
@param in      product name
@param paf_no  paf file number
@param paf     output paf file name
*/

void
uves_get_paf_name(const char* in, int paf_no, char** paf) 
{
    char* tmp=NULL;
    char  name_b[512] ;
    
    if (strstr(in, "." ) != NULL ) 
    {
        tmp = uves_get_rootname(in);
        strcpy(name_b, tmp);
    } 
    else
    {
        sprintf(name_b, "%s", in) ;
    }
    
    sprintf(*paf, "%s-%d.paf", name_b, paf_no);
    
    return;
}
/**
@brief replace a blank to a dot
@param in input string 
@param ou output string
*/
int uves_blank2dot(const char * in, char* ou) 
{
  int len=0;
  int i=0;

  strcpy(ou,in);
  len = strlen(in);
  for (i=0;i<len;i++)
    {
      if (in[i] == ' ') {
          ou[i] =  '.';
      }
    }
  return 0;
}


/*----------------------------------------------------------------------------*/


/**
  @brief    Init QC-LOG table
  @param filename        input filename. This is the filename of the
                         associated recipe product
  @param paf_no          paf file number, counting from zero. The paf
                         filename is created from the 'filename' and
             from this number. (This is to support several
             paf files for one recipe product).
  @param rec_id          input recipe id
  @param qclog           QC parameters table
  @param plist           property list
  @param rhead           reference frame header
  @param pro_catg        product category
  @return   a cpl_table with 0 rows and proper structure to do hold QC-LOG keys
 */
/*----------------------------------------------------------------------------*/
int uves_save_paf(const char* filename, 
          int paf_no,
                  const char* rec_id,  
                  const cpl_table* qclog,
                  uves_propertylist*   plist,
                  const uves_propertylist*   rhead,
                  const char*      pro_catg)
{


    FILE            *   paf ;
    const char      *   sval ;
    char            key_name[FILE_NAME_SZ] ;
    char            key_paf[FILE_NAME_SZ] ;
    char            key_dpaf[FILE_NAME_SZ] ;
    char            key_type[FILE_NAME_SZ] ;
    char            key_value[FILE_NAME_SZ] ;
    char            key_help[FILE_NAME_SZ] ;
    char* name_p=NULL;

    int i =0;
    int n=0;

    name_p = cpl_malloc(FILE_NAME_SZ * sizeof(char));
    uves_get_paf_name(filename, paf_no, &name_p);


    uves_msg( "Writing %s" , name_p) ;
    /* Create the default PAF header */
    if ((paf = uves_paf_print_header(
         name_p,
         rec_id,
         "QC file",
         "login-name",
         uves_get_datetime_iso8601())) == NULL) {
        uves_msg_error( "cannot open file [%s] for output", name_p) ;
        return -1 ;
    }
    cpl_free(name_p);


    strcpy(key_name,KEY_NAME_QC_DID);        
    strcpy(key_paf,PAF_NAME_QC_DID);
    uves_blank2dot(key_paf,key_dpaf);
    fprintf(paf,"%-21s \"%s\" ; # %s \n",key_dpaf,
        uves_propertylist_get_string(plist,key_name),KEY_HELP_QC_DID);


    uves_blank2dot(PAF_NAME_PIPE_ID,key_dpaf);
    fprintf(paf,"%-21s \"%s/%s\" ; # %s \n", key_dpaf,
                 PACKAGE, PACKAGE_VERSION,KEY_HELP_PIPE_ID);

    strcpy(key_name, KEY_NAME_PIPEFILE);        
    strcpy(key_paf, KEY_NAME_PIPEFILE);        
    uves_blank2dot(key_paf, key_dpaf);
    if (uves_propertylist_contains(plist, key_name)) {
       fprintf(paf,"%-21s \"%s\" ; # %s \n", key_dpaf,
       uves_propertylist_get_string(plist,key_name),KEY_HELP_PIPEFILE);
    }

    /* Value: "TEMPORARY", "PREPROCESSED", "REDUCED" or "QCPARAM". */
    strcpy(key_name,KEY_NAME_PRO_TYPE);
    strcpy(key_paf,PAF_NAME_PRO_TYPE);    
    uves_blank2dot(key_paf,key_dpaf);
    if (uves_propertylist_contains(plist, key_name)) {
       fprintf(paf,"%-21s \"%s\" ; # %s \n", key_dpaf,
       uves_propertylist_get_string(plist,key_name),KEY_HELP_PRO_TYPE);
    }

    strcpy(key_name,KEY_NAME_PRO_RECID);  
    strcpy(key_paf,PAF_NAME_PRO_RECID);  
    uves_blank2dot(key_paf,key_dpaf);        
    if (uves_propertylist_contains(plist, key_name)) {
      fprintf(paf,"%-21s \"%s\" ; # %s \n", key_dpaf,
          uves_propertylist_get_string(plist,key_name),KEY_HELP_PRO_RECID);
    }

    /* sprintf(cval, "CPL-%s", get_cpl_version()); */
    strcpy(key_name,KEY_NAME_PRO_DRSID); 
    strcpy(key_paf,PAF_NAME_PRO_DRSID); 
    uves_blank2dot(key_paf,key_dpaf);        
    if (uves_propertylist_contains(plist, key_name)) {
      fprintf(paf,"%-21s \"%s\" ; # %s \n",key_dpaf, 
          uves_propertylist_get_string(plist,key_name),KEY_HELP_PRO_DRSID);
    }
    /*
    if (uves_propertylist_contains(plist,KEY_NAME_DATE_OBS)) {
      sval = uves_pfits_get_date_obs(plist);
      strcpy(key_paf,KEY_NAME_DATE_OBS); 
      uves_blank2dot(key_paf,key_dpaf);        
      fprintf(paf, "%-21s \"%s\" ; # %s\n",key_dpaf, 
                sval,KEY_HELP_DATE_OBS) ;
    }
    */

      if (uves_propertylist_contains(rhead,KEY_NAME_ARCFILE)) {
      sval = uves_pfits_get_arcfile(rhead);
      strcpy(key_paf,KEY_NAME_ARCFILE);
      uves_blank2dot(key_paf,key_dpaf);      
      fprintf(paf, "%-21s \"%s\" ; # %s \n", key_dpaf,sval,KEY_HELP_ARCFILE) ;
    } else if (uves_propertylist_contains(rhead,KEY_NAME_PRO_REC1_RAW1_NAME)) {
      sval = uves_pfits_get_rec1raw1name(rhead);
      strcpy(key_paf,KEY_NAME_ARCFILE);
      uves_blank2dot(key_paf,key_dpaf);        
   } else {
      uves_msg_error("%s is missing QC LOG will fail!",KEY_NAME_ARCFILE);
    }


    if (uves_propertylist_contains(plist,KEY_NAME_TPL_ID)) {
      sval = uves_pfits_get_templateid(plist);
      strcpy(key_paf,PAF_NAME_TPL_ID);
      uves_blank2dot(key_paf,key_dpaf);       
      fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
          sval,KEY_HELP_TPL_ID) ;
 
    }
    
    if (uves_propertylist_contains(plist,KEY_NAME_DPR_TYPE)) {
      sval = uves_pfits_get_dpr_type(plist);
      strcpy(key_paf,PAF_NAME_DPR_TYPE); 
      uves_blank2dot(key_paf,key_dpaf);        
      fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
                sval, KEY_HELP_DPR_TYPE) ;
    }
    
    if (uves_propertylist_contains(plist,KEY_NAME_DPR_TECH)) {
      sval = uves_pfits_get_dpr_tech(plist);
      strcpy(key_paf,PAF_NAME_DPR_TECH); 
      uves_blank2dot(key_paf,key_dpaf);   
      fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
                sval, KEY_HELP_DPR_TECH) ;
    }
    
    if (uves_propertylist_contains(plist,KEY_NAME_DPR_CATG)) {
      sval = uves_pfits_get_dpr_catg(plist);
      strcpy(key_paf,PAF_NAME_DPR_CATG); 
      uves_blank2dot(key_paf,key_dpaf); 
      fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
                sval, KEY_HELP_DPR_CATG) ;
    }
    
    strcpy(key_paf,PAF_NAME_PRO_CATG); 
    uves_blank2dot(key_paf,key_dpaf); 
    fprintf(paf, "%-21s \"%s\" ; # %s\n", key_dpaf,
                pro_catg, KEY_HELP_PRO_CATG) ;

    n=cpl_table_get_nrow(qclog);
    for(i=0;i<n;i++) 
    {
        strcpy(key_paf,cpl_table_get_string(qclog,"key_name",i));
        uves_blank2dot(key_paf,key_name);
        strcpy(key_type,  cpl_table_get_string(qclog, "key_type",i));
        strcpy(key_value, cpl_table_get_string(qclog, "key_value",i));
        strcpy(key_help,  cpl_table_get_string(qclog, "key_help" , i));
        
        if (strcmp(key_type, "CPL_TYPE_STRING") == 0)
        {
            fprintf(paf,"%s               \"%s\"\n",
                key_name, key_value);
        }
        else
        {
            fprintf(paf,"%s               %s\n",
                key_name, key_value);
        }
    }
    fprintf(paf, "\n");
    fclose(paf) ;

    return 0;

}


/*----------------------------------------------------------------------------*/
/**
  @brief    Init QC-LOG table
  @param    raw_header     input FITS header
  @param    chip           CCD chip
  @return   a cpl_table with proper structure to do hold QC-LOG keys
            and QC parameters common to all recipes
 */
/*----------------------------------------------------------------------------*/
cpl_table *
uves_qclog_init(const uves_propertylist *raw_header,
        enum uves_chip chip)
{
  cpl_table *qclog = NULL;

  qclog = cpl_table_new(0);
  cpl_table_new_column(qclog,"key_name", CPL_TYPE_STRING);
  cpl_table_new_column(qclog,"key_type", CPL_TYPE_STRING);
  cpl_table_new_column(qclog,"key_value", CPL_TYPE_STRING);
  cpl_table_new_column(qclog,"key_help", CPL_TYPE_STRING);

  check_nomsg(uves_qclog_add_string(qclog,
                    uves_remove_string_prefix(KEY_NAME_QC_DID,"ESO "),
                    "UVES-1.14",
                    KEY_NAME_QC_DID,
                    "%s"));

  /* Not present in bias/dark frames: */
  if (uves_propertylist_contains(raw_header, UVES_INSPATH))
      {
      check_nomsg(uves_qclog_add_string(qclog,
                        uves_remove_string_prefix(UVES_INSPATH,"ESO "),
                        uves_pfits_get_inspath(raw_header),
                        "Optical path used.",
                        "%s"));
      }
  else
      {
      uves_msg_debug("Missing descriptor %s", UVES_INSPATH);
      }

  if (uves_propertylist_contains(raw_header, UVES_INSMODE))
      {
      check_nomsg(uves_qclog_add_string(qclog,
                        uves_remove_string_prefix(UVES_INSMODE,"ESO "),
                        uves_pfits_get_insmode(raw_header),
                        "Instrument mode used.",
                        "%s"));
      }
  else
      {
      uves_msg_debug("Missing descriptor %s", UVES_INSMODE);
      }

    
  if (uves_propertylist_contains(raw_header, UVES_GRATNAME(chip)))
      {
      check_nomsg(uves_qclog_add_string(qclog,
                        uves_remove_string_prefix(UVES_GRATNAME(chip),"ESO "),
                        uves_pfits_get_gratname(raw_header,chip),
                        "Grating common name",
                        "%s"));
      }
  else
      {
      uves_msg_debug("Missing descriptor %s", UVES_GRATNAME(chip));
      }
  
  /* Always present: */
  check_nomsg(uves_qclog_add_string(qclog,
                    uves_remove_string_prefix(UVES_READ_SPEED,"ESO "),
                    uves_pfits_get_readspeed(raw_header),
                    "Readout speed",
                    "%s"));
  
  check_nomsg(uves_qclog_add_int(qclog,
                 uves_remove_string_prefix(UVES_BINX, "ESO "),
                 uves_pfits_get_binx(raw_header),
                 "Binning factor along X",
                 "%d"));
  
  check_nomsg(uves_qclog_add_int(qclog,
                 uves_remove_string_prefix(UVES_BINY, "ESO "),
                 uves_pfits_get_biny(raw_header),
                 "Binning factor along Y",
                 "%d"));
  
  if (uves_propertylist_contains(raw_header, UVES_CHIP_NAME(chip)))
      {
      check_nomsg(uves_qclog_add_string(qclog,
                        uves_remove_string_prefix(UVES_CHIP_NAME(chip),"ESO "),
                        /* UVES_QC_CHIP_VAL(chip), */
                        uves_pfits_get_chip_name(raw_header, chip),
                        "Detector chip name",
                        "%s"));
      }
  else
      {
      uves_msg_warning("Missing descriptor %s", UVES_CHIP_NAME(chip));
      }
  
  cleanup:
  return qclog;
}



/*----------------------------------------------------------------------------*/
/**
  @brief    Add integer key to QC-LOG table
  @param    table       QC-LOG table
  @param    key_name    QC-LOG key name
  @param    value       QC-LOG key value
  @param    key_help    QC-LOG key help
  @param    format      QC-LOG key format

  @return   0 after successfull keyword addition
 */
/*----------------------------------------------------------------------------*/

int
uves_qclog_add_int(cpl_table* table,
                  const char*  key_name,  
                  const int    value,
                  const char*  key_help,
                  const char*  format)
{
  int sz = cpl_table_get_nrow(table);
  int raw = sz;
  char key_value[FILE_NAME_SZ];
  char key_type[FILE_NAME_SZ];

  sprintf(key_value,format,value);
  strcpy(key_type,"CPL_TYPE_INT"); 
 
  cpl_table_set_size(table,sz+1);

  cpl_table_set_string(table,"key_name" ,raw,key_name);
  cpl_table_set_string(table,"key_type" ,raw,key_type);
  cpl_table_set_string(table,"key_value",raw,key_value);
  cpl_table_set_string(table,"key_help" ,raw,key_help);

  return 0;

}



/**
  @brief    Add boolean key to QC-LOG table
  @param    table       QC-LOG table
  @param    key_name    QC-LOG key name
  @param    value       QC-LOG key value
  @param    key_help    QC-LOG key help
  @param    format      QC-LOG key format

  @return   0 after successfull keyword addition
 */
/*----------------------------------------------------------------------------*/

int
uves_qclog_add_bool(cpl_table* table,
                  const char*  key_name,  
                  const char   value,
                  const char*  key_help,
                  const char*  format)
{
  int sz = cpl_table_get_nrow(table);
  int raw = sz;
  char key_value[FILE_NAME_SZ];
  char key_type[FILE_NAME_SZ];

  sprintf(key_value,format,value);
  strcpy(key_type,"CPL_TYPE_BOOL"); 

  cpl_table_set_size(table,sz+1);

  cpl_table_set_string(table,"key_name" ,raw,key_name);
  cpl_table_set_string(table,"key_type" ,raw,key_type);
  cpl_table_set_string(table,"key_value",raw,key_value);
  cpl_table_set_string(table,"key_help" ,raw,key_help);

  return 0;

}

/**
  @brief    Add float key to QC-LOG table
  @param    table       QC-LOG table
  @param    key_name    QC-LOG key name
  @param    value       QC-LOG key value
  @param    key_help    QC-LOG key help
  @param    format      QC-LOG key format

  @return   0 after successfull keyword addition
 */
/*----------------------------------------------------------------------------*/


int
uves_qclog_add_float(cpl_table* table,
                  const char*  key_name,  
                  const float  value,
                  const char*  key_help,
                  const char*  format)
{
  int sz = cpl_table_get_nrow(table);
  int raw = sz;
  char key_value[FILE_NAME_SZ];
  char key_type[FILE_NAME_SZ];

  sprintf(key_value,format,value);
  strcpy(key_type,"CPL_TYPE_FLOAT"); 
 
  cpl_table_set_size(table,sz+1);

  cpl_table_set_string(table,"key_name" ,raw,key_name);
  cpl_table_set_string(table,"key_type" ,raw,key_type);
  cpl_table_set_string(table,"key_value",raw,key_value);
  cpl_table_set_string(table,"key_help" ,raw,key_help);

  return 0;

}



/**
  @brief    Add double key to QC-LOG table
  @param    table       QC-LOG table
  @param    key_name    QC-LOG key name
  @param    value       QC-LOG key value
  @param    key_help    QC-LOG key help
  @param    format      QC-LOG key format

  @return   0 after successfull keyword addition
 */
/*----------------------------------------------------------------------------*/


int
uves_qclog_add_double(cpl_table* table,
                  const char*  key_name,  
                  const double value,
                  const char*  key_help,
                  const char*  format)
{
  int sz = cpl_table_get_nrow(table);
  int raw = sz;
  char key_value[FILE_NAME_SZ];
  char key_type[FILE_NAME_SZ];

  sprintf(key_value,format,value);
  strcpy(key_type,"CPL_TYPE_DOUBLE"); 

  cpl_table_set_size(table,sz+1);

  cpl_table_set_string(table,"key_name" ,raw,key_name);
  cpl_table_set_string(table,"key_type" ,raw,key_type);
  cpl_table_set_string(table,"key_value",raw,key_value);
  cpl_table_set_string(table,"key_help" ,raw,key_help);

  return 0;

}


/**
  @brief    Add string key to QC-LOG table
  @param    table       QC-LOG table
  @param    key_name    QC-LOG key name
  @param    value       QC-LOG key value
  @param    key_help    QC-LOG key help
  @param    format      QC-LOG key format

  @return   0 after successfull keyword addition
 */
/*----------------------------------------------------------------------------*/




int
uves_qclog_add_string(cpl_table* table,
                  const char*  key_name,  
                  const char*  value,
                  const char*  key_help,
                  const char*  format)
{
  int sz = cpl_table_get_nrow(table);
  int raw = sz;
  char key_value[FILE_NAME_SZ];
  char key_type[FILE_NAME_SZ];

  sprintf(key_value,format,value);
  strcpy(key_type,"CPL_TYPE_STRING"); 
 
  cpl_table_set_size(table,sz+1);

  cpl_table_set_string(table,"key_name" ,raw,key_name);
  cpl_table_set_string(table,"key_type" ,raw,key_type);
  cpl_table_set_string(table,"key_value",raw,key_value);
  cpl_table_set_string(table,"key_help" ,raw,key_help);

  return 0;

}

/**
  @brief    delete QC-LOG table
  @param    table       QC-LOG table
  @return   0 after successfull delete
 */
/*----------------------------------------------------------------------------*/

int
uves_qclog_delete(cpl_table** table)
{
  if (table != NULL) { 
    cpl_table_delete(*table);
    *table = NULL;
  }
  return 0;
}


/**
  @brief    Add QC-LOG to FITS header
  @param    plist       propertylist (FITS header)
  @param    qclog       QC-LOG table
  @return   0 after successfull keywords addition, -1 if not
 */
/*----------------------------------------------------------------------------*/


int uves_pfits_put_qc(
        uves_propertylist       *   plist,
        const cpl_table          *   qclog)
{
  char            key_name[FILE_NAME_SZ];
  char            key_value[FILE_NAME_SZ];
  char            key_type[FILE_NAME_SZ];
  char            key_help[FILE_NAME_SZ] ;

  int             i =0;
  int n =0;
  /* Test entries */
  if (plist == NULL) {
    uves_msg_error("plist=NULL, something strange");
    return -1 ;
  }
  /* Parameter Name:    PIPEFILE */

  n=cpl_table_get_nrow(qclog);
  for(i=0;i<n;i++) {
    strcpy(key_name,"ESO ");
    strcat(key_name,cpl_table_get_string(qclog,"key_name",i));
    strcpy(key_type,cpl_table_get_string(qclog,"key_type",i));
    strcpy(key_value,cpl_table_get_string(qclog,"key_value",i));
    strcpy(key_help,cpl_table_get_string(qclog,"key_help",i));

    /* uves_msg("name=%s type=%s value=%s\n",key_name,key_type,key_value); */
    if(!uves_propertylist_contains(plist,key_name)) {
      if(strcmp(key_type,"CPL_TYPE_STRING") == 0) {
    uves_propertylist_append_string(plist, key_name,key_value) ;
    uves_propertylist_set_comment(plist, key_name,key_help) ;
      } else if(strcmp(key_type,"CPL_TYPE_BOOL") == 0) {
      /* printf("key_value=%s\n",key_value); */
    uves_propertylist_append_bool(plist, key_name,atoi(key_value)) ;
    uves_propertylist_set_comment(plist, key_name,key_help) ;
      } else if(strcmp(key_type,"CPL_TYPE_INT") == 0) {
    uves_propertylist_append_int(plist,key_name,atoi(key_value)) ;
    uves_propertylist_set_comment(plist, key_name,key_help) ;
      } else if(strcmp(key_type,"CPL_TYPE_FLOAT") == 0) {
        uves_propertylist_append_float(plist, key_name,(float)atof(key_value)) ;
        uves_propertylist_set_comment(plist, key_name,key_help) ;
      } else if(strcmp(key_type,"CPL_TYPE_DOUBLE") == 0) {
        uves_propertylist_append_double(plist, key_name,atof(key_value)) ;
        uves_propertylist_set_comment(plist, key_name,key_help) ;
      }
      else
      {
          uves_msg_error("Unrecognized type: %s", key_type);
          return -1;
      }
    }

  }

  return 0 ;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Write QC parameters related to science reduction
   @param    qclog         QC table to write to
   @param    raw_header    input frame FITS header
   @param    raw_image     input science image
   @param    slit          extraction slit length (pixels)
   @param    info_tbl      containing the previously computed parameters
*/
/*----------------------------------------------------------------------------*/
void
uves_qclog_add_sci(cpl_table *qclog,
           const uves_propertylist *raw_header,
           const cpl_image *raw_image,
           double slit,
           const cpl_table *info_tbl)
{
    char key_name[80];
    
    /* These QC parameters are computed only in optimal extraction.
       Update: After request from DFO, these are also calculated for
       average+linear extraction.
    */

    if (info_tbl != NULL) {
    int minorder = cpl_table_get_int(info_tbl, "Order", 0, NULL);
    int maxorder = minorder;
    int norder;          /* Number of orders extracted */
    int i;

    for(i = 0; i < cpl_table_get_nrow(info_tbl); i++) {
        int order = cpl_table_get_int(info_tbl,"Order", i, NULL);

        minorder = uves_min_int(minorder, order);
        maxorder = uves_max_int(maxorder, order);

        /*
          sprintf(key_name, "QC ORDER NUM%d", i);
          check_nomsg(uves_qclog_add_int(qclog,
          key_name,
          cpl_table_get_int(info_tbl,"Order",i, NULL),
          "Order Number",
          "%d"));
        */
        
        uves_msg_debug("QC-LOG: Order = %d, S/N = %g, Pos = %g, FHWM = %g, RI = %g", 
               order,
               cpl_table_get_double(info_tbl, "ObjSnBlzCentre"  ,i ,NULL),
               cpl_table_get_double(info_tbl, "ObjPosOnSlit"  ,i ,NULL),
               cpl_table_get_double(info_tbl, "ObjFwhmAvg" ,i ,NULL),
                           cpl_table_get_double(info_tbl, "Ripple" ,i ,NULL));
        
        
        sprintf(key_name, "QC ORD%d OBJ SN", order);
        check_nomsg(uves_qclog_add_double(qclog,
                          key_name,
                          cpl_table_get_double(info_tbl,"ObjSnBlzCentre",i, NULL),
                          "Av. S/N at order center",
                          "%8.4f"));
        
        sprintf(key_name, "QC ORD%d OBJ POS", order);
        check_nomsg(uves_qclog_add_double(qclog,
                          key_name,
                          cpl_table_get_double(info_tbl,"ObjPosOnSlit",i, NULL),
                          "Av. OBJ POS at order center",
                          "%8.4f"));
        
        sprintf(key_name, "QC ORD%d OBJ FWHM", order);
        check_nomsg(uves_qclog_add_double(qclog,
                          key_name,
                          cpl_table_get_double(info_tbl,"ObjFwhmAvg",i, NULL),
                          "Av. FWHM on order",
                          "%8.4f"));

        sprintf(key_name, "QC ORD%d OBJ RPLPAR", order);
        check_nomsg(uves_qclog_add_double(qclog,
                          key_name,
                          cpl_table_get_double(info_tbl,"Ripple",i, NULL),
                          "Av. relative ripple amplitude",
                          "%8.4f"));
    } /* for i */

       /* !WARNING!: Duplicate code follows. If changed, please
        * synchronize with the place where ORDER_TRACE_xxxx
        * is saved.
        *
        * These parameters used to (MIDAS),
        * be added only to ORDER_TRACE_xxxx.
        * Now add them to the same products as other
        * QC parameters
        */

    norder = maxorder - minorder + 1;
    check_nomsg(uves_qclog_add_int(qclog,
                       "QC EX NORD",
                       norder,
                       "No. of orders extracted",
                       "%d"));
    
    check_nomsg(uves_qclog_add_int(qclog,
                       "QC EX XSIZE",
                       cpl_image_get_size_x(raw_image),
                       "Input image width (pixels)",
                       "%d"));
    
    check_nomsg(uves_qclog_add_int(qclog,
                       "QC EX YSIZE",
                       uves_round_double(slit),
                       "Extraction slit (pixels)",
                       "%d"));
    } /* if info_tbl != NULL */
    
    {
    double barycor, helicor;

    check( uves_baryvel(raw_header, 
                &barycor,
                &helicor),
           "Could not compute velocity corrections");
    
    check_nomsg(uves_qclog_add_double(qclog,
                      "QC VRAD BARYCOR",
                      barycor,
                      "Barycentric radial velocity correction ",
                      "%13.6f"));
    
    check_nomsg(uves_qclog_add_double(qclog,
                      "QC VRAD HELICOR",
                      helicor,
                      "Heliocentric radial velocity correction ",
                      "%13.6f"));
    }
           
  cleanup:
  return;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Write common QC parameters
   @param    raw_header    raw frame FITS header
   @param    chip          CCD chip
   @param    qclog         QC table to write to

   This function writes some QC parameters which are common
   for several recipes/QC-tests.
*/
/*----------------------------------------------------------------------------*/

void 
uves_qclog_add_common_wave(const uves_propertylist *raw_header,
               enum uves_chip chip,
               cpl_table *qclog)
{

    check_nomsg(uves_qclog_add_double(qclog,
                                      uves_remove_string_prefix(UVES_SLITWIDTH(chip),"ESO "),
                                      uves_pfits_get_slitwidth(raw_header,chip),
                                      "Slit width (arcsec) [arcsec] (hs).",
                                      "%.1f"));

    check_nomsg(uves_qclog_add_double(qclog,
                      uves_remove_string_prefix(UVES_GRATWLEN(chip),"ESO "),
                      uves_pfits_get_gratwlen(raw_header,chip),
                      "Grating central wavelength [nm] (hs).",
                      "%.1f"));
    



    check_nomsg(uves_qclog_add_double(qclog,
                      uves_remove_string_prefix(UVES_TEMPCAM(chip),"ESO "),
                      uves_pfits_get_tempcam(raw_header,chip),
                      "Average temperature [C] (ho).",
                      "%.1f"));



  cleanup:
    return;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Create QC parameter name
   @param    name          last part of QC name
   @param    flames        FLAMES?
   @param    trace_number  for FLAMES only
   @return   newly allocated QC name string
*/
/*----------------------------------------------------------------------------*/

const char *
uves_qclog_get_qc_name(const char *name,
               bool flames, int trace_number)
{
    if (flames)
    {
        return uves_sprintf("QC FIB%d %s", trace_number + 1, name);
    }
    else
    {
        return uves_sprintf("QC %s", name);
    }
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Create common QC log 
   @param    name          last part of QC name
   @param    flames        FLAMES?
   @param    trace_number  for FLAMES only
   @return   newly allocated QC name string
*/
/*----------------------------------------------------------------------------*/

int
uves_qclog_dump_common(const uves_propertylist *plist,
               enum uves_chip chip, 
                       cpl_table* qclog)

{


  int binx=0;
  int biny=0;
  const char* read_speed=NULL;
  const char* dpr_type=NULL;
  const char* tpl_id=NULL;
  const char* arcfile=NULL;
  const char* pro_catg=NULL;
  const char* pipefile=NULL;
  const char* ins_path=NULL;
  const char* ins_mode=NULL;
  const char* name_cross=NULL;
  const char* name_ccd=NULL;
  

  check_nomsg(binx=uves_pfits_get_binx(plist));
  check_nomsg(biny=uves_pfits_get_biny(plist));
  check_nomsg(read_speed=uves_pfits_get_readspeed(plist));
  check_nomsg(dpr_type=uves_pfits_get_dpr_catg(plist));
  check_nomsg(tpl_id=uves_pfits_get_templateid(plist));    
  check_nomsg(arcfile=uves_pfits_get_arcfile(plist));
  check_nomsg(pro_catg=uves_pfits_get_pro_catg(plist));
  check_nomsg(pipefile=uves_pfits_get_pipefile(plist)); 
  check_nomsg(ins_path=uves_pfits_get_inspath(plist)); 
  check_nomsg(ins_mode=uves_pfits_get_insmode(plist)); 
  check_nomsg(name_cross=uves_pfits_get_gratname(plist,chip)); 
  check_nomsg(name_ccd=uves_pfits_get_chipid(plist,chip)); 

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "PRO REC1 PIPE ID",
                  PACKAGE_VERSION,
                  "Pipeline (unique) identifier",
                  "%s"));

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "DPR TYPE",
                  dpr_type,
                  "Data Processing Type",
                  "%s"));

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "TPL ID",
                  tpl_id,
                  "Template Id",
                  "%s"));

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "ARCFILE",
                  arcfile,
                  "Archive file name",
                  "%s"));

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "PRO CATG",
                  pro_catg,
                  "Product Category",
                  "%s"));


  ck0_nomsg(uves_qclog_add_string(qclog,
                  "PIPEFILE",
                  pipefile,
                  "Pipeline file name",
                  "%s"));


  ck0_nomsg(uves_qclog_add_string(qclog,
                  "INS PATH",
                  ins_path,
                  "Instrument Path",
                  "%s"));


  ck0_nomsg(uves_qclog_add_string(qclog,
                  "INS MODE",
                  ins_mode,
                  "Instrument mode",
                  "%s"));

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "NAME CROSS",
                  name_cross,
                  "Grating common name",
                  "%s"));

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "NAME CCD",
                  name_ccd,
                  "Detector chip name",
                  "%s"));


  /*
  qc1log/out 1 {p2} "PRO.REC1.PIPE.ID" "{p_version}" "Pipeline (unique) identifier" PRO
  qc1log/out 1 {p1} {h_dprtype} "{dpr_type}" + DPR
  qc1log/out 1 {p2} {h_tpid} "{tpl_id}" + TPL
  qc1log/out 1 {p2} {h_arcfile} {arcfile} + PRIMARY-FITS
  qc1log/out 1 {p2} {h_procatg} {pro_catg} + PRO
  qc1log/out 1 {p2} {h_pipefile} {p2} + PRO
  set/format i1 f8.4,f8.4
   qc1log/out 1 {p2} {h_inspath} {{p1},{h_inspath}}                 "Optical path used."         UVES_ICS
   qc1log/out 1 {p2} {h_insmode} {{p1},{h_insmode}}                 "Instrument mode used."      UVES_ICS  
qc1log/out 1 {p2} "{h_namecros({PATHID})(5:18)}" {{p1},{h_namecros({PATHID})}}         "Grating common name"            UVES_ICS
nameccd =  "{{p1},{h_fits_nameccd({PATHID})}}"
qc1log/out 1 {p2} "{h_fits_nameccd({PATHID})(5:)}" "{nameccd}" "Detector chip name"              CCDDCS
qc1log/out 1 {p2} "{h_speed}"   {speed} "Readout speed"     CCDDCS
qc1log/out 1 {p2} "{h_xwinbin}" {binx}  "Binning factor along X"  CCDDCS
qc1log/out 1 {p2} "{h_ywinbin}" {biny}  "Binning factor along Y"  CCDDCS
set/format
  */

  ck0_nomsg(uves_qclog_add_string(qclog,
                  "DET READ SPEED",
                  read_speed,
                  "Readout speed",
                  "%8.4f"));


  ck0_nomsg(uves_qclog_add_double(qclog,
                  "DET BINX",
                  binx,
                  "Binning factor along X",
                  "%8.4f"));


  ck0_nomsg(uves_qclog_add_double(qclog,
                  "DET BINY",
                  biny,
                  "Binning factor along Y",
                  "%8.4f"));



  return 0;
 cleanup:
  return -1;

}


/*----------------------------------------------------------------------------*/
/**
   @brief    Create common QC log 
   @param    name          last part of QC name
   @param    flames        FLAMES?
   @param    trace_number  for FLAMES only
   @return   newly allocated QC name string
*/
/*----------------------------------------------------------------------------*/

int
uves_qclog_dump_common_wave(const uves_propertylist *plist,
                    enum uves_chip chip, 
                            cpl_table* qclog)
  {
    double slit_width=0;
    double temp_cam=0;
    double wave_len=0;
    check_nomsg(wave_len=uves_pfits_get_gratwlen(plist,chip));
    check_nomsg(temp_cam=uves_pfits_get_tempcam(plist,chip));
    check_nomsg(slit_width=uves_pfits_get_slitwidth(plist,chip));


 ck0_nomsg(uves_qclog_add_double(qclog,
                  "DET SLIT WIDTH",
                  slit_width,
                  "Slit width (arcsec) [arcsec] (hs).",
                  "%8.4f"));


 ck0_nomsg(uves_qclog_add_double(qclog,
                  "DET WCENT",
                  wave_len,
                  "Grating central wavelength [nm] (hs).",
                  "%8.4f"));


 ck0_nomsg(uves_qclog_add_double(qclog,
                  "TEMP CAM",
                  temp_cam,
                  "Average temperature [c] (ho).",
                  "%8.4f"));


    /*
    set/format i1 f8.4,f8.4
   qc1log/out 1 {p2} "{h_slwidth({PATHID})}"         {swid}     "Slit width (arcsec) [arcsec] (hs)."    UVES_ICS
qc1log/out 1 {p2} "{h_cwlen({PATHID})(5:)}"       {wlen}     "Grating central wavelength [nm] (hs)." UVES_ICS
qc1log/out 1 {p2} "{h_tempcam({PATHID})(5:)}"      {temp}    "Average temperature [c] (ho)."         UVES_ICS
set/format

    */
    return 0;

  cleanup:
    return -1;

}

/**@}*/