/**C*
 *
 * Vaisala software source code file
 *
 * Copyright (c) Vaisala Oyj 2006, 2010, 2011, 2012, 2013.
 * All rights reserved.
 *
 **C*/

/** @file
 *
 * Dual Pol Applications library header file
 *
 * $URL: https://subversion.vaisala.com/svn/repos/sigmet/wr_iris_rda/tags/release8.13.6/base/dpolapp_lib/dpolapp_lib.h $
 *
 * $Id: dpolapp_lib.h 30048 2014-05-29 22:52:18Z rke $
 *
 */

#ifndef SIGMET_DPOLAPP_LIB_H
#define SIGMET_DPOLAPP_LIB_H 1

/** Build option for classification of stratiform and convective rain, melting layer */
#define DPOLAPP_DEF_CELLCLASSES           1

/** The known use cases of dpolapp methods - must be specified when initializing HydroClass  */
#define DPOLAPP_USER_UNKNOWN  0       /**< Unidentified dpolapp user */
#define DPOLAPP_USER_DPOLAPP  1       /**< dpolapp utility: manages all the classifier methods */
#define DPOLAPP_USER_RDA      2       /**< RDA: runs Pre-, MeteoClassifer, Precip- and CellClassifiers(phase A),  or a subset. */
#define DPOLAPP_USER_REINGEST 3       /**< reingest: runs Pre-, MeteoClassifer, Precip- and CellClassifiers(phase A), or a subset. */
#define DPOLAPP_USER_INGFIO   4       /**< ingfio: runs the 2nd phase of CellClassifier, only. */
#define DPOLAPP_USER_MLHGT    5       /**< MLHGT product: runs the MeltClassifier, PreClassifier if not done. */


/* dpolapp messages */
#define DPOLAPP_MSGES       128 /*< allocation for DPOLAPP messages */
#define DPOLAPP_MSGSWITCHOFF  6 /*< msg ID repeats DPOLAPP_MSGSWITCHOFF times after which mute */
#define DPOLAPP_ONE_STRING    "but %d data type missing: %s" ///< int & single string add-on to a sig_message
#define DPOLAPP_TWO_STRINGS   "but %d data type(s) missing: %s %s " ///< two strings add-on to a sig_message string 
#define DPOLAPP_MANY_STRINGS  "but %d data type(s) missing: %s %s etc. " ///< > two strings add-on to a sig_message string

