program process

  use Std_Variables
  use IO_Params
  use FileInfo_Type
  use Dimensions_Type
  use Dimensions_Constructor
  use TopLevel
  use FileIO
!
  use Constants , except=>pi
  use Dynamics
  use Math

! load FST       <- force comp90.d loading of ARMNLIB (FST library)

  !  This program template serves as the basis for the developer's
  !  interface for the MRG Interactive MetCal program and library.  
  !  Simple setup and IO operations are performed here; the developer
  !  is expected to extend upon this code as needed during further
  !  subprogram development for the library.

  implicit none

  !  Program Name: 
  !  Version: 1.0
  !  Programmer: 
  !  Date Modified:
  !  Modified by:

!!$    MRG_Interactive MetCal metorological calculator - developer's template
!!$    Copyright 2001 (C) Ron McTaggart-Cowan
!!$
!!$    This file is part of MRG Interactive MetCal (MIM).
!!$
!!$    MIM is free software; you can redistribute it and/or modify
!!$    it under the terms of the GNU General Public License as published by
!!$    the Free Software Foundation; either version 2 of the License or
!!$    any later version.
!!$
!!$    MIM is distributed in the hope that it will be useful,
!!$    but WITHOUT ANY WARRANTY; without even the implied warranty of
!!$    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!!$    GNU General Public License for more details.
!!$
!!$    You should have received a copy of the GNU General Public License
!!$    along with MIM; if not, write to the Free Software
!!$    Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
!!$    MA  02111-1307  USA

  !  Declaration of variables
  logical :: gotLat                                     !whether or not Lat/Long was obtained
  logical, dimension(:), allocatable :: inOpen,outOpen  !switches to open input,output files
  integer :: t,i,j,k,error                              !counters and error messages
  integer :: myLevel                                    !level to read/write
  character(len=1) :: vCoord                            !vertical coordinate ('p'<-pressure, 'z'<-height)
  character(len=4) :: mapProj                           !map projection
  character(len=12) :: myVar                            !variable name for read/write

  !  Declaration of work fields
  integer, dimension(:,:), allocatable :: timer         !array for date/forecast hours
  integer, dimension(2) :: oldPosition_ll,newPosition_ll
  real, dimension(:,:), allocatable :: lat,long,dxs     !lat/long/scaled resolution arrays
  real, dimension(:,:,:), allocatable :: zgaIO,tgIO,qgIO,ugIO,vgIO  !3D field
  real, dimension(:,:),   allocatable :: p0,me,pmsl               !2D filed
  real :: centralPressure,radius
  character(len=132), parameter :: myFile="boguspos.cfg"
  character(len=1)   :: isBoguscorrect
  character(len=64)  :: string
  integer, parameter :: inUnit=1
  integer :: err

