File: getcoll.c

package info (click to toggle)
cfitsio 3.490-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,872 kB
  • sloc: ansic: 106,470; yacc: 4,916; sh: 3,259; fortran: 2,613; lex: 539; makefile: 162
file content (621 lines) | stat: -rw-r--r-- 23,298 bytes parent folder | download | duplicates (16)
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
/*  This file, getcoll.c, contains routines that read data elements from   */
/*  a FITS image or table, with logical datatype.                          */

/*  The FITSIO software was written by William Pence at the High Energy    */
/*  Astrophysic Science Archive Research Center (HEASARC) at the NASA      */
/*  Goddard Space Flight Center.                                           */

#include <stdlib.h>
#include <string.h>
#include "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffgcvl( fitsfile *fptr,   /* I - FITS file pointer                       */
            int  colnum,      /* I - number of column to read (1 = 1st col)  */
            LONGLONG  firstrow,   /* I - first row to read (1 = 1st row)         */
            LONGLONG  firstelem,  /* I - first vector element to read (1 = 1st)  */
            LONGLONG  nelem,      /* I - number of values to read                */
            char  nulval,     /* I - value for null pixels                   */
            char *array,      /* O - array of values                         */
            int  *anynul,     /* O - set to 1 if any values are null; else 0 */
            int  *status)     /* IO - error status                           */
