File: vmccdtable.c

package info (click to toggle)
cpl-plugin-vimos 4.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,228 kB
  • sloc: ansic: 169,271; cpp: 16,177; sh: 4,344; python: 3,678; makefile: 1,138; perl: 10
file content (590 lines) | stat: -rw-r--r-- 16,201 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
/* $Id: vmccdtable.c,v 1.2 2013-03-25 11:43:04 cgarcia Exp $
 *
 * This file is part of the VIMOS Pipeline
 * Copyright (C) 2002-2004 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * $Author: cgarcia $
 * $Date: 2013-03-25 11:43:04 $
 * $Revision: 1.2 $
 * $Name: not supported by cvs2svn $
 */

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

#include <string.h>

#include <fitsio.h>

#include <pilmemory.h>
#include <pilstrutils.h>
#include <pilmessages.h>
#include <cpl_msg.h>
#include <piltranslator.h>

#include "vmtable.h"
#include "vmmath.h"
#include "vmccdtable.h"
#include "cpl.h"


#define MAX_BAD_PIXEL_FRACTION (0.15)


static VimosTable *
createCcdTable()
{

    VimosTable      *ccdTable;
    VimosColumn     *currentColumn;

    if ((ccdTable = newCcdTable())) {

        /*
         * Columns initialization
         */

        ccdTable->cols = newColumn();           /* Column X */
        currentColumn = ccdTable->cols;
        strcpy(currentColumn->colName,"X");
        currentColumn->colType = VM_INT;

        currentColumn->next = newColumn();      /* Column Y */
        currentColumn = currentColumn->next;
        strcpy(currentColumn->colName,"Y");
        currentColumn->colType = VM_INT;

        ccdTable->numColumns = 2;
    }

    return(ccdTable);   /* ccdTable == NULL in case of failures above */

}


/* 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   VimosTable *newCcdTable()

   Description:
   Allocate a new Ccd Table. This is a VimosTable with name VM_CCD (which
   has value "CCD").
   Only a Descriptor "TABLE" is present, with value "CCD".
   
   Input:
   void
   
   Return Value (success):
   Pointer to the new VimosTable

   Return Value (error):
   function calls exit(-1)
   
   Updates:
   11 Feb 99: Created (TAO)

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

VimosTable *
newCcdTable()
{

  VimosTable *newTab;
  
  /* allocate new VimosTable */
  newTab = newTable();

  /* check if space was allocated */
  if (newTab == NULL) {
    cpl_msg_error("newCcdTable","The function newTable has returned NULL");
    return(NULL);
  }
  
  /* copy "CCD" into name of table */
  strcpy(newTab->name, VM_CCD);
  newTab->descs = newStringDescriptor(pilTrnGetKeyword("Table"), VM_CCD,
                                      pilTrnGetComment("Table"));

  if (newTab->descs == NULL) {
    cpl_free(newTab);
    cpl_msg_error("newCcdTable","The function newStringDescriptor has "
                "returned NULL");
    return(NULL);
  }

  /* should create all the descriptors and columns here ?*/
  
  return(newTab);
  
}


/* 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   void deleteCcdTable(VimosTable *cTable)
   
   Description:
   Delete a Ccd Table. This is just an esthetic wrapper for
   deleteTable(cTable)
   
   Input: 
   VimosTable *cTable
   Pointer of Ccd Table to be deleted
   
   Return Value:
   void
   
   Updates:
   11 Feb 99: Created (TAO)

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

void
deleteCcdTable(VimosTable *ccdTable)
{
  /* just a wrapper for deleteTabel() */
  deleteTable(ccdTable);
}