!
! Get bogus data
    open (inUnit,file=myFile,status='OLD',iostat=err)
    if (err.ne.0) then
         print*, "FATAL: Could not find settings file ",myFile
         stop
        endif
        write(6,*)'The following is your bogus data'
        read(inUnit,*) string, centralPressure
        write(6,*) string, centralPressure
        read(inUnit,*) string, radius
        write(6,*) string, radius
        read(inUnit,*) string, oldPosition_ll
        write(6,*) string, oldPosition_ll
        read(inUnit,*) string, newPosition_ll
        write(6,*) string, newPosition_ll
        write(6,*)'Please make sure it is correct? Y(y) or N(n)'
        read(*,*) isBoguscorrect
        if(isBoguscorrect .eq. 'N' .or. isBoguscorrect .eq. 'n')then
          stop  
        endif 

  !  Get user-defined parameters                                        (use IO_Params)
  call getParams(input,output,gridFile,fileType)

  !  Initialization and opening                                         (use TopLevel)
  allocate(inOpen(size(input)))
  allocate(outOpen(size(output)))
  inOpen = .true. ; outOpen = .true.
  call openGridFiles(inOpen,outOpen,fileType)
  !  Get grid dimensions from primary input file                        (use TopLevel)
  call getGridSize(input(1),fileType,ni,nj,nk,ntimes,                   &
    mapFile=gridFile)

  !  Memory allocation
  allocate( dxs(ni,nj)            )	
  allocate( lat(ni,nj)            )
  allocate( long(ni,nj)           )
  allocate( timer(ntimes,2)       )
  allocate( lev(nk)               )
  allocate( me(ni,nj)             )
  allocate( p0(ni,nj)             )
  allocate( pmsl(ni,nj)           )

  allocate( zgaIO(ni,nj,nk)       )
  allocate( tgIO(ni,nj,nk)        )
  allocate( qgIO(ni,nj,nk)        )
  allocate( ugIO(ni,nj,nk)        )
  allocate( vgIO(ni,nj,nk)        )


  !  Get file information (take lat/long from grid file)                (use TopLevel)
  call getGridInfo(input(1),fileType,ni,nj,nk,ntimes,lev,               &
             vCoord,timer,labl,progType,dx,mapProj,dgrw,infoFld,        &
             gotLat,lat,long,gridFile)

  !  Construct dim                                                      (use Dimensions_Constructor)
  if (gotLat) then
     call makeDim(ni,nj,nk,dx,lat,dxs,lev,vCoord,infoFld,fileType,      &
          progType,dim,mapProj=mapProj,maplong=long)
  else
     call makeDim(ni,nj,nk,dx,lev,vCoord,infoFld,fileType,progType,	&
	  dim)
  endif
  
  !  Loop over time for read/write
  !do t=1,ntimes
   do t=1,1              ! Pay attention here. 

  !  Read in input variable                                             (use FileIO)
     call readGrid(input(1),'GZ',timer(t,:),dim,zgaIO)
     call readGrid(input(1),'TT',timer(t,:),dim,tgIO)
     call readGrid(input(1),'UU',timer(t,:),dim,ugIO)
     call readGrid(input(1),'VV',timer(t,:),dim,vgIO)
     call readGrid(input(1),'HU',timer(t,:),dim,qgIO)
     call readGrid(input(1),'ME',0,timer(t,:),dim,me)
     call readGrid(input(1),'P0',-1,timer(t,:),dim,p0)

  !  Call tcBogus

     call tcBogus(zgaIO,tgIO,qgIO,ugIO,vgIO,dim,centralPressure,            &
              radius,oldPosition_ll,newPosition_ll) 

     call writeGrid(output(1),zgaIO,'GZ',timer(t,:),Dim)
     call writeGrid(output(1),tgIO,'TT',timer(t,:),Dim)
     call writeGrid(output(1),ugIO,'UU',timer(t,:),Dim)
     call writeGrid(output(1),vgIO,'VV',timer(t,:),Dim)
     call writeGrid(output(1),qgIO,'HU',timer(t,:),Dim)
     call writeGrid(output(1),me,  'ME',0,timer(t,:),Dim)
     pmsl = 1000.*exp(9.8*zgaIO(:,:,1)*damtom/(281.*(tgIO(:,:,1)+to)))   
     call writeGrid(output(1),pmsl,'PN',0,timer(t,:),Dim)
     call writeGrid(output(1),p0,  'P0',0,timer(t,:),Dim)
!
! Output grad file
!
     open(33,file='ugIO.grd',form='unformatted',      &
          access='direct', recl=4*ni*nj,status='unknown')
     do k=1,nk
        write(33,rec=k)((ugIO(i,j,k),i=1,ni),j=1,nj)
     enddo
     open(33,file='vgIO.grd',form='unformatted',      &
          access='direct', recl=4*ni*nj,status='unknown')
     do k=1,nk
        write(33,rec=k)((vgIO(i,j,k),i=1,ni),j=1,nj)
     enddo
     open(33,file='tgIO.grd',form='unformatted',      &
          access='direct', recl=4*ni*nj,status='unknown')
     do k=1,nk
        write(33,rec=k)((tgIO(i,j,k),i=1,ni),j=1,nj)
     enddo
     open(33,file='qgIO.grd',form='unformatted',      &
          access='direct', recl=4*ni*nj,status='unknown')
     do k=1,nk
        write(33,rec=k)((qgIO(i,j,k),i=1,ni),j=1,nj)
     enddo
     open(33,file='zgaIO.grd',form='unformatted',      &
          access='direct', recl=4*ni*nj,status='unknown')
     do k=1,nk
        write(33,rec=k)((zgaIO(i,j,k),i=1,ni),j=1,nj)
        enddo
     open(33,file='p0.grd',form='unformatted',      &
          access='direct', recl=4*ni*nj,status='unknown')
      write(33,rec=1)((p0(i,j),i=1,ni),j=1,nj)

  enddo        ! End of time loop

  !  Close input/output files                                           (use TopLevel)
  call closeGridFiles(inOpen,outOpen,fileType)
  close(inUnit)
!C  stop 'User-forced termination'

  !  End of main program
end program process

