File: vmmosmodels.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 (681 lines) | stat: -rw-r--r-- 20,188 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
/* $Id: vmmosmodels.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

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

#include "vmimage.h"
#include "vmtable.h"
#include "vmfit.h"
#include "vmmath.h"
#include "vmmosutils.h"
#include "vmifuutils.h"
#include "vmmosmodels.h"


/**
 * @brief vmmosmodels MOS Optical Models
 *
 * The module provides functions to model the instrument optical distortions
 * and the spectral curvature.
 */

/**@{*/

int
VmSpDerCurves(VimosImage **imageData, VimosExtractionTable *extractionTable,
              int fuzz)
{
  char                 modName[] = "VmSpDerCurves";
  int                  i, j, k;
  int                  outOfImage = 0;
  int                  xOut, yOut;
  int                  numRows;
  int                  numPoints;
  int                  numFitPointsLower, numFitPointsUpper;
  VimosUlong32         index;
  int                  nPixBelow, nPixAbove;
  int                  imageXlen, imageYlen;
  int                  crvOrder; 

  double               datVal;
  double               xOutF,yOutF;
  double               frac;
  double               fitVariance;
  double              *curve = NULL;
  float                xOffset;
  float                xStart, xEnd;
  float                xZero,yZero;
  double               zeroUpper,zeroLower;
  float                zeroWidth;
  int                  extraWidth = 10;
  
  int                  doZero;
  int mycount = 0;
  VimosImage          *firstOrderIma;
  VimosImage          *zeroOrderIma;
  VimosDpoint         *lower = NULL;
  VimosDpoint         *upper = NULL;
  VimosFloatArray     *tmpArray;
  VimosExtractionSlit *slit;
  
  /*ALEX */
  char insMode[80];

  cpl_msg_info(modName, "Deriving Curvature Polynomials");

  /*ALEX: determine numSlit depending on MOS or IFU: if IFU, numSlit equal to
    number of good fibers (to prevent zeros in medianWirth on offsetYArray */
  readStringDescriptor(imageData[0]->descs,
                       pilTrnGetKeyword("InstrumentMode"), 
		       insMode, NULL);

  firstOrderIma = newImage(imageData[0]->xlen,imageData[0]->ylen,
                           imageData[0]->data);
  firstOrderIma->descs = imageData[0]->descs;
  imageXlen = firstOrderIma->xlen;
  imageYlen = firstOrderIma->ylen;
  xStart = 0.;

  if (readIntDescriptor(extractionTable->descs,
                        pilTrnGetKeyword("ZeroOrderFlag"), 
                        &doZero, NULL) == VM_FALSE) {
    cpl_msg_error(modName, "Cannot find descriptor %s",
                pilTrnGetKeyword("ZeroOrderFlag"));
    return EXIT_FAILURE;
  }

  if (doZero) {
    zeroOrderIma = newImage(imageData[1]->xlen,imageData[1]->ylen,
                            imageData[1]->data);
    zeroOrderIma->descs = imageData[1]->descs;
  }

  /* read the order of the Curvature Polynomials in the header of the
     Extraction Table */
  readIntDescriptor(extractionTable->descs, "ESO PRO CRV POL ORD",
                     &crvOrder, NULL);

  /* read length of spectrum in pixels in wavelength direction */
  readIntDescriptor(extractionTable->descs, "ESO PRO SPECT LLEN LO", 
                    &nPixBelow, NULL);
  readIntDescriptor(extractionTable->descs, "ESO PRO SPECT LLEN HI",
                    &nPixAbove, NULL);
  /* length of spectrum inpixels in the wavelength direction */
  numPoints = nPixBelow + nPixAbove + 1;
  
  /* create buffers to store slit edges as function of wavelength */
  upper = newDpoint(numPoints);
  lower = newDpoint(numPoints);
  
  /* first slit in table */
  slit = extractionTable->slits;
  /* loop over all  slits */
  while (slit) {

    /* ALEX: when IFU, do nothing for dead fibers */
    if (slit->IFUfibTrans >= 0.0)
      {
	/* spatial length of slit */
	numRows = slit->numRows;
    
	/* create temp buffer */
	tmpArray = newFloatArray(numRows + 2*fuzz);  
	numFitPointsLower = 0;
	numFitPointsUpper = 0;

	/* loop over 'wavelength' */
	for (j = -nPixBelow; j <= nPixAbove; j++) {
	  numPoints = 0;

	  /* extract a cut across the slit, with a bit of extra space */
	  for (i = -fuzz; i < numRows+fuzz; i++) {
	    if (i < 0)
	      {
		k = 0;
		yOut = slit->ccdY->data[k] + j;
		xOutF = slit->ccdX->data[k] + 
		  computeDistModel1D(slit->crvPol[k], yOut) + i;
	      }
	    else if (i >= numRows)
	      {
		k = numRows - 1;
		yOut = slit->ccdY->data[k] + j;
		xOutF = slit->ccdX->data[k] + 
		  computeDistModel1D(slit->crvPol[k], yOut) + i - (numRows-1);
	      }
	    else
	      {
		k = i;
		yOut = slit->ccdY->data[k] + j;
		xOutF = slit->ccdX->data[k] + 
		  computeDistModel1D(slit->crvPol[k], yOut);
	      }
	    /* use curvature polynomial of edge to tracte these  data */
	    xOut = xOutF;

            /*
             * 100 = safety marging from X image borders.
             */

	    if (yOut<0 || yOut >= imageYlen || 
                xOut < 100 || xOut >= imageXlen - 100) {
	      outOfImage = 1; 
	      continue;
	    } 
	    outOfImage = 0;
	    numPoints++;

	    /* quick interpolation of data */
	    frac = xOutF-xOut;
	    index = xOut+yOut*imageXlen;
	    datVal = ( (1.0-frac)*firstOrderIma->data[index] +
		       frac*firstOrderIma->data[index+1] );
	    /* and store in buffer */
	    tmpArray->data[i+fuzz] = datVal;

	    if (i == 0)
	      xStart = slit->ccdX->data[0] + 
		computeDistModel1D(slit->crvPol[0],yOut);
	    if (i == numRows-1)
	      xEnd = slit->ccdX->data[numRows-1] +
		computeDistModel1D(slit->crvPol[numRows-1],yOut);
	  }
      
	  if (numPoints < numRows) {
	    continue;
	  }
	  else {
	    if (!strncmp(insMode,"IFU",3)) {
	      if (!(findIfuBorders(tmpArray, &(upper[numFitPointsUpper].y),
				   &(lower[numFitPointsLower].y)))) {

/*		cpl_msg_error(modName, 
		"function findIfuBorders returned error"); */
/*    		cpl_msg_warning(modName, 
		"%d %d %f %d \n",slit->IFUslitNo,slit->IFUfibNo,xStart,yOut);*/

		mycount++;

	      } else {
		upper[numFitPointsUpper].y += xStart-fuzz;
		lower[numFitPointsLower].y += xStart-fuzz;
		/* write pixel index as 'X' coordinate in buffer */
		upper[numFitPointsUpper].x =  j;
		lower[numFitPointsLower].x =  j;
		
		numFitPointsUpper++;
		numFitPointsLower++;
	      }
	    } else {  /* MOS */
	      findSpectrumBorders(tmpArray, &(upper[numFitPointsUpper].y),
				  &(lower[numFitPointsLower].y), fuzz); 
	      /* add the starting offset to the 'Y' coordinate in buffer */
	      /* NOTE: we add 1 to the lower edge and subtract 1 to the upper
		 edge to reduce the problems with handling of edge effects */

              if (upper[numFitPointsUpper].y > 0.0) {
	        upper[numFitPointsUpper].y += xStart-fuzz;
	        upper[numFitPointsUpper].x =  j;
	        numFitPointsUpper++;
              }

              if (lower[numFitPointsLower].y > 0.0) {
	        lower[numFitPointsLower].y += xStart-fuzz;
	        lower[numFitPointsLower].x =  j;
	        numFitPointsLower++;
              }
	    }
	  }
	}

	if (numFitPointsLower == 0 || numFitPointsUpper == 0) {
	  outOfImage = 0; 
	  slit = slit->next;
	  continue;
	}

        if (fabs(numFitPointsLower - numFitPointsUpper) / numFitPointsUpper 
            > 0.25 ) {
          outOfImage = 0;
          slit = slit->next;
          continue;
        }

	/* delete array with coefficients, if it exists */
	if (curve!= NULL) {
	  pil_free(curve);
	  curve = NULL;
	}
	/* delete the existing curvature polynomials in slit, in case the order
	   is not the same as we are fitting */
	for (i = 0; i < numRows; i++ ) {
	  deleteDistModel1D(slit->crvPol[i]);
	  /* and create of the right order */
	  slit->crvPol[i] = newDistModel1D(crvOrder);
	}
	/* fit polynomial to lower edge of slit */
	curve = fit1DPoly(crvOrder, lower, numFitPointsLower, &fitVariance);
	if (curve == NULL) {
	  printf("VmSpDerCurves: Invalid fit\n");
	  /* no fit, abort */
	  return EXIT_FAILURE;
	}
	/* copy coefficients to curvature polynomial */
	for (i = 0; i <= crvOrder; i++) {
	  slit->crvPol[0]->coefs[i] = curve[i];
	}

	slit->crvPolRms->data[0] = sqrt(fitVariance);

	/* set the right offset for the curvature polynomial */
	slit->crvPol[0]->offset = slit->ccdY->data[0];
	/* compute where lower edge for central wavelength is according to 
	   model */

	xOffset =  slit->crvPol[0]->coefs[0] - slit->ccdX->data[0];
	/* and correct models for this offset, so we can fit both a global
	   curvature model AND an optical distortion model (at least in X) to
	   the results of this function */    
	slit->crvPol[0]->coefs[0] = 0.;
	for (i = 0; i < numRows; i++)
	  slit->ccdX->data[i] += (int) xOffset;
    
	/* free buffer */
	pil_free(curve);
	curve = NULL;
    
	/* same story for upper edge of slit */
	curve = fit1DPoly(crvOrder, upper, numFitPointsUpper, &fitVariance);
	if (curve == NULL) {
	  return EXIT_FAILURE;
	}
	for (i = 0; i <= crvOrder; i++) {
	  slit->crvPol[numRows-1]->coefs[i] = curve[i];
	}
	slit->crvPolRms->data[numRows-1] = sqrt(fitVariance);
	slit->crvPol[numRows-1]->offset = slit->ccdY->data[numRows-1];
	slit->crvPol[numRows-1]->coefs[0] = 0.;
  
	/* destruct temp */
	deleteFloatArray(tmpArray);

	/* This is a new pice of code for the fitting of the Zero order
	   spectrum position */

	if (doZero) {
	  if (readFloatDescriptor(extractionTable->descs,
				  pilTrnGetKeyword("ZeroOrderWidth"), 
				  &zeroWidth, NULL) == VM_FALSE)
	    {
	      cpl_msg_error(modName, "Cannot find descriptor %s", 
			  pilTrnGetKeyword("ZeroOrderWidth"));
	      return EXIT_FAILURE;
	    }

	  yZero = 0.5 * (slit->ccdY->data[0]+slit->zeroY->data[0] + 
			 slit->ccdY->data[numRows-1] + 
			 slit->zeroY->data[numRows-1]);
	  xZero = 0.5 * (slit->ccdX->data[0]+slit->zeroX->data[0] + 
			 slit->ccdX->data[numRows-1] + 
			 slit->zeroX->data[numRows-1]);
	  tmpArray = newFloatArray((int) zeroWidth + 2 * extraWidth);
	  xOut = xZero;
	  for (i = 0; i < (int) zeroWidth + 2 * extraWidth; i++)
	    {
	      yOutF = yZero - 0.5 * (float) (zeroWidth + 2 * extraWidth) + 
		(float) i;
	      yOut = yOutF;
	      if (yOut < 0 || yOut >= imageYlen) {
		outOfImage = 1; 
		break;
	      }
	      frac = yOutF - yOut;
	      index = xOut+yOut*imageXlen;
	      datVal = ( (1.0-frac)*zeroOrderIma->data[index] +
			 frac*zeroOrderIma->data[index+1] );
	      /* and store in buffer */
	      tmpArray->data[i] = datVal;
	    }
	  if (outOfImage)
	    outOfImage = 0;
	  else 
	    findSpectrumBorders(tmpArray, &zeroUpper, &zeroLower, extraWidth); 
    
	  deleteFloatArray(tmpArray);
	  tmpArray = newFloatArray(numRows + 2*fuzz);  

	  yZero = 0.5 * (zeroLower + zeroUpper);
	  yOut = yZero;
	  for (i = -fuzz; i < numRows+fuzz; i++) {
	    if (i < 0)
	      k = 0;
	    else if (i >= numRows)
	      k = numRows - 1;
	    else
	      k = i;
	    xOutF = slit->ccdX->data[k] + slit->zeroX->data[k] + i;
	    xOut = xOutF;
	    /* quick interpolation of data */
	    frac = xOutF-xOut;
	    index = xOut+yOut*imageXlen;
	    datVal = ( (1.0-frac)*zeroOrderIma->data[index] +
		       frac*zeroOrderIma->data[index+1] );
	    /* and store in buffer */
	    tmpArray->data[i+fuzz] = datVal;
	    if (i == 0)
	      xStart = slit->ccdX->data[0] + slit->zeroX->data[0];
	    if (i == numRows-1)
	      xEnd= slit->ccdX->data[numRows-1] + slit->zeroX->data[numRows-1];
	  }
	  findSpectrumBorders(tmpArray, &zeroUpper, &zeroLower, fuzz); 
	  zeroLower += xStart-fuzz;
	  zeroUpper += xStart-fuzz;

	  slit->zeroX->data[0] = zeroLower - slit->ccdX->data[0];
	  slit->zeroY->data[0] = yZero - slit->ccdY->data[0];
	  slit->zeroX->data[numRows-1]=zeroUpper - slit->ccdX->data[numRows-1];
	  slit->zeroY->data[numRows-1] = yZero - slit->ccdY->data[numRows-1];
	}
	/* end of the new part dealing with zero order stuff */

      } /* ALEX: end if on dead fibers */

    /* go to next slit */
    slit = slit->next;    
  }
  
  /* clean up... */
  if (curve != NULL) {
    pil_free(curve);
  }
  deleteDpoint(upper);
  deleteDpoint(lower);
  
  return EXIT_SUCCESS;
}