#define         MSG_NOLICENSE       97 /*< license leak */
#define DPOLAPP_MSG_NOLICENSE       (DPOLAPP_FACILITY+MSG_NOLICENSE)/*< Facility ibid.*/
#define         MSG_SWITCHINGOFF    98 /*< ".. from now on, this message is muted */
#define DPOLAPP_MSG_SWITCHINGOFF    (DPOLAPP_FACILITY+MSG_SWITCHINGOFF)/*< Facility ibid.*/
#define         MSG_UNKNOWN         99 /*< Unknown message */ 
#define DPOLAPP_MSG_UNKNOWN         (DPOLAPP_FACILITY+MSG_UNKNOWN)/*< Facility ibid.*/
#define         MSG_CONFUPDATECOK   100  /*<  Configuration update & task list ok. */ 
#define DPOLAPP_MSG_CONFUPDATECOK   (DPOLAPP_FACILITY+MSG_CONFUPDATECOK)/*< Facility ibid.*/
#define         MSG_CONFUPDATESOK   101  /*<  Configuration update & task list ok. */
#define DPOLAPP_MSG_CONFUPDATESOK   (DPOLAPP_FACILITY+MSG_CONFUPDATESOK)/*< Facility ibid.*/
#define         MSG_CONFUPGRADECOK  1  /*<  Configuration update & task list using internal defaults. */
#define DPOLAPP_MSG_CONFUPGRADECOK  (DPOLAPP_FACILITY+MSG_CONFUPGRADECOK)/*< Facility ibid.*/
#define         MSG_CONFUPGRADESOK  2  /*<  Configuration update & task list using internal defaults. */
#define DPOLAPP_MSG_CONFUPGRADESOK  (DPOLAPP_FACILITY+MSG_CONFUPGRADESOK)/*< Facility ibid.*/
#define         MSG_MLINITOK        3  /*<  ML init ok. */
#define DPOLAPP_MSG_MLINITOK        (DPOLAPP_FACILITY+MSG_MLINITOK)/*< Facility ibid.*/
#define         MSG_MLUPDATEOK      4  /*<  ML update ok. */
#define DPOLAPP_MSG_MLUPDATEOK      (DPOLAPP_FACILITY+MSG_MLUPDATEOK)/*< Facility ibid.*/
#define         MSG_MLCONFINITOK    5  /*<  ML conf init ok. */
#define DPOLAPP_MSG_MLCONFINITOK    (DPOLAPP_FACILITY+MSG_MLCONFINITOK)/*< Facility ibid.*/
#define         MSG_MLCONFUPDATEOK  6  /*<  ML conf update ok. */
#define DPOLAPP_MSG_MLCONFUPDATEOK  (DPOLAPP_FACILITY+MSG_MLCONFUPDATEOK)/*< Facility ibid.*/
#define         MSG_MLAZOUT         7  /*<  Az out of bounds in variable ML. */
#define DPOLAPP_MSG_MLAZOUT         (DPOLAPP_FACILITY+MSG_MLAZOUT)/*< Facility ibid.*/
#define         MSG_MLELOUT         8  /*<  El out of bounds in variable ML. */
#define DPOLAPP_MSG_MLELOUT         (DPOLAPP_FACILITY+MSG_MLELOUT)/*< Facility ibid.*/
#define         MSG_MLWRONGSCAN     9  /*<  El out of bounds in variable ML. */
#define DPOLAPP_MSG_MLWRONGSCAN     (DPOLAPP_FACILITY+MSG_MLWRONGSCAN)/*< Facility ibid.*/
#define         MSG_MLNOTREADY      10  /*< XOP_MLSPCES not done, not ready. */
#define DPOLAPP_MSG_MLNOTREADY      (DPOLAPP_FACILITY+MSG_MLNOTREADY)/*< Facility ibid.*/
#define         MSG_UPCNFSTTFAILS   15  /*<  HClassUpdateConf stat fails */
#define DPOLAPP_MSG_UPCNFSTTFAILS   (DPOLAPP_FACILITY+MSG_UPCNFSTTFAILS)/*< Facility ibid.*/
#define         MSG_BUNCFATAL       21  /*<  HClassBunchCalculus fatal */ 
#define DPOLAPP_MSG_BUNCFATAL       (DPOLAPP_FACILITY+MSG_BUNCFATAL)/*< Facility ibid.*/
#define         MSG_INPRSCPPPHCNOTASKS 31/*< initProcSection_pppHClass tasks not ok */
#define DPOLAPP_MSG_INPRSCPPPHCNOTASKS (DPOLAPP_FACILITY+MSG_INPRSCPPPHCNOTASKS)/*< ibid.*/
#define         MSG_INPRSCPPPHCNOTINHV 32/*< initProcSection_pppHClass not in H+V mode */
#define DPOLAPP_MSG_INPRSCPPPHCNOTINHV (DPOLAPP_FACILITY+MSG_INPRSCPPPHCNOTINHV)/*< ibid.*/
#define         MSG_INPRSCPPPHCMISTYPE 33/*< initProcSection_pppHClass tasks not ok */
#define DPOLAPP_MSG_INPRSCPPPHCMISTYPE (DPOLAPP_FACILITY+MSG_INPRSCPPPHCMISTYPE)/*< ibid.*/
#define         MSG_INPRSCPPPHCMISSML  34/*< initProcSection_pppHClass RDA ML unknown */
#define DPOLAPP_MSG_INPRSCPPPHCMISSML (DPOLAPP_FACILITY+MSG_INPRSCPPPHCMISSML)/*< ibid.*/
#define         MSG_ASSRES_ANOMOUT   40/*< HCassignResult: anomalous output, goes FATAL */
#define DPOLAPP_MSG_ASSRES_ANOMOUT   (DPOLAPP_FACILITY+MSG_ASSRES_ANOMOUT) /*< Facility ibid.*/
/* #define         MSG_RTRCFG_FCTFAIL   41 *< HClassRetrieveConfiguration: failed factory goes FATAL, obsolete*/
/* #define DPOLAPP_MSG_RTRCFG_FCTFAIL   (DPOLAPP_FACILITY+MSG_RTRCFG_FCTFAIL) *< Facility ibid., obsolete*/
#define         MSG_PRQ_MULTIDER     42/*< HClassPrepareQuality: multiple  derived inputs, goes FATAL*/
#define DPOLAPP_MSG_PRQ_MULTIDER     (DPOLAPP_FACILITY+MSG_PRQ_MULTIDER) /*< Facility ibid.*/
#define         MSG_PRP_MULTIDER     43/*< HClassPrepareQuality: multiple  derived inputs, goes FATAL*/
#define DPOLAPP_MSG_PRP_MULTIDER     (DPOLAPP_FACILITY+MSG_PRP_MULTIDER) /*< Facility ibid.*/
#define         MSG_CHKCNFVERMISM    50/*< CheckConfiguration IRIS/RDA/dpolapp version mismatch */
#define DPOLAPP_MSG_CHKCNFVERMISM    (DPOLAPP_FACILITY+MSG_CHKCNFVERMISM) /*< Facility ibid.*/
#define         MSG_CHKCNFSAVED      51/*< CheckConfiguration save due to user renaming */
#define DPOLAPP_MSG_CHKCNFSAVED      (DPOLAPP_FACILITY+MSG_CHKCNFSAVED) /*< Facility ibid.*/
#define         MSG_CHKCNFFAIL       52/*< CheckConfiguration limits/syntax TEST */
#define DPOLAPP_MSG_CHKCNFFAIL       (DPOLAPP_FACILITY+MSG_CHKCNFFAIL) /*< Facility ibid.*/
#define         MSG_CHKCNFUPGRADED   53/*< CheckConfiguration upgrade due IRIS/RDA/dpolapp version mismatch */
#define DPOLAPP_MSG_CHKCNFUPGRADED   (DPOLAPP_FACILITY+MSG_CHKCNFUPGRADED) /*< Facility ibid.*/
#define         MSG_CHKCNFGENERATED  54/*< CheckConfiguration fresh conf generate */
#define DPOLAPP_MSG_CHKCNFGENERATED  (DPOLAPP_FACILITY+MSG_CHKCNFGENERATED) /*< Facility ibid.*/
#define         MSG_INITVOL_MISSML   55/*< HClassInitVolume: RAW ML undefined */
#define DPOLAPP_MSG_INITVOL_MISSML   (DPOLAPP_FACILITY+MSG_INITVOL_MISSML) /*< Facility ibid.*/ 
#define         MSG_LDPSTPFAIL       56/*< LoadDPolappSetup fails */
#define DPOLAPP_MSG_LDPSTPFAIL       (DPOLAPP_FACILITY+MSG_LDPSTPFAIL) /*< Facility ibid.*/
#define         MSG_LDPSTPFAILFIX    57/*< LoadDPolappSetup fails, but will try fixing it */
#define DPOLAPP_MSG_LDPSTPFAILFIX    (DPOLAPP_FACILITY+MSG_LDPSTPFAILFIX) /*< Facility ibid.*/
#define         MSG_LDPSTPFCTFAIL    58/*< LoadDPolappSetup: factory settings failed goes FATAL*/
#define DPOLAPP_MSG_LDPSTPFCTFAIL    (DPOLAPP_FACILITY+MSG_LDPSTPFCTFAIL) /*< Facility ibid., */
#define         MSG_RPSTPFAIL        59/*< LoadDPolappSetup repair fails */
#define DPOLAPP_MSG_RPSTPFAIL        (DPOLAPP_FACILITY+MSG_RPSTPFAIL) /*< Facility ibid.*/
#define         MSG_LDPSTPSTRFAIL    60/*< ConstructConfPath: failed path and/or file names, goes FATAL*/
#define DPOLAPP_MSG_LDPSTPSTRFAIL    (DPOLAPP_FACILITY+MSG_LDPSTPSTRFAIL) /*< Facility ibid., */
#define         MSG_SYSTEMFAIL       61/*< HClassConvertConfiguration: system( sSystemCommand ) failed. goes FATAL*/
#define DPOLAPP_MSG_SYSTEMFAIL       (DPOLAPP_FACILITY+MSG_SYSTEMFAIL) /*< Facility ibid., */
#define         MSG_MVFAIL           62/*< HClassConvertConfiguration: saving obsolete dpolapp in retired failed.*/
#define DPOLAPP_MSG_MVFAIL           (DPOLAPP_FACILITY+MSG_MVFAIL) /*< Facility ibid., */
#define         MSG_CNVRTFAIL        63/*< HydroClass: problem in converting obsolete dpolapp.conf. Switches OFF.*/
#define DPOLAPP_MSG_CNVRTFAIL        (DPOLAPP_FACILITY+MSG_CNVRTFAIL) /*< Facility ibid., */
#define         MSG_RETIREDOK        64/*< HydroClass: obsolete dpolapp.conf moved into Retired subtree.*/
#define DPOLAPP_MSG_RETIREDOK        (DPOLAPP_FACILITY+MSG_RETIREDOK) /*< Facility ibid., */
#define         MSG_CTASSERR         65/*< bHClassAssignAvailable: wrong assignment requested*/
#define DPOLAPP_MSG_CTASSERR        (DPOLAPP_FACILITY+MSG_CTASSERR) /*< Facility ibid., */
#define         MSG_MISLINK          66/*< HydroClass: missing link to classifier. Switches OFF*/
#define DPOLAPP_MSG_MISLINK          (DPOLAPP_FACILITY+MSG_MISLINK) /*< Facility ibid.*/
#define         MSG_STATUS_ERROR     67/*< Cannot run because of status error*/
#define DPOLAPP_MSG_STATUS_ERROR     (DPOLAPP_FACILITY+MSG_STATUS_ERROR) /*< Facility ibid.*/


