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

      File: setpmask.c

      Purpose: Sets the data processing mask for a set of block
               files within a CODAS database.

      Input: CODAS database
             control file
      Output: CODAS database

     Control file options

     dbname:           {database name}
     block_range:      {range of blocks for setting new mask bits}
                       {in form --- to ---, ie. 2 to 100 }
                       {eliminating the line defaults to all}
     dpmask_codes:     {list of codes that will have bits set }
                       {for a list of bit codes see 
                        codas3/doc/dpmask.doc}
     P.Caldwell 6/feb/95
     ----------------------------------------------------------*/

#include "geninc.h"
#include "ioserv.h"
#include "dbinc.h"
#include "use_db.h"  /* check_db*() */
#include "adcp.h"
#include "dpmask.h"

typedef struct
{
   SHORT min;
   SHORT max;
} RANG_TYPE;

/* init functions */
#if PROTOTYPE_ALLOWED
int read_range(FILE *fpcnt, RANG_TYPE *range, int code);
int init_mask( FILE *fpcnt);
#else
int read_range();
int init_mask();
#endif

ULONG dpmask = 0L;
ULONG db_dpmask = 0L;

static FILE_NAME_TYPE dbname;
static RANG_TYPE block_range;

static OPTION_TYPE extract_options[] =
{
   "end"              , 0          , NULL         , NULL,
   "dbname:"          , TYPE_STRING, op_get_param , dbname,
   "block_range:"     , 0          , read_range   , (char *) &block_range,
   "dpmask_codes:"    , 1          ,  init_mask   , (char*)   NULL};    /* not sure on code */

static OPTION_TYPE mask_options[] =
{
 {"end"                   ,     0              , NULL        ,              NULL},
 {"TIME_CORRECTED",        TIME_CORRECTED,      set_long_bit , (char *)&dpmask},
 {"TRANSDUCER_ORIENTATION",TRANSDUCER_ORIENTATION,set_long_bit , (char *)&dpmask},
 {"COMPASS_ERROR",         COMPASS_ERROR       ,set_long_bit , (char *)&dpmask}  ,
 {"TRANSDUCER_BEAM_ANGLES",TRANSDUCER_BEAM_ANGLES,set_long_bit , (char *)&dpmask},
 {"SOUND_SPEED_VEL_COR ",  SOUND_SPEED_VEL_COR   ,set_long_bit , (char *)&dpmask} ,
 {"SOUND_SPEED_DEPTH_COR", SOUND_SPEED_DEPTH_COR ,set_long_bit , (char *)&dpmask},
 {"GLITCHES_REMOVED"    ,  GLITCHES_REMOVED      ,set_long_bit , (char *)&dpmask},
 {"GLITCHES_CORRECTED"  ,  GLITCHES_CORRECTED    ,set_long_bit , (char *)&dpmask},
 {"BOTTOM_SOUGHT"       ,  BOTTOM_SOUGHT         ,set_long_bit , (char *)&dpmask},
 {"BOTTOM_MGB"          ,  BOTTOM_MGB            ,set_long_bit , (char *)&dpmask},
 {"MGB_SET"             ,  MGB_SET               ,set_long_bit , (char *)&dpmask},
 {"DEPTH_RANGE_COR"     ,  DEPTH_RANGE_COR       ,set_long_bit , (char *)&dpmask},
 {"RELATIVE_PROFILES"   ,  RELATIVE_PROFILES     ,set_long_bit , (char *)&dpmask},
 {"REFERENCE_VELOCITIES",  REFERENCE_VELOCITIES  ,set_long_bit , (char *)&dpmask},
 {"VEL_TO_ABSOLUTE"    ,   VEL_TO_ABSOLUTE       ,set_long_bit , (char *)&dpmask},
 {"VEL_TO_REF_LAYER"   ,   VEL_TO_REF_LAYER      ,set_long_bit , (char *)&dpmask},
 {"POSITIONS_OPTIMIZED",   POSITIONS_OPTIMIZED   ,set_long_bit , (char *)&dpmask},
 {"NAV_SOURCE_GPS"     ,   NAV_SOURCE_GPS        ,set_long_bit , (char *)&dpmask},
 {"NAV_SOURCE_TRANSIT" ,   NAV_SOURCE_TRANSIT    ,set_long_bit , (char *)&dpmask},
 {"NAV_SOURCE_LORAN"   ,   NAV_SOURCE_LORAN      ,set_long_bit , (char *)&dpmask},
 {"NAV_SOURCE_OMEGA"   ,   NAV_SOURCE_OMEGA      ,set_long_bit , (char *)&dpmask},
 {"NAV_SOURCE_RADAR"   ,   NAV_SOURCE_RADAR      ,set_long_bit , (char *)&dpmask},
 {"NOT_TIME_CORRECTED",        NOT_TIME_CORRECTED,      set_long_bit , (char *)&dpmask},
 {"NOT_TRANSDUCER_ORIENTATION",NOT_TRANSDUCER_ORIENTATION,set_long_bit , (char *)&dpmask},
 {"NOT_COMPASS_ERROR",         NOT_COMPASS_ERROR       ,set_long_bit , (char *)&dpmask}  ,
 {"NOT_TRANSDUCER_BEAM_ANGLES",NOT_TRANSDUCER_BEAM_ANGLES,set_long_bit , (char *)&dpmask},
 {"NOT_SOUND_SPEED_VEL_COR ",  NOT_SOUND_SPEED_VEL_COR   ,set_long_bit , (char *)&dpmask} ,
 {"NOT_SOUND_SPEED_DEPTH_COR", NOT_SOUND_SPEED_DEPTH_COR ,set_long_bit , (char *)&dpmask},
 {"NOT_GLITCHES_REMOVED"    ,  NOT_GLITCHES_REMOVED      ,set_long_bit , (char *)&dpmask},
 {"NOT_GLITCHES_CORRECTED"  ,  NOT_GLITCHES_CORRECTED    ,set_long_bit , (char *)&dpmask},
 {"NOT_BOTTOM_SOUGHT"       ,  NOT_BOTTOM_SOUGHT         ,set_long_bit , (char *)&dpmask},
 {"NOT_BOTTOM_MGB"          ,  NOT_BOTTOM_MGB            ,set_long_bit , (char *)&dpmask},
 {"NOT_MGB_SET"             ,  NOT_MGB_SET               ,set_long_bit , (char *)&dpmask},
 {"NOT_DEPTH_RANGE_COR"     ,  NOT_DEPTH_RANGE_COR       ,set_long_bit , (char *)&dpmask},
 {"NOT_RELATIVE_PROFILES"   ,  NOT_RELATIVE_PROFILES     ,set_long_bit , (char *)&dpmask},
 {"NOT_REFERENCE_VELOCITIES",  NOT_REFERENCE_VELOCITIES  ,set_long_bit , (char *)&dpmask},
 {"NOT_VEL_TO_ABSOLUTE"    ,   NOT_VEL_TO_ABSOLUTE       ,set_long_bit , (char *)&dpmask},
 {"NOT_VEL_TO_REF_LAYER"   ,   NOT_VEL_TO_REF_LAYER      ,set_long_bit , (char *)&dpmask},
 {"NOT_POSITIONS_OPTIMIZED",   NOT_POSITIONS_OPTIMIZED   ,set_long_bit , (char *)&dpmask},
 {"NOT_NAV_SOURCE_GPS"     ,   NOT_NAV_SOURCE_GPS        ,set_long_bit , (char *)&dpmask},
 {"NOT_NAV_SOURCE_TRANSIT" ,   NOT_NAV_SOURCE_TRANSIT    ,set_long_bit , (char *)&dpmask},
 {"NOT_NAV_SOURCE_LORAN"   ,   NOT_NAV_SOURCE_LORAN      ,set_long_bit , (char *)&dpmask},
 {"NOT_NAV_SOURCE_OMEGA"   ,   NOT_NAV_SOURCE_OMEGA      ,set_long_bit , (char *)&dpmask},
 {"NOT_NAV_SOURCE_RADAR"   ,   NOT_NAV_SOURCE_RADAR      ,set_long_bit , (char *)&dpmask},
 {NULL                , 0                        , NULL        , NULL } 
};

