IDENTIFICATION DIVISION.
PROGRAM-ID. WEATHER.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT FILE-HANDLE ASSIGN TO FILE-NAME
        ORGANIZATION IS LINE SEQUENTIAL
        ACCESS MODE  IS SEQUENTIAL
        FILE STATUS  IS WS-FILE-STATUS.

DATA DIVISION.
FILE SECTION.
FD FILE-HANDLE.
01 FILE-LINE PIC X(200).

WORKING-STORAGE SECTION.
01 FILE-NAME      PIC X(255) VALUE "weather.csv".

01 WS-FILE-STATUS  PIC XX.
    88 END-OF-FILE VALUE "10".
    88 FILE-STATUS-GOOD VALUES ZERO, "10".
01 WS-DATE PIC X(23).

01 WEATHER-DATA-SIZE PIC 9(4) VALUE ZERO.

01 WEATHER-INFO OCCURS 500 TIMES INDEXED BY IDX.
    05 STATION-ID PIC 9(6).
    05 STATION-NAME PIC X(30).
    05 LATITUDE  PIC X(10).
    05 LONGITUDE PIC X(10).
    05 ELEVATION PIC X(10).
    05 TEMPERATURE PIC X(4).
    05 QUALITY PIC XXX.
        88 QUALITY-OK VALUE 'G'.
        88 QUALITY-AVER VALUE 'Y'.
        88 QUALITY-ERR VALUE ZERO.
    05 EXTRA-DATA PIC X(8).
    05 COMMENT PIC X(50).

01 WEATHER-DATA-TIMESTAMP PIC X(50).
01 WEATHER-DATA-BUFFER PIC X(50). *> for unneeded crap

01  SC-RESPONSE   PIC X VALUE "N".
    88  RESPONSE-NEXT   VALUES "N", "n", ".".
    88  RESPONSE-PREV   VALUES "P", "p",", ","b".
    88  RESPONSE-QUIT   VALUES "Q", "q".

01 SC-TEMP-CONTROL PIC X(30) VALUE "HIGHLIGHT".

SCREEN SECTION.
01  DATA-VIEW-SCREEN.
    05  HEADER-SECTION.
    10  VALUE "WEATHER INFO SCREEN" UNDERLINE BLANK SCREEN    LINE 1 COL 23.

    10  VALUE "DATE "                               LINE 3 COL 37.
    10  SC-STATION-ID                               LINE 3 COL 42
              PIC X(20)  HIGHLIGHT FROM WEATHER-DATA-TIMESTAMP.

    10  VALUE "STATION ID #"                        LINE 3 COL 10.
    10  SC-STATION-ID                               LINE 3 COL 25
              PIC  X(6)     FROM STATION-ID(IDX).

    10  VALUE "STATION NAME"                        LINE 5 COL 10.
    10  SC-STATION-NAME                             LINE 5 COL 25
            PIC X(30)  BACKGROUND-COLOR IS 2
                       FOREGROUND-COLOR IS 0
               FROM STATION-NAME(IDX).

    10  VALUE "GEO LOCATION"                        LINE 7 COL 10.
    10  VALUE "LATITUDE"                            LINE 8 COL 12.
    10  SC-LATITUDE                                 LINE 8 COL 22
            PIC X(10)     FROM LATITUDE(IDX).
    10  VALUE "LONGITUDE"                           LINE 9 COL 12.
    10  SC-LONGITUDE                                LINE 9 COL 22
            PIC X(10)     FROM LONGITUDE(IDX).
    10  VALUE "ELEVATION"                           LINE 10 COL 12.  
    10  SC-ELEVATION                                LINE 10 COL 22
            PIC X(10)     FROM ELEVATION(IDX).

    05  TEMPERATURE1-SECTION.  
    10  VALUE "TEMPERATURE"                         LINE 13 COL 10.

    10  SC-TEMPERATURE                              LINE 13 COL 25 
            PIC X(4)   HIGHLIGHT
                      FROM TEMPERATURE(IDX).

    05  TEMPERATURE2-SECTION.     
    10  VALUE "TEMPERATURE"                         LINE 13 COL 10.
    10  SC-TEMPERATURE                              LINE 13 COL 25
            PIC X(4)   BACKGROUND-COLOR IS 1
                        FOREGROUND-COLOR IS 0
                      FROM TEMPERATURE(IDX).
 
    05  DATA-QUALITY1-SECTION. 
    10  VALUE "DATA QUALITY"                        LINE 15 COL 10.  
    10  SC-QUALITY                                  LINE 15 COL 25
            PIC X(20)     FROM QUALITY(IDX).
    10  SC-QUALITY1-DESC                             LINE 15 COL 27
            PIC X(45).

    05  DATA-QUALITY2-SECTION. 
    10  VALUE "DATA QUALITY"                        LINE 15 COL 10.  
    10  SC-QUALITY                                  LINE 15 COL 25
            PIC X(20)     FROM QUALITY(IDX).
    10  SC-QUALITY2-DESC                             LINE 15 COL 27
            PIC X(45)    BACKGROUND-COLOR IS 8
                         FOREGROUND-COLOR IS 0.

    05  DATA-QUALITY3-SECTION. 
    10  VALUE "DATA QUALITY"                        LINE 15 COL 10.  
    10  SC-QUALITY                                  LINE 15 COL 25
            PIC X(20)     FROM QUALITY(IDX).
    10  SC-QUALITY3-DESC                             LINE 15 COL 27
            PIC X(45)     BACKGROUND-COLOR IS 4
                          FOREGROUND-COLOR IS 0.

    05  INPUT-SECTION.        
    10  VALUE "N - NEXT STATION"                    LINE 18 COL 30.
    10  VALUE "P - PREV STATION"                    LINE 19 COL 30.
    10  VALUE "Q - TO QUIT"                         LINE 20 COL 30.
    10  VALUE "ENTER RESPONSE"                      LINE 21 COL 30.
    10  RESPONSE-INPUT                              LINE 21 COL 45
                    PIC X    AUTO    TO SC-RESPONSE.

