***********************************************************************
* PROGRAM: SEASONAL_VERIF
* LANGUAGE: FORTRAN 77
*---------------------------------------------------------------------
* PURPOSE: Categorizes input observations into categories based on
*          class limits. Produces output file consisting of categorical
*          observations
* USAGE: seasonal_verif.Linux.exe ${1} ${2} ${3} ${4}
*---------------------------------------------------------------------
* INPUT FILES
*    ${vp}${parm}_lowerlimit - lower class limit data
*    ${vp}${parm}_upperlimit -  upper class limit data
*    ${parm}_${vp}_archive.bi - 2 degree x 2 degree grids containing
*       observation data
*    mask_2x2.data - 2 degree x 2 degree mask data file
* OUTPUT FILES
*    ${parm}_${vp}_verif.bi - 2 degree x 2 degree grids containing
*       categorized observation data
*---------------------------------------------------------------------
* SUBROUTINES USED
*    NONE
* FUNCTIONS USED
*    NONE
*--------------------------------------------------------------------
* INPUT VARIABLES
*    mrec - record number of current observation period in output
*       archive binary file (indexed to 1st month of valid period)
*    parm - abbreviation for observed parameter
*    vp - abbreviation of observation period
*    dir - output directory for binary gridded output files
* LOCAL VARIABLES
*    blimit - lower tercile limits
*    i - current season or month
*    i - east/west grid number
*    ii - current record number
*    imo - current season or month
*    j - east/west grid number
*    j - north/south grid number
*    k - north/south grid number
*    irec - record number of current observation period in output
*       archive binary file (indexed to 1st month of valid period)
*    iunit - input file unit number
*    krec - record number
*    lower - lower tercile limits
*    m - east/west grid number
*    n - north/south grid number
*    t - input obs data
*    ulimit - upper tercile limits
*    upper - upper tercile limits
*    xmask - mask data
*    y - categorical observations
*--------------------------------------------------------------------
* AUTHOR(S): MIKE HALPERT
*            SCOTT HANDEL
*--------------------------------------------------------------------
* DATE: APRIL 24, 2012
*--------------------------------------------------------------------
* MODIFICATIONS
*********************************************************************
       dimension t(36,19),ulimit(36,19),blimit(36,19)
       real upper(36,19,12),lower(36,19,12)
       dimension y(36,19),xmask(36,19)
       character*4 mrec,parm,vp
       character*100 dir
c
       data y/684*-9999./
*********************************************************************
* GET COMMAND LINE ARGUMENTS
*********************************************************************
      call getarg (1,mrec)
      call getarg (2,parm)
      call getarg (3,vp)
      call getarg (4,dir)
*********************************************************************
* OPEN INPUT AND OUTPUT FILES
*********************************************************************
       open (11,file='../library/'//vp//parm//'_lowerlimit',
     x       access='direct',form='unformatted',recl=36*19*4,
     x       status='old')
       open (12,file='../library/'//vp//parm//'_upperlimit',
     x       access='direct',form='unformatted',recl=36*19*4,
     x       status='old')
       open (40,file='../library/mask_2x2.data',access='direct',
     x       form='unformatted',recl=36*19*4,status='old')
       open (50,file=dir(1:LEN_TRIM(dir))//'/'//parm//'_'//
     x       vp//'_archive.bi',access='direct',form='unformatted',
     x       recl=36*19*4,status='old')
       open (60,file=dir(1:LEN_TRIM(dir))//'/'//parm//'_'//
     x       vp//'_verif.bi',access='direct',form='unformatted',
     x       recl=36*19*4,status='unknown')
***********************************************************************
* READ MASK DATA
***********************************************************************
       read(40,rec=1)((xmask(i,j),i=1,36),j=1,19)
***********************************************************************
*    Convert character record to integer
***********************************************************************
      read(mrec,'(i4)') irec
***********************************************************************
*    read in upper and lower tercile limits
***********************************************************************    
        do 200 i=1,12
        read(11,rec=i)blimit
        read(12,rec=i)ulimit
        print*, 'reading tercile limits for season ',i
           do 300 j=1,36
           do 300 k=1,19
             upper(j,k,i)=ulimit(j,k)
             lower(j,k,i)=blimit(j,k)
300     continue
200     continue
        iunit=50
        krec=1
        imo=1
***********************************************************************
*   Change loop to indicate total number of records
***********************************************************************
        do 400 ii=1,irec
        read(iunit,rec=krec)t
***********************************************************************
*    compare observed temperature to tercile limits
***********************************************************************
        do 100 i=1,36
        do 100 j=1,19
        if(xmask(i,j).lt.0.0) go to 100
        if(t(i,j).le.lower(i,j,imo)) y(i,j)=-1.0
        if(t(i,j).ge.upper(i,j,imo)) y(i,j)=1.0
        if(t(i,j).gt.lower(i,j,imo).and.t(i,j).lt.upper(i,j,imo)) 
     x  y(i,j)=0.0
100     continue
        if(krec.eq.irec) write(60,rec=krec)y
        imo=imo+1
        if(imo.gt.12) imo=1
        krec=krec+1
          do 700 m=1,36
          do 700 n=1,19
            y(i,j)=-9999.
 700    continue
400     continue
        stop
        end
