/* FITS specific macros and structures */

# ifndef FITSIO
# define FITSIO

/********** FITS Constants *********/
# define FT_TEXT_LENGTH          18  /* max length text header field */
# define FT_MAX_NAXES            10  /* max number of axes */
# define FT_FIELD_LENGTH          8  /* max length header field */   
# define FT_COMMENT_LENGTH       47  /* max length comment field */
# define FT_HISTORY_LENGTH       72  /* max length history / comment */
# define FT_LINE_LENGTH          80  /* FITS header line length */
# define FT_RECORD_SIZE        2880  /* FITS block size */

extern int FT_UNSIGN_MODE; /* is unsigned the default? */

/* this structure defines the buffer which contains a header
   and contains the minimum required keywords */

typedef struct {
  int                     simple;
  int                     unsign;
  int                     extend;
  int                     bitpix;
  int                     Naxes;
  int                     Naxis[FT_MAX_NAXES];
  int                     size;
  double                  bzero;
  double                  bscale;
  char                   *buffer;
} Header;

/* this structure defines the buffer which contains a matrix
   and contains some relavant info */

typedef struct {
  int                     unsign;
  int                     bitpix;
  int                     Naxes;
  int                     Naxis[FT_MAX_NAXES];
  int                     size;
  double                  bzero;
  double                  bscale;
  char                   *buffer;
} Matrix;
   
/* the FTable represents a complete table on disk */          
typedef struct {
  Header                 *header;
  char                   *buffer;
  int                     size;
} FTable;

/* the VTable represents rows from a table on disk */          
typedef struct {
  Header                 *header;
  char                  **buffer;
  int                     Nrow;
  int                    *row;
  int                     size;  /* total buffer size */
  int                     pad;   /* bytes of padding at the end */
} VTable;

# ifndef PROTO
# define PROTO(A) A
# endif

/******************************* Header functions *************/
int   fits_read_header            PROTO((char *, Header *));
int   fits_copy_header            PROTO((Header *, Header *)); 
void  fits_free_header            PROTO((Header *)); 
int   fits_write_header           PROTO((char *, Header *)); 
int   fits_create_header          PROTO((Header *)); 
int   fits_delete                 PROTO((Header *, char *, int)); 
char *fits_header_field           PROTO((Header *, char *, int)); 
int   fits_read_Xheader           PROTO((char *filename, Header *header, int N));
int   fits_load_header            PROTO((FILE *f, Header *header));

int   fits_fread_header           PROTO((FILE *, Header *));
int   fits_fread_Theader          PROTO((FILE *, Header *));
int   fits_fread_ftable           PROTO((FILE *, FTable *, char *)); 
int   fits_fread_vtable           PROTO((FILE *f, VTable *table, char *extname, int Nrow, int *row));
int   fits_fwrite_header          PROTO((FILE *, Header *)); 
int   fits_fwrite_Theader         PROTO((FILE *, Header *));
int   fits_fwrite_matrix          PROTO((FILE *, Matrix *));      
int   fits_fwrite_table           PROTO((FILE *, FTable *)); 
int   fits_fwrite_vtable          PROTO((FILE *, VTable *)); 

int   fits_matrix_size            PROTO ((Header *));
int   fits_init_header            PROTO ((Header *));
int   fits_create_table_header    PROTO ((Header *, char *, char *));
int   fits_table_to_vtable        PROTO ((FTable *ftable, VTable *vtable, int start, int Nkeep));
int   fits_create_table           PROTO ((Header *, FTable *));
int   fits_vadd_rows              PROTO ((VTable *, char *, int, int));
int   fits_add_rows               PROTO ((FTable *, char *, int, int));
char *fits_table_print            PROTO ((FTable *,...));

