/*****************************************************************************
*                                                                            *
*      COMMON OCEANOGRAPHIC DATA ACCESS SYSTEM (CODAS)                       *
*                                                                            *
*      WRITTEN BY:  RAMON CABRERA, ERIC FIRING AND JULIE RANADA              *
*                   JOINT INSTITUTE FOR MARINE AND ATMOSPHERIC RESEARCH      *
*                   1000 POPE ROAD  MSB 312                                  *
*                   HONOLULU, HI 96822                                       *
*                                                                            *
*      VERSION:     3.10                                                     *
*                                                                            *
*      DATE:        APRIL 1989, AUGUST 1995                                  *
*                                                                            *
*****************************************************************************/
/*

       FILE:  DBGLO.H

              GLOBAL DATABASE VARIABLES

              It is necessary (and desirable) to reserve space for these
              global variables only once.  Hence, FIRST_MODULE should be
              defined at the very top of the first module to be compiled.
              Ordinarily, the basic database system, codas3\source\db*.c
              would be compiled and placed in a library.
              Programs using these subroutines to build and access databases
              DO NOT NEED these global variables.  In other words, this file
              (dbglo.h), is needed only for compiling the core database
              subroutines.

              The strategy for declaration of global variables may be compiler-
              dependent.  If in doubt, check your compiler manual and make
              adjustments as needed.
*/
#ifndef dbglo_included
#define dbglo_included

#ifndef dbext_included
#include "dbext.h"       /* UBYTE, CHAR, ... */
#endif
#ifndef dbinc_included
#include "dbinc.h"       /* DATABASE_TYPE, MAX_OPEN_DATABASES */
#endif

#define MS_SIZE 18

#ifdef FIRST_MODULE
#undef FIRST_MODULE
DATABASE_TYPE *db;
DATABASE_TYPE *database_table[MAX_OPEN_DATABASES];
int current_database = 0, first_database_call = 1;
/*-----------------------------------------------------------------------------

       SPECIAL ARRAYS & CONSTANTS USED BY DATABASE MODULES

*/
/*
       SIZE OF VALUE TYPES IN BYTES
*/
UBYTE VALUE_SIZE[] =
{
   sizeof(BYTE),
   sizeof(UBYTE),
   sizeof(CHAR),
   sizeof(SHORT),
   sizeof(USHORT),
   sizeof(LONG),
   sizeof(ULONG),
   sizeof(FLOAT),      /* 4 */
   sizeof(DOUBLE),     /* 8 */
   sizeof(COMPLEX),    /* 8 */
   sizeof(CHAR),
   sizeof(BYTE)
};

struct
{
   DOUBLE d;
   FLOAT  f;
   LONG   l;
   SHORT  s;
} MACHINE_SIGNATURE[] =
{ {
   -123456.78, -123456.78, -0x12345678L, -0x1234 }
};

/* Important Note:  The indices into this table should match the
   definitions of *_COMPATIBLE_HOST in dbhost.h */
struct
{
   CHAR ms_name[5], ms_code[5];
   unsigned char signature[18];
} MS_TABLE[] =
{
   {"PC"         , "1111"   , {0xAE, 0x47, 0xE1, 0x7A, 0x0C, 0x24, 0xFE, 0xC0,
                             0x64, 0x20, 0xF1, 0xC7, 0x88, 0xA9, 0xCB, 0xED,
                             0xCC, 0xED} },
   {"VAXD"       , "2211"   , {0xF1, 0xC8, 0x63, 0x20, 0x0A, 0xD7, 0x71, 0x3D,
                             0xF1, 0xC8, 0x64, 0x20, 0x88, 0xA9, 0xCB, 0xED,
                             0xCC, 0xED} },
   {"SUN3"       , "0000"   , {0xC0, 0xFE, 0x24, 0x0C, 0x7A, 0xE1, 0x47, 0xAE,
                             0xC7, 0xF1, 0x20, 0x64, 0xED, 0xCB, 0xA9, 0x88,
                             0xED, 0xCC} },
   {""           , ""       , ""}
};

