File: fitshandle.cc

package info (click to toggle)
healpix-cxx 3.50.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,008 kB
  • sloc: cpp: 13,882; ansic: 5,930; sh: 4,451; makefile: 216
file content (807 lines) | stat: -rw-r--r-- 22,865 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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
 *  This file is part of libcxxsupport.
 *
 *  libcxxsupport 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.
 *
 *  libcxxsupport 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 libcxxsupport; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
 *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
 *  (DLR).
 */

/*
 *  This file contains the implementation of the FITS I/O helper class
 *  used by the Planck LevelS package.
 *
 *  Copyright (C) 2002-2011 Max-Planck-Society
 *  Author: Martin Reinecke
 */

#include <sstream>
#include <cstring>
#include <vector>
#include "fitsio.h"
#include "fitshandle.h"
#include "string_utils.h"

#define FPTR (static_cast<fitsfile *> (fptr))
#define OFPTR (static_cast<fitsfile *> (orig.fptr))

using namespace std;

namespace {

template<typename T> inline int fitsType();
template<> inline int fitsType<float> () { return TFLOAT;  }
template<> inline int fitsType<double>() { return TDOUBLE; }

int type2bitpix (PDT type)
  {
  switch (type)
    {
    case PLANCK_FLOAT32: return FLOAT_IMG;
    case PLANCK_FLOAT64: return DOUBLE_IMG;
    default: planck_fail ("unsupported component type");
    }
  }

/*! Converts a FITS type code (i.e. the data type of a FITS column) to the
    corresponding Planck type code. */
PDT ftc2type (int ftc)
  {
  switch (ftc)
    {
    case TLOGICAL : return PLANCK_BOOL;
    case TBYTE    : return PLANCK_INT8;
    case TSHORT   : return PLANCK_INT16;
    case TINT     :
    case TINT32BIT: return PLANCK_INT32;
    case TLONGLONG: return PLANCK_INT64;
    case TFLOAT   : return PLANCK_FLOAT32;
    case TDOUBLE  : return PLANCK_FLOAT64;
    case TSTRING  : return PLANCK_STRING;
    default: planck_fail ("unsupported component type");
    }
  }

/*! Converts a Planck type code to the corresponding FITS type code
    (i.e. the data type of a FITS column). */
int type2ftc (PDT type)
  {
  switch (type)
    {
    case PLANCK_BOOL   : return TLOGICAL;
    case PLANCK_INT8   :
    case PLANCK_UINT8  : return TBYTE;
    case PLANCK_INT16  : return TSHORT;
    case PLANCK_INT32  : return TINT;
    case PLANCK_INT64  : return TLONGLONG;
    case PLANCK_FLOAT32: return TFLOAT;
    case PLANCK_FLOAT64: return TDOUBLE;
    case PLANCK_STRING : return TSTRING;
    default: planck_fail ("unsupported component type");
    }
  }

const char *type2fitschar (PDT type)
  {
  switch (type)
    {
    case PLANCK_BOOL   : return "L";
    case PLANCK_FLOAT32: return "E";
    case PLANCK_FLOAT64: return "D";
    case PLANCK_INT8   :
    case PLANCK_UINT8  : return "B";
    case PLANCK_INT16  : return "I";
    case PLANCK_INT32  : return "J";
    case PLANCK_INT64  : return "K";
    case PLANCK_STRING : return "A";
    default:
      planck_fail(string("unknown data type ")+type2string(type));
    }
  }

const char *type2asciiform (PDT type)
  {
  switch (type)
    {
    case PLANCK_FLOAT32: return "E14.7";
    case PLANCK_FLOAT64: return "D23.15";
    case PLANCK_UINT8  : return "I3";
    case PLANCK_INT8   : return "I4";
    case PLANCK_INT16  : return "I6";
    case PLANCK_INT32  : return "I11";
    case PLANCK_INT64  : return "I22";
    default:
      planck_fail(string("unknown data type ")+type2string(type));
    }
  }

string fixkey (const string &key)
  {
  for (tsize m=0; m<key.size(); ++m)
    if (islower(key[m])) return string("HIERARCH "+key);

  return key;
  }
} // unnamed namespace

fitscolumn::fitscolumn()
  : repcount_(0), type_(PLANCK_INVALID) {}

fitscolumn::fitscolumn (const string &nm, const string &un, int64 rc, PDT tp)
  : name_(nm), unit_(un), repcount_(rc), type_(tp) {}

fitscolumn::~fitscolumn () {}