VimosTable *badPixelImage2CcdTable(VimosImage *badPixelImage)
{
  int              i,npix;
  int              x,y;
  int              nbadpix = 0;

  VimosTable      *ccdTable;
  VimosColumn     *currentColumn;


  if ((ccdTable = createCcdTable())) {
      const char *hint = cpl_strdup(pilTrnGetKeyword("Table"));

      /*
       * The table is created from the bad pixel map, which
       * is assumed to already have a correct and complete
       * list of header keywords.
       */

      vimosDscCopy(&ccdTable->descs, badPixelImage->descs,
                   "[A-Z].*", hint);

      cpl_free((void *)hint);

    npix = badPixelImage->xlen * badPixelImage->ylen;

    for (i = 0; i < npix; i++) nbadpix += badPixelImage->data[i];

    currentColumn = ccdTable->cols;
    currentColumn->colValue->iArray = (int *)cpl_malloc(nbadpix * sizeof(int));
    currentColumn->len = nbadpix;

    currentColumn = currentColumn->next;
    currentColumn->colValue->iArray = (int *)cpl_malloc(nbadpix * sizeof(int));
    currentColumn->len = nbadpix;

    for (x = 0; (x < badPixelImage->xlen) && nbadpix; x++) {
      for (y = 0; (y < badPixelImage->ylen) && nbadpix; y++) {
        if (badPixelImage->data[x + y * badPixelImage->xlen] > .5) {
          nbadpix--;
          currentColumn = ccdTable->cols;
          currentColumn->colValue->iArray[nbadpix] = x + 1;
          currentColumn = currentColumn->next;
          currentColumn->colValue->iArray[nbadpix] = y + 1;
        }
      }
    }
  }
  return(ccdTable);   /* ccdTable == NULL in case of failures above */
}


