File: fors_qc.c

package info (click to toggle)
cpl-plugin-vimos 2.9.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,560 kB
  • ctags: 6,341
  • sloc: ansic: 148,777; sh: 11,457; cpp: 724; makefile: 606; python: 287; perl: 10
file content (701 lines) | stat: -rw-r--r-- 20,092 bytes parent folder | download
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
/* $Id: fors_qc.c,v 1.1 2010-06-16 11:07:49 cizzo Exp $
 *
 * This file is part of the FORS Library
 * Copyright (C) 2002-2006 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

/*
 * $Author: cizzo $
 * $Date: 2010-06-16 11:07:49 $
 * $Revision: 1.1 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

#include <cpl.h>
#include <fors_paf.h>
#include <fors_dfs.h>
#include <fors_qc.h>

#define DICT_LINE_LENGTH    (80)
#define MAX_PAF_NAME_LENGTH (80)
#define PAF_ROOT_NAME       "qc"

/**
 * @defgroup forsqc Quality Control Utilities
 *
 * The module collects utility functions for quality control operations.
 */

/**@{*/
const char *const fors_qc_dic_version = "2.0";

static ForsPAF *pafFile = NULL;
static int     pafIndex = 0;


/**
 * @brief
 *   Initiate a new QC1 group.
 *
 * @param header           product header
 * @param qcdic_version    dictionary ID
 * @param instrument       instrument name
 * @return @c CPL_ERROR_NONE on success
 *
 * A new PAF object is initiated, with the name depending on the root
 * PAF_ROOT_NAME and the current pafIndex. If the previous QC1 PAF file
 * is found open, this is an error: fors_qc_end_group() should be called
 * first.
 */

cpl_error_code fors_qc_start_group(cpl_propertylist *header,
                                   const char *qcdic_version, 
                                   const char *instrument)
{
    char pafName[MAX_PAF_NAME_LENGTH];

    if (pafFile)
        return cpl_error_set("fors_qc_start_group", CPL_ERROR_FILE_ALREADY_OPEN);

    sprintf(pafName, "%s%.4d.paf", PAF_ROOT_NAME, pafIndex);

    if (!(pafFile = newForsPAF(pafName, "QC1 parameters", NULL, NULL)))
        return cpl_error_set("fors_qc_start_group", CPL_ERROR_FILE_NOT_CREATED);
    
    fors_qc_write_qc_string(header, 
                            "QC.DID", qcdic_version, "QC1 dictionary",
                            instrument);

    return CPL_ERROR_NONE;

}


/**
 * @brief
 *   Close current QC1 PAF file.
 *
 * @return @c CPL_ERROR_NONE on success
 *
 * The current QC1 PAF object is written to disk file. If no PAF object
 * is present, this is an error: @c fors_qc_start_group() should be called
 * first. If the PAF file is empty, the PAF object is destroyed, but
 * no PAF file is created.
 */

cpl_error_code fors_qc_end_group(void)
{

    if (!pafFile)
        return cpl_error_set("fors_qc_end_group", CPL_ERROR_DATA_NOT_FOUND);

    if (!forsPAFIsEmpty(pafFile)) {
        forsPAFWrite(pafFile);
        pafIndex++;
    }

    deleteForsPAF(pafFile);
    pafFile = NULL;

    return CPL_ERROR_NONE;

}


/**
 * @brief
 *   Add string parameter to current QC1 group.
 *
 * @param name       Parameter name
 * @param value      Parameter value
 * @param comment    Parameter comment
 * @param instrument Instrument name
 *
 * @return @c CPL_ERROR_NONE on success
 *
 * To the current QC1 PAF object is appended a string parameter. If no
 * PAF object is present, this is an error: fors_qc_start_group() should be
 * called first.
 */