void fitshandle::check_errors() const
  {
  char msg[81];
  if (status==0)
    {
    while (fits_read_errmsg(msg))
      cerr << "STALE FITS ERROR MESSAGE: " << msg << endl;
    fits_clear_errmsg();
    return;
    }
  fits_get_errstatus (status, msg);
  cerr << msg << endl;
  while (fits_read_errmsg(msg)) cerr << msg << endl;
  fits_clear_errmsg();
  status=0;
  planck_fail("FITS error");
  }

void fitshandle::clean_data()
  {
  if (!fptr) return;
  axes_.clear();
  columns_.clear();
  hdutype_=INVALID;
  bitpix_=INVALID;
  nrows_=0;
  }

void fitshandle::clean_all()
  {
  if (!fptr) return;
  clean_data();
  fits_close_file (FPTR, &status);
  check_errors();
  fptr=0;
  }

bool fitshandle::table_hdu (tsize col) const
  {
  if ((hdutype_!=ASCII_TBL) && (hdutype_!=BINARY_TBL)) return false;
  if ((col<=0) || (col>columns_.size())) return false;
  return true;
  }
bool fitshandle::image_hdu () const
  { return hdutype_==IMAGE_HDU; }

void fitshandle::init_image()
  {
  int naxis;
  fits_get_img_type (FPTR, &bitpix_, &status);
  fits_get_img_dim (FPTR, &naxis, &status);
  check_errors();
  arr<LONGLONG> naxes(naxis);
  if (naxis>0) fits_get_img_sizell (FPTR, naxis, naxes.begin(), &status);
  for (long m=0; m<naxis; ++m) axes_.push_back(naxes[naxis-m-1]);
  check_errors();
  }

void fitshandle::init_asciitab()
  {
  char ttype[81], tunit[81], tform[81];
  int ncol, typecode;
  fits_get_num_cols (FPTR, &ncol, &status);
  { LONGLONG tmp; fits_get_num_rowsll (FPTR, &tmp, &status); nrows_=tmp; }
  check_errors();
  for (int m=1; m<=ncol; ++m)
    {
    fits_get_acolparms (FPTR, m, ttype, 0, tunit, tform,
                        0, 0, 0, 0, &status);
    fits_ascii_tform (tform, &typecode, 0,0, &status);
    check_errors();
    columns_.push_back (fitscolumn (ttype,tunit,1,ftc2type(typecode)));
    }
  }

void fitshandle::init_bintab()
  {
  char ttype[81], tunit[81], tform[81];
  LONGLONG repc;
  int ncol, typecode;
  fits_get_num_cols (FPTR, &ncol, &status);
  { LONGLONG tmp; fits_get_num_rowsll (FPTR, &tmp, &status); nrows_=tmp; }
  check_errors();
  for (int m=1; m<=ncol; ++m)
    {
    fits_get_bcolparmsll (FPTR, m, ttype, tunit, tform, &repc,
                        0, 0, 0, 0, &status);
    fits_binary_tform (tform, &typecode, 0,0, &status);
    check_errors();
    columns_.push_back (fitscolumn (ttype,tunit,repc,ftc2type(typecode)));
    }
  }

void fitshandle::init_data()
  {
  clean_data();
  fits_get_hdu_type (FPTR, &hdutype_, &status);
  check_errors();
  switch(hdutype_)
    {
    case IMAGE_HDU:
      init_image(); break;
    case ASCII_TBL:
      init_asciitab(); break;
    case BINARY_TBL:
      init_bintab(); break;
    default:
      planck_fail("init_data(): unsupported HDU type"); break;
    }
  }

void fitshandle::read_col (int colnum, void *data, int64 ndata, PDT type,
  int64 offset) const
  {
  planck_assert(table_hdu(colnum),"incorrect FITS table access");
  int64 repc = columns_[colnum-1].repcount();
  planck_assert (ndata<=(repc*nrows_-offset),"read_column(): array too large");
  int64 frow = offset/repc+1;
  int64 felem = offset%repc+1;
  fits_read_col (FPTR, type2ftc(type), colnum, frow, felem, ndata, 0, data, 0,
    &status);
  check_errors();
  }
void fitshandle::write_col (int colnum, const void *data, int64 ndata,
  PDT type, int64 offset)
  {
  planck_assert(table_hdu(colnum),"incorrect FITS table access");
  int64 repc = columns_[colnum-1].repcount();
  int64 frow = offset/repc+1;
  int64 felem = offset%repc+1;
  fits_write_col (FPTR, type2ftc(type), colnum, frow, felem, ndata,
    const_cast<void *>(data), &status);
  nrows_ = max(nrows_,offset+ndata);
  check_errors();
  }