int cleanBadPixels(VimosImage *image, VimosTable *ccdTable, int correctLevel)
{
  char         modName[] = "cleanBadPixels";

  int         *isBadPix;
  int          i, j, k, d;
  int          xlen, ylen, totPix;
  int          nBadPixels = 0;
  int          sign, foundFirst;
  VimosColumn *currColumn;
  VimosColumn *colX;
  VimosColumn *colY;
  VimosColumn *colLevel;
  int         *xValue = NULL;
  int         *yValue = NULL;
  int         *level  = NULL;
  float        save = 0.;
  double       sumd;
  int          cx, cy;
  int          nPairs;
  float        estimate[4];
  int          sx[] = {0, 1, 1, 1};
  int          sy[] = {1,-1, 0, 1};
  int          searchHorizon = 100;
  int          percent = MAX_BAD_PIXEL_FRACTION * 100;

  char         instMode[80];
  int          imageMode, ccdTableMode;


  if (image == NULL) {
    cpl_msg_error(modName, "Null input image: aborting bad pixels cleaning");
    return(EXIT_FAILURE);
  }
  if (ccdTable == NULL) {
    cpl_msg_warning(modName, "No CCD Table: no bad pixel cleaning");
    return(EXIT_SUCCESS);
  }

  readStringDescriptor(image->descs, pilTrnGetKeyword("InstrumentMode"),
                       instMode, NULL);
  imageMode = (instMode[1] == 'M' ? 1 : 0);  /* 0 = MOS, 1 = IMG */

  /*
   * Check that the given CCD_TABLE is the right one, i.e., the one with
   * the right instrument mode. If the instrument mode is missing, this
   * is a pure cosmic ray events table, that is assumed correct anyway.
   */

  if (VM_TRUE == readStringDescriptor(ccdTable->descs, 
                 pilTrnGetKeyword("InstrumentMode"), instMode, NULL)) {
    ccdTableMode = (instMode[1] == 'M' ? 1 : 0);  /* 0 = MOS, 1 = IMG */
  }
  else
    ccdTableMode = imageMode;

  if (ccdTableMode != imageMode) {
    cpl_msg_error(modName, "The CCD table doesn't match the image to be cleaned");
    return(EXIT_FAILURE);
  }

 /* 
  *  Read bad pixels listed in CCD table 
  */

  if (ccdTable->cols == NULL)  {
    nBadPixels = 0;
  }
  else {
    currColumn = ccdTable->cols;
    nBadPixels = currColumn->len;
  }

  if (nBadPixels) {
    xlen = image->xlen;
    ylen = image->ylen;
    totPix = xlen * ylen;
    if (((float) nBadPixels) / ((float) totPix) < MAX_BAD_PIXEL_FRACTION) {
      isBadPix = (int *) cpl_malloc(totPix * sizeof(int));
      for (i = 0; i < totPix; i++) isBadPix[i] = 0;
    }
    else {
      cpl_msg_warning(modName, 
      "Too many bad pixels (> %d%%): skip bad pixel correction", percent);
      return(EXIT_FAILURE);
    }
  }
  else {
    cpl_msg_info(modName, "No bad pixels to correct");
    return(EXIT_SUCCESS);
  }
  if ((colX = findColumn(currColumn,"X")) 
                                    && (colY = findColumn(currColumn,"Y"))) {
    xValue = colX->colValue->iArray;
    yValue = colY->colValue->iArray;
    for (i = 0; i < nBadPixels; i++) {
        isBadPix[xValue[i]-1 + (yValue[i]-1) * xlen] = 1;
    }
  }
  else {
    cpl_msg_error(modName, "Missing coordinates columns in CCD table");
    cpl_free(isBadPix);
    return(EXIT_FAILURE);
  }

  if (correctLevel) {
    if ((colLevel = findColumn(currColumn, "level"))) {
      level = colLevel->colValue->iArray;
    }
    else {
      cpl_msg_error(modName, "Missing level column in CCD Table");
      cpl_free(isBadPix);
      return(EXIT_FAILURE);
    }
  }

  for (i = 0; i < nBadPixels; i++) { 
    if (correctLevel && correctLevel != level[i]) continue;
   /*
    *  Search for the closest good pixel along the 4 fundamental directions
    *  (in both senses):
    *                            \ | /
    *                             \|/
    *                           --- ---
    *                             /|\
    *                            / | \
    *
    *  Then collect pairs of values to interpolate linearly.
    */
    nPairs = 0;
    for (j = 0; j < 4; j++) {
      estimate[nPairs] = 0.;  /* Pairs interpolation results are stored here */
      sumd = 0.;
      foundFirst = 0;
      for (k = 0; k < 2; k++) {
        sign = 2 * k - 1;
        d = 0;
        cx = xValue[i]-1;
        cy = yValue[i]-1;
        do {
          cx += sign * sx[j];
          cy += sign * sy[j];
          if (cx < 0 || cx >= xlen || cy < 0 || cy >= ylen) break;
          d++;
        } while (isBadPix[cx + cy * xlen] && d < searchHorizon);
        if (cx >= 0 && cx < xlen && cy >= 0 && cy < ylen &&
            d < searchHorizon) {
         /*
          *  In this block is cripted the linear interpolation...
          */
          save = image->data[cx + cy * xlen];
          estimate[nPairs] += save / d;
          sumd += 1. / (double) d;
          if (k) {
            estimate[nPairs] /= sumd;
            nPairs++;
          }
          else {
            foundFirst = 1;
          }
        }
        else {
         /* 
          * Image borders were crossed, incomplete pair of values
          */
          if (k) {
            if (foundFirst) {
              estimate[nPairs] = save;
              nPairs++;
            }
          }
        }
      }
    }
   /*
    * Replace pixel value of the input image, corresponding to
    * the current bad pixel, with the median of the estimates
    * resulted from the 4 linear interpolations.
    */
    if (nPairs > 2) {
      image->data[(xValue[i]-1) + (yValue[i]-1) * xlen] = 
                                               medianWirth(estimate, nPairs);
    }
    else if (nPairs == 2) {
      image->data[(xValue[i]-1) + (yValue[i]-1) * xlen] = 
                                            (estimate[0] + estimate[1]) / 2.;
    }
    else if (nPairs == 1) {
      image->data[(xValue[i]-1) + (yValue[i]-1) * xlen] = estimate[0];
    }
    else {
      cpl_msg_warning(modName, "Cannot correct bad pixel %d,%d\n",
                                                       xValue[i], yValue[i]);
    }
  }
  cpl_free(isBadPix);
  return(EXIT_SUCCESS);
}

int shiftCcdTableCoords(VimosTable *ccdTable, int vertical, int shift)
{
  VimosColumn *coord;
  int          numBad;
  int          i;
  int          exitStatus = EXIT_FAILURE;

  if (ccdTable) {
    if (ccdTable->numColumns > 0) {
      numBad = ccdTable->cols->len;
      coord = ccdTable->cols;
      if (vertical) coord = coord->next;
      for (i = 0; i < numBad; i++) coord->colValue->iArray[i] += shift;
      exitStatus = EXIT_SUCCESS;
    }
  }
  return(exitStatus);
}


