File: vmifusphotcalib.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 (379 lines) | stat: -rw-r--r-- 10,990 bytes parent folder | download | duplicates (3)
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
/* $Id: vmifusphotcalib.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <pilmessages.h>

#include "vmimage.h"
#include "vmimageset.h"
#include "vmmatrix.h"
#include "vmmath.h"
#include "vmfit.h"
#include "vmccdtable.h"
#include "vmifutable.h"
#include "vmobjecttable.h"
#include "vmifutypes.h"
#include "vmmossphotcalib.h"
#include "vmifusphotcalib.h"


#define NUMFIBS 6400
#define IFU_HEAD_SIZE 80


int
VmIfuCalPhot(VimosImageSet *imageSet, int fitOrd, float int_frac)
{
  int i, k, right, got, quadNum, ngood;
  int referenceL, referenceM, specLen, objNumber = 0;
  int fibL[NUMFIBS], fibM[NUMFIBS], fibLS[NUMFIBS];
  int fibMS[NUMFIBS], toSort[NUMFIBS];
  int refSlitNo = 0, refFibNo = 0, refQuad = 0, stdRow = 0, xLen, yLen;
  int jj, ll, ok;

  float referenceFlux, refTotIntensity = 0, wLenInc;
  float threshold, fraction;
  float fibIntFlux[NUMFIBS], fibIntFluxS[NUMFIBS];

  double dValue;
  char comment[80];
  char modName[] = "VmIfuCalPhot";

  VimosFloatArray *spectrum;
  VimosImage *ifuImage, *ifuMask, *stdImage = NULL;

  VimosSingleImage *oneImage;

  VimosObjectTable *objTable;
  VimosObjectObject *imaObjs;

  VimosTable *stdTable = NULL;
  VimosIfuTable *ifuTable;
  VimosIfuQuad *theQuads;
  VimosIfuSlit *theIfuSlits = NULL;
  VimosIfuFiber *theIfuFibers;

  VimosPixelRegion *pixRegion;
  VimosPixelList *thePixList;
  VimosPixelData *thePixData;

  pilMsgInfo (modName, "Start computing spectro-photometric calibration");

  puts("TBD: numbering for L,M from 0 or 1 (below) - CHECK IT");

  oneImage = imageSet->images;

  /* this is the subsctript for fibM, fibL, fibIntFlux arrays */
  got = 0;

  while (oneImage)
  {
    /* take the object table and IFU table of this image */
    objTable = oneImage->objectTable;
    ifuTable = oneImage->ifuTable;

    /* length of spectra in wavelength */  
    specLen = oneImage->theImage->xlen;
    
    /* Allocate spectrum */
    spectrum = newFloatArray(specLen);

    /* CHECK wLenInc DOUBLE OR FLOAT! */
    /* read wlen inc keyword: expected in nm (DRS Document) */
    readDoubleDescriptor(objTable->descs, "ESO PRO WLEN INC", 
                         &dValue, comment);
    wLenInc = (float) dValue;

    /* counter for all objects in image */  
    objNumber = 0;

    /* read which quadrant this image refers */
    readIntDescriptor(objTable->descs, "ESO PRO QUAD", &quadNum, comment);
    theQuads = ifuTable->quads;

    /* take from ifuTable only those slits in the quadrant corresponding */
    /* to the image in use */
    while (theQuads)
    {
      if (theQuads->quadNo == quadNum)
      {
        theIfuSlits = theQuads->ifuSlits;
      }
      theQuads = theQuads->next;
    }

    /* take objects in the image object table */
    imaObjs = objTable->objs;
    while (imaObjs)
    {
      /* loop on IFU slits for this quadrant to find L,M of each object */
      while (theIfuSlits)
      {
        /* if this is the "right" slit go on to take fibers */
        if (theIfuSlits->ifuSlitNo == imaObjs->IFUslitNo)
        {
          theIfuFibers = theIfuSlits->fibers;

          /* loop on fibers of this slit until find the one */
          /* corresponding to the object */
          while (theIfuFibers)
          {
            if (theIfuFibers->fibNo == imaObjs->IFUfibNo)
            {
              /* take this spectrum, to integrate */
              for (i = 0; i < specLen; i++ )
              {
                spectrum->data[i] = 
                    oneImage->theImage->data[i + ((imaObjs->rowNum)*
                        specLen)];
              }
              fibIntFlux[got] = integrateSpectrum(spectrum, wLenInc);

              fibL[got] = theIfuFibers->fiberL;
              fibM[got] = theIfuFibers->fiberM;
              got++;
            }
            theIfuFibers = theIfuFibers->next;
          }
        }

        theIfuSlits = theIfuSlits->next;
      }

      imaObjs = imaObjs->next;

      /* increment counter of objects done */
      objNumber++;

    }

    oneImage = oneImage->next; 
    deleteFloatArray(spectrum);
  }

  if (got != NUMFIBS) printf("WRONG NUMBER of DETECTED OBJECTS: %d",got);

  /* now retrieve array of subscripts to sort in flux */
  Indexx(NUMFIBS, fibIntFlux, toSort);

  /* actual sorting. This mantains correspondence between flux array and */
  /* L,M coordinates of fibers                                           */
  for (i=0; i<NUMFIBS; i++)
  {
    fibIntFluxS[i] = fibIntFlux[toSort[i]];
    fibLS[i] = fibL[toSort[i]];
    fibMS[i] = fibM[toSort[i]];
  }

  /* WARNING! CHECK THE SORTING */
  /* take the flux reference fiber, i.e. the one with the max. flux */
  referenceFlux = fibIntFluxS[0];
  referenceL = fibLS[0];
  referenceM = fibMS[0];

  ifuImage = newImageAndAlloc(IFU_HEAD_SIZE,IFU_HEAD_SIZE);

  /* NOTE: pixel (0,0) correspond to (L,M) = (1,1) */
  /* put the fiber (fibL, fibM) = (1,1) at the pixel (0,0) */
  /* CHECK IF THIS WORKS CORRECTLY */
  puts("Check renumbering of fibers -> pixels");
  for (k=0; k<NUMFIBS; k++)
  {
    ifuImage->data[(fibL[k]-1) + (fibM[k]-1)*IFU_HEAD_SIZE] = fibIntFlux[k];
  }

  /* now we can use the image for thresholding & looking for adjacent pixels*/
  ifuMask = newImageAndAlloc(IFU_HEAD_SIZE,IFU_HEAD_SIZE);

  /* set the threshold value */
  threshold = referenceFlux * int_frac;

  ngood = 0;
  thresholdImage(ifuImage, threshold, ifuMask, &ngood);

  /* now detect adjacent pixels, i.e. regions */
  pixRegion = newPixelRegion();

  pixRegion = findRegionsOnPixelMap(ifuImage, ifuMask, pixRegion,
      ngood);

  /* if more than one blob, look for the one with reference fiber */
  if (pixRegion->numRegions > 1)
  {
    puts("VmIfuCalPhot: number of regions above threshold > 1");
  }

  thePixList = pixRegion->pixelList;

  right = 0;
  while (thePixList)
  {
    thePixData = thePixList->pixelData;
    while (thePixData)
    {
      if ( (thePixData->x == referenceL) &&
          (thePixData->y == referenceM) ) right++;
      thePixData = thePixData->next;
    }
    if (right > 1) puts("something wrong in detecting reference region");
    if (right == 1) refTotIntensity = thePixList->totIntensity;

    thePixList = thePixList->next;
  }

  if(right == 0) puts("something wrong in detecting reference region");

  /* loop on IFU Table to find slit and fiber number of reference spectrum */
  oneImage = imageSet->images;
  ifuTable = oneImage->ifuTable;
  theQuads = ifuTable->quads;
  while (theQuads)
  {
    theIfuSlits = theQuads->ifuSlits;
    while (theIfuSlits)
    {
      theIfuFibers = theIfuSlits->fibers;
      while (theIfuFibers)
      {
        if ((theIfuFibers->fiberL == referenceL) &&
            (theIfuFibers->fiberM == referenceM))
        {
          refFibNo = theIfuFibers->fibNo;
          refSlitNo = theIfuSlits->ifuSlitNo;
          refQuad = theQuads->quadNo;
        }
        theIfuFibers = theIfuFibers->next;
      }
      theIfuSlits = theIfuSlits->next;
    }
    theQuads = theQuads->next;
  }

  /* determine the fraction factor: fraction of light lost by the ref. fibre*/
  fraction = refTotIntensity/referenceFlux;

  /* now loop on imageSet and objectTables to find the image with reference 
     spectrumand relative spectro-phot table, find row number for the 
     reference spectrum, correct flux loss for reference spectrum, and evaluate
     spectro-photometric calibration */

  ok = 0;
  oneImage = imageSet->images;
  while (oneImage)
  {
    /* take the object table and IFU table of this image */
    objTable = oneImage->objectTable;

    /* read which quadrant this image refers to */
    readIntDescriptor(objTable->descs, "ESO PRO QUAD", &quadNum, "");

    /* if this image refers to the quadrant with reference spectrum, go on */
    if (quadNum == refQuad)
    {
      /* take objects in the image object table */
      imaObjs = objTable->objs;
      while (imaObjs)
      {
        if ((imaObjs->IFUslitNo == refSlitNo) &&
            (imaObjs->IFUfibNo == refFibNo))
        {
          if (ok > 1) 
          {
            pilMsgError(modName, ": More than one image for reference spectrum. Exiting");
            return EXIT_FAILURE;
          }

          /* take row number of the std spectrum in the image */
          stdRow = imaObjs->rowNum;

          xLen = oneImage->theImage->xlen;
          yLen = oneImage->theImage->ylen;

          /* correct the ref. fibre spectrum for flux loss
		 before deriving response function as for MOS */

          /* CHECK if THIS IS OK! */
          puts("Check flux correction!");
          for (ll=0; ll<xLen; ll++)
            oneImage->theImage->data[ll + stdRow*yLen] *= fraction;

          stdImage = newImageAndAlloc(xLen, yLen);
          /* take image for spectro-photometric calibration */
          for (jj=0; jj<yLen; jj++)
          {
            for (ll=0; ll<xLen; ll++)
            {
              stdImage->data[ll +jj*yLen] = 
                  oneImage->theImage->data[ll +jj*yLen];
            }
          }

          /* take spectro-photometric table of this image */
          stdTable = oneImage->sphotStdTable;

          ok++;
        } /* end if this object is the reference spectrum */

        imaObjs = imaObjs->next;
      } /* end loop on objects for this image */

    } /* end if the image comes from the quadrant with reference spectrum */

    oneImage = oneImage->next;
  }

  /* now call VmSpCalPhot for spectro-photometric calibration */
  if (EXIT_FAILURE == VmSpCalPhot(stdImage, stdTable, stdRow, fitOrd))
  {
    pilMsgError(modName," Failure in deriving spectro-photom. calibration");
    return EXIT_FAILURE;
  }

  /* NOTE: ifu table is NOT updated with refL and refM*/


  deleteImage(ifuImage);
  deleteImage(ifuMask);
  deleteImage(stdImage);

  deleteObjectTable(objTable);
  deleteObjectObject(imaObjs);
  deleteTable(stdTable);
  deleteIfuTable(ifuTable);

  deletePixelRegion(pixRegion);

  return EXIT_SUCCESS;
}