void fitshandle::getKeyHelper(const string &name) const
  {
  if (status==KEY_NO_EXIST)
    {
    fits_clear_errmsg();
    status=0;
    planck_fail("fitshandle::get_key(): key '"+name+"' not found");
    }
  check_errors();
  }

fitshandle::fitshandle ()
  : status(0), fptr(0), hdutype_(INVALID), bitpix_(INVALID), nrows_(0) {}

fitshandle::~fitshandle()
  { clean_all(); }

void fitshandle::open (const string &fname)
  {
  clean_all();
  fitsfile *ptr;
  fits_open_file(&ptr, fname.c_str(), READONLY, &status);
  fptr=ptr;
  check_errors();
  init_data();
  }

void fitshandle::create (const string &fname)
  {
  clean_all();
  fitsfile *ptr;
  fits_create_file(&ptr, fname.c_str(), &status);
  fptr=ptr;
  fits_write_imghdr(FPTR, 8, 0, 0, &status); // insert empty PHDU
  fits_write_date(FPTR, &status);
  check_errors();
  init_data();
  }

// static
void fitshandle::delete_file (const string &name)
  {
  fitsfile *ptr;
  int stat = 0;
  fits_open_file(&ptr, name.c_str(), READWRITE, &stat);
  fits_delete_file(ptr, &stat);
  if (stat==0) return;

  char msg[81];
  fits_get_errstatus (stat, msg);
  cerr << msg << endl;
  while (fits_read_errmsg(msg)) cerr << msg << endl;
  planck_fail("FITS error");
  }

string fitshandle::fileName() const
  {
  planck_assert(connected(),"handle not connected to a file");
  char *fname = new char[2048];
  fits_file_name(FPTR, fname, &status);
  check_errors();
  string result(fname);
  delete[] fname;
  return result;
  }

void fitshandle::goto_hdu (int hdu)
  {
  int curhdu;
  fits_get_hdu_num(FPTR,&curhdu);
  if (curhdu!=hdu)
    {
    fits_movabs_hdu(FPTR, hdu, &hdutype_, &status);
    check_errors();
    init_data();
    }
  }

int fitshandle::num_hdus () const
  {
  int result;
  fits_get_num_hdus (FPTR, &result, &status);
  check_errors();
  return result;
  }

void fitshandle::insert_bintab (const vector<fitscolumn> &cols,
  const string &extname)
  {
  clean_data();
  int ncol=cols.size();
  arr2b<char> ttype(ncol,81), tform(ncol,81), tunit(ncol,81);

  for (long m=0; m<ncol; ++m)
    {
    strcpy (ttype[m], cols[m].name().c_str());
    strcpy (tunit[m], cols[m].unit().c_str());
    ostringstream x;
    x << cols[m].repcount() << type2fitschar(cols[m].type());
    strcpy (tform[m], x.str().c_str());
    }
  fits_insert_btbl (FPTR, nrows_, ncol, ttype.p0(), tform.p0(), tunit.p0(),
    const_cast<char *>(extname.c_str()), 0, &status);
  check_errors();
  init_data();
  }

void fitshandle::insert_asctab (const vector<fitscolumn> &cols,
  const string &extname)
  {
  clean_data();
  int ncol=cols.size();
  arr2b<char> ttype(ncol,81), tform(ncol,81), tunit(ncol,81);

  for (long m=0; m<ncol; ++m)
    {
    strcpy (ttype[m], cols[m].name().c_str());
    strcpy (tunit[m], cols[m].unit().c_str());
    ostringstream x;
    if (cols[m].type()!=PLANCK_STRING)
      {
      planck_assert (cols[m].repcount()==1,"bad repcount for ASCII table");
      x << type2asciiform(cols[m].type());
      }
    else
      {
      x << "A" << dataToString(cols[m].repcount());
      }
    strcpy (tform[m], x.str().c_str());
    }
  fits_insert_atbl (FPTR, 0, nrows_, ncol, ttype.p0(), 0, tform.p0(),
    tunit.p0(), const_cast<char *>(extname.c_str()), &status);
  check_errors();
  init_data();
  }