/**
 * @memo
 *   Fit a Curvature Distorsion Model
 *
 * @return EXIT_SUCCESS / EXIT_FAILURE
 * 
 * @param extractionTable Input Extraction Table
 * @param grismTable      Input (and perhaps output) Grism Table
 * @param grismFlag       Flag to indicate whether to update or not
 *                        the Grism Table with the new Curvature Model
 *
 * @doc
 *   Just fit a polynomial in two variables to the coefficients
 *   of the polynomials fitting the edges of spectra found in a 
 *   spectral Flat Field
 *
 * @author M. Scodeggio
 */

int
VmSpCurveModel(VimosExtractionTable *extractionTable, VimosTable *grismTable,
               int grismFlag)
{
  int i; 
  int numSlits;
  int slitDone;
  int polOrder, xOrder, yOrder;
  double rmsFit;
  
  VimosExtractionSlit *slit;
  VimosPixel *coefArray;
  VimosDistModelFull *crvMod;
  
  char modName[]   ="VmSpCurveModel";

  
  cpl_msg_debug(modName, "Compute curvature model");

  numSlits = 0;
  slit = extractionTable->slits;

  while (slit) {
    numSlits++;
    slit = slit->next;
  }
  
  coefArray = newPixel(2*numSlits);
  readIntDescriptor(extractionTable->descs, "ESO PRO CRV POL ORD", 
                    &polOrder, NULL);
  readIntDescriptor(extractionTable->descs, "ESO PRO CRV MOD XORD", 
                    &xOrder, NULL);
  readIntDescriptor(extractionTable->descs, "ESO PRO CRV MOD YORD", 
                    &yOrder, NULL);
    
  /* note that for the moment we keep the order in X equeal to the one in Y!!*/
  crvMod = newDistModelFull(polOrder, xOrder, xOrder);
  
  for (i = 0; i <= polOrder; i++) {
    slit = extractionTable->slits;
    slitDone = 0;
    while (slit) {
      coefArray[slitDone].x = slit->maskX->data[0];
      coefArray[slitDone].y = slit->maskY->data[0];
      coefArray[slitDone].i = slit->crvPol[0]->coefs[i];
      slitDone++;
      coefArray[slitDone].x = slit->maskX->data[slit->numRows-1];
      coefArray[slitDone].y = slit->maskY->data[slit->numRows-1];
      coefArray[slitDone].i = slit->crvPol[slit->numRows-1]->coefs[i];
      slitDone++;
      slit = slit->next;
    }
    /* ugly!!!! */
    deleteDistModel2D(crvMod->coefs[i]);
      
    if (fitDistModel2D(coefArray, 2*numSlits, xOrder, 0.0, 0.0,
                       &(crvMod->coefs[i]), &rmsFit) == VM_FALSE)
      return EXIT_FAILURE;
  }

  writeCurvatureModel(&(extractionTable->descs), crvMod);

  if (grismFlag)
    writeCurvatureModel(&(grismTable->descs), crvMod);
  
  deletePixel(coefArray);
  deleteDistModelFull(crvMod);
  
  return EXIT_SUCCESS;
}
/**
 * @memo
 *   Fit an Optical Distorsion Model
 *
 * @return EXIT_SUCCESS / EXIT_FAILURE
 *
 * @param extractionTable Input Extraction Table
 * @param grismTable      Input (and perhaps output) Grism Table
 * @param grismFlag       Flag to indicate whether to update or
 *                        not the Grism Table with the new Optical 
 *                        Distortion Model.
 * @doc
 *   [MISSING]
 *
 * @author M. Scodeggio
 */