/*
  Read an array of logical values from a column in the current FITS HDU.
  Any undefined pixels will be set equal to the value of 'nulval' unless
  nulval = 0 in which case no checks for undefined pixels will be made.
*/
{
    char cdummy;

    ffgcll( fptr, colnum, firstrow, firstelem, nelem, 1, nulval, array,
            &cdummy, anynul, status);

    return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcl(  fitsfile *fptr,   /* I - FITS file pointer                       */
            int  colnum,      /* I - number of column to read (1 = 1st col)  */
            LONGLONG  firstrow,   /* I - first row to read (1 = 1st row)         */
            LONGLONG  firstelem,  /* I - first vector element to read (1 = 1st)  */
            LONGLONG  nelem,      /* I - number of values to read                */
            char *array,      /* O - array of values                         */
            int  *status)     /* IO - error status                           */
/*
  !!!! THIS ROUTINE IS DEPRECATED AND SHOULD NOT BE USED !!!!!!
                  !!!! USE ffgcvl INSTEAD  !!!!!!
  Read an array of logical values from a column in the current FITS HDU.
  No checking for null values will be performed.
*/
{
    char nulval = 0;
    int anynul;

    ffgcvl( fptr, colnum, firstrow, firstelem, nelem, nulval, array,
            &anynul, status);

    return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcfl( fitsfile *fptr,   /* I - FITS file pointer                       */
            int  colnum,      /* I - number of column to read (1 = 1st col)  */
            LONGLONG  firstrow,   /* I - first row to read (1 = 1st row)         */
            LONGLONG  firstelem,  /* I - first vector element to read (1 = 1st)  */
            LONGLONG  nelem,      /* I - number of values to read                */
            char *array,      /* O - array of values                         */
            char *nularray,   /* O - array of flags = 1 if nultyp = 2        */
            int  *anynul,     /* O - set to 1 if any values are null; else 0 */
            int  *status)     /* IO - error status                           */
/*
  Read an array of logical values from a column in the current FITS HDU.
*/
{
    char nulval = 0;

    ffgcll( fptr, colnum, firstrow, firstelem, nelem, 2, nulval, array,
            nularray, anynul, status);

    return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcll( fitsfile *fptr,   /* I - FITS file pointer                       */
            int  colnum,      /* I - number of column to read (1 = 1st col)  */
            LONGLONG  firstrow,   /* I - first row to read (1 = 1st row)         */
            LONGLONG  firstelem, /* I - first vector element to read (1 = 1st)  */
            LONGLONG  nelem,      /* I - number of values to read                */
            int   nultyp,     /* I - null value handling code:               */
                              /*     1: set undefined pixels = nulval        */
                              /*     2: set nularray=1 for undefined pixels  */
            char nulval,      /* I - value for null pixels if nultyp = 1     */
            char *array,      /* O - array of values                         */
            char *nularray,   /* O - array of flags = 1 if nultyp = 2        */
            int  *anynul,     /* O - set to 1 if any values are null; else 0 */
            int  *status)     /* IO - error status                           */
/*
  Read an array of logical values from a column in the current FITS HDU.
*/
{
    double dtemp;
    int tcode, maxelem, hdutype, ii, nulcheck;
    long twidth, incre;
    long ntodo;
    LONGLONG repeat, startpos, elemnum, readptr, tnull, rowlen, rownum, remain, next;
    double scale, zero;
    char tform[20];
    char message[FLEN_ERRMSG];
    char snull[20];   /*  the FITS null value  */
    unsigned char buffer[DBUFFSIZE], *buffptr;

    if (*status > 0 || nelem == 0)  /* inherit input status value if > 0 */
        return(*status);

    if (anynul)
       *anynul = 0;

    if (nultyp == 2)      
       memset(nularray, 0, (size_t) nelem);   /* initialize nullarray */

    /*---------------------------------------------------*/
    /*  Check input and get parameters about the column: */
    /*---------------------------------------------------*/
    if (ffgcprll( fptr, colnum, firstrow, firstelem, nelem, 0, &scale, &zero,
        tform, &twidth, &tcode, &maxelem, &startpos,  &elemnum, &incre,
        &repeat, &rowlen, &hdutype, &tnull, snull, status) > 0)
        return(*status);

    if (tcode != TLOGICAL)   
        return(*status = NOT_LOGICAL_COL);
 
    /*------------------------------------------------------------------*/
    /*  Decide whether to check for null values in the input FITS file: */
    /*------------------------------------------------------------------*/
    nulcheck = nultyp; /* by default, check for null values in the FITS file */

    if (nultyp == 1 && nulval == 0)
       nulcheck = 0;    /* calling routine does not want to check for nulls */

    /*---------------------------------------------------------------------*/
    /*  Now read the logical values from the FITS column.                  */
    /*---------------------------------------------------------------------*/

    remain = nelem;           /* remaining number of values to read */
    next = 0;                 /* next element in array to be read   */
    rownum = 0;               /* row number, relative to firstrow   */
    ntodo = (long) remain;           /* max number of elements to read at one time */

    while (ntodo)
    {
      /*
         limit the number of pixels to read at one time to the number that
         remain in the current vector.    
      */
      ntodo = (long) minvalue(ntodo, maxelem);      
      ntodo = (long) minvalue(ntodo, (repeat - elemnum));

      readptr = startpos + (rowlen * rownum) + (elemnum * incre);

      ffgi1b(fptr, readptr, ntodo, incre, buffer, status);

      /* convert from T or F to 1 or 0 */
      buffptr = buffer;
      for (ii = 0; ii < ntodo; ii++, next++, buffptr++)
      {
        if (*buffptr == 'T')
          array[next] = 1;
        else if (*buffptr =='F') 
          array[next] = 0;
        else if (*buffptr == 0)
        {
          array[next] = nulval;  /* set null values to input nulval */
          if (anynul)
              *anynul = 1;

          if (nulcheck == 2)
          {
            nularray[next] = 1;  /* set null flags */
          }
        }
        else  /* some other illegal character; return the char value */
        {
          if (*buffptr == 1) {
            /* this is an unfortunate case where the illegal value is the same
               as what we set True values to, so set the value to the character '1'
               instead, which has ASCII value 49.  */
            array[next] = 49;
          } else {
            array[next] = (char) *buffptr;
          }
        }
      }

      if (*status > 0)  /* test for error during previous read operation */
      {
	dtemp = (double) next;
        snprintf(message,FLEN_ERRMSG,
          "Error reading elements %.0f thruough %.0f of logical array (ffgcl).",
           dtemp+1., dtemp + ntodo);
        ffpmsg(message);
        return(*status);
      }

      /*--------------------------------------------*/
      /*  increment the counters for the next loop  */
      /*--------------------------------------------*/
      remain -= ntodo;
      if (remain)
      {
        elemnum += ntodo;

        if (elemnum == repeat)  /* completed a row; start on later row */
          {
            elemnum = 0;
            rownum++;
          }
      }
      ntodo = (long) remain;  /* this is the maximum number to do in next loop */

    }  /*  End of main while Loop  */

    return(*status);
}
/*--------------------------------------------------------------------------*/
int ffgcx(  fitsfile *fptr,  /* I - FITS file pointer                       */
            int   colnum,    /* I - number of column to write (1 = 1st col) */
            LONGLONG  frow,      /* I - first row to write (1 = 1st row)        */
            LONGLONG  fbit,      /* I - first bit to write (1 = 1st)            */
            LONGLONG  nbit,      /* I - number of bits to write                 */
            char *larray,    /* O - array of logicals corresponding to bits */
            int  *status)    /* IO - error status                           */
/*
  read an array of logical values from a specified bit or byte
  column of the binary table.    larray is set = TRUE, if the corresponding
  bit = 1, otherwise it is set to FALSE.
  The binary table column being read from must have datatype 'B' or 'X'. 
*/
{
    LONGLONG bstart;
    long offset, ndone, ii, repeat, bitloc, fbyte;
    LONGLONG  rstart, estart;
    int tcode, descrp;
    unsigned char cbuff;
    static unsigned char onbit[8] = {128,  64,  32,  16,   8,   4,   2,   1};
    tcolumn *colptr;

    if (*status > 0)           /* inherit input status value if > 0 */
        return(*status);

    /*  check input parameters */
    if (nbit < 1)
        return(*status);
    else if (frow < 1)
        return(*status = BAD_ROW_NUM);
    else if (fbit < 1)
        return(*status = BAD_ELEM_NUM);

    /* position to the correct HDU */
    if (fptr->HDUposition != (fptr->Fptr)->curhdu)
        ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);

    /* rescan header if data structure is undefined */
    else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
        if ( ffrdef(fptr, status) > 0)               
            return(*status);

    fbyte = (long) ((fbit + 7) / 8);
    bitloc = (long) (fbit - 1 - ((fbit - 1) / 8 * 8));
    ndone = 0;
    rstart = frow - 1;
    estart = fbyte - 1;

    colptr  = (fptr->Fptr)->tableptr;   /* point to first column */
    colptr += (colnum - 1);     /* offset to correct column structure */

    tcode = colptr->tdatatype;

    if (abs(tcode) > TBYTE)
        return(*status = NOT_LOGICAL_COL); /* not correct datatype column */

    if (tcode > 0)
    {
        descrp = FALSE;  /* not a variable length descriptor column */
        /* N.B: REPEAT is the number of bytes, not number of bits */
        repeat = (long) colptr->trepeat;

        if (tcode == TBIT)
            repeat = (repeat + 7) / 8;  /* convert from bits to bytes */

        if (fbyte > repeat)
            return(*status = BAD_ELEM_NUM);

        /* calc the i/o pointer location to start of sequence of pixels */
        bstart = (fptr->Fptr)->datastart + ((fptr->Fptr)->rowlength * rstart) +
               colptr->tbcol + estart;
    }
    else
    {
        descrp = TRUE;  /* a variable length descriptor column */
        /* only bit arrays (tform = 'X') are supported for variable */
        /* length arrays.  REPEAT is the number of BITS in the array. */

        ffgdes(fptr, colnum, frow, &repeat, &offset, status);

        if (tcode == -TBIT)
            repeat = (repeat + 7) / 8;

        if ((fbit + nbit + 6) / 8 > repeat)
            return(*status = BAD_ELEM_NUM);

        /* calc the i/o pointer location to start of sequence of pixels */
        bstart = (fptr->Fptr)->datastart + offset + (fptr->Fptr)->heapstart + estart;
    }

    /* move the i/o pointer to the start of the pixel sequence */
    if (ffmbyt(fptr, bstart, REPORT_EOF, status) > 0)
        return(*status);

    /* read the next byte */
    while (1)
    {
      if (ffgbyt(fptr, 1, &cbuff, status) > 0)
        return(*status);

      for (ii = bitloc; (ii < 8) && (ndone < nbit); ii++, ndone++)
      {
        if(cbuff & onbit[ii])       /* test if bit is set */
          larray[ndone] = TRUE;
        else
          larray[ndone] = FALSE;
      }

      if (ndone == nbit)   /* finished all the bits */
        return(*status);

      /* not done, so get the next byte */
      if (!descrp)
      {
        estart++;
        if (estart == repeat) 
        {
          /* move the i/o pointer to the next row of pixels */
          estart = 0;
          rstart = rstart + 1;
          bstart = (fptr->Fptr)->datastart + ((fptr->Fptr)->rowlength * rstart) +
               colptr->tbcol;

          ffmbyt(fptr, bstart, REPORT_EOF, status);
        }
      }
      bitloc = 0;
    }
}
/*--------------------------------------------------------------------------*/
int ffgcxui(fitsfile *fptr,   /* I - FITS file pointer                       */
            int  colnum,      /* I - number of column to read (1 = 1st col)  */
            LONGLONG  firstrow,   /* I - first row to read (1 = 1st row)         */
            LONGLONG  nrows,      /* I - no. of rows to read                     */
            long  input_first_bit, /* I - first bit to read (1 = 1st)        */
            int   input_nbits,     /* I - number of bits to read (<= 32)     */
            unsigned short *array, /* O - array of integer values            */
            int  *status)     /* IO - error status                           */
/*
  Read a consecutive string of bits from an 'X' or 'B' column and
  interprete them as an unsigned integer.  The number of bits must be
  less than or equal to 16 or the total number of bits in the column, 
  which ever is less.
*/
{
    int ii, firstbit, nbits, bytenum, startbit, numbits, endbit;
    int firstbyte, lastbyte, nbytes, rshift, lshift;
    unsigned short colbyte[5];
    tcolumn *colptr;
    char message[FLEN_ERRMSG];

    if (*status > 0 || nrows == 0)
        return(*status);

    /*  check input parameters */
    if (firstrow < 1)
    {
          snprintf(message,FLEN_ERRMSG, "Starting row number is less than 1: %ld (ffgcxui)",
                (long) firstrow);
          ffpmsg(message);
          return(*status = BAD_ROW_NUM);
    }
    else if (input_first_bit < 1)
    {
          snprintf(message,FLEN_ERRMSG, "Starting bit number is less than 1: %ld (ffgcxui)",
                input_first_bit);
          ffpmsg(message);
          return(*status = BAD_ELEM_NUM);
    }
    else if (input_nbits > 16)
    {
          snprintf(message, FLEN_ERRMSG,"Number of bits to read is > 16: %d (ffgcxui)",
                input_nbits);
          ffpmsg(message);
          return(*status = BAD_ELEM_NUM);
    }

    /* position to the correct HDU */
    if (fptr->HDUposition != (fptr->Fptr)->curhdu)
        ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);

    /* rescan header if data structure is undefined */
    else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
        if ( ffrdef(fptr, status) > 0)               
            return(*status);

    if ((fptr->Fptr)->hdutype != BINARY_TBL)
    {
        ffpmsg("This is not a binary table extension (ffgcxui)");
        return(*status = NOT_BTABLE);
    }

    if (colnum > (fptr->Fptr)->tfield)
    {
      snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d (ffgcxui)",
                colnum);
        ffpmsg(message);
        snprintf(message, FLEN_ERRMSG,"  There are %d columns in this table.",
                (fptr->Fptr)->tfield );
        ffpmsg(message);

        return(*status = BAD_COL_NUM);
    }       

    colptr  = (fptr->Fptr)->tableptr;   /* point to first column */
    colptr += (colnum - 1);     /* offset to correct column structure */

    if (abs(colptr->tdatatype) > TBYTE)
    {
        ffpmsg("Can only read bits from X or B type columns. (ffgcxui)");
        return(*status = NOT_LOGICAL_COL); /* not correct datatype column */
    }

    firstbyte = (input_first_bit - 1              ) / 8 + 1;
    lastbyte  = (input_first_bit + input_nbits - 2) / 8 + 1;
    nbytes = lastbyte - firstbyte + 1;

    if (colptr->tdatatype == TBIT && 
        input_first_bit + input_nbits - 1 > (long) colptr->trepeat)
    {
        ffpmsg("Too many bits. Tried to read past width of column (ffgcxui)");
        return(*status = BAD_ELEM_NUM);
    }
    else if (colptr->tdatatype == TBYTE && lastbyte > (long) colptr->trepeat)
    {
        ffpmsg("Too many bits. Tried to read past width of column (ffgcxui)");
        return(*status = BAD_ELEM_NUM);
    }

    for (ii = 0; ii < nrows; ii++)
    {
        /* read the relevant bytes from the row */
        if (ffgcvui(fptr, colnum, firstrow+ii, firstbyte, nbytes, 0, 
               colbyte, NULL, status) > 0)
        {
             ffpmsg("Error reading bytes from column (ffgcxui)");
             return(*status);
        }

        firstbit = (input_first_bit - 1) % 8; /* modulus operator */
        nbits = input_nbits;

        array[ii] = 0;

        /* select and shift the bits from each byte into the output word */
        while(nbits)
        {
            bytenum = firstbit / 8;

            startbit = firstbit % 8;  
            numbits = minvalue(nbits, 8 - startbit);
            endbit = startbit + numbits - 1;

            rshift = 7 - endbit;
            lshift = nbits - numbits;

            array[ii] = ((colbyte[bytenum] >> rshift) << lshift) | array[ii];

            nbits -= numbits;
            firstbit += numbits;
        }
    }

    return(*status);
}