cpl_error_code fors_qc_write_string(const char *name, const char *value, 
                                const char *comment, const char *instrument)
{

    int status;
    int   length = strlen(instrument) + 3;
    char *allComment;


    if (comment == NULL || name == NULL || instrument == NULL)
        return cpl_error_set("fors_qc_write_string", CPL_ERROR_NULL_INPUT);

    length += strlen(comment) + 1;

    allComment = cpl_malloc(length * sizeof(char));

    sprintf(allComment, "%s [%s]", comment, instrument);

    status = forsPAFAppendString(pafFile, name, value, allComment);

    cpl_free(allComment);

    if (status)
        cpl_msg_error("fors_qc_write_string", 
                      "Cannot write parameter %s to QC1 PAF", name);

    cpl_msg_debug(cpl_func, "%s [%s] = '%s'", comment, name, value);

    return CPL_ERROR_NONE;

}

cpl_error_code fors_qc_write_string_chat(const char *name, const char *value,
                                const char *comment, const char *instrument)
{

    int status;
    int   length = strlen(instrument) + 3;
    char *allComment;


    if (comment == NULL || name == NULL || instrument == NULL)
        return cpl_error_set("fors_qc_write_string_chat", CPL_ERROR_NULL_INPUT);

    length += strlen(comment) + 1;

    allComment = cpl_malloc(length * sizeof(char));

    sprintf(allComment, "%s [%s]", comment, instrument);

    status = forsPAFAppendString(pafFile, name, value, allComment);

    cpl_free(allComment);

    if (status)
        cpl_msg_error("fors_qc_write_string_chat",
                      "Cannot write parameter %s to QC1 PAF", name);

    cpl_msg_info(cpl_func, "%s [%s] = '%s'", comment, name, value);

    return CPL_ERROR_NONE;

}


/**
 * @brief
 *   Add double parameter to current QC1 group.
 *
 * @param name       Parameter name
 * @param value      Parameter value
 * @param unit       Parameter unit
 * @param comment    Parameter comment;
 * @param instrument Instrument name
 *
 * @return @c CPL_ERROR_NONE on success
 *
 * To the current QC1 PAF object is appended a double parameter.
 * The comment string is mandatory. The parameter unit must be
 * specified, unless the specified parameter is adimensional, otherwise
 * a null pointer should be passed. To the comment string the unit
 * string (if present) will be appended, enclosed in round brackets,
 * and then the string "[@em instrument]". If no PAF object is present, 
 * this is an error: fors_qc_start_group() should be called first.
 */

cpl_error_code fors_qc_write_double(const char *name, double value, 
                                const char *unit, const char *comment,
                                const char *instrument)
{

    cpl_error_code status;
    int   length = strlen(instrument) + 3;
    char *allComment;


    if (comment == NULL || name == NULL || instrument == NULL)
        return cpl_error_set("fors_qc_write_double", CPL_ERROR_NULL_INPUT);

    length += strlen(comment) + 1;

    if (unit)
      length += strlen(unit) + 3;

    allComment = cpl_malloc(length * sizeof(char));

    if (unit)
      sprintf(allComment, "%s (%s) [%s]", comment, unit, instrument);
    else
      sprintf(allComment, "%s [%s]", comment, instrument);

    status = forsPAFAppendDouble(pafFile, name, value, allComment);

    cpl_free(allComment);

    if (status)
        cpl_msg_error("fors_qc_write_double", 
                      "Cannot write parameter %s to QC1 PAF", name);

    cpl_msg_info(cpl_func, "%s [%s] = %f %s", 
                 comment, name, value, (unit != NULL) ? unit : "");

    return CPL_ERROR_NONE;

}


cpl_error_code fors_qc_write_int(const char *name, int value, const char *unit, 
                             const char *comment, const char *instrument)
{

    cpl_error_code status;
    int   length = strlen(instrument) + 3;
    char *allComment;


    if (comment == NULL || name == NULL || instrument == NULL)
        return cpl_error_set("fors_qc_write_int", CPL_ERROR_NULL_INPUT);

    length += strlen(comment) + 1;

    if (unit)
      length += strlen(unit) + 3;

    allComment = cpl_malloc(length * sizeof(char));

    if (unit)
      sprintf(allComment, "%s (%s) [%s]", comment, unit, instrument);
    else
      sprintf(allComment, "%s [%s]", comment, instrument);

    status = forsPAFAppendInt(pafFile, name, value, allComment);

    cpl_free(allComment);

    if (status)
        cpl_msg_error("fors_qc_write_int", 
                      "Cannot write parameter %s to QC1 PAF", name);

    cpl_msg_info(cpl_func, "%s [%s] = %d %s", 
                 comment, name, value, (unit != NULL) ? unit : "");
 
    return CPL_ERROR_NONE;

}


