/**---------------------------------------------------------------------------
 ** 
 ** xtract.h -- 
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/08/04 19:35:39
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/08/04 19:49:28
 ** Update Count    : 10
 ** Directory       : /home/pego/pcd1/codas3c/gfi/src/apps/db/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    Undocumented.
 ** 
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 **------------------------------------------------------------------------**/

#include "geninc.h"
#include "ioserv.h"
#include "dbsource.h"
#include "use_db.h"
#include "gfimisc.h"
#include "gfidb.h"

/* ##################################################################
	 -Definitions:
	 ################################################################## */

#define __VAR_CODE -1
#define X_VAR_CODE  1
#define Y_VAR_CODE  2
#define Z_VAR_CODE  3

#define MAX_PRDS            MAX_VARS

/* ##################################################################
	 -Type Definitions:

	 ################################################################## */

typedef struct
{
	int code;
	FILE_NAME_TYPE file;
	int   start_var;
	int   stop_var;
	ULONG bits;
} EXTRACT_PRODUCT_TYPE;

#define VAR_PRD_CODE  1
#define PRF_PRD_CODE  2
#define CNT_PRD_CODE  3

/* ##################################################################
	 -Global Variables:
	 ################################################################## */

static FILE_NAME_TYPE dbname;
static int steps, year_base;

static EXTRACT_VARIABLE_TYPE var_defs;
static EXTRACT_VARIABLE_TYPE var_list[MAX_VARS];
static int n_var = 0;

static EXTRACT_PRODUCT_TYPE prd_defs;
static EXTRACT_PRODUCT_TYPE prd_list[MAX_PRDS];
static int n_prd = 0;

#define COMBINED_OUTPUT_BIT  BIT0
#define NO_BAD_OUTPUT_BIT    BIT1
#define SEPARATE_OUTPUT_BIT  BIT2
#define UNDEFINED_OUTPUT_BIT BIT3
#define LONGITUDE_180_BIT    BIT4

/* ##################################################################
	 -Functions:
	 ################################################################## */

#if PROTOTYPE_ALLOWED
int init_extract_variable(FILE *fp, char *var_list, int code);
#else
int init_extract_variable();
#endif

#if PROTOTYPE_ALLOWED
int init_extract_product(FILE *fp, char *list, int code);
#else
int init_extract_product();
#endif


/* ##################################################################
	 -PARAMETERS:
	 ################################################################## */

static PARAMETER_LIST_ENTRY_TYPE extract_params[]=
{
	{" year_base:"     , " %d"    , (char *) &year_base, TYPE_INT},
	{" dbname:"        , " %79s"  , (char *) dbname    , TYPE_STRING},
	{" step_size:"     , " %d"    , (char *) &steps    , TYPE_INT},
	{NULL              , NULL     , (char *) NULL      , 0}
};

/* ##################################################################
	 -OPTIONS:
	 ################################################################## */

static OPTION_TYPE variable_defs_opts[]=
{
	{"end", 0, NULL, (char *) NULL},
	{"name:", TYPE_STRING, op_get_param, (char *) var_defs.name},
	{"min=" , TYPE_STRING, op_get_param, (char *) var_defs.min_str},
	{"max=" , TYPE_STRING, op_get_param, (char *) var_defs.max_str},
	{"scale=", TYPE_FLOAT, op_get_param, (char *) &var_defs.scale},
  {NULL, 0, NULL, (char *) NULL}
};

static OPTION_TYPE variable_opts[]=
{
	{"end", 0, NULL, (char *) NULL},
	{"file:", TYPE_STRING, op_get_param, (char *) prd_defs.file},
	{"combined",  0, set_long_bit, (char *) &prd_defs.bits},
	{"no_bad",    1, set_long_bit, (char *) &prd_defs.bits},
	{"separate",  2, set_long_bit, (char *) &prd_defs.bits},
	{"undefined", 3, set_long_bit, (char *) &prd_defs.bits},
	{"longitude_180", 4, set_long_bit, (char *) &prd_defs.bits},
	{"var:",  __VAR_CODE, init_extract_variable, (char *) variable_defs_opts},
  {NULL, 0, NULL, (char *) NULL}
};

static OPTION_TYPE profile_opts[]=
{
	{"end", 0, NULL, (char *) NULL},
	{"file:", TYPE_STRING, op_get_param, (char *) prd_defs.file},
	{"combined",  0, set_long_bit, (char *) &prd_defs.bits},
	{"no_bad",    1, set_long_bit, (char *) &prd_defs.bits},
	{"separate",  2, set_long_bit, (char *) &prd_defs.bits},
	{"undefined", 3, set_long_bit, (char *) &prd_defs.bits},
	{"longitude_180", 4, set_long_bit, (char *) &prd_defs.bits},
	{"X:",  X_VAR_CODE, init_extract_variable, (char *) variable_defs_opts},
	{"Y:",  Y_VAR_CODE, init_extract_variable, (char *) variable_defs_opts},
  {NULL, 0, NULL, (char *) NULL}
};

static OPTION_TYPE contour_opts[]=
{
	{"end", 0, NULL, (char *) NULL},
	{"file:", TYPE_STRING, op_get_param, (char *) prd_defs.file},
	{"combined",  0, set_long_bit, (char *) &prd_defs.bits},
	{"no_bad",    1, set_long_bit, (char *) &prd_defs.bits},
	{"separate",  2, set_long_bit, (char *) &prd_defs.bits},
	{"undefined", 3, set_long_bit, (char *) &prd_defs.bits},
	{"longitude_180", 4, set_long_bit, (char *) &prd_defs.bits},
	{"X:",  X_VAR_CODE, init_extract_variable, (char *) variable_defs_opts},
	{"Y:",  Y_VAR_CODE, init_extract_variable, (char *) variable_defs_opts},
	{"Z:",  Z_VAR_CODE, init_extract_variable, (char *) variable_defs_opts},
  {NULL, 0, NULL, (char *) NULL}
};

static OPTION_TYPE extract_options[]=
{
	{"end", 0, NULL, (char *) NULL},
	{"OPTIONS:", 0, NULL, (char *) NULL},
	{"contour:",  CNT_PRD_CODE, init_extract_product, (char *) contour_opts},
	{"profile:",  PRF_PRD_CODE, init_extract_product, (char *) profile_opts},
	{"variable:", VAR_PRD_CODE, init_extract_product, (char *) variable_opts},
  {NULL, 0, NULL, (char *) NULL}
};

/* ##################################################################
	 -Macros:
	 ################################################################## */

#define is_defined(x,y)  ( ( (y) < ((int) (x)->n) ) ? 1 : 0 )
#define is_good(x,y)     ( ( ( ((x)->data)[(y)] < ADJ_BADFLOAT) \
                          && ( ((x)->data)[(y)] >= (x)->min) \
                          && ( ((x)->data)[(y)] <= (x)->max) ) ? 1 : 0 )

#define is_x(x)          ( ( ((EXTRACT_VARIABLE_TYPE *)(x))->code \
                           == X_VAR_CODE ) ? 1 : 0 )
#define is_y(x)          ( ( ((EXTRACT_VARIABLE_TYPE *)(x))->code \
                           == Y_VAR_CODE ) ? 1 : 0 )
#define is_z(x)          ( ( ((EXTRACT_VARIABLE_TYPE *)(x))->code \
                           == Z_VAR_CODE ) ? 1 : 0 )



