       module fluidprops
         real, allocatable :: t(:),p(:)
         integer nd
c     t -  array containing temperatures
c     p -  array containing pressures
c     nd - number of temperature or pressure data values
c
       end module
c
       program proptest
       use fluidprops
       implicit none
       real tavg,tmax,tmin,pavg,pmin,pmax
c
c      Program to read in data on temperature and pressure
c      and print out the following:
c     tavg  -   average temperature
c     tmin  -   minimum temperature
c     tmax  -   maximum temperature
c     pavg  -   average pressure
c     pmin  -   minimum pressure
c     pmax  -   maximum pressure
c
c
c     Programmed by John Mahaffy   2/28/97
c
      call input
      call stats ( t,nd, tavg, tmin, tmax)
      call stats ( p,nd, pavg, pmin, pmax)
      call output( tavg,tmin,tmax,pavg,pmin,pmax)
       stop
       end
c*************************************************************************
       subroutine input
       use fluidprops
       implicit none
       logical lexist
       integer lines, filend, i
       character*40 filename
c
c      Prompt for and read the name of an input file.
c      Check for the existance of the file, and if found
c      open it count the number of data pairs, allocate the array
c      space, and read the pressure and temperature data.
c
c      Definition of variables
c
c      lexist = logical variable for indicating the existance of the input
c               file
c      lines = number of records in temperature data file 
c      filend = i/o variable which specifies status of the read
c
c     (insert the necessary statements to get the input file name check
c      for its existance, and print an appropriate message)
c---------------------------------------------------------------------
      write (*,2000,advance='no') 'File containing Temperature and'
     &                           ,' Pressure data: '
 2000 format(a,a)
      read *, filename
      inquire (file=filename, exist=lexist)
       if (lexist) then
          print *, trim(filename)//' being processed'
       else
          print *, trim(filename)//' not found'
          stop
       endif
c---------------------------------------------------------------------
c
       open(11,file=filename)
c
c      Counts lines to determine amount of memory to allocate
c      filend = i/o variable which specifies status of the read
c               command
c
       nd=-1
       filend=0
       do while(filend.ne.-1)
         read(11,*,iostat=filend)
         nd=nd+1
       end do 
       print  *, nd, ' Data pairs read'
c
c     Allocate array space for the temperature and pressure  data
c--------------------------------------------------------------------
      allocate( t(nd), p(nd))
c--------------------------------------------------------------------
c
c    Read the temperature, pressure pairs
c
         rewind(11)
       do i = 1,nd
         read(11,*) t(i), p(i)
       enddo
         close(11)
       return 
       end
c*************************************************************************
      subroutine output( tavg,tmin,tmax,pavg,pmin,pmax)
      implicit none
      real tavg,tmin,tmax,pavg,pmin,pmax
c
c     tavg  -   average temperature
c     tmin  -   minimum temperature
c     tmax  -   maximum temperature
c     pavg  -   average pressure
c     pmin  -   minimum pressure
c     pmax  -   maximum pressure
c
      write(*,2000) tavg, tmin, tmax
 2000 format ( ' Average Temperature = ',f7.1,/
     & ' Minimum Temperature = ', f7.1,/
     & ' Maximum Temperature = ', f7.1)
c
      write(*,2001) pavg, pmin, pmax
 2001 format ( ' Average Pressure = ',1p,e12.4,/
     & ' Minimum Pressure = ', e12.4,/
     & ' Maximum Pressure = ', e12.4)
      return
      end
c
c    Create the subroutine below to obtain the average, minimum, and
c    maximum from a given array, ignoring values less than or equal to
c    zero  (You must use SUM, COUNT, MINVAL, MAXVAL, and the MASK=
c    argument here)
c------------------------------------------------------------------------
      subroutine stats ( x,nx, xavg, xmin, xmax)
c
c    Obtain average, minimum and maximum values for the contents of an
c    array "x"
c
c    John Mahaffy   March 14, 1997
c
      implicit none
      integer nx
      real x(nx),xavg,xmin,xmax
      xavg=sum(x(1:nx),mask=x(1:nx).gt.0.0)/count(x(1:nx).gt.0.0)
      xmin=minval(x(1:nx),mask=x(1:nx).gt.0.0)
c
c    Given the mask and the nature of maxmal, the mask= is optional below
c
      xmax=maxval(x(1:nx),mask=x(1:nx).gt.0.0)
      return
      end

