File: flames_preordpos.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 (638 lines) | stat: -rw-r--r-- 23,949 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
/* @(#)flames_preordpos    */
/*===========================================================================
  Copyright (C) 2001 European Southern Observatory (ESO)

  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., 675 Massachusetss Ave, Cambridge, 
  MA 02139, USA.

  Corresponding concerning ESO-MIDAS should be addressed as follows:
    Internet e-mail: midas@eso.org
    Postal address: European Southern Observatory
            Data Management Division 
            Karl-Schwarzschild-Strasse 2
            D 85748 Garching bei Muenchen 
            GERMANY
===========================================================================*/
/* Program  : flames_preordpos.c                                           */
/* Author   : I. Porceddu  -  ITAL_FLAMES Consortium                       */
/* Date     :                                                              */
/*                                                                         */
/* Purpose  :                                                              */
/* Compute the y values at the middle x coordinate. First guess values are 
   being taken from the UVES physical model output 
   (table o[setup][lightpath][u/l] --> e.g. o580REDL                       */

/* Input:                                                                   */
/*      ORDTAB     Name of input table generated by uves_calmap (see above) */
/*      DEFPOL     Degree in x and m domain as computed during format check 
               loop                                                     */
/*      COEFFD     A descriptor containing the coeffs of the bivariate poly 
               as extracted from the format check application           */
/*      ECHORD     Number of the first guess detected orders                */

/* Output:                                                                  */
/*      LTAB       middummd.tbl                                            */ 
/*                                                                         */
/* DRS Functions called:                                                   */
/* none                                                                    */ 
/*                                                                         */ 
/* Pseudocode:                                                             */
/* Missing                                                                 */ 
/*                                                                         */ 
/* Version  :                                                              */
/* Last modification date: 2002/08/05                                      */
/* Who     When        Why                Where                            */
/* AMo     02-08-05   Add header         header                            */
/*-------------------------------------------------------------------------*/


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

#include <flames_preordpos.h>
#include <flames_getordpos.h>

#include <flames_midas_tblsys.h>
#include <flames_midas_tblerr.h>
#include <flames_midas_tbldef.h>
#include <flames_midas_macrogen.h>
#include <flames_midas_atype.h>
#include <flames_midas_def.h>
#include <flames_uves.h>
#include <flames_newmatrix.h>
#include <uves_msg.h>

#include <cpl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Prototypes of "local" functions here */

static
flames_err initorderpos(orderpos *orderstruct, int32_t xdeg, 
                        int32_t mdeg, int maxfibres);

static
flames_err free_orderpos(orderpos **orderstruct);

