summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/indi/fitsrw.h
blob: 9a24693099370622d87c5a09b55c599b6923e3f1 (plain)
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
/******************************************************************************/
/*                      Peter Kirchgessner                                    */
/*                      e-mail: pkirchg@aol.com                               */
/*                      WWW   : http://members.aol.com/pkirchg                */
/******************************************************************************/
/*  #BEG-HDR                                                                  */
/*                                                                            */
/*  Package       : FITS reading/writing library                              */
/*  Modul-Name    : fitsrw.h                                                  */
/*  Description   : Include file for FITS-r/w-library                         */
/*  Function(s)   :                                                           */
/*  Author        : P. Kirchgessner                                           */
/*  Date of Gen.  : 12-Apr-97                                                 */
/*  Last modified : 17-May-97                                                 */
/*  Version       : 0.10                                                      */
/*  Compiler Opt. :                                                           */
/*  Changes       :                                                           */
/*                                                                            */
/*  #END-HDR                                                                  */
/******************************************************************************/

/** \file fitsrw.h
    \brief FITS reading/writing library.
    \author Peter Kirchgessner
*/

#ifndef FITS_MAX_AXIS

#include <stdio.h>

#define FITS_CARD_SIZE      80
#define FITS_RECORD_SIZE  2880
#define FITS_MAX_AXIS      999

#define FITS_NADD_CARDS    128