/**
 * @brief
 *   Copy a keyword value to the currently active QC1 PAF object.
 *
 * @param header     Pointer to a keyword header.
 * @param name       Keyword name.
 * @param unit       Optional unit to be associated to keyword value.
 * @param comment    Optional comment to be associated to keyword value.
 * @param instrument Instrument name
 *
 * @return @c CPL_ERROR_NONE on success
 *
 *   A keyword with the specified name is searched in the @em header. 
 *   Its type is determined, then its value is read with the appropriate 
 *   interface. From the keyword name the corresponding PAF keyword 
 *   name is derived by removing any "ESO " at keyword name beginning, 
 *   and replacing blanks with dots (e.g., "ESO TPL ID" becomes "TPL.ID").
 *   Finally, the new PAF keyword, with the same type as the header keyword, 
 *   is written to the currently active QC1 PAF object. Note that before 
 *   calling this funtion a QC1 PAF object must be created with a call to 
 *   fors_qc_start_group().
 */

cpl_error_code fors_qc_keyword_to_paf(cpl_propertylist *header, 
                                     const char *name, const char *unit, 
                                     const char *comment, 
                                     const char *instrument)
{

  const char func[] = "fors_qc_keyword_to_paf";

  char            *keyName;
  char            *keep;
  char            *pos;
  int              ivalue;
  float            fvalue;
  double           dvalue;
  char            *svalue = NULL;
  int              status;
  int              i;


  if (header == NULL) {
    cpl_msg_error(func, "Empty header");
    return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
  }

  if (!cpl_propertylist_has(header, name)) {
    cpl_msg_error(func, "Keyword %s not found", name);
    return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
  }

  switch (cpl_propertylist_get_type(header, name)) {
  case CPL_TYPE_INT :
    ivalue = cpl_propertylist_get_int(header, name);
    break;
  case CPL_TYPE_FLOAT :
    fvalue = cpl_propertylist_get_float(header, name);
    break;
  case CPL_TYPE_DOUBLE :
    dvalue = cpl_propertylist_get_double(header, name);
    break;
  case CPL_TYPE_STRING :
    svalue = (char *)cpl_propertylist_get_string(header, name);
    break;
  default :
    cpl_msg_error(func, "Unsupported keyword type");
    return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
  }


  /*
   *  Construct entry name for PAF
   */

  keep = keyName = cpl_strdup(name);

  pos = strstr(keyName, "ESO ");

  if (pos == keyName)
    keyName += 4;

  for (i = 0; keyName[i] != '\0'; i++)
    if (keyName[i] == ' ')
      keyName[i] = '.';

  /*
   *  Now write entry to PAF object.
   */

  switch (cpl_propertylist_get_type(header, name)) {
  case CPL_TYPE_INT :
    status = fors_qc_write_int(keyName, ivalue, unit, comment, instrument);
    break;
  case CPL_TYPE_FLOAT :
    dvalue = fvalue;
  case CPL_TYPE_DOUBLE :
    status = fors_qc_write_double(keyName, dvalue, unit, comment, instrument);
    break;
  default :    /* CPL_TYPE_STRING */
    status = fors_qc_write_string(keyName, svalue, comment, instrument);
  }

  if (status)
    cpl_msg_error(func, "Could not copy keyword value to QC1 PAF!");

  cpl_free(keep);

  return status;

}

