"""
This standalone script generates time series at nominated "gauges".
Input: sww file in the results directory
       gauges as listed in  model_ini.gauges_filename
Output: csv files stage,speed,depth,elevation over time (sec)
        png plots of selected quantities from the csv files
Stored in the 'results/timeseries' folder for the respective .sww file


"""

from os import sep, rename, listdir, system
from os.path import join
from os import remove, rename
import sys
import anuga
import model_ini


verbose = model_ini.model_verbose
#######################################################################3######
# This script assumes the target sww(s) is/are in the results directory      #
# and timeseries csvs and plots will be written to the results/timeseries    #
# directory
# current directory is the scripts directory                                 #
##############################################################################
sww1_directory   = model_ini.results_dir+sep
sww1_pathname    = join(model_ini.results_dir,model_ini.domain_name ) +'.sww'
gauges_pathname  = model_ini.gauges_pathname

# A second sww is optional and can provide results for comparative plots
# this second sww is the first segment (t=0 to 16,980 secs of the four part 
# sww from the original GA 2009 study.  
sww2_directory =  '..'+sep+'results_GA2009'+sep         # use '' if not required
sww2_pathname  =  sww2_directory+'hobart_time_0_0.sww'  # use '' if not required

##########################USING csv2timeseries() ###########################
#    Inputs: 
#        NOTE: if using csv2timeseries_graphs after creating csv file,
#        it is essential to export quantities 'depth' and 'elevation'.
#        'depth' is good to analyse gauges on land and elevation is used
#        automatically by csv2timeseries_graphs in the legend.
#        
#        sww_file: path to any sww file
#        
#        gauge_file: Assumes that it follows this format
#            name, easting, northing, elevation
#            point1, 100.3, 50.2, 10.0
#            point2, 10.3, 70.3, 78.0
#        
#        NOTE: order of column can change but names eg 'easting', 'elevation' 
#        must be the same! ALL lowercaps!
#
#        out_name: prefix for output file name (default is 'gauge_')
#        
#    Outputs: 
#        one file for each gauge/point location in the points file. They
#        will be named with this format in the same directory as the 'sww_file'
#            <out_name><name>.csv
#        eg gauge_point1.csv if <out_name> not supplied
#           myfile_2_point1.csv if <out_name> ='myfile_2_'
#           
#        They will all have a header
#   
#    Usage: sww2csv_gauges(sww_file='test1.sww',
#                          quantities = ['stage', 'elevation','depth','bearing'],
#                          gauge_file='gauge.txt')    
#    
#    Interpolate the quantities at a given set of locations, given
#    an sww file.
#    The results are written to a csv file.
#
##############################################################################


sww_pathname = sww1_pathname   # create csvs for the first sww and gauges
print model_ini.prefix +' >>>>> creating csvs from the specified sww ', sww_pathname
csv_outname  = model_ini.results_dir+sep+'timeseries'+sep+'gauge_'
anuga.sww2csv_gauges(sww_file   = sww_pathname,
               gauge_file       = gauges_pathname,
               out_name         = csv_outname ,
               quantities       = ['stage','speed','depth','elevation','bearing'],
               verbose          = True)
			  
if sww2_pathname != '' :       # A second comparative sww is available with corresponding gauges
    sww_pathname = sww2_pathname
    csv_outname  = sww2_directory+'timeseries'+sep+'gauge_'
    print model_ini.prefix + ' >>>>> a second sww dataset is to be used named ', sww_pathname

    anuga.sww2csv_gauges(sww_file   = sww_pathname,
                   gauge_file       = gauges_pathname,
                   out_name         = csv_outname ,
                   quantities       = ['stage','speed','depth','elevation','bearing'],
                   verbose          = True)
  				   
print model_ini.prefix + ' >>>>> Requested sww(s) exported to csvs '



	
################# USING csv2timeseries_graphs() ######################
#    Read in csv files that have the right header information and
#    plot time series such as Stage, Speed, etc. Will also plot several
#    time series on one plot. Filenames must follow this convention,
#    <base_name><plot_number>.csv eg gauge_timeseries3.csv
#    
#    NOTE: relies that 'elevation' is in the csv file!
#
#    Each file represents a location and within each file there are
#    time, quantity columns.
#    
#   For example:    
#    if "directories_dic" defines 4 directories and in each directories
#    there is a csv files corresponding to the right "plot_numbers", 
#    this will create a plot with 4 lines one for each directory AND 
#    one plot for each "quantities".  ??? FIXME: unclear.
#    
#    Usage:
#       csv2timeseries_graphs(directories_dic={'slide'+sep:['Slide',0, 0],
#                                       'fixed_wave'+sep:['Fixed Wave',0,0]},
#                            output_dir='fixed_wave'+sep,
#                            base_name='gauge_timeseries_',
#                            plot_numbers='',
#                            quantities=['stage','speed'],
#                            extra_plot_name='',
#                            assess_all_csv_files=True,                            
#                            create_latex=False,
#                            verbose=True)
#            this will create one plot for stage with both 'slide' and 
#            'fixed_wave' lines on it for stage and speed for each csv
#            file with 'gauge_timeseries_' as the prefix. The graghs 
#            will be in the output directory 'fixed_wave' and the graph
#            axis will be determined by assessing all the 
#########################################################################


dir1 = model_ini.results_dir+sep+'timeseries'+sep        # directory containing the MRT2009 timeseries csv files
print model_ini.prefix + ' >>>>> creating plots  from csv files in directory ', dir1
if sww2_pathname != '' :                                 # A second comparative sww is available 
    dir2 = sww2_directory+'timeseries'+sep               # directory containing the GA2009  timeseries csv files
    print model_ini.prefix + ' >>>>> also adding plots  from csv files in directory ', dir2
    directoriesdic = {dir1:['MRT2009',0, 0], dir2:['GA2009',0,0]}
else   :
    directoriesdic = {dir1:['MRT2009',0, 0]}
	
anuga.csv2timeseries_graphs(directories_dic = directoriesdic,
                                 output_dir = dir1,
                                  base_name = 'gauge_',
                               plot_numbers = '',
                                 quantities = ['stage','speed'],
                            extra_plot_name = '',
                       assess_all_csv_files = True,                            
                               create_latex = False,
                                    verbose = True)
print model_ini.prefix + ' >>>>> All plots  completed and available in ',dir1
