
! Program to calculate sea level temperature and density from 1000Mb
! temperature and mean sea level pressure (msl)

SUBROUTINE calc_tempes_densi_sealev(ic,nd,pathSI,den)

USE MOD_CSTS

     IMPLICIT NONE

!ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
!ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      INTEGER, INTENT(IN) :: nd
      INTEGER, INTENT(IN) :: ic
      CHARACTER(LEN=*), INTENT(IN) :: pathSI
      REAL, INTENT(OUT) :: den(nd,ic)

      real c,yy
      real psl(ic),tmil(ic),tsl(ic)
      integer anno,mes,dia
      integer i,j
      character for10*100
      character for13*100
      character path*100

      c=r*a/g
      path=pathSI

      print*,"program 1: sea level temperature and density"

      write(for10,'(a,i05,a)') '(7x,i4,1x,i2,1x,i2,3x,',ic,'(f5.0,1x))'
      write(for13,'(a,i05,a)') '(20x,',ic,'(f5.1,1x))'

! Input files
       open(1,file=trim(path)//'msl.dat')     
       open(2,file=trim(path)//'t1000.dat')

      do i=1,nd
       read(1,for10) anno,mes,dia,(psl(j),j=1,ic)
       read(2,for13)(tmil(j),j=1,ic)
       do j=1,ic
        yy=log(tmil(j))-c*log(1000./psl(j))
        tsl(j)=exp(yy)
! Air density equation
        den(i,j)=(psl(j)*100.)/(r*tsl(j))
       enddo
      enddo

      close(2)
      close(1)

END SUBROUTINE calc_tempes_densi_sealev

