!*******************************************************************************
!$RCSfile$
!$Revision$
!$Date$
!*******************************************************************************
!*******************************************************************************
!            Initialize Tool
!*******************************************************************************
INTEGER FUNCTION InitTool( UserID,UserCall,request,limit,cstr_INIfile )

USE SCIMgr_fd
USE SWIMparam_fd
USE files_fi
USE error_fi
USE SCIMgr_fi
USE plotlist_fi, ONLY:MemoryField, CasualtyCutOff, UsePlotCN2
USE metparam_fd
USE puff_fi
USE matl_fi
USE surface_fi
USE param_fd
USE search_fd
USE SCIMgrState

IMPLICIT NONE

INTEGER, PARAMETER :: CURRENT_LAND_USE_VERSION = 200
INTEGER, PARAMETER :: CURRENT_SYSTOOL_VERSION  = 200
INTEGER, PARAMETER :: CURRENT_SWIM_VERSION     = 1400
INTEGER, PARAMETER :: CLOCK_UPDATE = 1000
INTEGER, PARAMETER :: CLOCK_DELAY  = 200
REAL,    PARAMETER :: DEFAULT_CASUALTY_CUTOFF = 0.0  ![0-100] percent

INTEGER,              INTENT( IN    ) :: UserID
INTEGER(LEN_ADDRESS), INTENT( IN    ) :: UserCall
INTEGER,              INTENT( INOUT ) :: request
TYPE( limitT ),       INTENT( IN    ) :: limit
TYPE( fileNameT ),    INTENT( IN    ) :: cstr_INIfile

CHARACTER(PATH_MAXLENGTH) INIfile

INTEGER ios, initValue
INTEGER landuseVersion, sysVersion, versionSWIM
INTEGER irv

CHARACTER(PATH_MAXLENGTH) section,string,defstr,datapath,binpath,temppath,scipath
CHARACTER(PATH_MAXLENGTH) rtnstr

TYPE( char128T ) charStruct

TYPE( messageT ) error

INTEGER,              EXTERNAL :: InitMessageHandler
INTEGER(LEN_ADDRESS), EXTERNAL :: GetMessageHandler
INTEGER(LEN_ADDRESS), EXTERNAL :: ADDRESSOF
INTEGER,              EXTERNAL :: SWIMInit
LOGICAL,              EXTERNAL :: CheckPathT

LOGICAL, EXTERNAL :: InitDatums
INTEGER, EXTERNAL :: GetVersionString
INTEGER, EXTERNAL :: InitPlotTool

INTEGER, EXTERNAL :: SAG_InitList

INTEGER, EXTERNAL :: sysCurrentDirectory,sysGetProfileInt
INTEGER, EXTERNAL :: sysDeleteFile,sysGetLastError,sysWriteProfileString
INTEGER, EXTERNAL :: sysGetProfileString

INTEGER, EXTERNAL :: GetLandUseVersion, GetLandUsePathLength
INTEGER, EXTERNAL :: sysGetVersion, sysGetPathLength
INTEGER, EXTERNAL :: SWIMversion, SWIMPathLength

!==== Initialize tool state and error

CALL SCIMgrInitState()
CALL init_error()
clockUpdate = CLOCK_UPDATE
clockdelay  = CLOCK_DELAY
MemoryField = .FALSE.

!==== Initialize return

irv = SCIMgrSetState( HS_IDLE )

IF( SCIMgrCheckState(HS_ANYSTATE) )THEN !Always available but shouldn't be called
  irv = SCIMgrSetState( HS_BUSY )
END IF

CALL ExceptionTest()

InitTool = SCIPfailure

SCIPCallBack = UserCall
IF( GetMessageHandler() == 0 )THEN
  irv = InitMessageHandler( SCIPCallBack )
  IF( irv == SCIPfailure )THEN
    CALL GetMessageHandlerError( error )
    nError   = error%iParm
    eRoutine = 'InitTool'
    eMessage = TRIM(error%aString)
    eInform  = TRIM(error%bString)
    GOTO 9999
  END IF