#ifdef __cplusplus
extern "C" {
#endif

/* Data representations */
/** \typedef FITS_BITPIX8
    \brief One byte declared as unsigned char.
*/
typedef unsigned char FITS_BITPIX8;

/** \typedef FITS_BITPIX16
    \brief Two bytes declared as short.
*/
typedef short         FITS_BITPIX16;

/** \typedef FITS_BITPIX32
    \brief Four bytes declared as long.
*/
typedef long          FITS_BITPIX32;

/** \typedef FITS_BITPIXM32
    \brief IEEE -32 FITS format declared as 4-byte float.
*/
typedef float         FITS_BITPIXM32;

/** \typedef FITS_BITPIXM64
    \brief IEEE -64 FITS format declared as 8-byte double.
*/
typedef double        FITS_BITPIXM64;

typedef int           FITS_BOOL;
typedef long          FITS_LONG;
typedef double        FITS_DOUBLE;
typedef char          FITS_STRING[FITS_CARD_SIZE];

typedef enum {
 typ_bitpix8, typ_bitpix16, typ_bitpix32, typ_bitpixm32, typ_bitpixm64,
 typ_fbool, typ_flong, typ_fdouble, typ_fstring
} FITS_DATA_TYPES;

/** \struct FITS_PIX_TRANSFORM
    \brief A struct that describes how to transform FITS pixel values.
*
* The pixel values represent the lower and upper boundaries of the pixel data from the FITS file. The data values represent the range of the final pixel values. For example, to transform FITS pixel values to grey scale, the data min is 0 and data max is 255. The transformation is linear.
*/
typedef struct {
 double pixmin, pixmax;    /** The pixel values [pixmin,pixmax] that should be mapped */
 double datamin, datamax;  /** The data values [datamin,datamax] that the pixel values should be mapped to*/
 double replacement;       /** datavalue to use for blank or NaN pixels */
 char dsttyp;              /** Destination typ ('c' = char) */
} FITS_PIX_TRANSFORM;

typedef union {
 FITS_BITPIX8   bitpix8;
 FITS_BITPIX16  bitpix16;
 FITS_BITPIX32  bitpix32;
 FITS_BITPIXM32 bitpixm32;
 FITS_BITPIXM64 bitpixm64;

 FITS_BOOL   fbool;
 FITS_LONG   flong;
 FITS_DOUBLE fdouble;
 FITS_STRING fstring;
} FITS_DATA;

/** \struct FITS_RECORD_LIST
    \brief Record list.
*/
typedef struct fits_record_list { 
 unsigned char data[FITS_RECORD_SIZE];
 struct fits_record_list *next_record;
} FITS_RECORD_LIST;

/** \struct FITS_HDU_LIST
    \brief Header and Data Unit List.
    
* The structure hold header and data unit lists. The \p used struct contains flags specifying if some cards are used.
*/
typedef struct fits_hdu_list {    
 long header_offset;              /** Offset of header in the file */
 long data_offset;                /** Offset of data in the file */
 long data_size;                  /** Size of data in the HDU (including pad)*/
 long udata_size;                 /** Size of used data in the HDU (excl. pad) */
 int  bpp;                        /** Bytes per pixel */
 int numpic;                      /** Number of interpretable images in HDU */
 int naddcards;                   /** Number of additional cards */
 char addcards[FITS_NADD_CARDS][FITS_CARD_SIZE];
 struct {
   char nan_value;                /** NaN's found in data ? */
   char blank_value;              /** Blanks found in data ? */
                                  
   char blank;                    
   char datamin;                  
   char datamax;
   char simple;                   /** This indicates a simple HDU */
   char xtension;                 /** This indicates an extension */
   char gcount;
   char pcount;
   char bzero;
   char bscale;
   char groups;
   char extend;
 } used;
 double pixmin, pixmax;           /** Minimum/Maximum pixel values */
                                  /* Some decoded data of the HDU: */
 int naxis;                       /** Number of axes */
 int naxisn[FITS_MAX_AXIS];       /** Sizes of axes (NAXIS1 --> naxisn[0]) */
 int bitpix;                      /** Data representation (8,16,32,-16,-32) */
                                  /* When using the following data, */
                                  /* the used-flags must be checked before. */
 long blank;                      /** Blank value. Check the \p used struct to verify if this is a valid value*/
 double datamin, datamax;         /** Minimum/Maximum physical data values. Check the \p used struct to verify if this is a valid value*/
 char xtension[FITS_CARD_SIZE];   /** Type of extension. Check the \p used struct to verify if this is a valid value*/ 
 long gcount, pcount;             /** Used by XTENSION. Check the \p used struct to verify if this is a valid value*/ 
 double bzero, bscale;            /** Transformation values. Check the \p used struct to verify if this is a valid value*/ 
 int groups;                      /** Random groups indicator. Check the \p used struct to verify if this is a valid value*/ 
 int extend;                      /** Extend flag. Check the \p used struct to verify if this is a valid value*/ 

 FITS_RECORD_LIST *header_record_list; /** Header records read in */
 struct fits_hdu_list *next_hdu;
} FITS_HDU_LIST;

/** \struct FITS_FILE
    \brief Structure to hold FITS file information and pointers.
*/
typedef struct {
 FILE *fp;                    /** File pointer to fits file */
 char openmode;               /** Mode the file was opened (0, 'r', 'w') */

 int n_hdu;                   /** Number of HDUs in file */
 int n_pic;                   /** Total number of interpretable pictures */
 int nan_used;                /** NaN's used in the file ? */
 int blank_used;              /** Blank's used in the file ? */

 FITS_HDU_LIST *hdu_list;     /** Header and Data Unit List */
} FITS_FILE;


/* User callable functions of the FITS-library */
/**
 * \defgroup fitsFunctions User callable functions of the FITS-library.
 */
/*@{*/

/** \brief open a FITS file.
    \param filename name of file to open
    \param openmode mode to open the file ("r", "w")
    \return On success, a FITS_FILE-pointer is returned. On failure, a NULL-pointer is returned. The functions scans through the file loading each header and analyzing them.
*/
FITS_FILE     *fits_open (const char *filename, const char *openmode);

/** \brief close a FITS file.
    \param ff FITS file pointer.
*/
void           fits_close (FITS_FILE *ff);

/** \brief add a HDU to the file.
    \param ff FITS file pointer.
    \return Adds a new HDU to the list kept in ff. A pointer to the new HDU is returned. On failure, a NULL-pointer is returned.
*/
FITS_HDU_LIST *fits_add_hdu (FITS_FILE *ff);

/** \brief add a card to the HDU.
    The card must follow the standards of FITS. The card must not use a 
    keyword that is written using *hdulist itself.
    \param hdulist HDU listr.
    \param card card to add.
    \return On success 0 is returned. On failure -1 is returned.
*/
int            fits_add_card (FITS_HDU_LIST *hdulist, char *card);

/** \brief print the internal representation of a single header.
    \param hdr pointer to the header
*/
void           fits_print_header (FITS_HDU_LIST *hdr);

/** \brief write a FITS header to the file.
    \param ff FITS-file pointer.
    \param hdulist pointer to header.
    \return On success, 0 is returned. On failure, -1 is returned.
*/
int            fits_write_header (FITS_FILE *ff, FITS_HDU_LIST *hdulist);

/** \brief get information about an image.
    \param ff FITS-file pointer.
    \param picind Index of picture in file (1,2,...)
    \param hdupicind Index of picture in HDU (1,2,...)
    \return The function returns on success a pointer to a FITS_HDU_LIST. hdupicind then gives the index of the image within the HDU. On failure, NULL is returned.
*/
FITS_HDU_LIST *fits_image_info (FITS_FILE *ff, int picind, int *hdupicind);

/** \brief position to a specific image.
    The function positions the file pointer to a specified image.
    \param ff FITS-file pointer.
    \param picind Index of picture to seek (1,2,...)
    \return The function returns on success a pointer to a FITS_HDU_LIST. This pointer must also be used when reading data from the image. On failure, NULL is returned.
*/
FITS_HDU_LIST *fits_seek_image (FITS_FILE *ff, int picind);

/** \brief decode a card
    Decodes a card and returns a pointer to the union, keeping the data. 
    \param card pointer to card image.
    \param data_type datatype to decode.
    \return If card is NULL or on failure, a NULL-pointer is returned. If the card does not have the value indicator, an error is generated, but its tried to decode the card. The data is only valid up to the next call of the function.
*/
FITS_DATA     *fits_decode_card (const char *card, FITS_DATA_TYPES data_type);

/** \brief search a card in the record list.
    A card is searched in the reord list. Only the first eight characters of keyword are significant. If keyword is less than 8 characters, its filled with blanks.
    \param rl record list to search.
    \param keyword keyword identifying the card.
    \return If the card is found, a pointer to the card is returned. The pointer does not point to a null-terminated string. Only the next 80 bytes are allowed to be read. On failure a NULL-pointer is returned.
*/
char          *fits_search_card (FITS_RECORD_LIST *rl, const char *keyword);

/** \brief read pixel values from a file
    The function reads npix pixel values from the file, transforms them checking for blank/NaN pixels and stores the transformed values in buf. hdulist must be a pointer returned by fits_seek_image(). Before starting to read an image, fits_seek_image() must be called. Even for successive images.
    \param ff FITS file structure.
    \param hdulist pointer to hdulist that describes image.
    \param npix number of pixel values to read.
    \param trans pixel transformation.
    \param buf buffer where to place transformed pixels.
    \return The number of transformed pixels is returned. If the returned value is less than npix (or even -1), an error has occured.
*/
int            fits_read_pixel (FITS_FILE *ff, FITS_HDU_LIST *hdulist,
                                int npix, FITS_PIX_TRANSFORM *trans, void *buf);

/** \brief get an error message.
    \return If an error message has been set, a pointer to the message is returned. Otherwise a NULL pointer is returned. An inquired error message is removed from the error FIFO.
*/
char           *fits_get_error (void);

/*@}*/

int 	       fits_nan_32 (unsigned char *v);
int            fits_nan_64 (unsigned char *v);

/* Demo functions */
#define FITS_NO_DEMO
int fits_to_pgmraw (char *fitsfile, char *pgmfile);
int pgmraw_to_fits (char *pgmfile, char *fitsfile);

#ifdef __cplusplus
}
#endif

#endif