/******************************* Matrix functions *************/
int   fits_read_matrix            PROTO((char *, Matrix *));      
int   fits_read_portion           PROTO((char *filename, Matrix *matrix, int Nskip, int Npix));
void  fits_free_matrix            PROTO((Matrix *)); 
int   fits_copy_matrix            PROTO((Matrix *, Matrix *)); 
int   fits_write_matrix           PROTO((char *,Matrix *)); 
int   fits_create_matrix          PROTO((Header *, Matrix *)); 
double  fits_get_matrix_value     PROTO((Matrix *, int, int)); 
void  fits_set_matrix_value       PROTO((Matrix *, int, int, double)); 
void  fits_add_matrix_value       PROTO((Matrix *, int, int, double)); 
int   fits_multiply_matrix        PROTO((Matrix *, Matrix *, Matrix *)); 
int   fits_divide_matrix          PROTO((Matrix *, Matrix *, Matrix *)); 
void  fits_insert_array           PROTO((Matrix *, Matrix *, int, int)); 
int   fits_load_matrix            PROTO((FILE *f, Matrix *matrix, Header *header));
int   fits_fread_matrix           PROTO((FILE *f, Matrix *matrix, Header *header));
int   fits_read_segment           PROTO((char *filename, Matrix *matrix, char *region));
int   fits_convert_format         PROTO((Header *header, Matrix *matrix, int outBitpix, double outScale, double outZero, int outUnsign));

/******************************* Table functions *************/
int   fits_load_Theader           PROTO((FILE *f, Header *Theader));
int   fits_read_Theader           PROTO((char *, Header *));     
int   fits_read_table             PROTO((char *, FTable *)); 
int   fits_write_Theader          PROTO((char *, Header *header));
int   fits_write_table            PROTO((char *, FTable *)); 
int   fits_bintable_format        PROTO((char *, char *, int *, int *));
int   fits_table_format           PROTO((char *, char *, int *, int *));
int   fits_save_header            PROTO((FILE *, Header *header));
int   fits_set_bintable_column    PROTO((Header *header, FTable *table, char *, void *, int));
int   fits_set_table_column       PROTO((Header *header, FTable *table, char *, void *, int));
int   fits_define_bintable_column PROTO((Header *header, char *, char *, char *, char *, double, double));
int   fits_define_table_column    PROTO((Header *header, char *, char *, char *, char *));

int   fits_free_table             PROTO((FTable *table));
int   fits_free_vtable            PROTO((VTable *table));

/* prototyping demanded for these four -- could be a problem on non-ANSI comp comp */
int   fits_scan                   PROTO((Header *, char *, char *, int,...)); 
int   fits_print                  PROTO((Header *, char *, char *, int,...)); 
int   fits_modify                 PROTO((Header *, char *, char *, int,...)); 
int   fits_table_column           PROTO((FTable *, char *, char *,...)); 
int   fits_table_column           PROTO((FTable *table, char *, char *,...));

char *fits_keyword_end            PROTO((char *line));
int   fits_vscan                  PROTO((Header *header, char *field, char *mode, int N, va_list argp));
int   fits_scan                   PROTO((Header *header, char *field, char *mode, int N,...));
int   fits_stripwhite             PROTO((char *string));
int   fits_matrix_size            PROTO((Header *header));

int   fits_get_bintable_column_type PROTO((Header *header, char *label, char *type, int *Nval));
int   fits_get_bintable_column    PROTO((Header *header, FTable *table, char *label, void **data));
int   fits_get_table_column_type  PROTO((Header *header, char *label, char *type));
int   fits_get_table_column       PROTO((Header *header, FTable *table, char *label, void **data));

#endif /* FITSIO */

/* this structure defines the buffer which contains a table
   and contains some relavant info */

# if (0)
/* an old construct, no longer used.  remove if not needed */
typedef struct {
  int                     bitpix;
  int                     Nfields;
  int                     Naxes;
  int                     Naxis[FT_MAX_NAXES];
  int                     size;
  char                   *buffer;
  Header                  header;
} Table;
# endif
          
