/*
    use_db.h
    Headers for library functions that call codas routines,
    or are otherwise closely associated with CODAS use.

*/

#ifndef use_db_included
#define use_db_included
#ifndef dbhost_included
#include "dbhost.h"       /* PROTOTYPE_ALLOWED */
#endif
#ifndef dbext_included
#include "dbext.h"        /* YMDHMS_TIME_TYPE, DMSH_POSITION_TYPE */
#endif

/******** position type definitions: ***************************/

typedef struct
{
   DMSH_POSITION_TYPE lon, lat;
} DMSH_LL_TYPE;

typedef struct
{
   double lon, lat;
} DOUBLE_LL_TYPE;

typedef struct
{
   long lon, lat;
} HUN_LL_TYPE;

/******** position conversion macros: **************************/
/* convert decimal degrees to hundredths of a second */
#define  to_hundredths(x)  (long) ((x) * 360000.0)
/* convert hundredths of a second to decimal degrees */
#define  to_degrees(x)   ((x) / 360000.0)

/******** range.c definitions: ***************/
#define TIME_RANGE          1
#define DAY_RANGE           2
#define BLOCK_RANGE         3
#define BLKPRF_RANGE        4

#define GAP         1
#define OVERLAP     2
#define OKAY        0
#define BAD_RANGE  -1

typedef struct
{
   YMDHMS_TIME_TYPE start, end;
} TIME_RANGE_TYPE;

typedef struct
{
   double start, end;
} DAY_RANGE_TYPE;

typedef struct
{
   int start, end;
} BLOCK_RANGE_TYPE;

typedef struct
{
   int block, profile;
} BLKPRF_INDEX_TYPE;

typedef struct
{
   BLKPRF_INDEX_TYPE start, end;
} BLKPRF_RANGE_TYPE;

typedef struct
{
   UBYTE type;
   union
   {
      BLOCK_RANGE_TYPE  block;
      BLKPRF_RANGE_TYPE blkprf;
      DAY_RANGE_TYPE    day;
      TIME_RANGE_TYPE   time;
   } ru;
} RANGE_TYPE;

#if PROTOTYPE_ALLOWED

/*
    FUNCTIONS IN CHKTIM
*/
int check_time(
   YMDHMS_TIME_TYPE *now,               /* output */
   YMDHMS_TIME_TYPE *start,             /* input */
   YMDHMS_TIME_TYPE *end);              /* input */
/*
    FUNCTIONS IN TIME_IO
*/
int get_time_range(FILE *fp, YMDHMS_TIME_TYPE *t1, YMDHMS_TIME_TYPE *t2);
void write_ymdhms_time(FILE *fp, YMDHMS_TIME_TYPE *t);
void print_time_range(FILE *fp, YMDHMS_TIME_TYPE *start,
                                YMDHMS_TIME_TYPE *end);
int get_time(FILE *fp, YMDHMS_TIME_TYPE *t1);
/*
    FUNCTIONS IN NBLKPRF
*/
int get_nprofs(void);
int get_nblocks(void);
/*
    FUNCTIONS IN POS_IO
*/
void get_latlon(DOUBLE_LL_TYPE *pos);
void get_hun_latlon(HUN_LL_TYPE *pos);
void copy_latlon(DOUBLE_LL_TYPE *dest, DOUBLE_LL_TYPE *source);
void copy_hun_latlon(HUN_LL_TYPE *dest, HUN_LL_TYPE *source);
/*
    FUNCTIONS IN CHECK_DB
*/
unsigned int check_dbget_f(int type, float *x, unsigned int n, char *err_msg);
unsigned int check_dbget(int type, char *x, unsigned int n, char *err_msg);
void check_dbset(int dbid, char *err_msg);
void check_dbopen(int dbid, char *dbname, int access, int mem_mode);
void check_dbsrch(int search_type, char *search_parameters);
void check_dbclose(void);
void check_dbmove(int nsteps);
void flag_data(float data[], UBYTE profile_flags[], int n, UBYTE mask);
/*
    FUNCTIONS IN DB_CNF
*/
int dbopen_cnf(int dbid, char *dbname, int accmode, int memmode);
void dbclose_cnf(void);
int dbget_cnf(int type, char *data, unsigned int *nbytes, char * msg);
int dbget_f_cnf(int type, float *data, unsigned int *nbytes, char * msg);
int dbadd_cnf(int type, char *data, unsigned int nbytes, char *msg);
int dbput_cnf(int type, char *data, unsigned int *nbytes, char *msg);
int dbsrch_cnf(int type, char *param);
int dbmove_cnf(int nsteps);
int check_prftime(YMDHMS_TIME_TYPE *now, YMDHMS_TIME_TYPE *end);
int check_blkprf(int c_blkprf[], int e_blkprf[]);
int set_block_dpmask(int bit_num);
int set_db_dpmask(int bit_num);
int check_block_dpmask(int bit_num);
/*
    FUNCTIONS IN range_io.c
*/
int get_range(FILE *fp, RANGE_TYPE *range_type);
int in_range(char *value, RANGE_TYPE *range);
int goto_start_of_range(RANGE_TYPE *range);
int goto_end_of_range(RANGE_TYPE *range);
int check_range(RANGE_TYPE *range);
int check_for_gap_at_end(RANGE_TYPE *range);
void print_range(FILE *fp, RANGE_TYPE *range, char *string);
int BPCMP(BLKPRF_INDEX_TYPE *bp1, BLKPRF_INDEX_TYPE *bp2);

#else
/*
    FUNCTIONS IN CHKTIM
*/
int check_time();
/*
    FUNCTIONS IN TIME_IO
*/
int get_time_range();
void write_ymdhms_time();
int check_year_base();
void print_time_range();
/*
    FUNCTIONS IN NBLKPRF
*/
int get_nprofs();
int get_nblocks();
/*
    FUNCTIONS IN POS_IO
*/
void get_latlon();
void get_hun_latlon();
void copy_latlon();
void copy_hun_latlon();
/*
    FUNCTIONS IN CHECK_DB
*/
unsigned int check_dbget_f();
unsigned int check_dbget();
void check_dbset();
void check_dbopen();
void check_dbsrch();
void check_dbclose();
void check_dbmove();
void flag_data();
/*
    FUNCTIONS IN DB_CNF
*/
int dbopen_cnf();
void dbclose_cnf();
int dbget_cnf();
int dbget_f_cnf();
int dbadd_cnf();
int dbput_cnf();
int dbsrch_cnf();
int dbmove_cnf();
int check_prftime();
int check_blkprf();
int set_block_dpmask();
int set_db_dpmask();
int check_block_dpmask();
/*
    FUNCTIONS IN range_io.c
*/
int get_range();
int in_range();
int goto_start_of_range();
int goto_end_of_range();
int check_range();
int check_for_gap_at_end();
void print_range();
int BPCMP();

#endif

/* 1998/01/22 Pierre Jaccard 
   These functions are included in library use_db; include here their
   prototype 
*/
#include "pos_to_m.h"

#endif /* ifndef use_db_included */