void fitshandle::insert_image (PDT type, const vector<int64> &Axes)
  {
  clean_data();
  arr<LONGLONG> tmpax(Axes.size());
  for (long m=0; m<long(Axes.size()); m++) tmpax[m]=Axes[Axes.size()-1-m];
  fits_insert_imgll (FPTR, type2bitpix(type), Axes.size(), &tmpax[0], &status);
  check_errors();
  init_data();
  }

template<typename T>
  void fitshandle::insert_image (PDT type, const arr2<T> &data)
  {
  clean_data();
  arr<LONGLONG> tmpax(2);
  tmpax[0] = data.size2(); tmpax[1] = data.size1();
  fits_insert_imgll (FPTR, type2bitpix(type), 2, &tmpax[0], &status);
  arr2<T> &tmparr = const_cast<arr2<T> &> (data);
  fits_write_img (FPTR, fitsType<T>(), 1, tmpax[0]*tmpax[1],
    &tmparr[0][0], &status);
  check_errors();
  init_data();
  }

template void fitshandle::insert_image (PDT type, const arr2<double> &data);
template void fitshandle::insert_image (PDT type, const arr2<float> &data);

void fitshandle::write_checksum()
  {
  planck_assert(connected(),"handle not connected to a file");
  fits_write_chksum (FPTR, &status);
  check_errors();
  }

const vector<int64> &fitshandle::axes() const
  {
  planck_assert(image_hdu(),"not connected to an image");
  return axes_;
  }
const string &fitshandle::colname(int i) const
  {
  planck_assert(table_hdu(i),"incorrect FITS table access");
  return columns_[i-1].name();
  }
const string &fitshandle::colunit(int i) const
  {
  planck_assert(table_hdu(i),"incorrect FITS table access");
  return columns_[i-1].unit();
  }
int64 fitshandle::repcount(int i) const
  {
  planck_assert(table_hdu(i),"incorrect FITS table access");
  return columns_[i-1].repcount();
  }
PDT fitshandle::coltype(int i) const
  {
  planck_assert(table_hdu(i),"incorrect FITS table access");
  return columns_[i-1].type();
  }
int fitshandle::ncols() const
  {
  planck_assert(table_hdu(1),"incorrect FITS table access");
  return columns_.size();
  }
int64 fitshandle::nrows() const
  {
  planck_assert(table_hdu(1),"incorrect FITS table access");
  return nrows_;
  }
int64 fitshandle::nelems(int i) const
  {
  planck_assert(table_hdu(i),"incorrect FITS table access");
  if (columns_[i-1].type()==PLANCK_STRING) return nrows_;
  return nrows_*columns_[i-1].repcount();
  }
int64 fitshandle::efficientChunkSize(int i) const
  {
  planck_assert(table_hdu(1),"incorrect FITS table access");
  long int res;
  fits_get_rowsize(FPTR, &res, &status);
  planck_assert(res>=1,"bad recommended FITS chunk size");
  check_errors();
  return res*columns_[i-1].repcount();
  }

void fitshandle::get_all_keys(vector<string> &keys) const
  {
  keys.clear();
  char card[81];
  const char *inclist[] = { "*" };
  planck_assert(connected(),"handle not connected to a file");
  fits_read_record (FPTR, 0, card, &status);
  check_errors();
  while (true)
    {
    fits_find_nextkey (FPTR, const_cast<char **>(inclist), 1,
      0, 0, card, &status);
    if (status!=0) break;
    if (fits_get_keyclass(card)==TYP_USER_KEY)
      {
      char keyname[80];
      int dummy;
      fits_get_keyname(card, keyname, &dummy, &status);
      check_errors();
      keys.push_back(trim(keyname));
      }
    check_errors();
    }
  if (status==KEY_NO_EXIST) { fits_clear_errmsg(); status=0; }
  check_errors();
  }

void fitshandle::set_key_void (const string &key, const void *value,
  PDT type, const string &comment)
  {
  planck_assert(connected(),"handle not connected to a file");
  string key2 = fixkey(key);
  switch (type)
    {
    case PLANCK_INT8:
    case PLANCK_UINT8:
    case PLANCK_INT16:
    case PLANCK_INT32:
    case PLANCK_INT64:
    case PLANCK_FLOAT32:
    case PLANCK_FLOAT64:
      fits_update_key (FPTR, type2ftc(type), const_cast<char *>(key2.c_str()),
        const_cast<void *>(value), const_cast<char *>(comment.c_str()),
        &status);
      break;
    case PLANCK_BOOL:
      {
      int val = *(static_cast<const bool *>(value));
      fits_update_key (FPTR, TLOGICAL, const_cast<char *>(key2.c_str()),
        &val, const_cast<char *>(comment.c_str()), &status);
      break;
      }
    case PLANCK_STRING:
      {
      string val = *(static_cast<const string *>(value));
      fits_update_key_longstr (FPTR, const_cast<char *>(key2.c_str()),
        const_cast<char *>(val.c_str()), const_cast<char *>(comment.c_str()),
        &status);
      break;
      }
    default:
      planck_fail ("unsupported data type in set_key_void()");
    }
  check_errors();
  }

