C***********************************************************************
C***********************************************************************
      SUBROUTINE STARTUP
C
C startup routine for SACO_MOODY 
C reads the inq file and the mesh files and stuff like that 
C 19 May 1998 Charles Hannah
C 23 Oct 1998 Charles Hannah
C Hacked from the QUODDY4 main routine
C
C
C STANDARD IN:
C
C   FILINQ - name of the .inq file being used to conduct the simulation
C
C SUBROUTINE INPUTQ4:
C
C   FILICQ - name of hot start .icq4 file
C            (set to 'COLD-START' if a cold start is desired)
C   IDD0,IMM0,IYEAR0,SEC0 - timing parameters for the beginning
C                           of the simulation (only set set if 
C                           a cold start simulation is being
C                           conducted)
C   IDDL,IMML,IYEARL,SECL - timing parameters for the end
C                           of the simulation
C   DEG - degrees latitude to be used for coriolis terms (f-plane)
C   DELT - time step
C   NNV - number of vertical nodes
C   THETA - implicit gravity wave factor for the wave equation:
C              THETA=0.0 => explicit
C              THETA=1.0 => implicit
C   TAU0 - constant for weight applied to the continuity equation
C          when it is added to the vertically averaged momentum
C          equation to obtain the wave equation (typically .0002)
C   NLBS - integer flag to indicate linearized bottom stress
C          coefficient (AK) parameterizaton in horizontal momentum
C          equations (bottom stress=AK*bottom velocity):
C              NLBS=0 => apply constant AK=AKMIN
C              NLBS=1 => obtain fields of AK from user subroutine 
C                        LINSTRESS 
C   AKMIN - minimum linearized bottom stress coefficient
C   EKMIN - minimum vertical eddy diffusivity for momentum, heat,
C           and salinity
C   EPSN - implicit time weighting factor for vertical diffusion
C          of momentum:
C              EPSN=0.0 => explicit
C              EPSN=0.5 => Crank-Nicholson (centered)
C              EPSN=1.0 => implicit
C
C      Horizontal Mesh Information:
C
C   MESHNAME - name of the horizontal mesh
C   NN - number of horizontal nodes
C   NE - the number of horizontal 2-D triangular elements
C   NHBW - half bandwidth of horizontal finite element mesh
C   X(I),Y(I) - cartesian nodal coordinates
C   IN(3,L) - LABLIB STANDARD triangular element incidence list
C   HDOWN(I) - Bathymetric depth at nodes
C   SUMX(I) - cosine value for boundary nodes where V.n is 
C           specified
C   SUMY(I) - sine value for boundary nodes where V.n is 
C           specified
C   DSBDRY(I) - surface integral information
C   INBE(2,K) - LABLIB STANDARD boundary element incidence list
C   NBE - number of boundary elements
C   INGEO(2,K) - LABLIB STANDARD geostrophic boundary element
C           incidence list
C   NGEO - number of geostrophic boundary elements
C   NORM(I) - boundary node numbers where normal velocity is set
C           I =1,NVN       are V.n .NE. 0.0    (NCD=3)
C           I =KST,NNDIM   are V.n=0.0         (NCD=1,2)
C   LRVS(I) - node numbers of set depth and set velocity nodes
C           I=1,NR         are set depth nodes (NCD=5,6,9)
C           I=NR+1,NR+NPCN are V=0.0           (NCD=10)
C           I=LST,NNDIM    are V .NE. 0.0      (NCD=8,11)
C
C
C       Other Information:
C
C   BCFTR(I)  - Real binary multiplicative factor which deletes
C               the Coriolis term in momentum equation at solid
C               boundaries
C           BCFTR(I)=0.0 for land boundary nodes
C           BCFTR(I)=1.0 for all other nodes
C
C MAIN PROGRAM:
C
C   G - Gravitational acceleration
C   OMEGA2 - Radian frequency (rad/sec)
C   RAD - Radius of the earth
C
C   CLOSURE - Character string to indicate turbulence closure:
C             'CONSTANT' => vertical eddy viscosity/diffusivities set
C                           to EKMIN
C             'SPATIAL' => vertical eddy viscosities
C                           are obtained from the user subroutine 
C                           LINENZ  
C ++++++++++++++++++++++++++
C + INITIALIZATION SECTION +
C ++++++++++++++++++++++++++
C
C   IDD0,IMM0,IYEAR0,SEC0 - timing parameters for the beginning
C                           of the simulation, as set in the
C                           .icq4 hot-start file (the input
C                           values are only overwritten
C                           if a hot-start is begin conducted)
C
C   *OLD arrays are initialized with dependent variable data at one
C   time step prior to the beginning of the simulation  
C
C   ZETAOLD(I) - total depth
C   UOLD(I),VOLD(I) - x,y components of vertically averaged
C               horizontal velocity
C
C   *MID arrays are initialized with dependent variable data at the
C   beginning of the simulation  
C
C   ZETAMID(I) - total depth
C   UMID(I),VMID(I) - x,y components of vertically averaged
C                     horizontal velocity
C   ZMID(I,J) - 3-D nodal grid 
C   UZMID(I,J),VZMID(I,J) - x,y components of  velocity 
C 
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
C
C Define  the variables and common blocks required. 
      INCLUDE 'CASCO.DIM'
      INCLUDE 'mesh.h'
      INCLUDE 'saco.h' 
      INCLUDE 'startstop.h'
      INCLUDE 'friction.h' 
      INCLUDE 'constants.h'
      INCLUDE 'misc.h' 