int VmSpOptModel
(VimosExtractionTable *extractionTable, VimosTable *grismTable, int grismFlag)
{
  int numSlits;
  int slitDone;
  int order;
  double rmsFit;
  
  VimosExtractionSlit *slit;
  VimosPixel *coefArray;
  VimosDistModel2D *optModX;
  VimosDistModel2D *optModY;
  int               doZero;

  char modName[]   ="VmSpOptModel";

  cpl_msg_debug(modName, "Fit curvature model");

  numSlits = 0;
  slit = extractionTable->slits;

  while (slit) {
    numSlits++;
    slit = slit->next;
  }
  
  coefArray = newPixel(2*numSlits);
  
  slit = extractionTable->slits;
  slitDone = 0;
  while (slit) {
    coefArray[slitDone].x = slit->maskX->data[0];
    coefArray[slitDone].y = slit->maskY->data[0];
    coefArray[slitDone].i = slit->ccdX->data[0];
    slitDone++;
    coefArray[slitDone].x = slit->maskX->data[slit->numRows-1];
    coefArray[slitDone].y = slit->maskY->data[slit->numRows-1];
    coefArray[slitDone].i = slit->ccdX->data[slit->numRows-1];
    slitDone++;
    slit = slit->next;
  }

  readIntDescriptor(extractionTable->descs, "ESO PRO OPT DIS XORD", 
                    &order, NULL);

  if (fitDistModel2D(coefArray, 2*numSlits, order, 
                     0.0, 0.0, &optModX, &rmsFit) == VM_FALSE)
    return EXIT_FAILURE;

  slit = extractionTable->slits;
  slitDone = 0;
  while (slit) {
    coefArray[slitDone].x = slit->maskX->data[0];
    coefArray[slitDone].y = slit->maskY->data[0];
    coefArray[slitDone].i = slit->ccdY->data[0];
    slitDone++;
    coefArray[slitDone].x = slit->maskX->data[slit->numRows-1];
    coefArray[slitDone].y = slit->maskY->data[slit->numRows-1];
    coefArray[slitDone].i = slit->ccdY->data[slit->numRows-1];
    slitDone++;
    slit = slit->next;
  }

  readIntDescriptor(extractionTable->descs, "ESO PRO OPT DIS YORD", 
                    &order, NULL);
 
  if (fitDistModel2D(coefArray, 2*numSlits, order, 
                     0.0, 0.0, &optModY, &rmsFit) == VM_FALSE)
    return EXIT_FAILURE;

  writeOptDistModel(&(extractionTable->descs), optModX, optModY);

  if (grismFlag) {
    writeOptDistModel(&(grismTable->descs), optModX, optModY);
  }
  
  if (readIntDescriptor(extractionTable->descs, 
                        pilTrnGetKeyword("ZeroOrderFlag"),
                        &doZero, NULL) == VM_FALSE) {
    cpl_msg_error(modName, "Cannot find descriptor %s",
                pilTrnGetKeyword("ZeroOrderFlag"));
    return EXIT_FAILURE;
  }

  if (doZero)
  {
    slit = extractionTable->slits;
    slitDone = 0;
    while (slit) {
      coefArray[slitDone].x = slit->maskX->data[0];
      coefArray[slitDone].y = slit->maskY->data[0];
      coefArray[slitDone].i = slit->zeroX->data[0];
      slitDone++;
      coefArray[slitDone].x = slit->maskX->data[slit->numRows-1];
      coefArray[slitDone].y = slit->maskY->data[slit->numRows-1];
      coefArray[slitDone].i = slit->zeroX->data[slit->numRows-1];
      slitDone++;
      slit = slit->next;
    }

    readIntDescriptor(extractionTable->descs, "ESO PRO ZERO XORD", 
		      &order, NULL);

    if (fitDistModel2D(coefArray, 2*numSlits, order, 
                       0.0, 0.0, &optModX, &rmsFit) == VM_FALSE)
      return EXIT_FAILURE;

    slit = extractionTable->slits;
    slitDone = 0;
    while (slit) {
      coefArray[slitDone].x = slit->maskX->data[0];
      coefArray[slitDone].y = slit->maskY->data[0];
      coefArray[slitDone].i = slit->zeroY->data[0];
      slitDone++;
      coefArray[slitDone].x = slit->maskX->data[slit->numRows-1];
      coefArray[slitDone].y = slit->maskY->data[slit->numRows-1];
      coefArray[slitDone].i = slit->zeroY->data[slit->numRows-1];
      slitDone++;
      slit = slit->next;
    }

    readIntDescriptor(extractionTable->descs, "ESO PRO ZERO YORD", 
		      &order, NULL);
 
    if (fitDistModel2D(coefArray, 2*numSlits, order, 
                       0.0, 0.0, &optModY, &rmsFit) == VM_FALSE)
      return EXIT_FAILURE;
    
    writeContaminationModel(&(extractionTable->descs), optModX, optModY);

    if (grismFlag) {
      writeContaminationModel(&(grismTable->descs), optModX, optModY);
    }
  }

  deletePixel(coefArray);
  deleteDistModel2D(optModX);
  deleteDistModel2D(optModY);
  
  return EXIT_SUCCESS;
}
/**@}*/