!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software 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 above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
***s/r itf_phy_rdfile -- Reading a file for the physics package
*
#include "model_macros_f.h"
*

      subroutine itf_phy_rdfile (F_fichier_S,F_read_cb,F_messg_s,mode),3
      implicit none
*
      character* (*) F_fichier_S, F_messg_s
      integer mode
      external F_read_cb
*
*author
*     M. Desgagne  (Spring 2008)
*
*revision
* v3_31 - Desgagne M.       - initial version
*
*arguments
*  Name        I/O                 Description
*----------------------------------------------------------------
* F_fichier_S   I      file name of input file
* F_read_cb     I      read call back routine (from physics)
*
#include "ptopo.cdk"
#include "path.cdk"

      character*1024 filename
      logical found_L
      integer iun,ilir,inbr,status,ierr,max_ndim
      parameter (max_ndim=1000)
      integer dim(max_ndim)
      integer fnom,fstouv,fstopc,fstfrm,fclos,wkoffit,bufnml(1000000)
      real, dimension (:), allocatable :: rbuf
*-----------------------------------------------------------------
*
      status = 0
*
      filename = trim(Path_input_S)//'/'//trim(F_fichier_S)

      if (Ptopo_myproc.eq.0) then
*
         inquire (FILE=filename,EXIST=found_L)
*
         if (found_L) then
            ilir = wkoffit(filename)
            if (  (ilir.eq.1) .or.(ilir.eq.2).or.
     $            (ilir.eq.33).or.(ilir.eq.34) ) then
               write (6,1001) trim(F_messg_s),mode,trim(filename)
            else
               print*, ' FILE ',trim(filename)
               print*, ' NOT FST FILE FORMAT -- ABORT --'
               status = -1
            endif
         else
            print*
            print *,'********************************************'
            print *,'   CAN NOT FIND FILE: ',trim(filename)
            print *,'********************************************'
            status = -1
         endif

      endif
*
      call gem_stop('itf_phy_rdfile',status)
*
      select case (mode)

         case (1)

            if (Ptopo_myproc.gt.0) inbr = fstopc ('MSGLVL','SYSTEM',.false.)
            iun    = 0
            ilir   = fnom    (iun,filename,'STD+RND+OLD',0)
            ilir   = fstouv  (iun,'RND')
            status = 200
            call F_read_cb (iun,rbuf,dim,status)
            if (status.lt.0) goto 9988
            allocate (rbuf(dim(2)))
            status = 300
            call F_read_cb (iun,rbuf,dim,status)
            inbr   = fstfrm  (iun)
            inbr   = fclos   (iun)
            deallocate (rbuf) 

         case (2)

            if (Ptopo_myproc.eq.0) then
               call array_from_file(bufnml,size(bufnml),filename)
            else
               inbr = fstopc ('MSGLVL','SYSTEM',.false.)
            endif
            call RPN_COMM_bcast(bufnml,size(bufnml),"MPI_INTEGER",0,
     $                                                 "grid",ierr )
            filename=trim(F_fichier_S)
            call array_to_file (bufnml,size(bufnml),filename)

            iun  = 0
            ilir = fnom (iun,filename,'STD+RND+OLD',0)
            ilir = fstouv (iun,'RND')

            status = 200
            call F_read_cb (iun,rbuf,dim,status)
            if (status.lt.0) goto 9988
            allocate (rbuf(dim(2)))
            status = 300
            call F_read_cb (iun,rbuf,dim,status)

            inbr = fstfrm (iun)
            inbr = fclos  (iun)    
            deallocate (rbuf) 
          
         case (3)

            status = 0
            if (Ptopo_myproc.eq.0) then
               iun  = 0
               ilir = fnom    (iun,filename,'STD+RND+OLD',0)
               ilir = fstouv  (iun,'RND')

               status = 200
               call F_read_cb (iun,rbuf,dim,status)
               if (status.lt.0) goto 9977
               allocate (rbuf(dim(2)))
               status = 250
               call F_read_cb (iun,rbuf,dim,status)
               inbr = fstfrm  (iun)
               inbr = fclos   (iun)    
            endif

 9977       call gem_stop ('itf_phy_rdfile',status)
            call RPN_COMM_bcast (dim,max_ndim,"MPI_INTEGER",0,"grid",ierr)
            if (Ptopo_myproc.gt.0) allocate (rbuf(dim(2)))
            call RPN_COMM_bcast (rbuf,dim(2),"MPI_REAL",0,"grid",ierr)
            status = 400
            call F_read_cb (iun,rbuf,dim,status)
            deallocate (rbuf) 

         case DEFAULT

 	    if (Ptopo_myproc.eq.0) print*, 
     $      'itf_phy_rdfile: make up your mind'

      end select
*
 9988 call gem_stop ('itf_phy_rdfile',status)
*
      inbr = fstopc ('MSGLVL','INFORM',.false.)

 1001 format (/'READING ',a,' FILE in MODE ',i1,' from:'/a)
*
*-----------------------------------------------------------------
*
      return
      end