#ifdef __cplusplus
extern "C" {
#endif

/* HydroClass processing states: 
 */
/** HClass task list is active and ready for calculus */
#define DPOLAPP_STATUS_READYTORUN  1
/** HClass is running the task list; 
    some sweeps, rays might have been processed, but the file/data sample is incomplete */ 
#define DPOLAPP_STATUS_RUNNING     2 
/** HClass has run MakeDatafile and all the output is ok; 
HydroClass processing status bits in the file header have been set; 
"READYTORUN" features are true */
#define DPOLAPP_STATUS_SWEEPOK     3
/** HClass is active in IRIS/RDA setup; has been called and peeked into data, 
but has chosen to remain passive for time being (skips the files); 
configuration and task list may/may not be up */
#define DPOLAPP_STATUS_PASSIVE     10
/** System status not ok, e.g. rvp8 not in H+V */
#define DPOLAPP_STATUS_SYSFAIL     80
/** Anomalous output specified */
#define DPOLAPP_STATUS_ANOMOUT    81
/** ML input missing (value undefined)*/
#define DPOLAPP_STATUS_MLFAIL      82
/** Input data types required do not match with the  specified IRIS/RDA outputs, i.e. some input missing*/
#define DPOLAPP_STATUS_INPUTSFAIL  83
/** Input data types required are specified as IRIS/RDA outputs i.e. are available to HydroClass*/
#define DPOLAPP_STATUS_INPUTSOK    84
/** HClass runtime task list has not been built up  */
#define DPOLAPP_STATUS_TASKSNOTBUILT 85
/** HClass runtime task list has been built up ok */
#define DPOLAPP_STATUS_TASKSBUILT  86
/** HClass configuration setup has been retrieved (updated); 
needs to build up of the internal task list */
#define DPOLAPP_STATUS_BUILDTASKS  87
/** HClass configuration needs to be retrieved  */
#define DPOLAPP_STATUS_RETRIEVE    88
/** HClass needs to get ready i.e. update (check/retrieve) user configuration, 
and prepare internal run tasks */
#define DPOLAPP_STATUS_MAKEREADY   89
/** HClass is in a reset state; internal constants have been initialized, 
but all the configuration and tasks are absent. */
#define DPOLAPP_STATUS_RESET       90
/** HClass unlicensed; */
#define DPOLAPP_STATUS_NOLICENSE   98
/** HClass fatal state; will never process data again; 
will not try to load configurations; 
will not try to become active; keeps silent   */
#define DPOLAPP_STATUS_FATAL       99



/** input data polarization mode H+V: IRIS:pIhd_a->tcf.misc.ipolar==3 
                                      RDA: POL_FROM_OPF(GM->nv.opprm.iflags)==DPOLAPP_POLMODEHV*/ 
#define DPOLAPP_POLMODEHV           3 
/** input data polarization mode H-only: LDRH data avaialble &&
                                      IRIS:pIhd_a->tcf.misc.ipolar == 1 
                                      RDA: POL_FROM_OPF(GM->nv.opprm.iflags)==DPOLAPP_POLMODEH*/
#define DPOLAPP_POLMODEH            1  
/** input data polarization mode single LDRH data not available  &&
                                      IRIS:pIhd_a->tcf.misc.ipolar == 1
                                      RDA: POL_FROM_OPF(GM->nv.opprm.iflags)==DPOLAPP_POLMODEH*/
#define DPOLAPP_POLMODE0            4   

/** certain messages are muted off after they have been repeated DPOLAPP_MAX_MUTE times */
#define DPOLAPP_MAX_MUTE (6)

/** maximum number of input data types used in HydroClass ("OR" of all algorithms)*/
#define DPOLAPP_MAX_INPUTS          6  
/** 0: refuse using alternative measurands
    1: allow alternative inputs, such as DBZc (or DBT) for DBZ */
#define DPOLAPP_DEF_ALLOW_ALTTYPES         0 
/** the length of the task steering parameter list */
#define DPOLAPP_LEN_STEERINGS       8  
/** maximum number of input observables in a row method */
#define DPOLAPP_MAX_OBSERVABLES     8  
/** max number of HClass calculus input integer and floating point parameters 
    (separated)*/
#define DPOLAPP_MAX_PARAMS         24  
/** default length of names */
#define DPOLAPP_NAME16_SIZE        16  
/** number of HydroClass output data types, reported simultaneously. */
#define DPOLAPP_MAX_OUTPUTS         10
/** number of HydroClass internal data types */
#define DPOLAPP_MAX_INTERNS         10

/** max number of sweeps & rays in the ML model */
#define DPOLAPP_MAX_ML_SWEEPS   30
#define DPOLAPP_MAX_ML_RAYS   90
#define DPOLAPP_MAX_ML_BINS   100


/** cathegory index for calculus of derived observables from pristine inputs */
#define DPOLAPP_DERIVED         0  
/** cathegory base offset in task list of derived observables */
#define DPOLAPP_OFF_DERIVED     NUM_DEFINED_DATA 
/** cathegory index for calculus on PreClassifier MBF  */
#define DPOLAPP_PREMBF          1
/** cathegory base offset for calculus of PreClassifier MBF  */
#define DPOLAPP_OFF_PREMBF      2*NUM_DEFINED_DATA 
/** cathegory index for calculus on PreClassifier rule strengths RS and quality index */
#define DPOLAPP_PRERS           2
/** cathegory base offset for calculus of PreClassifier RS and quality index */
#define DPOLAPP_OFF_PRERS       2*NUM_DEFINED_DATA+15  
/** cathegory index for calculus on PreClassifier class assignment */
#define DPOLAPP_PRECL           3
/** cathegory base offset for calculus of PreClassifier class assignment */
#define DPOLAPP_OFF_PRECL       2*NUM_DEFINED_DATA+19
/** cathegory index for calculus of MeteoClassifiers MBF  */
#define DPOLAPP_METMBF          4
/** cathegory base offset for calculus of MeteoClassifiers MBF  */
#define DPOLAPP_OFF_METMBF      2*NUM_DEFINED_DATA+20
/** cathegory index for calculus of MeteoClassifiers RS */
#define DPOLAPP_METRS           5
/** cathegory base offset for calculus on  MeteoClassifiers RS */
#define DPOLAPP_OFF_METRS       2*NUM_DEFINED_DATA+60  
/** cathegory index for calculus on MeteoClassifiers class assignment */
#define DPOLAPP_METCL           6
/** cathegory base offset for calculus of MeteoClassifiers class assignment */
#define DPOLAPP_OFF_METCL       2*NUM_DEFINED_DATA+70
/** cathegory index for calculus of PrecipClassifier MBF  */
#define DPOLAPP_PRECIPMBF         7
/** cathegory base offset for calculus of PrecipClassifier MBF  */
#define DPOLAPP_OFF_PRECIPMBF     2*NUM_DEFINED_DATA+72
/** cathegory index for calculus of PrecipClassifier RS */
#define DPOLAPP_PRECIPRS          8
/** cathegory base offset for calculus on  PrecipClassifier RS */
#define DPOLAPP_OFF_PRECIPRS      2*NUM_DEFINED_DATA+92  
/** cathegory index for calculus on PrecipClassifier class assignment */
#define DPOLAPP_PRECIPCL          9
/** cathegory base offset for calculus of PrecipClassifiers class assignment */
#define DPOLAPP_OFF_PRECIPCL       2*NUM_DEFINED_DATA+96
/** cathegory index for calculus of CellClassifier MBF  */
#define DPOLAPP_CELLMBF          10
/** cathegory base offset for calculus of CellClassifier MBF  */
#define DPOLAPP_OFF_CELLMBF       2*NUM_DEFINED_DATA+98
/** cathegory index for calculus of CellClassifier RS */
#define DPOLAPP_CELLRS           11
/** cathegory base offset for calculus on  CellClassifier RS*/
#define DPOLAPP_OFF_CELLRS        2*NUM_DEFINED_DATA+102
/** cathegory index for calculus on CellClassifier  assignments */
#define DPOLAPP_CELLCL           12
/** cathegory base offset for calculus of CellClassifiers class assignment */
#define DPOLAPP_OFF_CELLCL        2*NUM_DEFINED_DATA+104
/** cathegory index for calculus of MeltClassifier MBF  */
#define DPOLAPP_MELTMBF          13
/** cathegory base offset for calculus of MeltClassifier MBF  */
#define DPOLAPP_OFF_MELTMBF       2*NUM_DEFINED_DATA+105
/** cathegory index for calculus of MeltClassifier RS */
#define DPOLAPP_MELTRS           14
/** cathegory base offset for calculus on  MeltClassifier RS*/
#define DPOLAPP_OFF_MELTRS        2*NUM_DEFINED_DATA+145
/** cathegory index for calculus on MeltClassifier  assignments */
#define DPOLAPP_MELTCL           15
/** cathegory base offset for calculus of MeltClassifiers class assignment */
#define DPOLAPP_OFF_MELTCL        2*NUM_DEFINED_DATA+155

/** depth of the NUM_DEFINED_DATA extension area, also allocation for calculus
    cathegories (minimum: sum(methods)*2 + [1]if derived */
#define DPOLAPP_MAX_CATHEGORIES     16
/** max number of HClass computational steps. NOTE: uint8_t type insufficient! */
#define DPOLAPP_MAX_ACTIONS  DPOLAPP_OFF_MELTCL+2


/** pointer type to an unsigned integer valued HClass library function */
typedef uint16_t (*HCUINCT)(const uint16_t*, const uint16_t*, const uint16_t*, const int32_t*, const double*);
/** ptr to JPOLE texture function */
#define DPOLAPP_HCUINCT_uiHClassTexture   1  
/** ptr to dB offset function */
#define DPOLAPP_HCUINCT_uiHClassdBoffset  2  
/** ptr to function of defuzzification choosing the MAX rule strength  */
#define DPOLAPP_HCUINCT_uiHClassMAXclass  3  
/** ptr to dummy integer valued routine */
#define DPOLAPP_HCUINCT_uiHClassUnity    99  

/** pointer type to a real valued HClass library function (bin-by-bin) */
typedef float  (*HCFNCT)(const float* const,  const uint16_t* const, const uint16_t* const, const int32_t* const, const double* const); 
/** pointer type to a real valued HClass library function (ray) */
// typedef float  (*HCFNCT)(const float* const,  const uint16_t* const, const uint16_t* const, const int32_t* const, const double* const);
#define DPOLAPP_HCFNCT_fHClassMBFtrap1D  10 /**< ptr to a MBF function (JPOLE 1D trapezoid) */
#define DPOLAPP_HCFNCT_fHClassMBFtrap2D  11 /**< ptr to a MBF function (JPOLE 2D trapezoid) */
#define DPOLAPP_HCFNCT_fHClassMBFbeta1D  12 /**< ptr to a MBF function (CSU beta 1D) */
#define DPOLAPP_HCFNCT_fHClassMBFbeta2D  13 /**< ptr to a MBF function (CSU beta 2D) */
#define DPOLAPP_HCFNCT_fHClassMBFbetaML  14 /**< ptr to a MBF function (CSU beta 1D, with ML) */
#define DPOLAPP_HCFNCT_fHClassMBFprod2D  15 /**< ptr to a MBF function (2D direct product) */
#define DPOLAPP_HCFNCT_fHClassRSadd      20 /**< ptr to a RS function (JPOLE additive, normalized)*/
#define DPOLAPP_HCFNCT_fHClassRShybCSU   21 /**< ptr to a RS function (CSU 2005 hybrid, normalized)*/
#define DPOLAPP_HCFNCT_fHClassRSaddW     22 /**< ptr to a RS function (additive with weights */
#define DPOLAPP_HCFNCT_fHClassRSmultiply 23 /**< ptr to a RS function (direct multiplicative)*/
#define DPOLAPP_HCFNCT_fHClassDFmaxJPOLE 30 /**< ptr to a classification function (MAX RS) */
#define DPOLAPP_HCFNCT_fHClassDFmaxCSU   31 /**< ptr to a classification function (CSU: MAX RS, with merging) */
#define DPOLAPP_HCFNCT_fHClassLHratio    32 /**< ptr to a preferred classification likelihood ratio; in scale (0..1) (PMI) */
#define DPOLAPP_HCFNCT_fHClassDFcolumn   33 /**< ptr to a classification function (column sum of RS) */
#define DPOLAPP_HCFNCT_fHClassDFmaxMELT  34 /**< ptr to a classification function (MELT: MAX RS, with merging) */
#define DPOLAPP_HCFNCT_fHClassTexture    41 /**< ptr to JPOLE texture function, float version */
#define DPOLAPP_HCFNCT_fHClassdBoffset   42 /**< ptr to dB offset function, float version */

#define DPOLAPP_HCFNCT_fHClassUnity      98 /**< index for a dummy real valued function */

#define DPOLAPP_DB_INTERNAL 99 /**< generic internal data type (software allocates internally) */


/** derived (non standard) measurand identifier DB_x, 
    defined as DB_x_derived = DB_X+DPOLAPP_DERIVED_OFFSET */
#define DPOLAPP_DERIVED_OFFSET    NUM_DEFINED_DATA  
/** the dpolapp utility command line max length */                                   
#define DPOLAPP_COMMAND_LINE_SIZE (4+PATHNAME_SIZE) 
#define DPOLAPP_DB_DBZTEXT2 NUM_DEFINED_DATA+DB_DBZ2 /**< internal data type of DB_DBZ2 texture */
#define DPOLAPP_DB_DBZTEXT  NUM_DEFINED_DATA+DB_DBZ  /**< ibid. of DB_DBZ  texture */
#define DPOLAPP_DB_ZDROFF2  NUM_DEFINED_DATA+DB_ZDR2 /**< ibid. of DB_ZDR2 corrected for off-sets */
#define DPOLAPP_DB_ZDROFF   NUM_DEFINED_DATA+DB_ZDR  /**< ibid. of DB_ZDR corrected for off-sets */
#define DPOLAPP_DB_RHOHV    NUM_DEFINED_DATA+DB_RHOHV  /**< ibid. of RhoHV, corrected for finite SNR (if not applied). */
#define DPOLAPP_DB_RHOHVG   NUM_DEFINED_DATA+DB_RHOHV2 /**< ibid. of DPOLAPP_DB_RHOHV, converted to normal */
#define DPOLAPP_DB_SNR      NUM_DEFINED_DATA+DB_SNR8   /**< ibid. DB_SNR */
#define DPOLAPP_DB_SNR16    NUM_DEFINED_DATA+DB_SNR16   /**< ibid. DB_SNR16 */
#define DPOLAPP_DB_PHIDPTEXT2 NUM_DEFINED_DATA+DB_PHIDP2 /**< ibid. of DB_PHIDP2 texture */
#define DPOLAPP_DB_PHIDPTEXT  NUM_DEFINED_DATA+DB_PHIDP  /**< ibid. of DB_PHIDP  texture */
#define DPOLAPP_DB_HEIGHT     NUM_DEFINED_DATA+DB_HEIGHT /**< ibid. height (altitude) of the observation volume */
#define DPOLAPP_DB_MLHEIGHT   NUM_DEFINED_DATA+DB_OTHER  /**< ibid. height (altitude) of the (CSU) melting layer */
#define DPOLAPP_DB_MLDIFF     NUM_DEFINED_DATA+DB_NULL   /**< ibid. height over/under (IRIS/RDA) melting layer */
#define DPOLAPP_DB_BBWIDTH     NUM_DEFINED_DATA+DB_USER   /**< ibid. melting layer/bright band width due beam broadening */
#define DPOLAPP_DB_PRECLASS    NUM_DEFINED_DATA+DB_USER2 /**< internal data type for  PreClassifier results*/
#define DPOLAPP_DB_CELLCLASS   NUM_DEFINED_DATA+DB_TEMPERATURE16 /**< internal data type for CellClassifier results*/
#define DPOLAPP_DB_PRECIPCLASS NUM_DEFINED_DATA+DB_HCLASS /**< internal data type for PrecipClassifier results*/
#define DPOLAPP_DB_METEOCLASS  NUM_DEFINED_DATA+DB_HCLASS2/**< internal data type for MeteoClassifiers results*/

#define DPOLAPP_DB_PMI         NUM_DEFINED_DATA+DB_SQI2   /**< internal data type for classifier likelihood ratio */

#define DPOLAPP_ML_CSUoffKM (-0.0)   /**< Offset of the CSU ML height w.r.t. IRIS/RDA [km] */

#define BF (FALSE)/**< short for reserved expression FALSE */
#define BT (TRUE) /**< short for reserved expression TRUE */

/** hclass result */
/* ////////////////////////////////////////////////////////////////]
*//*!
 //  <b>  Description:  </b>
 //  struct dpolapp_results manages the internal and final classification results.
 //  It contains  
 //    -# output data type (< NUM_DEFINED_DATA: IRIS/RDA, else internal
 //    -# template data type (used as model of the ray data structure)
 //    -# data type used as mask for this result (thresholding)
 //    -# cathegory index after which this result is available
 //    -# index in internal ray store
 //
*//*///////////////////////////////////////////////////////////////]
*/
struct dpolapp_result
{
  uint16_t uiOutType;  /**< data type of the result, if < NUM_DEFINED_DATA standard IRIS/RDA, else internal <  DPOLAPP_MAX_ACTIONS */ 
  uint16_t uiTempType; /**< IRIS/RDA data type used as template (e.g. ray header construction in IRIS */
  uint16_t uiMaskType; /**< data type used as masking (thresholding) */
  uint16_t uiLevel;    /**< cathegory index, at which the result becomes available */
  uint16_t uiRayInd;   /**< index in  *RayDatasOut work store, where the requested result data is available, for uiOutType > NUM_DEFINED_DATA-1 */
  uint16_t uiTaskInd;  /**< index in task list where the  result was computed (owerwritten bin-by-bin in Bunch mode */
  uint16_t uiBitSlot;  /**< index of bit segment reserved in DB_HCLASS (if any) */
}; /* dpolapp_result */

/* ////////////////////////////////////////////////////////////////]
*//*!
 //  <b>  Description:  </b> 
 // struct hclass_task specifies a single operation in HClass calculus.
 // It contains
 //   -# mode of computation (DPOLAPP_xMODE x:bunch,ray,sweep,volume)
 //   -# number of output observables (always 1, currently)
 //   -# ptr to function specifying the calculation
 //   -# template data type threshold value (coarse thresholding scheme)
 //   -# mask threshold value (specific thresholding scheme)
 //   -# thresholding condition applied on output
 //   -# value used for thresholded results
 //   -# input observable list 
 //   -# input pristine observable list 
 //   -# steering paramters
 //   -# integer type parameters 
 //   -# floating point parameters
 //
*//*///////////////////////////////////////////////////////////////]
*/
struct hclass_task
{
/*   struct hclass_task_list *pHcTaskList;  **< ptr to mother hclass_task_list */
  uint8_t usCathegory; /**< task cathegory index */
  uint8_t usNrOutputs; /**< number of output observables, typically one */
  uint8_t usFCTtype; /**< process independent index of function pointers */
  uint8_t usReport;  /**< report outcome as IRIS/RDA (<NUM_DEFINED_DATA) or internal type */
  float fTempThreshold;/**< threshold applied to temp type of uiHCsteerings[2]*/
  float fMaskThreshold; /**< threshold applied to mask type of uiHCsteerings[3]; */
  uint16_t uiMaxNonMeteo;  /**< threshold applied on the output of uiHCsteerings[1]; */
  uint16_t uiNonMeteoID; /**< the value assigned to bins declared as 'non-meteo' */
/* reassigned in each process, using usFCTtype *
  HCFNCT fHCFunction;  **< ptr to floating point valued function *
  HCUINCT uiHCUinction;**< ptr to unsigned integer valued function  */
/** list of input observable addresses, 
    relative offsets from the runtime work sheet base address, 
    also data type DB_xxxx) */
  uint16_t uiHCinputs[DPOLAPP_MAX_OBSERVABLES];  
/** list of pristine input observable addresses, DB_x */
  uint16_t uiHCinputsP[DPOLAPP_MAX_OBSERVABLES]; 
/** the task parameter steerings: 
  (0) first pristine input type, 
  (1) first output type, 
  (2) template data type, 
  (3) mask data type, 
  (4) Nr inputs (middle bin in ray mode), 
  (5) Nr I params, 
  (6) Nr D params 
  (7) Nr bins (ray mode) */
  uint16_t uiHCsteerings[DPOLAPP_LEN_STEERINGS];   
  int32_t iHCParams[DPOLAPP_MAX_PARAMS]; /**< the integer parameter data */
  double  dHCParams[DPOLAPP_MAX_PARAMS]; /**< the floating point parameter data */
}; /* hclass_task */

/* ////////////////////////////////////////////////////////////////]
*//*!
 //  <b>  Description:  </b>
 //  struct hclass_links contains the run-time pointers to hclass_store.
 //  It contains links to 
 //    -# hclass_store
 //    -# dsp_data_mask
 //    -# dpolapp_setup (the retrieved HClass configuration)
 //    -# hclass_task (task work sheet)
 //    -# funtion pointer lists (integer and floating types)
 //    -# configuration strings
 //    -# debuggging and test file streams
 //
*//*///////////////////////////////////////////////////////////////]
*/
struct hclass_links
{
  struct hclass_store *pHcStore;  /**< base address of HClass store */
  struct dsp_data_mask *pHCMask;  /**< ptr to the mask of pristine measurands*/
  struct dpolapp_setup* pSetup;   /**< ptr to Setup */
  char* psCurrentConfName;        /**< ptr to current configuration name */
/** list of HClass calculation tasks, in blocks of DPOLAPP_MAX_CATHEGORIES intervals */
  struct hclass_task *pHcTasks[DPOLAPP_MAX_ACTIONS];
  HCFNCT fHCFunction[DPOLAPP_MAX_ACTIONS];  /**< ptr to floating point valued function */
  HCUINCT uiHCUinction[DPOLAPP_MAX_ACTIONS];/**< ptr to unsigned integer valued function  */
/** HydroClass/dpolapp history log file ptr; obsolete */
  FILE *log_file;
/** MBG dump file ptr; used in DPOLAPP_DBG = 1 mode */
  FILE *HcFileDumpMBF;
/** HydroClass/dpolapp task file ptr; used in DPOLAPP_DBG = 1 mode */
  FILE *lst_file;

/** ptrs to dpolapp intern data areas */
  uint16_t* puiRayDatasOut[DPOLAPP_MAX_INTERNS];
  float*   pfRayDatasOut[DPOLAPP_MAX_INTERNS];
}; /* struct hclass_links */

/** Root struct of runtime hclass_store; */
/* ////////////////////////////////////////////////////////////////]
*//*!
 //  <b>  Description:  </b> 
 //  struct hclass_store is the main data structure of the run time HClass.
 //  It contains
 //    -# the package runtime status flags,
 //    -# the run time mapping of the retrieved HClass configuration
 //       i.e. the task work sheet
 //
*//*///////////////////////////////////////////////////////////////]
*/
#ifdef NR_CLASSIFIERS
struct hclass_store
{
  uint8_t bLogToFile;  /**< Enable package logging to dpolapp history file in $IRIS_LOG  */
  uint8_t bLogToTerm;  /**< Enable package logging to terminal */
  char sLogPath[PATHNAME_SIZE]; /**< dpolapp history logging file location */
/** first test flag settings */
  uint16_t uiTaskCalFlags;
/** momentary sweep number */
  int32_t iDBGSweep;
/** momentary ray number */
  int32_t iDBGRay;
/** momentary gate number */
  int32_t iDBGBin;
/** momentary AZ */
  float fAz;
/** momentary EL */
  float fEl; 
/** current configuration nick name */
  char sCurrentConfName[DPOLAPP_NAME16_SIZE];
/** the modification time of the runtime HClass configuration file 
    DPOLAPP_NAME.conf; 
     0: never retrieved */ 
  time_t st_mHConfigurationTime;
  off_t  st_size; /**< additional check on runtime file status */
/** active classifier IDs and conversion info */
  struct enum_convert Classifiers[NR_CLASSIFIERS]; 

  uint32_t uiMsgs[DPOLAPP_MSGES]; /**< HydroClass message counts*/
/*-- run time module status flags
 */

/** carry over of the HClass status; extension of lMakeHc 
    0: overall, 1: configuration 2: data*/
  uint8_t usHCstatus[3]; 
/** status flag of the configuration, one of  {DPOLAPP_RUNCFGSTATE_xxx} */
  uint8_t usHCCfgStatus;
  uint8_t usHCmode; /**< run mode: H+V or H-only */
  uint8_t usHCinit; /**< the thread initialization state DPOLAPP_INIT_DONE: done */
  uint8_t usHClist; /**< the task listing state DPOLAPP_INIT_DONE: done */
  uint8_t usHCdumpMBF; /**< the MBF listing state DPOLAPP_INIT_DONE: done */
/** number of HClass result data types; 
    typically 1 (DB_HCLASS2, or DB_USER2 reported); 
    FUTURE VERSIONS: >1 for reporting intermediate results (debugging) */
  uint8_t usNrDpolappResults;
/** dpolapp result type specifications */
  struct dpolapp_result DpolappResults[DPOLAPP_MAX_OUTPUTS];

/** work sheet for dpolapp tasks;  */
  struct hclass_task   HcTasks[DPOLAPP_MAX_ACTIONS];  
/** total number of HydroClass computation steps, in cathegories of DPOLAPP_MAX_CATHEGORIES */
  uint16_t uiNrHcTasks[DPOLAPP_MAX_CATHEGORIES];
/** cathegory offsets */
  uint16_t uiHcCathegoryOffset[DPOLAPP_MAX_CATHEGORIES];

/** Mask of pristine measurands (i.e. IRIS/RDA types< NUM_DEFINE_DATA) required to compute through the HClass calculus */
  struct dsp_data_mask   HCMask;
/** min  data type index, speeds up interation in reingest */
  uint16_t uiNumMin;
/** min  data type index, speeds up interation in reingest */
  uint16_t uiNumMax;

/** ray extension depth needed in the HClass ray calculus */
  uint16_t uiRayExtension;

/** cross reference table for RDA (PPRMS_NXXX) vs IRIS (DB_XXXX) data type numbers */
  uint8_t usRdaOfIris[NUM_DEFINED_DATA]; 
/** cross reference tables for IRIS (DB_XXXX) vs RDA (PPRMS_NXXX) data type numbers */ 
//  unused uint8_t usIrisOfRda[PPRMS_N_COUNT][DPOLAPP_NUM_ALTTYPES];
/*
* allocation for the HClass retrieved runtime configuration, now with calloc *
  struct dpolapp_setup   Setup;      
*/

/*-- data for release identifications & compiler time configuration links
 */

  float fTempThreshold;  /**< template level additional thresholding */
/** mode of quality considerations 
    0: consider the input data passing quality thresholds 
    1: consider all computed input data */
  uint8_t usQualityMode;
/** arrangement for transmitting ML info from dpolapp.conf to run time input */
  int32_t iMLinfo[2]; 
/** store the radar antenna height w.r.t. mean sea level [m]*/
  int32_t iAntMSLHeightM;
/** floating point presentations of default data values (typically thresholded data)*/
  float fThresholds[DPOLAPP_MAX_ACTIONS]; 
/** number  of internmediate data types */
  uint8_t usNrInterns;
/**  internmediate data types */
  uint16_t uiInternTypes[DPOLAPP_MAX_INTERNS];
/** intermediate measurands ray data */
  float     fRayDatasOut[DPOLAPP_MAX_INTERNS][RVPX_MAXBINS ];
/** intermediate measurands availability and enum data */
  uint16_t   uiRayDatasOut[DPOLAPP_MAX_INTERNS][RVPX_MAXBINS ];
/** intermediate measurands column/layer data */
  float     fCellDatasOut[400][360][2];
/** intermediate measurands availability and enum data */
  float     fCellDatasWgt[400][360][2];
/** Melting layer / Bright band information */
  struct rvpxMLinfo MLinfo;
  int16_t iMLaveMslM;
  int16_t iMLminMslM;
  int16_t iMLmaxMslM;

  /// the next are tentative arrangments
  BIN2 uiMLstaticAngles[DPOLAPP_MAX_ML_SWEEPS];
  BIN2 uiMLrayAngles[DPOLAPP_MAX_ML_RAYS];
  uint8_t usMLheights[DPOLAPP_MAX_ML_SWEEPS*DPOLAPP_MAX_ML_RAYS*DPOLAPP_MAX_ML_BINS];
#ifdef DPOLAPP_DEF_ALLOW_ALTTYPES
/** general list of alternative IRIS data type identifiers, e.g. DBT for DBZ */
  uint16_t uiAltType[DPOLAPP_MAX_ACTIONS];   
#endif  /*  DPOLAPP_DEF_ALLOW_ALTTYPES */
};
#endif  /* #ifdef NR_CLASSIFIERS */

void dpolapp_lib_nls_init(void);
#define DPOLAPP_LIB_INIT dpolapp_lib_nls_init()

/** default size of convective column (in high resolution scans, accounts for wind drift */
#define DPOLAPP_CONVECTION_SIZE_KM        1.

#ifdef SIGMET_INGEST_H

void HClassInitResources(void);

void HClassFreeAllResources(void);     /* File: HClassReingest.C */

void HClassFreeVolumeResources(void);  /* File: HClassReingest.C */

void HClassInitSweep                   /* File: HClassReingest.C */
( BIN2 iNomEl_c );
#endif  /* #ifdef SIGMET_INGEST_H */

uint8_t HClassInit /**<  File: HydroClass.C  */
(struct hclass_store* const pHcStore_a,
 struct hclass_links* const pHcPtrs_a);  

/** HClass once-in-a-process life-time initialization flag. Technical. */
#define DPOLAPP_INIT_DONE          8
/** HClass once-in-a-process life-time test flag. Technical. */
#define DPOLAPP_RUN_TEST   	   9
/** HClass once-in-a-process life-time test flag. Technical. */
#define DPOLAPP_NOTEST             7 


uint8_t HClassInitPtrs /**<  File: HydroClass.c  */
(struct hclass_store* const pHcStore_a, 
 struct hclass_links* const pHcPtrs_a, 
 const uint8_t bInit_a);

MESSAGE HClassUpdateConf /**< File: HydroClass.c */
(const uint8_t iUserID_a,
 const float fWaveLengthCM, 
 struct hclass_links* pHcPtrs_a);

#ifdef SIGMET_INGEST_HEADER_DEFINED
bool HClassInitVolume             /* File: HClassReingest.C */
( struct ingest_header *pIhd_a,          
  const struct dsp_data_mask *pData_read_a,
  struct dsp_data_mask *pData_write_a,
  uint8_t iUserID_a);   
#endif  /* #ifdef SIGMET_INGEST_HEADER_DEFINED */

/* Make a complete HydroClass (HCl) INGEST parameter data file.
 * This is just for one sweep.
 */
#ifdef SIGMET_INGEST_HEADER_DEFINED
MESSAGE HClassMakeDatafile          /* File: HClassReingest.C */
( const struct ingest_header *pIhd_a,
  struct ingest_data_file * const idaddr_a[NUM_DEFINED_DATA]); 
#endif  /* #ifdef SIGMET_INGEST_HEADER_DEFINED */

#ifdef SIGMET_INGEST_HEADER_DEFINED
MESSAGE HClassUpdateDatafile          /* File: HClassReingest.C */
( struct ingest_header *pIhd_a,
  struct ingest_data_file * const idaddr_a[NUM_DEFINED_DATA]);
#endif  /* #ifdef SIGMET_INGEST_HEADER_DEFINED */

MESSAGE HClassRayCalculusF /**< File: HydroClass.c  */
( const struct hclass_links* pHcPtrs_a,
  const uint8_t usNrInVars_a,
  const int32_t iStartBin_a,
  const int32_t iNrBins_a,
  const int32_t iNrTotBins_a,
  uint16_t* const puiRayArrayIn_a[],
  float*  const pfRayArrayIn_a[],
  uint16_t* const puiRayArrayOut_a[],
  float* const pfRayArrayOut_a[]);

MESSAGE HClassBunchCalculus /**<  File: HydroClass.c     */
( const struct hclass_links* const pHcPtrs_a, 
  const uint8_t usStartCathegory_a, 
  const uint8_t usEndCathegory_a,   
  float * const  pfInBunch_a);



double fDpolappGetRhoHVsnr                // File: HydroClass.C
( const uint8_t lSNRok_a,
  const uint8_t lZdrok_a,
  const uint8_t lZRelNse_a,
  const uint8_t lPolCNoise_a,
  const float   fMesNoise_a,
  const float   fFilterSNR_a,
  const float   fRhoHV_a,
  const float   fZdr_a,
  const float   fZdrOffset_a,
  const float * const  fRxTxNoises_a);

#ifdef __cplusplus
}
#endif

void mtInitMajorMode_pppHClass( void );   // File: mt_HClassRDA.c 
MESSAGE iConfigErrorDualPol_HClass( const UINT1 iOp_a );  // File: mt_HClassRDA.c
void initProcSection_pppHClass( void ) ;  // File: mt_HClassRDA.c 
void exitProcSection_pppHClass( void );   // File: mt_HClassRDA.c 
void ctInitMajorMode_pppHClass( void );   // File: HClassRDA.c 

#endif // #ifndef SIGMET_DPOLAPP_LIB_H 