/**
 * @brief
 *   Write a string value to the active QC1 PAF object and to a header.
 *
 * @return @c CPL_ERROR_NONE on success
 *
 * @param header     Product header
 * @param name       QC1 PAF entry name.
 * @param value      Value to write.
 * @param unit       Optional unit to be associated to value.
 * @param comment    Comment to be associated to value.
 * @param instrument Instrument name
 *
 *   An entry with the specified @em name is written to the current QC1 PAF
 *   object. From the entry @em name, the name of the QC keyword that
 *   should be written to header is derived prepending the string "ESO "
 *   and replacing all '.' with a blank (e.g., "QC.BIAS.MASTER.MEAN"
 *   becomes "ESO QC BIAS MASTER MEAN"). Finally, the new keyword
 *   is written to the header. Note that before calling this funtion
 *   a QC1 PAF object must be created with a call to fors_qc_start_group().
 */
cpl_error_code fors_qc_write_qc_string(cpl_propertylist *header,
                                       const char *name, const char *value, 
                                       const char *comment, 
                                       const char *instrument)
{
    const char func[] = "fors_qc_write_qc_string";

    char *header_name;
    int   i;

    if (strcmp("QC.DID", name)) {
        if (fors_qc_write_string_chat(name, value, comment, instrument)) {
            cpl_error_set_where(func);
            return cpl_error_get_code();
        }
    }
    else {
        if (fors_qc_write_string(name, value, comment, instrument)) {
            cpl_error_set_where(func);
            return cpl_error_get_code();
        }
    }

    header_name = cpl_malloc((strlen(name) + 6) * sizeof(char *));

    strcpy(header_name, "ESO ");
    strcat(header_name, name);

    for (i = 0; header_name[i] != '\0'; i++)
        if (header_name[i] == '.')
            header_name[i] = ' ';

    if (cpl_propertylist_update_string(header, header_name, value)) {
        cpl_free(header_name);
        cpl_error_set_where(func);
        return cpl_error_get_code();
    }

    cpl_propertylist_set_comment(header, header_name, comment);

    cpl_free(header_name);

    return CPL_ERROR_NONE;
}

/**
 * @brief
 *   Write an integer value to the active QC1 PAF object and to a header.
 *
 * @return @c CPL_ERROR_NONE on success
 *
 * @param header     Product header
 * @param value      Value to write.
 * @param name       QC1 PAF entry name.
 * @param unit       Optional unit to be associated to value.
 * @param comment    Comment to be associated to value.
 * @param instrument Instrument name
 *
 *   This function writes the header entries directly to the header
 *   of the FITS file written to disk, using the qfits_replace_card() call.
 *   An entry with the specified @em name is written to the current QC1 PAF
 *   object. From the entry @em name, the name of the QC keyword that
 *   should be written to header is derived prepending the string "ESO "
 *   and replacing all '.' with a blank (e.g., "QC.BIAS.MASTER.MEAN"
 *   becomes "ESO QC BIAS MASTER MEAN"). Finally, the new keyword
 *   is written to the header. Note that before calling this funtion
 *   a QC1 PAF object must be created with a call to fors_qc_start_group().
 */

cpl_error_code fors_qc_write_qc_double(cpl_propertylist *header, double value, 
                                      const char *name, const char *unit, 
                                      const char *comment,
                                      const char *instrument)
{

  const char func[] = "fors_qc_write_qc_double";

  char *header_name;
  int   i;


  if (fors_qc_write_double(name, value, unit, comment, instrument)) {
      cpl_error_set_where(func);
      return cpl_error_get_code();
  }

  header_name = cpl_malloc((strlen(name) + 6) * sizeof(char *));

  strcpy(header_name, "ESO ");
  strcat(header_name, name);

  for (i = 0; header_name[i] != '\0'; i++)
    if (header_name[i] == '.')
      header_name[i] = ' ';

  if (cpl_propertylist_update_double(header, header_name, value)) {
      cpl_free(header_name);
      cpl_error_set_where(func);
      return cpl_error_get_code();
  }

  cpl_propertylist_set_comment(header, header_name, comment);

  cpl_free(header_name);

  return CPL_ERROR_NONE;

}