/*--------------------------------------------------------------------------*/
int ffgcxuk(fitsfile *fptr,   /* I - FITS file pointer                       */
            int  colnum,      /* I - number of column to read (1 = 1st col)  */
            LONGLONG  firstrow,   /* I - first row to read (1 = 1st row)         */
            LONGLONG  nrows,      /* I - no. of rows to read                     */
            long  input_first_bit, /* I - first bit to read (1 = 1st)        */
            int   input_nbits,     /* I - number of bits to read (<= 32)     */
            unsigned int *array,   /* O - array of integer values            */
            int  *status)     /* IO - error status                           */
/*
  Read a consecutive string of bits from an 'X' or 'B' column and
  interprete them as an unsigned integer.  The number of bits must be
  less than or equal to 32 or the total number of bits in the column, 
  which ever is less.
*/
{
    int ii, firstbit, nbits, bytenum, startbit, numbits, endbit;
    int firstbyte, lastbyte, nbytes, rshift, lshift;
    unsigned int colbyte[5];
    tcolumn *colptr;
    char message[FLEN_ERRMSG];

    if (*status > 0 || nrows == 0)
        return(*status);

    /*  check input parameters */
    if (firstrow < 1)
    {
          snprintf(message, FLEN_ERRMSG,"Starting row number is less than 1: %ld (ffgcxuk)",
                (long) firstrow);
          ffpmsg(message);
          return(*status = BAD_ROW_NUM);
    }
    else if (input_first_bit < 1)
    {
          snprintf(message, FLEN_ERRMSG,"Starting bit number is less than 1: %ld (ffgcxuk)",
                input_first_bit);
          ffpmsg(message);
          return(*status = BAD_ELEM_NUM);
    }
    else if (input_nbits > 32)
    {
          snprintf(message, FLEN_ERRMSG,"Number of bits to read is > 32: %d (ffgcxuk)",
                input_nbits);
          ffpmsg(message);
          return(*status = BAD_ELEM_NUM);
    }

    /* position to the correct HDU */
    if (fptr->HDUposition != (fptr->Fptr)->curhdu)
        ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);

    /* rescan header if data structure is undefined */
    else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
        if ( ffrdef(fptr, status) > 0)               
            return(*status);

    if ((fptr->Fptr)->hdutype != BINARY_TBL)
    {
        ffpmsg("This is not a binary table extension (ffgcxuk)");
        return(*status = NOT_BTABLE);
    }

    if (colnum > (fptr->Fptr)->tfield)
    {
      snprintf(message, FLEN_ERRMSG,"Specified column number is out of range: %d (ffgcxuk)",
                colnum);
        ffpmsg(message);
        snprintf(message, FLEN_ERRMSG,"  There are %d columns in this table.",
                (fptr->Fptr)->tfield );
        ffpmsg(message);

        return(*status = BAD_COL_NUM);
    }       

    colptr  = (fptr->Fptr)->tableptr;   /* point to first column */
    colptr += (colnum - 1);     /* offset to correct column structure */

    if (abs(colptr->tdatatype) > TBYTE)
    {
        ffpmsg("Can only read bits from X or B type columns. (ffgcxuk)");
        return(*status = NOT_LOGICAL_COL); /* not correct datatype column */
    }

    firstbyte = (input_first_bit - 1              ) / 8 + 1;
    lastbyte  = (input_first_bit + input_nbits - 2) / 8 + 1;
    nbytes = lastbyte - firstbyte + 1;

    if (colptr->tdatatype == TBIT && 
        input_first_bit + input_nbits - 1 > (long) colptr->trepeat)
    {
        ffpmsg("Too many bits. Tried to read past width of column (ffgcxuk)");
        return(*status = BAD_ELEM_NUM);
    }
    else if (colptr->tdatatype == TBYTE && lastbyte > (long) colptr->trepeat)
    {
        ffpmsg("Too many bits. Tried to read past width of column (ffgcxuk)");
        return(*status = BAD_ELEM_NUM);
    }

    for (ii = 0; ii < nrows; ii++)
    {
        /* read the relevant bytes from the row */
        if (ffgcvuk(fptr, colnum, firstrow+ii, firstbyte, nbytes, 0, 
               colbyte, NULL, status) > 0)
        {
             ffpmsg("Error reading bytes from column (ffgcxuk)");
             return(*status);
        }

        firstbit = (input_first_bit - 1) % 8; /* modulus operator */
        nbits = input_nbits;

        array[ii] = 0;

        /* select and shift the bits from each byte into the output word */
        while(nbits)
        {
            bytenum = firstbit / 8;

            startbit = firstbit % 8;  
            numbits = minvalue(nbits, 8 - startbit);
            endbit = startbit + numbits - 1;

            rshift = 7 - endbit;
            lshift = nbits - numbits;

            array[ii] = ((colbyte[bytenum] >> rshift) << lshift) | array[ii];

            nbits -= numbits;
            firstbit += numbits;
        }
    }

    return(*status);
}