END IF

CALL set_messaging( userID )

SCIPinProgress = .FALSE.

!==== Initialize plotting

irv = InitPlotTool()
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'InitPlotTool error'
  GOTO 9999
END IF

!==== Initialize file unit numbers

INIfile = cstr_INIfile%string
CALL SetFileUnitsT()


!==== Initialize dll

LastError = 0

!==== Get Default Bin path - same as INIfile path

CALL SplitName( INIfile,string,binpath )

!==== Check INI file for SCIPbin, SCIPdata and SCIPTempDir entries
! Default Directories
!  SCIPBinDir  = INI file path
!  SCIPDataDir = SCIPBinDir\..\data
!  SCIPTempDir = SCIPDataDir\temp
!  SciDataDir  = SCIPDataDir\scidata

section = 'Paths'
defstr  = ' '
string  = 'SCIPBinDir'
irv = sysGetProfileString( section,string,defstr,datapath,INIfile )
IF( irv == SCIPnull )THEN
  string  ='scimaindir'
  irv = sysGetProfileString( section,string,defstr,datapath,INIfile )
  IF( irv == SCIPnull )THEN
    datapath = binpath
  END IF
END IF
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysGetProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF
binpath = datapath

string = binpath
CALL BackUpPath( string,1 )
defstr = 'data'
CALL AddPath( defstr,string )
string  ='SCIPDataDir'
irv = sysGetProfileString( section,string,defstr,datapath,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysGetProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

defstr = 'temp'
CALL AddPath( defstr,datapath )
string = 'SCIPTempDir'
irv = sysGetProfileString( section,string,defstr,temppath,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysGetProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

defstr = 'Scidata'
CALL AddPath( defstr,datapath )
string = 'SciDataDir'
irv = sysGetProfileString( section,string,defstr,scipath,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysGetProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

!==== Find Landuse file

section = 'Land_Use'
defstr  = 'landuse.dat'
CALL AddPath( defstr,scipath )
string = 'LandUseDataFile'
irv = sysGetProfileString( section,string,defstr,file_lus,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysGetProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

!==== Check LandUse Currency

landuseVersion = GetLandUseVersion()

IF( landuseVersion/100 /= CURRENT_LAND_USE_VERSION/100 )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Incorrect version of the LandUse dll detected'
  WRITE(eInform,*)'Version=',landuseVersion, &
                 ' Expected=',CURRENT_LAND_USE_VERSION
  GOTO 9999
END IF

IF( GetLandUsePathLength() /= PATH_MAXLENGTH )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Incompatible path length in the LandUse dll'
  WRITE(eInform,*)'LandUse=',GetLandUsePathLength(), &
                 ' Expected=',PATH_MAXLENGTH
  GOTO 9999
END IF

!==== Check sysTool Currency

sysVersion = sysGetVersion()
IF( sysVersion/100 /= CURRENT_SYSTOOL_VERSION/100 )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Incorrect version of the sysTool dll detected'
  WRITE(eInform,*)'Version=',sysVersion, &
                 ' Expected=',CURRENT_SYSTOOL_VERSION
  GOTO 9999
END IF

IF( sysGetPathLength() /= PATH_MAXLENGTH )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Incompatible path length in the sysTool dll'
  WRITE(eInform,*)'sysTool=',sysGetPathLength(), &
                 ' Expected=',PATH_MAXLENGTH
  GOTO 9999
END IF

!==== Get Clock update parameters

section = 'SCIPtool'
string  = 'ClockDelay'
irv = sysGetProfileInt( section,string,CLOCK_DELAY,clockDelay,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Failure reading clock delay parameter from INI file'
  eInform  = 'INI file='//TRIM(INIfile)
  GOTO 9999
END IF

string = 'ClockUpdate'
irv = sysGetProfileInt( section,string,CLOCK_UPDATE,clockUpdate,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Failure reading clock update parameter from INI file'
  eInform  = 'INI file='//TRIM(INIfile)
  GOTO 9999
END IF

!==== Size parameters

MAX1D_MET = limit%met1D
MAXPUF    = limit%puffs
MAXSG     = limit%surfaceGrid

IF( MAX1D_MET <= 0 )MAX1D_MET = HUGE(ios)
IF( MAXPUF    <= 0 )MAXPUF    = MAXPUF_DEF
IF( MAXSG     <= 0 )MAXSG     = MAXSG_DEF

!==== Nullify material auxiliary array pointer

NULLIFY( mat_aux )

!==== Initialize return value

initValue = SCIPnull

!==== Init UTM

IF( BTEST(request,HIB_UTM) )THEN
  request = IBCLR(request,HIB_UTM)
  IF( InitDatums(INIfile) )THEN
    initValue = IBSET(initValue,HIB_UTM)
  ELSE
    initValue = IBCLR(initValue,HIB_UTM)
  END IF
ELSE
  initValue = IBCLR(initValue,HIB_UTM)
END IF

AvailUTM = BTEST(initValue,HIB_UTM)

!==== Read flag for CN2 processing

section = 'SCIPmode'
string  = 'Plot2Gauss'
defstr  = ' '
rtnstr  = defstr
irv = sysGetProfileString( section,string,defstr,rtnstr,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysGetProfileString returned an error on retrieving the 2-Gaussian Plot string'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF
IF( LEN_TRIM(rtnstr) == 0 )THEN
  UsePlotCN2 = .FALSE.
ELSE
  CALL cupper( rtnstr )
  IF( TRIM(rtnstr) == 'ON' )THEN
    UsePlotCN2 = .TRUE.
  ELSE
    UsePlotCN2 = .FALSE.
  END IF
END IF

!==== Check SWIM Currency

irv = SWIMversion( versionSWIM )

IF( versionSWIM/100 /= CURRENT_SWIM_VERSION/100 )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Incorrect version of the SWIM dll detected'
  WRITE(eInform,*)'Version=',versionSWIM, &
                 ' Expected=',CURRENT_SWIM_VERSION
  GOTO 9999
END IF

IF( SWIMPathLength() /= PATH_MAXLENGTH )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'Incompatible path length in the LandUse dll'
  WRITE(eInform,*)'SWIM=',SWIMPathLength(), &
                 ' Expected=',PATH_MAXLENGTH
  GOTO 9999
END IF

!==== Init SWIM DLL

irv = SWIMInit( request,initValue,INIfile )
IF( irv /= SWIMsuccess )THEN
  CALL setSWIMerror( 'SWIMInit' )
  GOTO 9999
END IF

!==== Update Currency

section = 'Currency'
string  = 'SCIPtoolVersion'
irv = GetVersionString( 1,charStruct )
irv = sysWriteProfileString( section,string,charStruct%string,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysWriteProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

section = 'Currency'
string  = 'SCIPUFF_Version'
irv = GetVersionString( 2,charStruct )
irv = sysWriteProfileString( section,string,charStruct%string,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysWriteProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

section = 'Currency'
string  = 'LandUse_Version'
CALL WriteVersionString( landuseVersion,defstr )
irv = sysWriteProfileString( section,string,defstr,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysWriteProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

section = 'Currency'
string  = 'SYStool_Version'
CALL WriteVersionString( sysVersion,defstr )
irv = sysWriteProfileString( section,string,defstr,INIfile )
IF( irv == SCIPfailure )THEN
  nError   = IV_ERROR
  eRoutine = 'InitTool'
  eMessage = 'sysWriteProfileString returned an error'
  WRITE(eInform,*)'Error =',irv
  GOTO 9999
END IF

!------ Initialize SAG linked list of grid structures

irv = SAG_InitList()

!------ Initialize inverse error function table

CALL init_erfci()

!==== return

InitTool = initValue

9999 CONTINUE

irv = SCIMgrSetState( HS_IDLE )

CALL reset_messaging()

RETURN
END