cpl_error_code fors_qc_write_qc_int(cpl_propertylist *header, int value,
                                   const char *name, const char *unit,
                                   const char *comment,
                                   const char *instrument)
{

  const char func[] = "fors_qc_write_qc_int";

  char *header_name;
  int   i;


  if (fors_qc_write_int(name, value, unit, comment, instrument)) {
      cpl_error_set_where(func);
      return cpl_error_get_code();
  }

  header_name = cpl_malloc((strlen(name) + 6) * sizeof(char *));

  strcpy(header_name, "ESO ");
  strcat(header_name, name);

  for (i = 0; header_name[i] != '\0'; i++)
    if (header_name[i] == '.')
      header_name[i] = ' ';

  if (cpl_propertylist_update_int(header, header_name, value)) {
      cpl_free(header_name);
      cpl_error_set_where(func);
      return cpl_error_get_code();
  }

  cpl_propertylist_set_comment(header, header_name, comment);

  cpl_free(header_name);

  return CPL_ERROR_NONE;

}

/*
 * @brief
 *   Write an integer value to the active QC1 PAF object and to a header.
 *
 * @return @c CPL_ERROR_NONE on success
 *
 * @param filnam  Name of existing FITS file.
 * @param value   Value to write.
 * @param name    QC1 PAF entry name.
 * @param unit    Optional unit to be associated to value.
 * @param comment Optional comment to be associated to value.
 *
 * @doc
 *   This function writes the header entries directly to the header 
 *   of the FITS file written to disk, using the qfits_replace_card() call.
 *   An entry with the specified @em name is written to the current QC1 PAF 
 *   object. From the entry @em name, the name of the QC keyword that
 *   should be written to header is derived prepending the string "ESO "
 *   and replacing all '.' with a blank (e.g., "QC.BIAS.MASTER.MEAN"
 *   becomes "ESO QC BIAS MASTER MEAN"). Finally, the new keyword
 *   is written to the header. Note that before calling this funtion 
 *   a QC1 PAF object must be created with a call to fors_qc_start_group().
 */

/*
cpl_error_code fors_qc_write_qc_int(char *filnam, int value, const char *name,
                               const char *unit, const char *comment,
                               const char *instrument)
{

  const char func[] = "fors_qc_write_qc_int";

  char             line[81];
  char             val[81];
  char            *descName;
  int              i;


  if (fors_qc_write_int(name, value, unit, comment, instrument)) {
    cpl_msg_error(func, "Could not copy value to QC1 PAF!");
    cpl_error_set_where(func);
    return cpl_error_get_code();
  }

  descName = cpl_malloc((strlen(name) + 15) * sizeof(char *));

  strcpy(descName, "HIERARCH ESO ");
  strcat(descName, name);

  for (i = 0; descName[i] != '\0'; i++)
    if (descName[i] == '.')
      descName[i] = ' ';

  sprintf(val, "%d", value);
  keytuple2str(line, descName, val, (char *)comment);
  qfits_replace_card(filnam, descName, line);

  cpl_free(descName);

  return CPL_ERROR_NONE;

}


cpl_error_code fors_qc_write_qc_double(char *filnam, double value, 
                                      const char *name, const char *unit, 
                                      const char *comment,
                                      const char *instrument)
{

  const char func[] = "fors_qc_write_qc_double";

  char             line[81];
  char             val[81];
  char            *descName;
  int              i;


  if (fors_qc_write_double(name, value, unit, comment, instrument)) {
    cpl_msg_error(func, "Could not copy value to QC1 PAF!");
    cpl_error_set_where(func);
    return cpl_error_get_code();
  }

  descName = cpl_malloc((strlen(name) + 15) * sizeof(char *));

  strcpy(descName, "HIERARCH ESO ");
  strcat(descName, name);

  for (i = 0; descName[i] != '\0'; i++)
    if (descName[i] == '.')
      descName[i] = ' ';

  sprintf(val, "%1.6e", value);
  keytuple2str(line, descName, val, (char *)comment);
  qfits_replace_card(filnam, descName, line);

  cpl_free(descName);

  return CPL_ERROR_NONE;

}

*/

/**@}*/