/******************************************************************************

       Definition of CODAS internal structures for use in machine conversion
       routines.
*/
struct
{
   STRUCT_DEF_HDR_TYPE block_hdr;
   STRUCT_DEF_ELEM_TYPE a[14];
   STRUCT_DEF_HDR_TYPE dataset_id;
   STRUCT_DEF_ELEM_TYPE b[1];
   STRUCT_DEF_HDR_TYPE producer_id;
   STRUCT_DEF_ELEM_TYPE c[1];
   STRUCT_DEF_HDR_TYPE data_list_entry;
   STRUCT_DEF_ELEM_TYPE d[5];
   STRUCT_DEF_HDR_TYPE struct_def_elem;
   STRUCT_DEF_ELEM_TYPE e[3];
   STRUCT_DEF_HDR_TYPE struct_def_hdr;
   STRUCT_DEF_ELEM_TYPE f[3];
   STRUCT_DEF_HDR_TYPE profile_dir_0_entry;
   STRUCT_DEF_ELEM_TYPE g[2];
   STRUCT_DEF_HDR_TYPE profile_dir_1_entry;
   STRUCT_DEF_ELEM_TYPE h[4];
   STRUCT_DEF_HDR_TYPE profile_dir_2_entry;
   STRUCT_DEF_ELEM_TYPE i[4];
   STRUCT_DEF_HDR_TYPE profile_dir_3_entry;
   STRUCT_DEF_ELEM_TYPE j[4];
   STRUCT_DEF_HDR_TYPE  data_dir_entry;
   STRUCT_DEF_ELEM_TYPE k[1];
   STRUCT_DEF_HDR_TYPE  block_dir_hdr;
   STRUCT_DEF_ELEM_TYPE l[10];
   STRUCT_DEF_HDR_TYPE  block_dir_entry;
   STRUCT_DEF_ELEM_TYPE m[8];
   STRUCT_DEF_HDR_TYPE terminator;
} dbint_sd[] =   /* array with only one (very complex) entry */
{
   "block_hdr"             , 14    ,                         "",
      "db_vers&prod_host"  , "none", CHAR_VALUE_CODE   ,  8, "",
      "dataset_id"         , "none", STRUCT_VALUE_CODE ,  1, "",
      "producer_id"        , "none", STRUCT_VALUE_CODE ,  1, "",
      "time1&2"            , "none", ULONG_VALUE_CODE  ,  2, "",
      "lon1&2&lat1&2"      , "none", LONG_VALUE_CODE   ,  4, "",
      "depth1&2"           , "none", SHORT_VALUE_CODE  ,  2, "",
      "dlo&do&bn&dt"       , "none", ULONG_VALUE_CODE  ,  4, "",
      "dln&den&dn&ddn"     , "none", USHORT_VALUE_CODE ,  4, "",
      "data&data_proc_mask", "none", ULONG_VALUE_CODE  ,  2, "",
      "struct_def_nentries", "none", USHORT_VALUE_CODE ,  1, "",
      "ms_code"            , "none", CHAR_VALUE_CODE   ,  4, "",
      "unassigned1"        , "none", USHORT_VALUE_CODE ,  1, "",
      "block_ftr_ofs"      , "none", ULONG_VALUE_CODE  ,  1, "",
      "unassigned2"        , "none", USHORT_VALUE_CODE ,  8, "",
   "dataset_id"            , 1     ,                         "",
      "instr_name&unused"  , "none", CHAR_VALUE_CODE   , 32, "",
   "producer_id"           , 1     ,                         "",
      "country/etc."       , "none", CHAR_VALUE_CODE   , 32, "",
   "data_list_entry"       , 5     ,                         "",
      "name&units"         , "none", CHAR_VALUE_CODE   , 32, "",
      "value&access_type"  , "none", SHORT_VALUE_CODE  ,  2, "",
      "access_0&_1"        , "none", ULONG_VALUE_CODE  ,  2, "",
      "offset&scale"       , "none", FLOAT_VALUE_CODE  ,  2, "",
      "unused"             , "none", ULONG_VALUE_CODE  ,  1, "",
   "struct_def_elem"       , 3     ,                         "",
      "name&units"         , "none", CHAR_VALUE_CODE   , 32, "",
      "value_type&count"   , "none", SHORT_VALUE_CODE  ,  2, "",
      "format_ptr"         , "none", CHAR_VALUE_CODE   ,  4, "",
   "struct_def_hdr"        , 3     ,                         "",
      "struct_name"        , "none", CHAR_VALUE_CODE   , 20, "",
      "nelem"              , "none", LONG_VALUE_CODE   ,  1, "",
      "link_ptr"           , "none", CHAR_VALUE_CODE   , 16, "",
   "profile_dir_0_entry"   , 2     ,                         "",
      "ofs"                , "none", LONG_VALUE_CODE   ,  1, "",
      "time"               , "none", ULONG_VALUE_CODE  ,  1, "",
   "profile_dir_1_entry"   , 4     ,                         "",
      "ofs"                , "none", LONG_VALUE_CODE   ,  1, "",
      "time"               , "none", ULONG_VALUE_CODE  ,  1, "",
      "depth1&2"           , "none", SHORT_VALUE_CODE  ,  2, "",
      "spare_key&lon&lat"  , "none", LONG_VALUE_CODE   ,  3, "",
   "profile_dir_2_entry"   , 4     ,                         "",
      "ofs"                , "none", LONG_VALUE_CODE   ,  1, "",
      "time"               , "none", ULONG_VALUE_CODE  ,  1, "",
      "depth1&2"           , "none", SHORT_VALUE_CODE  ,  2, "",
      "spare_key"          , "none", LONG_VALUE_CODE   ,  1, "",
   "profile_dir_3_entry"   , 4     ,                         "",
      "ofs"                , "none", LONG_VALUE_CODE   ,  1, "",
      "time"               , "none", ULONG_VALUE_CODE  ,  1, "",
      "depth1&2"           , "none", SHORT_VALUE_CODE  ,  2, "",
      "spare_key&lon&lat"  , "none", LONG_VALUE_CODE   ,  3, "",
   "data_dir_entry"        , 1     ,                         "",
      "ofs&nbytes"         , "none", ULONG_VALUE_CODE  ,  2, "",
   "block_dir_hdr"         , 10    ,                         "",
      "db_vers&dataset_id" , "none", CHAR_VALUE_CODE   , 36, "",
      "time1&time2"        , "none", ULONG_VALUE_CODE  ,  2, "",
      "lon1&lon2&lat1&lat2", "none", LONG_VALUE_CODE   ,  4, "",
      "depth1&depth2"      , "none", SHORT_VALUE_CODE  ,  2, "",
      "dir_type"           , "none", ULONG_VALUE_CODE  ,  1, "",
      "dir_entry_nb&ne"    , "none", USHORT_VALUE_CODE ,  2, "",
      "next_seq_number"    , "none", ULONG_VALUE_CODE  ,  1, "",
      "block_file_template", "none", CHAR_VALUE_CODE   , 40, "",
      "2masks&4unused"     , "none", ULONG_VALUE_CODE  ,  6, "",
      "producer_host"      , "none", CHAR_VALUE_CODE   ,  4, "",
   "block_dir_entry"       , 8     ,                         "",
      "producer_id"        , "none", CHAR_VALUE_CODE   , sizeof(PRODUCER_ID_TYPE), "",
      "time1&time2"        , "none", ULONG_VALUE_CODE  ,  2, "",
      "lon1&lon2&lat1&lat2", "none", LONG_VALUE_CODE   ,  4, "",
      "depth1&depth2"      , "none", SHORT_VALUE_CODE  ,  2, "",
      "block_nbytes"       , "none", ULONG_VALUE_CODE  ,  1, "",
      "file_id"            , "none", LONG_VALUE_CODE   ,  1, "",
      "2masks"             , "none", ULONG_VALUE_CODE  ,  2, "",
      "unassigned"         , "none", LONG_VALUE_CODE   ,  1, "",
   ""                      , 0     ,                         ""
};