void fitshandle::get_key_void (const string &name, void *value, PDT type) const
  {
  planck_assert(connected(),"handle not connected to a file");
  switch (type)
    {
    case PLANCK_INT8:
    case PLANCK_UINT8:
    case PLANCK_INT16:
    case PLANCK_INT32:
    case PLANCK_INT64:
    case PLANCK_FLOAT32:
    case PLANCK_FLOAT64:
      fits_read_key (FPTR, type2ftc(type), const_cast<char *>(name.c_str()),
        value, 0, &status);
      getKeyHelper(name);
      break;
    case PLANCK_BOOL:
      {
      int val;
      fits_read_key (FPTR, TLOGICAL, const_cast<char *>(name.c_str()), &val, 0,
        &status);
      getKeyHelper(name);
      *(static_cast<bool *>(value))=val;
      break;
      }
    case PLANCK_STRING:
      {
      char *tmp=0;
      fits_read_key_longstr (FPTR, const_cast<char *>(name.c_str()), &tmp, 0,
        &status);
      getKeyHelper(name);
      *(static_cast<string *>(value))=tmp;
      if (tmp) free(tmp);
      break;
      }
    default:
      planck_fail ("unsupported data type in get_key_void()");
    }
  check_errors();
  }

void fitshandle::delete_key (const string &name)
  {
  planck_assert(connected(),"handle not connected to a file");
  fits_delete_key (FPTR, const_cast<char *>(name.c_str()), &status);
  check_errors();
  }

void fitshandle::add_comment(const string &comment)
  {
  planck_assert(connected(),"handle not connected to a file");
  fits_write_comment(FPTR,const_cast<char *>(comment.c_str()),&status);
  check_errors();
  }

bool fitshandle::key_present(const string &name) const
  {
  char card[81];
  planck_assert(connected(),"handle not connected to a file");
  fits_read_card(FPTR, const_cast<char *>(name.c_str()), card, &status);
  if (status==KEY_NO_EXIST)
    { fits_clear_errmsg(); status=0; return false; }
  check_errors();
  return true;
  }

void fitshandle::assert_pdmtype (const string &pdmtype) const
  {
  string type;
  get_key("PDMTYPE",type);
  if (pdmtype==type) return;
  cerr << "PDMTYPE " << pdmtype << " expected, but found " << type << endl;
  }

void fitshandle::read_column_raw_void
  (int colnum, void *data, PDT type, int64 num, int64 offset) const
  {
  switch (type)
    {
    case PLANCK_INT8:
    case PLANCK_UINT8:
    case PLANCK_INT16:
    case PLANCK_INT32:
    case PLANCK_INT64:
    case PLANCK_FLOAT32:
    case PLANCK_FLOAT64:
    case PLANCK_BOOL:
      read_col (colnum, data, num, type, offset); break;
    case PLANCK_STRING:
      {
      string *data2 = static_cast<string *> (data);
      planck_assert(table_hdu(colnum),"incorrect FITS table access");
      planck_assert (num<=(nrows_-offset),
        "read_column(): array too large");
      arr2b<char> tdata(safe_cast<tsize>(num),
                        safe_cast<tsize>(columns_[colnum-1].repcount()+1));
      int dispwidth;
      fits_get_col_display_width(FPTR, colnum, &dispwidth, &status);
      planck_assert(dispwidth<=columns_[colnum-1].repcount(),"column too wide");
      fits_read_col (FPTR, TSTRING, colnum, offset+1, 1, num,
        0, tdata.p0(), 0, &status);
      check_errors();
      for (long m=0;m<num;++m) data2[m]=tdata[m];
      break;
      }
    default:
      planck_fail ("unsupported data type in read_column_raw_void()");
    }
  }

