!//////////////////////////////////////////////////////////////////////////////////
!//
!// main_14_1.f90
!//
!// Program to solve exercise 14.1 in the compendium
!//
!//////////////////////////////////////////////////////////////////////////////////

PROGRAM main_14_1
  USE flowdata
  IMPLICIT NONE
  !// Local valriables
  CHARACTER(LEN=10)               :: start_date
  CHARACTER(LEN=10)               :: end_date
  INTEGER                         :: start_index
  INTEGER                         :: end_index

  !// Number of command line arguments
  INTEGER                         :: argc
  !// Get the number of input arguments using
  !// the intrinsic function 
  !// COMMAND_ARGUMENT_COUNT()
  argc = COMMAND_ARGUMENT_COUNT()
  !// Test if the number of arguments are less
  !// than three
  IF(argc < 3) THEN
    !// Yes, print an error message and exit the
    !// program
    PRINT *, 'USAGE: main_14_1 datafile start_date end_date (dateformat = dd-mm-yyyy)'
    STOP
  END IF
  !// Call the GET_COMMAND_ARGUMENT for the 
  !// filename
  CALL GET_COMMAND_ARGUMENT(1,filename)
  
  !// Call the GET_COMMAND_ARGUMENT for the 
  !// start_date
  CALL GET_COMMAND_ARGUMENT(2,start_date)
  
  !// Call the GET_COMMAND_ARGUMENT for the 
  !// end_date
  CALL GET_COMMAND_ARGUMENT(3,end_date)

  !// Call read_data to read the input file
  CALL read_data()
  
  !// Call find_indexes to find the index in the water_flow
  !// array for the chosen period
  CALL find_indexes(start_date,end_date,start_index,end_index)
  
  !// Print the chosen part of the water_flow array to the screen
  PRINT *, water_flow(start_index:end_index)
  
END PROGRAM main_14_1