# include <ohana.h>
# include <dvo.h>
# include <signal.h>

typedef struct {
    float zpt;
    float zpt_err;
    e_time time;
    int found;
} ZptTable;

typedef struct {
  float Mcal;
  float dMcal;
  unsigned int imageID;
  unsigned int photom_map_id;
  unsigned int flags;
} ImageSubset;

/* global variables set in parameter file */
# define DVO_MAX_PATH 1024
char         ImageCat[DVO_MAX_PATH];
char        *CATDIR;
int          HOST_ID;
char        *HOSTDIR;
char        *IMAGES;
char        *SINGLE_CPT;
int          VERBOSE;
int          RESET;
int          UBERCAL; // load the supplied ubercal zero point fits table (with flat-field corrections)
int          NO_METADATA; // the supplied ubercal data has no descriptive metadata
int          UPDATE;
int          PARALLEL;
int          PARALLEL_MANUAL;
int          PARALLEL_SERIAL;
int          IMAGES_ONLY;

SkyRegion    UserPatch;

/***** prototypes ****/
int           main                PROTO((int argc, char **argv));

void          ConfigInit          PROTO((int *argc, char **argv));
void          initialize_setphot  PROTO((int argc, char **argv));
int           args_setphot        PROTO((int argc, char **argv));

void          initialize_setphot_client PROTO((int argc, char **argv));
int           args_setphot_client       PROTO((int argc, char **argv));
int           update_dvo_setphot_client PROTO((ImageSubset *image, off_t Nimage, FlatCorrectionTable *flatcorr));

ImageSubset  *ImageSubsetLoad     PROTO((char *filename, off_t *nimage));
int           ImageSubsetSave     PROTO((char *filename, Image *image, off_t Nimage));
Image        *ImagesFromSubset    PROTO((ImageSubset *subset, off_t N));

void 	      lock_image_db 	  PROTO((FITS_DB *db, char *filename));
void	      unlock_image_db 	  PROTO((FITS_DB *db));
void	      create_image_db 	  PROTO((FITS_DB *db));
void	      set_db 		  PROTO((FITS_DB *in));
int 	      Shutdown 		  PROTO((char *format, ...) OHANA_FORMAT(printf, 1, 2) );
void 	      TrapSignal 	  PROTO((int sig));
void 	      SetProtect 	  PROTO((int mode));
int 	      SetSignals 	  PROTO((void));

// setphot-specific prototypes
ZptTable     *load_zpt_table         PROTO((char *filename, int *nzpts));
Image        *load_images_setphot    PROTO((FITS_DB *db, off_t *Nimage));
int           match_zpts_to_images   PROTO((Image *image, off_t Nimage, ZptTable *zpts, int Nzpts));
int           update_dvo_setphot     PROTO((Image *image, off_t Nimage, FlatCorrectionTable *flatcorr));
void          update_catalog_setphot PROTO((Catalog *catalog, Image *image, off_t *index, off_t Nimage, FlatCorrectionTable *flatcorr));
void          update_catalog_setphot_client PROTO((Catalog *catalog, ImageSubset *image, off_t *index, off_t Nimage, FlatCorrectionTable *flatcorr));

ZptTable     *load_zpt_ubercal            PROTO((char *filename, int *nzpts, FlatCorrectionTable *flatcorrTable));
ZptTable     *load_zpt_ubercal_nometadata PROTO((char *filename, int *nzpts, FlatCorrectionTable *flatcorrTable));
int           match_flatcorr_to_images    PROTO((Image *image, off_t Nimage, FlatCorrectionTable *flatcorrTable));

int           parse_zpt_offsets           PROTO((char *ZPT_OFFSET_FILTERS, char *ZPT_OFFSET_VALUES));
float         apply_zpt_offset            PROTO((short code));