C Read from inq file and used to calculation KD0, SEC0, KDL, SECL 
      INTEGER IMM0, IMML, IYEAR0, IYEARL
      INTEGER IDD0, IDDL  
C
C  Local variables
      INTEGER IINQ
C
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
C INPUT SECTION
C
C Objectives: Read .ins file, input mesh geometry data, open and begin
C writing to echo file, and set all simulation control parameters.
C
C Enter the name of the input file (".ins") of the simulation
C
      WRITE(*,'(a)') 'Enter the name of the input file (".ins")'
      READ (*,'(a)') FILINQ
      IINQ=INDEX(FILINQ,' ')-1
      write(*,'(a)')FILINQ
C
C Load run information from .inq and associated input files
C
      CALL INPUTS2(FILINQ,
     & FILICQ,IDD0,IMM0,IYEAR0,SEC0,IDDL,IMML,IYEARL,SECL,
     & DEG,DELT,NNV,THETA,TAU0,NLBS,AKMIN,EKMIN,EPSN,
     & MESHNAME,NN,NE,NHBW,X,Y,IN,HDOWN,SUMX,SUMY,DSBDRY,
     & INBE,NBE,INGEO,NGEO,NORM,NVN,KST,LRVS,NR,NPCN,LST,
     & BCFTR)

C
C Standard physical constants (REAL)
C
      G=9.806
      OMEGA2=1.4584E-4
      RAD=6.3675E6
      DEG=DEG
      COR=OMEGA2*SIN(DEG/180.*ACOS(-1.))
C
      NLBS = NLBS 
C
C Numerical Model weighting parameters (REAL)
C
      THETA=THETA
      TAU0=TAU0
      EPSN=EPSN
C
C Minimum parameters
C
      EKMIN=EKMIN
      AKMIN=AKMIN
C
C Model relationships
C
      NEV=NNV-1
C
C Write plethora of settings to echo file 
C and check to ensure that settings are allowable options!
C
      CALL ECHOSETS3(G,OMEGA2,RAD,DEG,COR, NLBS,
     & CLOSURE,THETA,TAU0,EPSN,EKMIN,AKMIN,NEV)
C
      WRITE(2,'(/A)')'INPUT SECTION SUCCESSFULLY COMPLETED'

C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
C INITIALIZATION SECTION
C
C Objectives: Initialize dependent variable arrays using either
C *COLD-START* or *HOT-START* information.  Then correct for physics
C switch settings and the desired turbulence parameterization.
C
C Initialize dependent variables
C
      CALL INITIALIZES3(MESHNAME,FILICQ,NN,NNV,HDOWN,DELT,
     &             IDD0,IMM0,IYEAR0,SEC0,
     &             ZETAOLD,UOLD,VOLD,ZETAMID,UMID,VMID,
     &             ZMID,UZMID,VZMID)
C
c     STOP
C Now apply corrections to be consistent with physics switch settings
C and desired turbulence parameterization (arrays set to reference
C values are fixed throughout the simulation).
C
C
      WRITE(2,'(/A)')'INITIALIZATION SECTION SUCCESSFULLY COMPLETED'
C
C Write timing parameters to echo file (not done until because the .icq4
C file provides starting time for hot starts)
C
      WRITE(2,'(/A)')'Time-stepping parameters:'
      WRITE(2,'(5X,A35,1X,I2,1X,I2,3X,I4,1X,F18.8)')
     &'Simulation start date and time     ',IDD0,IMM0,IYEAR0,SEC0
      WRITE(2,'(5X,A35,1X,I2,1X,I2,3X,I4,1X,F18.8)')
     &'Simulation end date and time       ',IDDL,IMML,IYEARL,SECL
      WRITE(2,'(5X,A35,1X,E12.4)')
     &'Time step (seconds)                ', DELT
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------
C START/STOP TIME SPECIFICATION SECTION
C
C Objectives: Set timing parameters for later use (these are fixed
C throughout the simulation).
C
C Starting time:
C  IDD0,IMM0,IYEAR0=starting date of the simulation (day,month,year)
C  KD0 is the day number starting date based on the
C  the Gregorian calendar (KD0=1 is January 1, 0000)
C  SEC0 starting time, in seconds after midnight, on KD0
C Stopping time:
C  (IDDL,IMML,IYEARL,KDL,SECL) are similar parameters to those above
C  86400 is the number of seconds in 1 day
C
C Get KD0/KDL from starting/stopping date and time
      CALL GDAY(IDD0,IMM0,IYEAR0,KD0)
      IF(SEC0.GE.86400.)CALL UP_DATE(KD0,SEC0)
      CALL GDAY(IDDL,IMML,IYEARL,KDL)
      IF(SECL.GE.86400.)CALL UP_DATE(KDL,SECL)
C
      WRITE(2,'(/A)')'START/STOP TIME SPECIFICATION SECTION SUCCESSFULLY
     & COMPLETED'
C-----------------------------------------------------------------------
C-----------------------------------------------------------------------

      WRITE(2,*)' Leaving STARTUP ' 
      WRITE(*,*)' Leaving STARTUP ' 
      write(*,*)zetaold(1),zetamid(1),zmid(1,1)
      RETURN
      END