PROCEDURE DIVISION.

MAIN.
    PERFORM UPDATE-DATA
    PERFORM OPEN-FILE
    PERFORM LOAD-DATA
    PERFORM DISPLAY-SCREEN
    PERFORM FINISH
.

UPDATE-DATA.
    ACCEPT WS-DATE FROM DATE
    CALL 'SYSTEM' USING "./weather.sh"
.

OPEN-FILE.
    OPEN INPUT FILE-HANDLE
    IF NOT FILE-STATUS-GOOD THEN
        DISPLAY "File error!"
        PERFORM FINISH
    END-IF
.

LOAD-DATA.
    *> IGNORE 4 LINES OF HEADER
    READ FILE-HANDLE
    READ FILE-HANDLE
    READ FILE-HANDLE

    READ FILE-HANDLE
    PERFORM LINE-UNSTRING-HEADER *> capture timestamp from header
    SET IDX TO 1
    PERFORM UNTIL END-OF-FILE 
        READ FILE-HANDLE
        ADD 1 TO WEATHER-DATA-SIZE
        PERFORM LINE-UNSTRING
    END-PERFORM
    CLOSE FILE-HANDLE
.

LINE-UNSTRING.
    UNSTRING FILE-LINE DELIMITED BY ";" INTO
        STATION-ID(IDX),
        STATION-NAME(IDX),
        LATITUDE(IDX),
        LONGITUDE(IDX),
        ELEVATION(IDX),
        TEMPERATURE(IDX),
        QUALITY(IDX),
        EXTRA-DATA(IDX),
        COMMENT(IDX)      
    END-UNSTRING
 SET IDX UP BY 1
.

LINE-UNSTRING-HEADER.
    UNSTRING FILE-LINE DELIMITED BY ";" INTO
        WEATHER-DATA-BUFFER,
        WEATHER-DATA-BUFFER,
        WEATHER-DATA-BUFFER,
        WEATHER-DATA-BUFFER,
        WEATHER-DATA-BUFFER,
        WEATHER-DATA-TIMESTAMP,
        WEATHER-DATA-BUFFER,
        WEATHER-DATA-BUFFER,
        WEATHER-DATA-BUFFER      
    END-UNSTRING
.

SHOW-ALL-RECORDS.
    PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > WEATHER-DATA-SIZE
        DISPLAY WEATHER-INFO(IDX)
    END-PERFORM
.

DISPLAY-SCREEN.
    SET IDX TO 1
    PERFORM SCREEN-LOOP
	  UNTIL RESPONSE-QUIT
.

SCREEN-LOOP.

    PERFORM PREPATE-SCREEN-DATA
    ACCEPT INPUT-SECTION

    IF RESPONSE-NEXT AND IDX < WEATHER-DATA-SIZE THEN
        SET IDX UP BY 1
    END-IF
    IF RESPONSE-PREV AND IDX > 1 THEN
        SET IDX DOWN BY 1
    END-IF
.

PREPATE-SCREEN-DATA.

    DISPLAY HEADER-SECTION

    IF TEMPERATURE(IDX) IS LESS THAN ZERO THEN
        DISPLAY TEMPERATURE2-SECTION
    ELSE
        DISPLAY TEMPERATURE1-SECTION
    END-IF

    IF QUALITY-OK(IDX) THEN
        MOVE "Weather station is working OK" TO SC-QUALITY1-DESC
        DISPLAY DATA-QUALITY1-SECTION
    ELSE IF QUALITY-AVER(IDX) THEN
        MOVE "Some sensor data missing; Data aggregated" TO SC-QUALITY2-DESC
        DISPLAY DATA-QUALITY2-SECTION
    ELSE
        MOVE "Station is damaged; Data unreliable" TO SC-QUALITY3-DESC
        DISPLAY DATA-QUALITY3-SECTION
    END-IF

    DISPLAY INPUT-SECTION
.

FINISH.
    STOP RUN
.