void fitshandle::write_column_raw_void
  (int colnum, const void *data, PDT type, int64 num, int64 offset)
  {
  switch (type)
    {
    case PLANCK_INT8:
    case PLANCK_UINT8:
    case PLANCK_INT16:
    case PLANCK_INT32:
    case PLANCK_INT64:
    case PLANCK_FLOAT32:
    case PLANCK_FLOAT64:
    case PLANCK_BOOL:
      write_col (colnum, data, num, type, offset); break;
    case PLANCK_STRING:
      {
      const string *data2 = static_cast<const string *> (data);
      planck_assert(table_hdu(colnum),"incorrect FITS table access");
      tsize stringlen = safe_cast<tsize>(columns_[colnum-1].repcount()+1);
      arr2b<char> tdata(safe_cast<tsize>(num), stringlen);
      for (long m=0;m<num;++m)
        {
        strncpy(tdata[m],data2[m].c_str(),stringlen-1);
        tdata[m][stringlen-1] = '\0';
        }
      fits_write_col (FPTR, TSTRING, colnum, offset+1, 1, num,
        tdata.p0(), &status);
      nrows_ = max(nrows_,offset+num);
      check_errors();
      break;
      }
    default:
      planck_fail ("unsupported data type in write_column_raw_void()");
    }
  }

void fitshandle::write_image2D_void (const void *data, PDT type, tsize s1,
  tsize s2)
  {
  planck_assert(image_hdu(),"not connected to an image");
  planck_assert (axes_.size()==2, "wrong number of dimensions");
  planck_assert (axes_[0]==int64(s1), "wrong size of dimension 1");
  planck_assert (axes_[1]==int64(s2), "wrong size of dimension 2");

  fits_write_img (FPTR, type2ftc(type), 1, axes_[0]*axes_[1],
    const_cast<void *>(data), &status);
  check_errors();
  }

void fitshandle::write_subimage_void (const void *data, PDT type, tsize sz,
  int64 offset)
  {
  planck_assert(image_hdu(),"not connected to an image");
  fits_write_img (FPTR, type2ftc(type), 1+offset, sz, const_cast<void *>(data),
    &status);
  check_errors();
  }

template<typename T> void fitshandle::read_image (arr2<T> &data) const
  {
  planck_assert(image_hdu(),"not connected to an image");
  planck_assert (axes_.size()==2, "wrong number of dimensions");
  data.alloc(safe_cast<tsize>(axes_[0]), safe_cast<tsize>(axes_[1]));
  fits_read_img (FPTR, fitsType<T>(), 1, axes_[0]*axes_[1], 0, &data[0][0], 0,
    &status);
  check_errors();
  }

template void fitshandle::read_image (arr2<float> &data) const;
template void fitshandle::read_image (arr2<double> &data) const;

template<typename T> void fitshandle::read_image (arr3<T> &data) const
  {
  planck_assert(image_hdu(),"not connected to an image");
  planck_assert (axes_.size()==3, "wrong number of dimensions");
  data.alloc(safe_cast<tsize>(axes_[0]), safe_cast<tsize>(axes_[1]),
    safe_cast<tsize>(axes_[2]));
  fits_read_img (FPTR, fitsType<T>(), 1, axes_[0]*axes_[1]*axes_[2],
    0, &data(0,0,0), 0, &status);
  check_errors();
  }

template void fitshandle::read_image (arr3<float> &data) const;
template void fitshandle::read_image (arr3<double> &data) const;

template<typename T> void fitshandle::read_subimage
  (arr2<T> &data, int xl, int yl) const
  {
  planck_assert(image_hdu(),"not connected to an image");
  planck_assert (axes_.size()==2, "wrong number of dimensions");
  for (tsize m=0; m<data.size1(); ++m)
    fits_read_img (FPTR, fitsType<T>(), (xl+m)*axes_[1]+yl+1,
      data.size2(), 0, &data[m][0], 0, &status);
  check_errors();
  }

template void fitshandle::read_subimage
  (arr2<float> &data, int xl, int yl) const;
template void fitshandle::read_subimage
  (arr2<double> &data, int xl, int yl) const;

void fitshandle::read_subimage_void (void *data, PDT type, tsize ndata,
  int64 offset) const
  {
  planck_assert(image_hdu(),"not connected to an image");
  fits_read_img (FPTR, type2ftc(type), 1+offset, ndata, 0, data, 0, &status);
  check_errors();
  }

namespace {

class cfitsio_checker
  {
  public:
    cfitsio_checker()
      {
      float fitsversion;
      planck_assert(fits_get_version(&fitsversion),
        "error calling fits_get_version()");
      }
  };

cfitsio_checker Cfitsio_Checker;

} // unnamed namespace