#if PROTOTYPE_ALLOWED
void do_it(FILE *fpcnt)
#else
void do_it(fpcnt)
FILE *fpcnt;
#endif
{
   int iblk, ierr=0;
   int dpmask_type = DATA_PROC_MASK;
   unsigned int dpmask_size = sizeof(ULONG);
   int nprf;

   block_range.min = block_range.max =  BADSHORT;
   /* read user-specified options: */
   if (execute_options(fpcnt, extract_options, ECHO) < 0) return;

   /* OPEN DATABASE */
   dbopen_cnf(1, dbname, READ_WRITE, DIR_IN_MEMORY);
   printf("\n\n DATABASE:  %s SUCCESSFULLY OPENED\n\n", dbname);

   /* set range parameters to defaults if missing from control file*/
   if( block_range.min == BADSHORT && block_range.max == BADSHORT )
   {
      block_range.min = 0; 
      block_range.max =  db->block_dir_hdr.dir_nentries;
   }

   nprf = 0;
   iblk=block_range.min;
   /* loop thru each block */
   do 
   {
     ierr = 0;
     DBMOVE( &nprf, &ierr );

     DBGET(&dpmask_type, (char *)&db_dpmask, &dpmask_size, &ierr);
     if (error_found(ierr, "DBGET dpmask")) goto close_all;

     db_dpmask |= dpmask;     /* set new mask codes */

     DBPUT(&dpmask_type, (char *)&dpmask, &dpmask_size, &ierr);
     if (error_found(ierr, "DBPUT dpmask")) goto close_all;

     nprf = db->block_hdr.dir_nentries;
     iblk++;
   } while( iblk < block_range.max );

   close_all:
   DBCLOSE(&ierr);
   check_error(ierr, "DBCLOSE");
}
/*-----------------------------------------------------------------------------

   FUNCTION read_range

   PURPOSE read ranges from control file
*/ 
#if PROTOTYPE_ALLOWED
int read_range(FILE *fpcnt, RANG_TYPE *range, int code)
#else
int read_range(fpcnt, range, code)
FILE *fpcnt;
RANG_TYPE *range;
int code;
#endif
{
   if (fscanf(fpcnt, " %hd to %hd", &(range->min), &(range->max)) != 2)
      return(-1);
   if (ECHO)
      fprintf(stdout, " %d to %d", range->min, range->max);
   return(0);
}
/*  -----------------------------------------------------------------------

    FUNCTION INIT_DPMASK

    get output options for inventory list
*/
#if PROTOTYPE_ALLOWED
int  init_mask(FILE *fp_cnt )
#else
int  init_mask(fp_cnt)
FILE *fp_cnt;
#endif
{
   if (execute_options(fp_cnt, mask_options, ECHO) <= 0)
   {
      printf("\n ERROR in reading mask options.");
      exit(-1);
   }
   return(0);
}
