File: flames_writeallff.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 (737 lines) | stat: -rw-r--r-- 29,067 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
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*===========================================================================
  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  : writeallff.c                                                 */
/* Author   : G. Mulas  -  ITAL_FLAMES Consortium                          */
/* Date     :                                                              */
/*                                                                         */
/* Purpose  : Missing                                                      */
/*                                                                         */
/*                                                                         */
/* Input:  see interface                                                   */ 
/*                                                                      */
/* Output:                                                              */
/*                                                                         */
/* 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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <flames_writeallff.h>
#include <flames_midas_def.h>
#include <flames_uves.h>
#include <flames_newmatrix.h>




flames_err writeallff(allflats *myflats, 
                      const char *basename, 
                      cpl_frameset ** catname)
{

    int status=0;
    int unit=0;
    int null=0;
    int oldfileid=0;
    int dataid=0;
    int sigmaid=0;
    int badid=0;
    int commid=0;
    int normid=0;
    int nsigid=0;
    int32_t iframe=0;
    int32_t ifibre=0;
    int32_t ****longarray=0;
    char filename[CATREC_LEN+1];
    char dummychar[CATREC_LEN+1];
    float lhcuts[4]={0,0,0,0};
    int ibuf[4]={0,0,0,0};
    int *fibres=0;
    double dbuf[4]={0,0,0,0};
    char blank[]="     ";
    char cunit[81];

    frame_data *fdvecbuf1=0;
    frame_mask *fmvecbuf1=0;
    int32_t *lvecbuf1=0;
    int32_t iyixend=0;
    int32_t iorderifibreixend=0;
    int32_t iiorderifibreixend=0;
    int32_t iyixindex=0;
    int32_t iorderifibreixindex=0;
    int32_t iiorderifibreixindex=0;


    singleflat *currentflat=0;

    memset(filename, '\0', CATREC_LEN+1);
    memset(dummychar, '\0', CATREC_LEN+1);
    memset(cunit, '\0', 81);

    iyixend = myflats->subrows*myflats->subcols-1;
    iorderifibreixend = ((myflats->lastorder-myflats->firstorder+1)*
                    myflats->maxfibres*myflats->subcols)-1;
    iiorderifibreixend = (3*(myflats->lastorder-myflats->firstorder+1)*
                    myflats->maxfibres*myflats->subcols)-1;

    fibres = ivector(0,(int32_t)myflats->maxfibres-1);
    /* create the empty catalog catname */
    if ((status = SCCCRE(catname, F_IMA_TYPE, 0)) != 0) {
        /* could not create the catalog: complain... */
        return(MAREMMA);
    }

    /* run a loop over the flat frames */
    for(iframe=0; iframe<=myflats->nflats-1; iframe++) {
        currentflat = myflats->flatdata+iframe;
        /* open the old frame from which the descriptors must be copied */
        if ((status = SCFOPN(currentflat->framename, FLAMESDATATYPE,
                        0, F_IMA_TYPE, &oldfileid)) != 0) {
            /* could not open the file */
            return(MAREMMA);
        }

        /* data frame */
        /* create the filename */
        sprintf(filename,"%s_data%02d.fits",basename,iframe+1);
        /* create the normalised data frame on disk */
        if ((status = SCFCRE(filename, FLAMESDATATYPE, F_O_MODE, F_IMA_TYPE,
                        myflats->subrows*myflats->subcols, &dataid))
                        != 0) {
            /* could not create the file */
            return(MAREMMA);
        }
        /* copy the descriptors from the old frame to the normalised frame */
        if ((status = SCDCOP(oldfileid, dataid, 1)) != 0) {
            /* I could not copy the descriptors: complain... */
            return(MAREMMA);
        }
        /* find out and write LHCUTS */
        fdvecbuf1 = currentflat->data[0];
        lhcuts[0] = lhcuts[1] = 0;
        lhcuts[2] = lhcuts[3] = (float) fdvecbuf1[0];
        for (iyixindex=1; iyixindex<=iyixend; iyixindex++) {
            if (fdvecbuf1[iyixindex] < lhcuts[2]) {
                lhcuts[2] = (float) fdvecbuf1[iyixindex];
            }
            if (fdvecbuf1[iyixindex] > lhcuts[3]) {
                lhcuts[3] = (float) fdvecbuf1[iyixindex];
            }
        }
        if ((status = SCDWRR(dataid, "LHCUTS", lhcuts, 1, 4, &unit)) != 0) {
            /* error writing LHCUTS */
            return(MAREMMA);
        }
        /* write the IDENT descriptor */
        if ((status = SCDWRC(dataid, "IDENT", 1, "Cleaned fibre FF", 1,
                        CATREC_LEN+1, &null)) != 0) {
            /* I could not write the descriptor: complain... */
            return(MAREMMA);
        }
        /* add the name of this frame to the catalog */
        if ((status = SCCADD(*catname, filename, blank)) != 0) {
            /* coud not add filename to the catalog: complain */
            return(MAREMMA);
        }
        /* actually write the data frame to the new file*/
        if ((status = SCFPUT(dataid, 1, myflats->subrows*myflats->subcols,
                        (char *)fdvecbuf1)) != 0) {
            /* I could not write the data to disk: complain... */
            return(MAREMMA);
        }
        ibuf[0] = myflats->flatdata[iframe].numfibres;
        /* write the descriptors which are specific to each fibre FF frame */
        if ((status = SCDWRD(dataid, "YSHIFT", &(currentflat->yshift),
                        1, 1, &unit)) != 0) {
            /* error writing YSHIFT */
            return(MAREMMA);
        }
        if ((status = SCDWRI(dataid, "NUMFIBRES", ibuf, 1, 1, &unit)) != 0) {
            /* error writing NUMFIBRES */
            return(MAREMMA);
        }
        for(ifibre=0; ifibre<=(currentflat->numfibres-1); ifibre++){
            fibres[ifibre] = (int) currentflat->fibres[ifibre];
        }
        for(; ifibre<=myflats->maxfibres-1; ifibre++){
            fibres[ifibre] = 0;
        }
        if ((status = SCDWRI(dataid, "FIBRES", fibres, 1, myflats->maxfibres,
                        &unit)) != 0) {
            /* error writing NUMFIBRES */
            return(MAREMMA);
        }

        /* sigma frame */
        /* create the filename */
        sprintf(filename,"%s_sigma%02d.fits",basename,iframe+1);
        /* create the new sigma frame on disk */
        if ((status = SCFCRE(filename, FLAMESDATATYPE, F_O_MODE, F_IMA_TYPE,
                        myflats->subrows*myflats->subcols, &sigmaid))
                        != 0) {
            /* could not create the file */
            return(MAREMMA);
        }
        /* write the name of the sigma file as a descriptor of the data frame */
        if ((status = SCDWRC(dataid, "SIGMAFILE", 1, filename, 1, CATREC_LEN+1,
                        &null)) != 0) {
            /* I could not write the descriptor: complain... */
            return(MAREMMA);
        }
        /* copy the descriptors from the old frame to the sigma frame */
        if ((status = SCDCOP(oldfileid, sigmaid, 1)) != 0) {
            /* I could not copy the descriptors: complain... */
            return(MAREMMA);
        }
        /* find out and write LHCUTS */
        fdvecbuf1 = currentflat->sigma[0];
        lhcuts[0] = lhcuts[1] = 0;
        lhcuts[2] = lhcuts[3] = (float) fdvecbuf1[0];
        for (iyixindex=1; iyixindex<=iyixend; iyixindex++) {
            if (fdvecbuf1[iyixindex] < lhcuts[2]) {
                lhcuts[2] = (float) fdvecbuf1[iyixindex];
            }
            if (fdvecbuf1[iyixindex] > lhcuts[3]) {
                lhcuts[3] = (float) fdvecbuf1[iyixindex];
            }
        }
        if ((status = SCDWRR(sigmaid, "LHCUTS", lhcuts, 1, 4, &unit)) != 0) {
            /* error writing LHCUTS */
            return(MAREMMA);
        }
        /* actually write the sigma frame to the new file*/
        if ((status = SCFPUT(sigmaid, 1, myflats->subrows*myflats->subcols,
                        (char*) fdvecbuf1)) != 0) {
            /* I could not write the data to disk: complain... */
            return(MAREMMA);
        }
        /* close the sigma frame */
        if ((status = SCFCLO(sigmaid)) != 0) {
            /* I could not close the data file: complain... */
            return(MAREMMA);
        }

        /* badpixel frame */
        /* create the filename */
        sprintf(filename,"%s_badpixel%02d.fits",basename,iframe+1);
        /* create the badpixel frame on disk */
        if ((status = SCFCRE(filename, FLAMESMASKTYPE, F_O_MODE, F_IMA_TYPE,
                        myflats->subrows*myflats->subcols, &badid))
                        != 0) {
            /* could not create the file */
            return(MAREMMA);
        }
        /* write the name of the badpixel file as a descriptor of the data frame */
        if ((status = SCDWRC(dataid, "BADPIXELFILE", 1, filename, 1, CATREC_LEN+1,
                        &null)) != 0) {
            /* I could not write the descriptor: complain... */
            return(MAREMMA);
        }
        /* copy the descriptors from the old frame to the badpixel frame */
        if ((status = SCDCOP(oldfileid, badid, 1)) != 0) {
            /* I could not copy the descriptors: complain... */
            return(MAREMMA);
        }
        /* find out and write LHCUTS */
        lhcuts[0] = lhcuts[1] = lhcuts[2] = 0;
        lhcuts[3] = 1;
        if ((status = SCDWRR(badid, "LHCUTS", lhcuts, 1, 4, &unit)) != 0) {
            /* error writing LHCUTS */
            return(MAREMMA);
        }
        /* actually write the badpixel frame to the new file*/
        if ((status = SCFPUT(badid, 1, myflats->subrows*myflats->subcols,
                        currentflat->badpixel[0])) != 0) {
            /* I could not write the data to disk: complain... */
            return(MAREMMA);
        }
        /* close the badpixel frame */
        if ((status = SCFCLO(badid)) != 0) {
            /* I could not close the data file: complain... */
            return(MAREMMA);
        }

        /* additional data should be already present as descriptors of the
       data frame, since all old descriptors were copied */

        /* close the data frame */
        if ((status = SCFCLO(dataid)) != 0) {
            /* I could not close the data file: complain... */
            return(MAREMMA);
        }

        /* close the old frame */
        if ((status = SCFCLO(oldfileid)) != 0) {
            /* I could not close the old file: complain... */
            return(MAREMMA);
        }
    }

    /* now proceed to the data which are not in the slit member of the
     structure */
    /* I will need to separate the normalisation factors, as they are of
     double type*/
    sprintf(filename,"%s_norm.fits",basename);
    /* create the frame on disk */
    if ((status =
                    SCFCRE(filename, FLAMESDATATYPE, F_O_MODE, F_IMA_TYPE,
                                    (myflats->lastorder-myflats->firstorder+1)*myflats->maxfibres*
                                    myflats->subcols, &normid)) != 0) {
        /* could not create the file */
        return(MAREMMA);
    }
    /* write the 3D array to file */
    fdvecbuf1 = myflats->normfactors[0][0];
    if ((status =
                    SCFPUT(normid, 1,
                                    (myflats->lastorder-myflats->firstorder+1)*myflats->maxfibres*
                                    myflats->subcols,
                                    (char *) fdvecbuf1)) != 0) {
        /* I could not write this big frame to disk: complain... */
        return(MAREMMA);
    }
    /* write the standard descriptors */
    ibuf[0] = 3;
    if ((status = SCDWRI(normid, "NAXIS", ibuf, 1, 1, &unit)) != 0) {
        /* error writing NAXIS */
        return(MAREMMA);
    }
    ibuf[0] = (int) myflats->subcols;
    ibuf[1] = (int) myflats->maxfibres;
    ibuf[2] = (int) (myflats->lastorder-myflats->firstorder+1);
    if ((status = SCDWRI(normid, "NPIX", ibuf, 1, 3, &unit)) != 0) {
        /* error writing NPIX */
        return(MAREMMA);
    }
    dbuf[0] = myflats->substartx;
    dbuf[1] = (double) 0;
    dbuf[2] = (double) myflats->firstorder;
    if ((status = SCDWRD(normid, "START", dbuf, 1, 3, &unit)) != 0) {
        /* error writing START */
        return(MAREMMA);
    }
    dbuf[0] = myflats->substepx;
    dbuf[1] = 1;
    dbuf[2] = 1;
    if ((status = SCDWRD(normid, "STEP", dbuf, 1, 3, &unit)) != 0) {
        /* error writing START */
        return(MAREMMA);
    }
    /* initialise CUNIT an LHCUTS, otherwise MIDAS will complain*/
    memset(cunit, ' ', 16);
    cunit[16] = '\0';
    strncat(cunit, "PIXEL           ", 16);
    strncat(cunit, "FIBRE           ", 16);
    strncat(cunit, "ORDER           ", 16);
    /* write the CUNIT descriptor */
    if ((status = SCDWRC(normid, "CUNIT", 1, cunit, 1, 64, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    lhcuts[0] = lhcuts[1] = 0;
    lhcuts[2] = lhcuts[3] = fdvecbuf1[0];
    for (iorderifibreixindex=1; iorderifibreixindex<=iorderifibreixend;
                    iorderifibreixindex++) {
        if (fdvecbuf1[iorderifibreixindex]<lhcuts[2]) {
            lhcuts[2] = fdvecbuf1[iorderifibreixindex];
        }
        if (fdvecbuf1[iorderifibreixindex]>lhcuts[3]) {
            lhcuts[3] = fdvecbuf1[iorderifibreixindex];
        }
    }
    /* write the LHCUTS descriptor */
    if ((status = SCDWRR(normid, "LHCUTS", lhcuts, 1, 4, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    /* write the IDENT descriptor */
    if ((status = SCDWRC(normid, "IDENT", 1, "Normalisation data for fibre FF",
                    1, CATREC_LEN+1, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    /* write the name of this file to the output catalog */
    if ((status = SCCADD(*catname, filename, blank)) != 0) {
        /* coud not add filename to the catalog: complain */
        return(MAREMMA);
    }

    /* close this frame */
    if ((status = SCFCLO(normid)) != 0) {
        /* error closing common frame: complain */
        return(MAREMMA);
    }

    /* now the normsigma factors, this is almost the same as the normfactors */
    sprintf(filename,"%s_nsigma.fits",basename);
    /* create the frame on disk */
    if ((status =
                    SCFCRE(filename, FLAMESDATATYPE, F_O_MODE, F_IMA_TYPE,
                                    (myflats->lastorder-myflats->firstorder+1)*myflats->maxfibres*
                                    myflats->subcols, &nsigid)) != 0) {
        /* could not create the file */
        return(MAREMMA);
    }
    fdvecbuf1 = myflats->normsigmas[0][0];
    /* write the 3D array to file */
    if ((status = SCFPUT(nsigid, 1, (myflats->lastorder-myflats->firstorder+1)*
                    myflats->maxfibres*myflats->subcols,
                    (char *) fdvecbuf1)) != 0) {
        /* I could not write this big frame to disk: complain... */
        return(MAREMMA);
    }
    /* write the standard descriptors */
    ibuf[0] = 3;
    if ((status = SCDWRI(nsigid, "NAXIS", ibuf, 1, 1, &unit)) != 0) {
        /* error writing NAXIS */
        return(MAREMMA);
    }
    ibuf[0] = (int) myflats->subcols;
    ibuf[1] = (int) myflats->maxfibres;
    ibuf[2] = (int) (myflats->lastorder-myflats->firstorder+1);
    if ((status = SCDWRI(nsigid, "NPIX", ibuf, 1, 3, &unit)) != 0) {
        /* error writing NPIX */
        return(MAREMMA);
    }
    dbuf[0] = myflats->substartx;
    dbuf[1] = (double) 0;
    dbuf[2] = (double) myflats->firstorder;
    if ((status = SCDWRD(nsigid, "START", dbuf, 1, 3, &unit)) != 0) {
        /* error writing START */
        return(MAREMMA);
    }
    dbuf[0] = myflats->substepx;
    dbuf[1] = 1;
    dbuf[2] = 1;
    if ((status = SCDWRD(nsigid, "STEP", dbuf, 1, 3, &unit)) != 0) {
        /* error writing START */
        return(MAREMMA);
    }
    /* initialise CUNIT an LHCUTS, otherwise MIDAS will complain*/
    memset(cunit, ' ', 16);
    cunit[16] = '\0';
    strncat(cunit, "PIXEL           ", 16);
    strncat(cunit, "FIBRE           ", 16);
    strncat(cunit, "ORDER           ", 16);
    /* write the CUNIT descriptor */
    if ((status = SCDWRC(nsigid, "CUNIT", 1, cunit, 1, 64, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    lhcuts[0] = lhcuts[1] = 0;
    lhcuts[2] = lhcuts[3] = fdvecbuf1[0];
    for (iorderifibreixindex=1; iorderifibreixindex<=iorderifibreixend;
                    iorderifibreixindex++) {
        if (fdvecbuf1[iorderifibreixindex] < lhcuts[2]) {
            lhcuts[2] = fdvecbuf1[iorderifibreixindex];
        }
        if (fdvecbuf1[iorderifibreixindex] > lhcuts[3]) {
            lhcuts[3] = fdvecbuf1[iorderifibreixindex];
        }
    }
    /* write the LHCUTS descriptor */
    if ((status = SCDWRR(nsigid, "LHCUTS", lhcuts, 1, 4, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    /* write the IDENT descriptor */
    if ((status = SCDWRC(nsigid, "IDENT", 1,
                    "Normalisation sigmas for fibre FF",
                    1, CATREC_LEN+1, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    /* write the name of this file to the output catalog */
    if ((status = SCCADD(*catname, filename, blank)) != 0) {
        /* coud not add filename to the catalog: complain */
        return(MAREMMA);
    }

    /* close this frame */
    if ((status = SCFCLO(nsigid)) != 0) {
        /* error closing common frame: complain */
        return(MAREMMA);
    }

    /* now proceed to the last part, the data which are contained in
     goodfibres, lowfibrebounds and highfibrebounds */
    /* concoct a name for this pixel-order-fibre frame which will hold
     lowfibrebounds (first layer), highfibrebounds (second layer) and
     goodfibres (third layer) */
    sprintf(filename,"%s_common.fits",basename);
    /* create this frame on disk */
    if ((status =
                    SCFCRE(filename, D_I4_FORMAT, F_O_MODE, F_IMA_TYPE,
                                    3*(myflats->lastorder-myflats->firstorder+1)*
                                    myflats->maxfibres*myflats->subcols, &commid)) != 0) {
        /* could not create the file */
        return(MAREMMA);
    }
    /* build the 4D array to be dumped to disk */
    /* allocate the memory */
    longarray = l4tensor(0, 2, 0, myflats->lastorder-myflats->firstorder,
                    0, myflats->maxfibres-1, 0, myflats->subcols-1);
    /* fill the 4D array: use memcpy for the fibre bounds, which are already
     of the right data type, and a linearised loop for the mask, which needs
     to be cast into a int32_t before copying */
    memcpy(longarray[0][0][0], myflats->lowfibrebounds[0][0],
           (myflats->lastorder-myflats->firstorder+1)*myflats->maxfibres*
           myflats->subcols*sizeof(int32_t));
    memcpy(longarray[1][0][0], myflats->highfibrebounds[0][0],
           (myflats->lastorder-myflats->firstorder+1)*myflats->maxfibres*
           myflats->subcols*sizeof(int32_t));
    lvecbuf1 = longarray[2][0][0];
    fmvecbuf1 = myflats->goodfibres[0][0];
    for (iorderifibreixindex=0; iorderifibreixindex<=iorderifibreixend;
                    iorderifibreixindex++) {
        lvecbuf1[iorderifibreixindex] = (int32_t) fmvecbuf1[iorderifibreixindex];
    }
    /* write the 4D array to file */
    lvecbuf1 = longarray[0][0][0];
    if ((status =
                    SCFPUT(commid, 1, 3*(1+myflats->lastorder-myflats->firstorder)*
                                    myflats->maxfibres*myflats->subcols,
                                    (char *) lvecbuf1)) != 0) {
        /* I could not write this big frame to disk: complain... */
        return(MAREMMA);
    }
    /* initialise LHCUTS */
    lhcuts[0] = lhcuts[1] = 0;
    lhcuts[2] = lhcuts[3] = (float) lvecbuf1[0];
    for (iiorderifibreixindex=1; iiorderifibreixindex<=iiorderifibreixend;
                    iiorderifibreixindex++) {
        if ((float)lvecbuf1[iiorderifibreixindex] < lhcuts[2]) {
            lhcuts[2] = (float)lvecbuf1[iiorderifibreixindex];
        }
        if ((float)lvecbuf1[iiorderifibreixindex] > lhcuts[3]) {
            lhcuts[3] = (float)lvecbuf1[iiorderifibreixindex];
        }
    }
    /* free the 4D array */
    free_l4tensor(longarray, 0, 2, 0, myflats->lastorder-myflats->firstorder,
                  0, myflats->maxfibres-1, 0, myflats->subcols-1);
    /* write the standard descriptors */
    ibuf[0] = 4;
    if ((status = SCDWRI(commid, "NAXIS", ibuf, 1, 1, &unit)) != 0) {
        /* error writing NAXIS */
        return(MAREMMA);
    }
    ibuf[0] = (int) (myflats->subcols);
    ibuf[1] = (int) (myflats->maxfibres);
    ibuf[2] = (int) (myflats->lastorder-myflats->firstorder+1);
    ibuf[3] = 3;
    if ((status = SCDWRI(commid, "NPIX", ibuf, 1, 4, &unit)) != 0) {
        /* error writing NPIX */
        return(MAREMMA);
    }
    dbuf[0] = myflats->substartx;
    dbuf[1] = 0;
    dbuf[2] = (double) myflats->firstorder;
    dbuf[3] = 0;
    if ((status = SCDWRD(commid, "START", dbuf, 1, 4, &unit)) != 0) {
        /* error writing START */
        return(MAREMMA);
    }
    dbuf[0] = myflats->substepx;
    dbuf[1] = 1;
    dbuf[2] = 1;
    dbuf[3] = 1;
    if ((status = SCDWRD(commid, "STEP", dbuf, 1, 4, &unit)) != 0) {
        /* error writing START */
        return(MAREMMA);
    }
    /* initialise CUNIT */
    memset(cunit, ' ', 16);
    cunit[16] = '\0';
    strncat(cunit, "PIXEL           ", 16);
    strncat(cunit, "FIBRE           ", 16);
    strncat(cunit, "ORDER           ", 16);
    strncat(cunit, "TYPE            ", 16);
    /* write the CUNIT descriptor */
    if ((status = SCDWRC(commid, "CUNIT", 1, cunit, 1, 80, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    /* write LHCUTS */
    if ((status = SCDWRR(commid, "LHCUTS", lhcuts, 1, 4, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    /* write the IDENT descriptor */
    if ((status = SCDWRC(commid, "IDENT", 1, "Common data for fibre FF", 1,
                    CATREC_LEN+1, &null)) != 0) {
        /* I could not write the descriptor: complain... */
        return(MAREMMA);
    }
    /* write the name of this file to the output catalog */
    if ((status = SCCADD(*catname, filename, blank)) != 0) {
        /* coud not add filename to the catalog: complain */
        return(MAREMMA);
    }
    /* write the other scalars in the slitflats structure to descriptors
     in the common frame */
    ibuf[0] = (int) myflats->nflats;
    if ((status = SCDWRI(commid, "NFLATS", &ibuf[0], 1, 1, &unit))
                    != 0) {
        /* error writing NFLATS */
        return(MAREMMA);
    }
    ibuf[0] = (int) myflats->subrows;
    if ((status = SCDWRI(commid, "ROWS", &ibuf[0], 1, 1, &unit))
                    != 0) {
        /* error writing ROWS */
        return(MAREMMA);
    }
    ibuf[0] = (int) myflats->subcols;
    if ((status = SCDWRI(commid, "COLS", &ibuf[0], 1, 1, &unit))
                    != 0) {
        /* error writing COLS */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "STARTX", &myflats->substartx, 1, 1, &unit))
                    != 0) {
        /* error writing STARTX */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "STARTY", &myflats->substarty, 1, 1, &unit))
                    != 0) {
        /* error writing STARTY */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "STEPX", &myflats->substepx, 1, 1, &unit))
                    != 0) {
        /* error writing STEPX */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "STEPY", &myflats->substepy, 1, 1, &unit))
                    != 0) {
        /* error writing STEPY */
        return(MAREMMA);
    }
    if ((status = SCDWRC(commid, "CHIPCHOICE", 1, &myflats->chipchoice, 1, 1,
                    &unit)) != 0) {
        /* error writing chipchoice */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "RON", &myflats->ron, 1, 1, &unit)) != 0) {
        /* error writing RON */
        return(MAREMMA);
    }

    if ((status = SCDWRD(commid, "GAIN", &myflats->gain, 1, 1, &unit)) != 0) {
        /* error writing GAIN */
        return(MAREMMA);
    }

    ibuf[0] = (int)myflats->firstorder;
    if ((status = SCDWRI(commid, "ORDERLIM", ibuf, 1, 1, &unit)) != 0) {
        /* error writing FIRSTORDER */
        return(MAREMMA);
    }
    ibuf[0] = (int)myflats->lastorder;
    if ((status = SCDWRI(commid, "ORDERLIM", ibuf, 2, 1, &unit)) != 0) {
        /* error writing LASTORDER */
        return(MAREMMA);
    }
    if ((status = SCDWRI(commid, "TAB_IN_OUT_OSHIFT", &myflats->tab_io_oshift,
                    1, 1, &unit)) != 0) {
        /* error writing TAB_IN_OUT_OSHIFT */
        return(MAREMMA);
    }
    ibuf[0] = (int)myflats->maxfibres;
    if ((status = SCDWRI(commid, "MAXFIBRES", ibuf, 1, 1, &unit)) != 0) {
        /* error writing MAXFIBRES */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "PIXMAX", &myflats->pixmax, 1, 1, &unit))
                    != 0) {
        /* error writing PIXMAX */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "HALFIBREWIDTH", &myflats->halfibrewidth,
                    1, 1, &unit)) != 0) {
        /* error writing HALFIBREWIDTH */
        return(MAREMMA);
    }
    if ((status = SCDWRD(commid, "MINFIBREFRAC", &myflats->minfibrefrac,
                    1, 1, &unit)) != 0) {
        /* error writing MINFIBREFRAC */
        return(MAREMMA);
    }
    ibuf[0] = (int)myflats->numfibres;
    if ((status = SCDWRI(commid, "NUMFIBRES", ibuf, 1, 1, &unit)) != 0) {
        /* error writing NUMFIBRES */
        return(MAREMMA);
    }
    if ((status = SCDWRI(commid, "FIBREMASK", myflats->fibremask, 1,
                    myflats->maxfibres, &unit)) != 0) {
        /* error writing FIBREMASK */
        return(MAREMMA);
    }
    if ((status = SCDWRI(commid, "FIBRE2FRAME", myflats->fibre2frame, 1,
                    myflats->maxfibres, &unit)) != 0) {
        /* error writing FIBRE2FRAME */
        return(MAREMMA);
    }
    if ((status = SCDWRC(commid, "SHIFTABLE", 1, &myflats->shiftable, 1,
                    1, &unit)) != 0) {
        /* error writing SHIFTABLE */
        return(MAREMMA);
    }
    if ((status = SCDWRC(commid, "NORMALISED", 1, &myflats->normalised, 1,
                    1, &unit)) != 0) {
        /* error writing NORMALISED */
        return(MAREMMA);
    }

    /* that's all folks! close the common frame */
    if ((status = SCFCLO(commid)) != 0) {
        /* error closing common frame: complain */
        return(MAREMMA);
    }

    free_ivector(fibres, 0,(int32_t)myflats->maxfibres-1);

    return(NOERR);

}