***********************************************************************
* PROGRAM: WRITE_2X2_ASCII_OBS_OUTPUT
* LANGUAGE: FORTRAN 77
*---------------------------------------------------------------------
* PURPOSE: Generates 2x2 gridded ascii output for observations
*---------------------------------------------------------------------
* USAGE: ./write_2x2_ascii_output.Linux.exe ${1} ${2} ${3} ${4} ${5}
*     where ${1} - full path of input observation file
*           ${2} - full path of output file
*           ${3} - observation center month date [YYMMDD]
*           ${4} - number of months in valid period [0003|0001]
*           ${5} - record number of input binary observation archive file
*----------------------------------------------------------------*
* INPUT FILES:  
*    OBS_FILE - binary file containing 2 degree x 2 degree categorical obs
* OUTPUT FILES:  
*    OUTPUT_FILE - ascii file containing 2 degree x 2 degree categorical obs
*----------------------------------------------------------------*
* SUBROUTINES USED:      
*    NONE                       
* FUNCTIONS USED:      
*    NONE                 
*----------------------------------------------------------------*
* INPUT VARIABLES:   
*    DATE - observation center month date [YYMMDD]
*    IRECNO - record number of input binary observation archive file
*    OBS_FILE - full path of input observation file
*    OUTPUT_FILE - full path of output file
*    PERIOD - number of months in valid period [0003|0001]
* LOCAL VARIABLES: 
*    ABOVE - Categorical value for above normal|median observations
*    ABOVE_DATA - Matrix containing above normal probabilities
*    BELOW - Categorical value for below normal|median observations
*    BELOW_DATA - Matrix containing below normal probabilities
*    CAT_DATA - Matrix containing forecast categories
*    I - Counter for latitude
*    II - Counter for latitude
*    J - Counter for longitude
*    JJ - Counter for longitude
*    LAT - latitude
*    LON - longitude
*    NEAR_DATA - Matrix containing near normal probabilities
*    NORMAL - Categorical value for near normal|median observations
*    OBS_DATA - Matrix containing input categorical observation data
*    RECNO - record number of input binary observation archive file
*    XMAX2 - Maximum number of east-west grid points
*    YMAX2 - Maximum number of north-south grid points
*----------------------------------------------------------------*
* AUTHOR(S): SCOTT HANDEL
*----------------------------------------------------------------*
* DATE: APRIL 24, 2012
*----------------------------------------------------------------*
* MODIFICATIONS:
******************************************************************
      PROGRAM WRITE_2X2_ASCII_OBS_OUTPUT
      IMPLICIT NONE
      CHARACTER*2 II,JJ
      CHARACTER*4 PERIOD,IRECNO
      CHARACTER*6 DATE
      CHARACTER*300 OBS_FILE,OUTPUT_FILE
      INTEGER XMAX2,YMAX2,I,J,LAT,LON,RECNO
      REAL BELOW,NORMAL,ABOVE
      PARAMETER(YMAX2=36)
      PARAMETER(XMAX2=19)
      REAL OBS_DATA(1:YMAX2,1:XMAX2)
      REAL BELOW_DATA(1:YMAX2,1:XMAX2),ABOVE_DATA(1:YMAX2,1:XMAX2)
      REAL CAT_DATA(1:YMAX2,1:XMAX2),NEAR_DATA(1:YMAX2,1:XMAX2)
************************************************************************
* GET INPUT ARGUMENTS
************************************************************************
      CALL GETARG(1,OBS_FILE)
      CALL GETARG(2,OUTPUT_FILE)
      CALL GETARG(3,DATE)
      CALL GETARG(4,PERIOD)
      CALL GETARG(5,IRECNO)
***********************************************************************
* READ RECORD NUMBER
***********************************************************************
      READ(IRECNO,'(I4)') RECNO
***********************************************************************
* INITIALIZE OUTPUT ARRAYS
***********************************************************************
      DO I=1,19
         DO J=1,36
            BELOW_DATA(J,I)=-999.0
            NEAR_DATA(J,I)=-999.0
            ABOVE_DATA(J,I)=-999.0
            CAT_DATA(J,I)=-999.0
         ENDDO
      ENDDO
***********************************************************************
* DETERMINE CATEGORY 
***********************************************************************
      BELOW=-1.0
      NORMAL=0.0
      ABOVE=1.0
************************************************************************
* OPEN INPUT BINARY FILES
************************************************************************
      OPEN(13,file=OBS_FILE,access='direct',form='unformatted',
     $     recl=36*19*4,status='unknown')
************************************************************************
* OPEN OUTPUT ASCII FILE
************************************************************************
      OPEN(17,file=OUTPUT_FILE)
************************************************************************
* READ OBS DATA
************************************************************************
      READ(13,rec=RECNO) OBS_DATA
************************************************************************
* CLOSE INPUT BINARY FILES
************************************************************************
      CLOSE(13)
************************************************************************
* WRITE FILE HEADER
************************************************************************
      WRITE(17,*) 'PARM=LAT;LON;PRBB;PRBN;PRBA;CAT'
      WRITE(17,30) 'STN','YYMMDD/HHMM','LAT','LON','PRBB','PRBN',
     $             'PRBA','CAT'
30    FORMAT(2X,A3,2X,A11,6(5X,A4))
************************************************************************
* WRITE OUTPUT ASCII DATA
************************************************************************
      DO I=1,19
         LAT=(I*2)+18
         IF (I .LT. 10) THEN
             WRITE(II,'(A1,I1)') '0',I
         ELSE
             WRITE(II,'(I2)') I
         ENDIF
         DO J=1,36
            LON=132-(J*2)
            IF (J .LT. 10) THEN
                WRITE(JJ,'(A1,I1)') '0',J
            ELSE
                WRITE(JJ,'(I2)') J
            ENDIF
            IF (OBS_DATA(J,I) .EQ. BELOW) THEN
               BELOW_DATA(J,I)=100.00
               NEAR_DATA(J,I)=0.00
               ABOVE_DATA(J,I)=0.00 
               CAT_DATA(J,I)=1.0
            ELSEIF (OBS_DATA(J,I) .EQ. NORMAL) THEN
               BELOW_DATA(J,I)=0.00
               NEAR_DATA(J,I)=100.00
               ABOVE_DATA(J,I)=0.00
               CAT_DATA(J,I)=2.0
            ELSEIF (OBS_DATA(J,I) .EQ. ABOVE) THEN
               BELOW_DATA(J,I)=0.00
               NEAR_DATA(J,I)=0.00
               ABOVE_DATA(J,I)=100.00
               CAT_DATA(J,I)=3.0
            ELSE
               BELOW_DATA(J,I)=-999.0
               NEAR_DATA(J,I)=-999.0
               ABOVE_DATA(J,I)=-999.0
               CAT_DATA(J,I)=-999.0
            ENDIF
            IF (BELOW_DATA(J,I) .GE. 0.0 .OR. NEAR_DATA(J,I) .GE. 
     $      0.0 .OR. ABOVE_DATA(J,I) .GE. 0.0) THEN
               WRITE(17,20) JJ,II,DATE,PERIOD,LAT,LON,BELOW_DATA(J,I),
     $                     NEAR_DATA(J,I),ABOVE_DATA(J,I),CAT_DATA(J,I)
20             FORMAT(1X,A2,A2,2X,A6,'/',A4,I9,I9,4(F9.2))
            ENDIF
         ENDDO
      ENDDO
************************************************************************
* CLOSE OUTPUT ASCII FILE
************************************************************************
      CLOSE(17)
 
      END