/* 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   VimosBool readFitsCcdTable(VimosTable *ccdTable, fitsfile *fptr)

   Description:
   Read a FITS Table into a CCD Table. This requires the FITS file to 
   be already open, and does not close it at the end.
   
   Input:
   VimosCCDTable *ccdTable  
   Pointer to a CCD Table to copy the FITS table into.

   fitsfile *fptr
   Pointer to the FITS file where the table is to be found

   Return Value:
   VimosBool (true if the read is succesful, false otherwise)
   
   Updates:
   15 Dec 99: Created (BG)
   02 Feb 00: Changed the handling of the table, from a standalone FITS file,
              to an extension of the primary FITS file (MS)

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

VimosBool
readFitsCcdTable(VimosTable *ccdTable, fitsfile *fptr)
{

    int status;

    /* validate input */
    if (ccdTable == NULL) {
        cpl_msg_error("readFitsCcdTable","NULL input table");
        return(VM_FALSE);
    }
    if (fptr == NULL) {
        cpl_msg_error("readFitsCcdTable","NULL pointer to fitsfile");
        return (VM_FALSE);
    }

    if (strcmp(ccdTable->name, VM_CCD)) {
        cpl_msg_error("readFitsCcdTable","Invalid input table");
        return(VM_FALSE);
    }
  
    status = 0;

    /* open Table */
    if (fits_movnam_hdu(fptr, BINARY_TBL, "CCD", 0, &status) ){
        cpl_msg_error("readFitsCcdTable","The function fits_movnam_hdu has "
                    "returned an error (code %d)", status);
        return(VM_FALSE);
    }

    ccdTable->fptr = fptr;

    if (!readFitsTable(ccdTable, ccdTable->fptr)) {
        cpl_msg_info("readFitsCcdTable", "Error in reading the FITS file");
        return VM_FALSE;
    }

    return(VM_TRUE);
}  


/* 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   VimosBool writeFitsCcdTable(VimosTable *ccdTable, fitsfile *fptr)

   Description:
   Write a CCD  Table into a FITS Table. This requires the FITS file
   to be already open, and does not close it at the end. If a CCD
   Table extension is already present in the file, it is first removed, and
   then the new one is written into a new extension.  
   
   Input:
   VimosTable *ccdTable  
   Pointer to the CCD Table to write to a FITS Table

   fitsfile *fptr
   Pointer to the FITS file where the table is to be found

   Return Value:
   VimosBool (true if the write is succesful, false otherwise)
   
   Updates:
   15 Dec 99: Created (BG)
   02 Feb 00: Changed the handling of the table, from a standalone FITS file,
              to an extension of the primary FITS file (MS)

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

VimosBool
writeFitsCcdTable(VimosTable *ccdTable, fitsfile *fptr)
{
  int status;
 
  /* validate input */
  if (ccdTable == NULL) {
    cpl_msg_error("writeFitsCcdTable","NULL input table");
    return(VM_FALSE);
  }

  /* check table name, should be "CCD" */
  if ( strcmp(ccdTable->name, VM_CCD) ) {
    cpl_msg_error("writeFitsCcdTable","Invalid input table");
    return(VM_FALSE);
  }

  status = 0;
  ccdTable->fptr = fptr;

  /* if Table is already present, first remove it  */
  if (!fits_movnam_hdu(fptr, BINARY_TBL, "CCD", 0, &status) ) {
    if(fits_delete_hdu(fptr, NULL, &status)) {
      cpl_msg_error("writeFitsCcdTable","The function fits_delete_hdu has "
                  "returned an error (code %d)", status);
      return(VM_FALSE);
    }
  } else {
    status = 0;
  }

  /* create 1st HDU for BINARY TABLE */
  if (fits_create_tbl (ccdTable->fptr,BINARY_TBL,0,0,NULL, NULL,NULL,
		       "CCD", &status)) {
    cpl_msg_error("writeFitsCcdTable","The function fits_create_tbl has "
                "returned an error (code %d)", status);
    return(VM_FALSE);
  }
  if (fits_movnam_hdu(fptr, BINARY_TBL, "CCD", 0, &status) ) {
    cpl_msg_error("writeFitsCcdTable","The function fits_movnam_hdu has "
                "returned an error (code %d)", status);
    return (VM_FALSE);
  }

  /* write the table to the Fits file */
  if (!writeDescsToFitsTable(ccdTable->descs, ccdTable->fptr)) {
    cpl_msg_error("writeFitsCcdTable","The function writeDescsToFitsTable "
                "has returned an error");
    return(VM_FALSE);
  }

  return(VM_TRUE);
}