#else

extern DATABASE_TYPE *db;
extern DATABASE_TYPE *database_table[MAX_OPEN_DATABASES];
extern int current_database, first_database_call;
extern UBYTE VALUE_SIZE[];
extern struct
{
   DOUBLE d;
   FLOAT  f;
   LONG   l;
   SHORT  s;
} MACHINE_SIGNATURE[];

extern struct
{
   CHAR ms_name[5], ms_code[5], signature[18];
} MS_TABLE[];

extern struct
{
   STRUCT_DEF_HDR_TYPE block_hdr;
   STRUCT_DEF_ELEM_TYPE a[14];
   STRUCT_DEF_HDR_TYPE dataset_id;
   STRUCT_DEF_ELEM_TYPE b[1];
   STRUCT_DEF_HDR_TYPE producer_id;
   STRUCT_DEF_ELEM_TYPE c[1];
   STRUCT_DEF_HDR_TYPE data_list_entry;
   STRUCT_DEF_ELEM_TYPE d[5];
   STRUCT_DEF_HDR_TYPE struct_def_elem;
   STRUCT_DEF_ELEM_TYPE e[3];
   STRUCT_DEF_HDR_TYPE struct_def_hdr;
   STRUCT_DEF_ELEM_TYPE f[3];
   STRUCT_DEF_HDR_TYPE profile_dir_0_entry;
   STRUCT_DEF_ELEM_TYPE g[2];
   STRUCT_DEF_HDR_TYPE profile_dir_1_entry;
   STRUCT_DEF_ELEM_TYPE h[4];
   STRUCT_DEF_HDR_TYPE profile_dir_2_entry;
   STRUCT_DEF_ELEM_TYPE i[4];
   STRUCT_DEF_HDR_TYPE profile_dir_3_entry;
   STRUCT_DEF_ELEM_TYPE j[4];
   STRUCT_DEF_HDR_TYPE  data_dir_entry;
   STRUCT_DEF_ELEM_TYPE k[1];
   STRUCT_DEF_HDR_TYPE  block_dir_hdr;
   STRUCT_DEF_ELEM_TYPE l[10];
   STRUCT_DEF_HDR_TYPE  block_dir_entry;
   STRUCT_DEF_ELEM_TYPE m[8];
   STRUCT_DEF_HDR_TYPE terminator;
} dbint_sd[];

#endif

#endif /* ifndef dbglo_included */