int flames_preordpos(const char *GTAB,
                     const int *MAXFIBRES,
                     const cpl_frameset *CATNAME,
                     const int *MAXEXTEND,
                     const char *LTAB,
                     double *TAB_IO_YSHIFT)
{
    flames_err status;
    char ordtab[60],outtab[60],templatename[CATREC_LEN+4];
    const cpl_frameset *catname;
    char output[200];
    int orderpixel_deg[2] = {0,0};
    int xdeg = 0;
    int mdeg = 0;
    int upointer = 0;
    int null = 0;
    int no_orders=0;
    int read_elements;
    int i=0, j=0, maxextend=0, maxrows=0;
    int actvals=0;
    int TINid=0;
    int TOUTid=0;
    int colnum[4]={0,0,0,0};
    int noent=0;
    int last=0;
    int k=0;
    double **pcoeffd = 0;
    double yvalue = 0;
    double **central_y;
    double half_x[3] = {0,0,0};
    int templateid = 0;
    int naxis = 0;
    int unit = 0;
    double start[2] = {0,0};
    double step[2] = {0,0};
    int npix[2] = {0,0};
    double start2[2] = {0,0};
    double step2[2] = {0,0};
    int npix2[2] = {0,0};
    float fbuf[2] = {0,0};
    int firstorder = 0;
    int lastorder = 0;
    char chipchoice=0;
    char chipchoice2=0;
    char desctype=0;
    int noelem=0;
    int bytelem=0;
    int minorder=0;
    int maxfibres=0;
    double dbuf=0;
    int ibuf=0;
    double startyshift=0;
    orderpos *orderstruct=0;

    memset(ordtab, 0, 60);
    memset(outtab, 0, 60);
    memset(templatename, 0, CATREC_LEN+4);
    memset(output, 0, 200);

    orderstruct=calloc(1, sizeof(orderpos));

    /* === interface to MIDAS ================================================== */

    /*Let's initialize the MIDAS environment */
    SCSPRO(__func__);


    /* The ORDTAB keyword contains the name of the actual order table
     as written by uves_calmap */
    SCKGETC(GTAB,1,60,&actvals,ordtab);

    uves_msg_debug("GTAB = %s", GTAB);

    /* initialise MAXFIBRES from keyword */
    if (SCKRDI(MAXFIBRES, 1, 1, &actvals, &maxfibres, &unit, &null) != 0) {
        /* problems reading MAXFIBRES */
        SCTPUT("Error reading the MAXFIBRES keyword");
        free(orderstruct);
        return flames_midas_fail();
    }

    /* Let's open the order table file */
    TCTOPN(ordtab,F_IO_MODE,&TINid);

    /* the 2nd element of the COEFFI descriptor of the table contains the
     number of independent variables in the polynomial fit made on the 
     "first guess" order table, and it must be 2 */
    if(SCDRDI(TINid, "COEFFI", 2, 1, &actvals, orderpixel_deg, &unit, &null)!=0){
        sprintf(output, "Error: the guess polynomial fit has %d independent \
variables", orderpixel_deg[0]);
        SCTPUT(output);
        free(orderstruct);
        return flames_midas_fail();
    }
    /* The 6th and 7th elements of the COEFFI descriptor of the table contain
     the degrees of the x (pixel) and m (order) polynomial degrees. */
    if(SCDRDI(TINid, "COEFFI", 6, 2, &actvals, orderpixel_deg, &unit, &null)!=0){
        SCTPUT("Error reading the degrees of the guess polynomial fit");
        free(orderstruct);
        return flames_midas_fail();
    }
    xdeg = orderpixel_deg[0];
    mdeg = orderpixel_deg[1];

    /* Read the name of the FF frames catalog */
    SCKGETC_fs(CATNAME,1,30,&actvals,&catname);

    /* read the number of entries in the catalog */
    SCCSHO(catname,&noent,&last);
    if (noent<1) {
        /* less than 1 entry in the catalog, bail out */
        SCTPUT("Error: no entries in supplied image catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    /* open the first frame in the catalog */
    SCCFND(catname, 1, templatename);
    if (SCFINF(templatename,99,0)!=0) {
        /* the first file in the catalog does not exist, report and exit */
        SCTPUT("Error: the first file in the supplied catalog does not exist");
        free(orderstruct);
        return flames_midas_fail();
    }
    if (SCFOPN(templatename, FLAMESDATATYPE, 0, F_IMA_TYPE, &templateid)!=0) {
        /* error opening template file */
        SCTPUT("Error opening first file in the supplied catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    if (SCDRDI(templateid, "NAXIS", 1, 1, &actvals, &naxis, &unit, &null)!=0) {
        /* error reading NAXIS from template file */
        SCTPUT("Error reading NAXIS from first file in the supplied catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    if (naxis!=2) {
        SCTPUT("Error: NAXIS!=2 in first file in the supplied catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    if (SCDRDD(templateid, "START", 1, 2, &actvals, start, &unit, &null)!=0) {
        SCTPUT("Error reading START from first file in the supplied catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    if (SCDRDD(templateid, "STEP", 1, 2, &actvals, step, &unit, &null)!=0) {
        SCTPUT("Error reading STEP from first file in the supplied catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    if (SCDRDI(templateid, "NPIX", 1, 2, &actvals, npix, &unit, &null)!=0) {
        SCTPUT("Error reading NPIX from first file in the supplied catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    if (SCDRDC(templateid, "CHIPCHOICE", 1, 1, 1, &actvals, &chipchoice,
                    &unit, &null)!=0) {
        SCTPUT("Error reading CHIPCHOICE from first file in the supplied catalog");
        free(orderstruct);
        return flames_midas_fail();
    }
    SCFCLO(templateid);


    for (i=2; i<=noent; i++) {
        SCCFND(catname,i,templatename);
        if (SCFINF(templatename,99,0)!=0) {
            /* the file in the catalog does not exist, report and exit */
            sprintf(output,
                            "Error: file %d in the supplied catalog does not exist",
                            i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        if (SCFOPN(templatename, FLAMESDATATYPE, 0, F_IMA_TYPE, &templateid)!=0) {
            /* error opening template file */
            sprintf(output,
                            "Error opening file %d in the supplied catalog",
                            i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        if (SCDRDI(templateid, "NAXIS", 1, 1, &actvals, &naxis, &unit, &null)!=0) {
            /* error reading NAXIS from template file */
            sprintf(output,
                            "Error reading NAXIS from file %d in the supplied catalog",
                            i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        if (naxis!=2) {
            sprintf(output,"Error: NAXIS!=2 in file %d in the supplied catalog",i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        if (SCDRDD(templateid, "START", 1, 2, &actvals, start2, &unit, &null)!=0) {
            sprintf(output,
                            "Error reading START from file %d in the supplied catalog",i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        if (SCDRDD(templateid, "STEP", 1, 2, &actvals, step2, &unit, &null)!=0) {
            sprintf(output,
                            "Error reading STEP from file %d in the supplied catalog",i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        if (SCDRDI(templateid, "NPIX", 1, 2, &actvals, npix2, &unit, &null)!=0) {
            sprintf(output,
                            "Error reading NPIX from file %d in the supplied catalog",i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        if (SCDRDC(templateid, "CHIPCHOICE", 1, 1, 1, &actvals, &chipchoice2,
                        &unit, &null)!=0) {
            sprintf(output, "Error reading CHIPCHOICE from file %d in the \
supplied catalog",i);
            SCTPUT(output);
            free(orderstruct);
            return flames_midas_fail();
        }
        SCFCLO(templateid);


        /* check that sizes actually match among frames in the catalog */
        if ((start2[0] != start[0]) || (start2[1] != start[1])) {
            sprintf(output, "START mismatch between file 1 (START=%f,%f) and file %d (START=%f,%f) in the \
supplied catalog",
start[0], start[1], i,
start2[0], start2[1]);
            SCTPUT(output);
            free_orderpos(&orderstruct);
            return flames_midas_fail();
        }
        if ((step2[0] != step[0]) || (step2[1] != step[1])) {
            sprintf(output, "STEP mismatch between file 1 and file %d in the \
supplied catalog",i);
            SCTPUT(output);
            free_orderpos(&orderstruct);
            return flames_midas_fail();
        }
        if ((npix2[0] != npix[0]) || (npix2[1] != npix[1])) {
            sprintf(output, "NPIX mismatch between file 1 and file %d in the \
supplied catalog",i);
            SCTPUT(output);
            free_orderpos(&orderstruct);
            return flames_midas_fail();
        }
        if (chipchoice2 != chipchoice) {
            sprintf(output, "CHIPCHOICE mismatch between file 1 (%c) and file %d (%c) in the \
supplied catalog", chipchoice, i, chipchoice2);
            SCTPUT(output);
            free_orderpos(&orderstruct);
            return flames_midas_fail();
        }
    }

    /* now we read sizes from the frames, we do not need to read them from
     guess order table descriptors, just set them */

    /* We want to calculate for each order the y values corresponding
     to the CCD x central values */
    half_x[0] = start[0]+((double)(npix[0]-1)*step[0]*.4);
    half_x[1] = start[0]+((double)(npix[0]-1)*step[0]*.5);
    half_x[2] = start[0]+((double)(npix[0]-1)*step[0]*.6);
    /* printf("start[0]=%f step[0]=%f\n",start[0],step[0]); */
    /* printf("npix[0]=%f (npix[0]-1)=%f\n",npix[0], (float)(npix[0]-1));
  printf("(double)(npix[0]-1)*step[0]*.4=%g
          start[0]+((double)(npix[0]-1)*step[0]*.4=%g)\n",
     (double)(npix[0]-1)*step[0]*.4,
           start[0]+((double)(npix[0]-1)*step[0]*.4));

  printf("half_x: 0=%f, 1=%f, 2=%f\n",half_x[0],half_x[1],half_x[2]);
     */
    /* The MAXEXTEND keyword provides the maximum no. of orders to be
     considered above and below the ones detected in the format check */
    SCKRDI(MAXEXTEND,1,1,&actvals,&maxextend,&upointer,&null);

    if (maxextend<0) maxextend=0;

    /* The LTAB keyword does provide the actual name for output table */
    SCKGETC(LTAB,1,14,&actvals,outtab);

    /* We compute the number of coeffs we should extract from COEFFD */
    read_elements = (orderpixel_deg[0]+1)*(orderpixel_deg[1]+1);

    /* We allocate the memory for a data frame which will host the poly coeff */
    pcoeffd=dmatrix(0,orderpixel_deg[1],0,orderpixel_deg[0]);
    for (i=0; i<=orderpixel_deg[1]; i++) {
        for (k=0; k<=orderpixel_deg[0]; k++) {
            pcoeffd[i][k] = 0;
        }
    }

    /* We extract the coefficients from the COEFFD descriptor -
     RUNNING VERSION */
    if(SCDRDD(TINid,"COEFFD",1,read_elements,&actvals,pcoeffd[0],&upointer,&null) != 0) {
        sprintf(output,
                        "Error reading COEFFD from %s", GTAB);
        SCTPUT(output);
        free_orderpos(&orderstruct);
        return flames_midas_fail();
    }


    /* read the first and last found order from COEFFR */
    if (SCDRDR(TINid,"COEFFR",3,2,&actvals,fbuf,&upointer,&null) != 0) {
        sprintf(output,
                        "Error reading COEFFR from %s", GTAB);
        SCTPUT(output);
        free_orderpos(&orderstruct);
        return flames_midas_fail();
    }

    firstorder = (int) fbuf[0];
    lastorder = (int) fbuf[1];

    no_orders = lastorder-firstorder+1;


    SCDFND(TINid, "TAB_IN_OUT_OSHIFT", &desctype, &noelem, &bytelem);
    switch (desctype) {
    case ' ':
        /* the TAB_IN_OUT_OSHIFT descriptor is undefined, set minorder to zero */
        minorder = 0;
        break;
    case 'I':
        /* the TAB_IN_OUT_OSHIFT descriptor is defined and integer, use it */
        if (noelem > 0) {
            /* read the value anyway */
            SCDRDI(TINid, "TAB_IN_OUT_OSHIFT", 1, 1, &actvals, &minorder, &unit,
                            &null);
        }
        else {
            minorder = 0;
        }
        break;
    case 'R':
        /* the TAB_IN_OUT_OSHIFT descriptor is defined but of the wrong numerical
       type */
        SCTPUT("Warning: TAB_IN_OUT_OSHIFT should be an integer");
        SCTPUT("taking the value anyway, but deleting the old descriptor");
        if (noelem > 0) {
            /* read the value anyway */
            SCDRDR(TINid, "TAB_IN_OUT_OSHIFT", 1, 1, &actvals, fbuf, &unit, &null);
            minorder = (int) (*fbuf);
        }
        else {
            minorder = 0;
        }
        /* delete the existing descriptor anyway */
        SCDDEL(TINid, "TAB_IN_OUT_OSHIFT");
        break;
    case 'D':
        /* the TAB_IN_OUT_OSHIFT descriptor is defined but of the wrong numerical
       type */
        SCTPUT("Warning: TAB_IN_OUT_OSHIFT should be an integer");
        SCTPUT("taking the value anyway, but deleting the old descriptor");
        if (noelem > 0) {
            /* read the value anyway */
            SCDRDD(TINid, "TAB_IN_OUT_OSHIFT", 1, 1, &actvals, &dbuf, &unit, &null);
            minorder = (int) dbuf;
        }
        else {
            minorder = 0;
        }
        /* delete the existing descriptor anyway */
        SCDDEL(TINid, "TAB_IN_OUT_OSHIFT");
        break;
    default:
        /* the TAB_IN_OUT_OSHIFT descriptor is defined but not numeric */
        SCTPUT("Warning: TAB_IN_OUT_OSHIFT not found, should be an integer");
        SCTPUT("resetting to zero and deleting the non-numeric descriptor");
        minorder = 0;
        SCDDEL(TINid, "TAB_IN_OUT_OSHIFT");
        break;
    }

    SCDFND(TINid, "TAB_IN_OUT_YSHIFT", &desctype, &noelem, &bytelem);

    switch (desctype) {
    case ' ':
        /* the TAB_IO_YSHIFT descriptor is undefined, set startyshift to
       zero */
        startyshift = 0;
        break;
    case 'I':
        /* the TAB_IO_YSHIFT descriptor is defined but of the wrong
       numerical type */
        SCTPUT("Warning: TAB_IN_OUT_YSHIFT should be a double");
        SCTPUT("taking the value anyway, but deleting the old descriptor");
        if (noelem > 0) {
            /* read the value anyway */
            SCDRDI(TINid, "TAB_IN_OUT_YSHIFT", 1, 1, &actvals, &ibuf, &unit,
                            &null);
            startyshift = (double) ibuf;
        }
        else {
            startyshift = 0;
        }
        /* delete the existing descriptor anyway */
        SCDDEL(TINid, "TAB_IN_OUT_YSHIFT");
        break;
    case 'R':
        /* the TAB_IO_YSHIFT descriptor is defined but of the wrong numerical
       type */
        SCTPUT("Warning: TAB_IN_OUT_YSHIFT should be a double");
        SCTPUT("taking the value anyway, but deleting the old descriptor");
        if (noelem > 0) {
            /* read the value anyway */
            SCDRDR(TINid, "TAB_IN_OUT_YSHIFT", 1, 1, &actvals, fbuf, &unit, &null);
            startyshift = (int) (*fbuf);
        }
        else {
            startyshift = 0;
        }
        /* delete the existing descriptor anyway */
        SCDDEL(TINid, "TAB_IN_OUT_YSHIFT");
        break;
    case 'D':
        /* the TAB_IN_OUT_OSHIFT descriptor is defined and double, use it */
        if (noelem > 0) {
            /* read the value anyway */
            SCDRDD(TINid, "TAB_IN_OUT_YSHIFT", 1, 1, &actvals, &startyshift,
                            &unit, &null);
        }
        else {
            startyshift = 0;
        }
        break;
    default:
        /* the TAB_IO_YSHIFT descriptor is defined but not numeric */
        SCTPUT("Warning: TAB_IN_OUT_YSHIFT not found, should be an integer");
        SCTPUT("resetting to zero and deleting the non-numeric descriptor");
        startyshift = 0;
        SCDDEL(TINid, "TAB_IN_OUT_YSHIFT");
        break;
    }

    /* write the original TAB_IO_YSHIFT in a (local) keyword */
    SCKWRD(TAB_IO_YSHIFT, &startyshift, 1, 1, &unit);

    /* close the ORDPOS table */
    TCTCLO(TINid);

    /* Initializing the orderstruct structure */
    status = initorderpos(orderstruct, xdeg, mdeg,maxfibres);

    /* Filling up the orderpol element with the actual poly coefficients */
    orderstruct->orderpol = pcoeffd;

    /* Creating a new table */
    if ((maxrows=no_orders+2*maxextend)<40) maxrows=40;
    central_y = dmatrix(0,2,0,maxrows);
    /* jmlarsen: Use F_O_MODE for new table
     old code: TCTINI(outtab,F_IO_MODE,maxrows,&TOUTid); */
    TCTINI(outtab,F_O_MODE,maxrows,&TOUTid);

    /* Creating new columns */
    TCCINI(TOUTid, D_I4_FORMAT, 1, "I6", " ", "ORDER", &colnum[0]);
    TCCINI(TOUTid, D_R8_FORMAT, 1, "F8.2", " ", "YQ1FTCK", &colnum[1]);
    TCCINI(TOUTid, D_R8_FORMAT, 1, "F8.2", " ", "YMIDFTCK", &colnum[2]);
    TCCINI(TOUTid, D_R8_FORMAT, 1, "F8.2", " ", "YQ3FTCK", &colnum[3]);

    for (j=0; j<=2; j++) {
        for (i=firstorder-maxextend;i<=(lastorder+maxextend);i++) {
            /* if (j==0) printf("i=%d half_x=%f\n",i,half_x[j]); */
            if ((status=get_ordpos(orderstruct,(double)i,half_x[j],&yvalue))
                            != NOERR) return status;
            central_y[j][i+maxextend-firstorder] = yvalue;
        }
    }
    /* Writing table values */
    for (i=0; i<=(no_orders+2*maxextend-1);i++) {
        j = i+firstorder-maxextend+minorder;
        TCEWRI(TOUTid, i+1, colnum[0], &j);
        TCEWRD(TOUTid, i+1, colnum[1], &central_y[0][i]);
        /* printf("central_y[0][i]=%f\n",central_y[0][i]); */
        TCEWRD(TOUTid, i+1, colnum[2], &central_y[1][i]);
        /* printf("central_y[1][i]=%f\n",central_y[1][i]); */
        TCEWRD(TOUTid, i+1, colnum[3], &central_y[2][i]);
        /* printf("central_y[2][i]=%f\n",central_y[2][i]); */
    }

    /* Write also the START, STEP, NPIX and CHIPCHOICE variables as
     descriptors, so that we do not have to look them up any more later */
    SCDWRD(TOUTid, "REFSTART", start, 1, 2, &unit);
    SCDWRD(TOUTid, "REFSTEP", step, 1, 2, &unit);
    SCDWRI(TOUTid, "REFNPIX", npix, 1, 2, &unit);
    SCDWRC(TOUTid, "CHIPCHOICE", 1, &chipchoice, 1, 1, &unit);

    TCTCLO(TOUTid);

    //free(orderstruct);
    free_orderpos(&orderstruct);
    free_dmatrix(central_y,0,2,0,maxrows);
    free_dmatrix(pcoeffd,0,orderpixel_deg[1],0,orderpixel_deg[0]);
    return SCSEPI();

}

static
flames_err initorderpos(orderpos *orderstruct, int32_t xdeg, 
                        int32_t mdeg, int maxfibres)
{
    /*  orderstruct->orderpol = dmatrix(0, mdeg, 0, xdeg);*/
    orderstruct->xdegree = xdeg;
    orderstruct->mdegree = mdeg;

    /*
     TO BE CHECKED 
     We do not the name of the keyword which will contain the number of used 
     fibres. 
     So we suppose to have 9 (nine) fibres, and 0 is the first one:
     Be warned about it: firsfibreindex must be in agreement with this last 
     value!!!
     */
    orderstruct->maxfibres = maxfibres;
    orderstruct->fibrepos = dvector(0,maxfibres-1);
    orderstruct->fibremask = ivector(0,maxfibres-1);
    return(NOERR);
}


static
flames_err free_orderpos(orderpos **orderstruct)
{

    free_dvector((*orderstruct)->fibrepos,0,(*orderstruct)->maxfibres);
    free_ivector((*orderstruct)->fibremask,0,(*orderstruct)->maxfibres);
    free(*orderstruct);
    *orderstruct=NULL;

    return(NOERR);
}