""" 
Used with clawpack-5.2.2

Set up the plot figures, axes, and items to be done for each frame.

This module is imported by the plotting routines and then the
function setplot is called to set the plot parameters.

Gauge plot data are in the GeoClaw output file:  _output/fort.gauge
which has 7 columns:   gaugeno level t h hu hv eta

In setrun.py, set rundata.fgmax_data.num_fgmax_val = 5  # To save h hu hv eta
""" 

import pylab
import glob, os
from numpy import loadtxt
from matplotlib import image


# Specific to WA coast project:
try:
    WAcoast = os.environ['WAcoast']
except:
    raise Exception("Need to set WAcoast environment variable")

# Google Earth image for plotting on top of...
# need to change for specific location on coast

# The following dictionaries duplicate those in plot_fgmax.py

title_name = {}
FG_name = {}
fgmax_input_file = {}
FGfigno = {}
GEmap = {}
GEextent = {}

title_name[1] = "NW Region 1 "
FG_name[1] = "NW_Region_1"
fgmax_input_file[1] = "fgmax1.txt"
FGfigno[1] = 201
GEmap[1] = image.imread(WAcoast + '/maps/NW_Region_FGmax1.png')
GEextent[1] = (-124.792953, -124.367415, 48.270112, 48.44565)  # NW Region FGmax1

title_name[2] = "NW Region 2 "
FG_name[2] = "NW_Region_2"
fgmax_input_file[2] = "fgmax2.txt"
FGfigno[2] = 202
GEmap[2] = image.imread(WAcoast + '/maps/NW_Region_FGmax2.png')
GEextent[2] = (-124.792953, -124.617670, 48.160762, 48.399016) # NW Region FGmax2

title_name[3] = "NW Region 3 "
FG_name[3] = "NW_Region_3"
fgmax_input_file[3] = "fgmax3.txt"
FGfigno[3] = 203
GEmap[3] = image.imread(WAcoast + '/maps/NW_Region_FGmax3.png')
GEextent[3] = (-124.787063, -124.555294, 47.874455, 48.182077) # NW Region FGmax3

title_name[4] = "NW Region 4 "
FG_name[4] = "NW_Region_4"
fgmax_input_file[4] = "fgmax4.txt"
FGfigno[4] = 204
GEmap[4] = image.imread(WAcoast + '/maps/NW_Region_FGmax4.png')
GEextent[4] = (-124.671642, -124.322678, 47.549813, 47.905631) #  NW Region FGmax4

plot_zeta_map = True  # set to false if no image available

# --------------------------
def setplot(plotdata):
# --------------------------
    
    """ 
    Specify what is to be plotted at each frame.
    Input:  plotdata, an instance of clawpack.visclaw.data.ClawPlotData.
    Output: a modified version of plotdata.
    
    """ 

    from clawpack.visclaw import colormaps, geoplot

    plotdata.clearfigures()  # clear any old figures,axes,items dat
    plotdata.format = 'binary'

    clim_ocean = 8.0
    clim_coast = 8.0
    
    sealevel = 0.  # Level of tide in run relative to MHW
    cmax_ocean = clim_ocean + sealevel
    cmin_ocean = -clim_ocean + sealevel
    cmax_coast = clim_coast + sealevel
    cmin_coast = -clim_coast + sealevel
    
    
    # To plot gauge locations on pcolor or contour plot, use this as
    # an afteraxis function:
    
    def addgauges(current_data):
        from clawpack.visclaw import gaugetools
        gaugetools.plot_gauge_locations(current_data.plotdata, \
             gaugenos=[0,1,2,3,4,5,6,7,8,9,10,11,12], format_string='ko', add_labels=True)
    
    def timeformat(t):
        from numpy import mod, sign
        signt = sign(t)
        t = abs(t)
        hours = int(t/3600.) # seconds to integer number of hours
        tmin = mod(t,3600.)  # seconds of remaining time beyond integer number of hours
        min = int(tmin/60.)  # seconds to integer number of minutes
        sec = int(mod(tmin,60.)) # remaining integer sec
        tenth_sec = int(10*(t - int(t)))
        timestr = '%s:%s:%s.%s' % (hours,str(min).zfill(2),str(sec).zfill(2),str(tenth_sec).zfill(1))
        if signt < 0:
            timestr = '-' + timestr
        return timestr
        
    def title_hours(current_data):
        from pylab import title
        t = current_data.t
        timestr = timeformat(t)
        title('%s after earthquake' % timestr)
    
    # -----------------------------------------
    # Figure for Pacific
    #-----------------------------------------
    plotfigure = plotdata.new_plotfigure(name='Pacific', figno=0)
    plotfigure.kwargs = {'figsize': (8,10)}
    # plotfigure.show = True
    
    # Set up for axes in this figure:
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.title = 'Pacific'
    plotaxes.scaled = False
    plotaxes.xlimits = [-127.25, -123.25] 
    plotaxes.ylimits = [  45,      49]
    
    def aa(current_data):
        from pylab import ticklabel_format, xticks, gca, cos, pi, savefig
        title_hours(current_data)
        ticklabel_format(format='plain',useOffset=False)
        xticks(rotation=20)
        a = gca()
        a.set_aspect(1./cos(48*pi/180.))
    plotaxes.afteraxes = aa
    
    # Water
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.surface_or_depth
    my_cmap = colormaps.make_colormap({-1.0: [0.0,0.0,1.0], \
                                     -0.5: [0.5,0.5,1.0], \
                                      0.0: [1.0,1.0,1.0], \
                                      0.5: [1.0,0.5,0.5], \
                                      1.0: [1.0,0.0,0.0]})
    plotitem.imshow_cmap = my_cmap
    # plotitem.imshow_cmin = cmin_ocean
    # plotitem.imshow_cmax = cmax_ocean
    plotitem.imshow_cmin = -15.
    plotitem.imshow_cmax = 15.
    plotitem.add_colorbar = True
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]
    
    # Land
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.land
    plotitem.imshow_cmap = geoplot.land_colors
    plotitem.imshow_cmin = 0.0
    plotitem.imshow_cmax = 100.0
    plotitem.add_colorbar = False
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]
    #plotaxes.afteraxes = addgauges
    
    # Add contour lines of bathymetry:
    plotitem = plotaxes.new_plotitem(plot_type='2d_contour')
    plotitem.show = False
    plotitem.plot_var = geoplot.topo
    from numpy import arange, linspace
    plotitem.contour_levels = linspace(-6000,0,7)
    plotitem.amr_contour_colors = ['g']  # color on each level
    plotitem.kwargs = {'linestyles':'solid'}
    plotitem.amr_contour_show = [0,0,1,0]  # show contours only on finest level
    plotitem.celledges_show = 0
    plotitem.patchedges_show = 0
    
    # Add contour lines of topography:
    plotitem = plotaxes.new_plotitem(plot_type='2d_contour')
    plotitem.show = False
    plotitem.plot_var = geoplot.topo
    from numpy import arange, linspace
    plotitem.contour_levels = arange(0., 11., 1.)
    plotitem.amr_contour_colors = ['g']  # color on each level
    plotitem.kwargs = {'linestyles':'solid'}
    plotitem.amr_contour_show = [0,0,0,1]  # show contours only on finest level
    plotitem.celledges_show = 0
    plotitem.patchedges_show = 0
    
    #-----------------------------------------
    # Figure for NW Region (La Push DEM: [-125.05 -124.35 47.55 48.45]
    #-----------------------------------------
    plotfigure = plotdata.new_plotfigure(name="NW Coast", figno=11)
    plotfigure.show = True
    plotfigure.kwargs = {'figsize': (10,9)}
    
    # Set up for axes in this figure:
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.scaled = False
    plotaxes.xlimits = [-125.05,-124.35]
    plotaxes.ylimits = [47.55,48.45]
    plotaxes.afteraxes = aa
    
    # Water
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.surface_or_depth
    plotitem.imshow_cmap = my_cmap
    # plotitem.imshow_cmin = cmin_coast
    # plotitem.imshow_cmax = cmax_coast
    plotitem.imshow_cmin = -15.
    plotitem.imshow_cmax = 15.
    plotitem.add_colorbar = True
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]
    
    # Land
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.land
    plotitem.imshow_cmap = geoplot.land_colors
    plotitem.imshow_cmin = 0.0
    plotitem.imshow_cmax = 100.0
    plotitem.add_colorbar = False
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]
    
    #-----------------------------------------
    # Figure for Region 1
    #-----------------------------------------
    plotfigure = plotdata.new_plotfigure(name="Region_1", figno=12)
    plotfigure.show = True
    plotfigure.kwargs = {'figsize': (10,9)}

    # Set up for axes in this figure:
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.scaled = False
    plotaxes.xlimits = [-124.792953, -124.367415]
    plotaxes.ylimits = [  48.270112,   48.44565]
    plotaxes.afteraxes = aa    

    # Water
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.surface_or_depth
    plotitem.imshow_cmap = my_cmap
    # plotitem.imshow_cmin = cmin_coast
    # plotitem.imshow_cmax = cmax_coast
    plotitem.imshow_cmin = -30.
    plotitem.imshow_cmax = 30.
    plotitem.add_colorbar = True
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]

    # Land
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.land
    plotitem.imshow_cmap = geoplot.land_colors
    plotitem.imshow_cmin = 0.0
    plotitem.imshow_cmax = 100.0
    plotitem.add_colorbar = False
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]

    #-----------------------------------------
    # Figure for Region 2
    #-----------------------------------------
    plotfigure = plotdata.new_plotfigure(name="Region_2", figno=13)
    plotfigure.show = True
    plotfigure.kwargs = {'figsize': (10,9)}

    # Set up for axes in this figure:
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.scaled = False
    plotaxes.xlimits = [-124.792953, -124.617670]
    plotaxes.ylimits = [  48.160762,   48.399016]
    plotaxes.afteraxes = aa

    # Water
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.surface_or_depth
    plotitem.imshow_cmap = my_cmap
    # plotitem.imshow_cmin = cmin_coast
    # plotitem.imshow_cmax = cmax_coast
    plotitem.imshow_cmin = -30.
    plotitem.imshow_cmax = 30.
    plotitem.add_colorbar = True
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]

    # Land
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.land
    plotitem.imshow_cmap = geoplot.land_colors
    plotitem.imshow_cmin = 0.0
    plotitem.imshow_cmax = 100.0
    plotitem.add_colorbar = False
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]

    #-----------------------------------------
    # Figure for Region 3
    #-----------------------------------------
    plotfigure = plotdata.new_plotfigure(name="Region_3", figno=14)
    plotfigure.show = True
    plotfigure.kwargs = {'figsize': (10,9)}

    # Set up for axes in this figure:
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.scaled = False
    plotaxes.xlimits = [-124.787063, -124.555294]
    plotaxes.ylimits = [  47.874455,   48.182077]
    plotaxes.afteraxes = aa

    # Water
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.surface_or_depth
    plotitem.imshow_cmap = my_cmap
    # plotitem.imshow_cmin = cmin_coast
    # plotitem.imshow_cmax = cmax_coast
    plotitem.imshow_cmin = -30.
    plotitem.imshow_cmax = 30.
    plotitem.add_colorbar = True
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]

    # Land
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.land
    plotitem.imshow_cmap = geoplot.land_colors
    plotitem.imshow_cmin = 0.0
    plotitem.imshow_cmax = 100.0
    plotitem.add_colorbar = False
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]

    #-----------------------------------------
    # Figure for Region 4
    #-----------------------------------------
    plotfigure = plotdata.new_plotfigure(name="Region_4", figno=15)
    plotfigure.show = True
    plotfigure.kwargs = {'figsize': (10,9)}

    # Set up for axes in this figure:
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.scaled = False
    plotaxes.xlimits = [-124.671642, -124.322678]
    plotaxes.ylimits = [  47.549813,   47.905631]
    plotaxes.afteraxes = aa

    # Water
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.surface_or_depth
    plotitem.imshow_cmap = my_cmap
    # plotitem.imshow_cmin = cmin_coast
    # plotitem.imshow_cmax = cmax_coast
    plotitem.imshow_cmin = -30.
    plotitem.imshow_cmax = 30.
    plotitem.add_colorbar = True
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]

    # Land
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.land
    plotitem.imshow_cmap = geoplot.land_colors
    plotitem.imshow_cmin = 0.0
    plotitem.imshow_cmax = 100.0
    plotitem.add_colorbar = False
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.amr_patchedges_show = [0]
    
    #-----------------------------------------
    # Figures for gauges
    #-----------------------------------------
    
    plotfigure = plotdata.new_plotfigure(name='Flood Depth',figno=300,type='each_gauge')
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.title = 'h, Flood Depth'
    plotitem = plotaxes.new_plotitem(plot_type='1d_plot')
    def variables(current_data):
        from numpy import where
        from numpy import sqrt
        q = current_data.q
        h = q[0,:]
        hu = q[1,:]
        hv = q[2,:]
        return h
    plotitem.plot_var = variables
    plotitem.plotstyle = 'b-'
        
    plotfigure = plotdata.new_plotfigure(name='Speed',figno=301,type='each_gauge')
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.title = 's, Current Speed'
    plotitem = plotaxes.new_plotitem(plot_type='1d_plot')
    def variables(current_data):
        from numpy import where
        from numpy import sqrt
        q = current_data.q
        h = q[0,:]
        hu = q[1,:]
        hv = q[2,:]
        ss = where(h>0, (hu**2 + hv**2)/h**2, 0.)
        s = sqrt(ss)
        return s
    plotitem.plot_var = variables
    plotitem.plotstyle = 'b-'

    plotfigure = plotdata.new_plotfigure(name='Momentum Flux',figno=302,type='each_gauge')
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.title = 'hss, Momentum Flux'
    plotitem = plotaxes.new_plotitem(plot_type='1d_plot')
    def variables(current_data):
        from numpy import where, sqrt
        q = current_data.q
        h = q[0,:]
        hu = q[1,:]
        hv = q[2,:]
        hss = where(h>0, (hu**2 + hv**2)/h, 0.)
        return hss
    plotitem.plot_var = variables
    plotitem.plotstyle = 'b-'

    #-----------------------------------------
    # Figures for gauge positions on GE map
    #-----------------------------------------
    otherfigure = plotdata.new_otherfigure(name='Gauge Positions - NW_Region_1',fname='NW_Region_FGmax1.png')
    otherfigure = plotdata.new_otherfigure(name='Gauge Positions - NW_Region_2',fname='NW_Region_FGmax2.png')
    otherfigure = plotdata.new_otherfigure(name='Gauge Positions - NW_Region_3',fname='NW_Region_FGmax3.png')
    otherfigure = plotdata.new_otherfigure(name='Gauge Positions - NW_Region_4',fname='NW_Region_FGmax4.png')
    
    #-----------------------------------------
    # Figures for fgmax - max values on fixed grids
    #-----------------------------------------
    
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 arrival time',fname='NW_Region_1_arrival_times.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 arrival time of max zeta',fname='NW_Region_1_zetatimes.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 zeta max on map',fname='NW_Region_1_zeta_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 speed max on map',fname='NW_Region_1_speed_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 hs max on map',fname='NW_Region_1_hs_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region f hss max on map',fname='NW_Region_1_hss_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 h min on map',fname='NW_Region_1_min_depth_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 zeta max',fname='NW_Region_1_zeta.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 speed max',fname='NW_Region_1_speed.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 hs max',fname='NW_Region_1_hs.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 hss max',fname='NW_Region_1_hss.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 1 h min',fname='NW_Region_1_min_depth.png')

    otherfigure = plotdata.new_otherfigure(name='NW Region 2 arrival time',fname='NW_Region_2_arrival_times.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 arrival time of max zeta',fname='NW_Region_2_zetatimes.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 zeta max on map',fname='NW_Region_2_zeta_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 speed max on map',fname='NW_Region_2_speed_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 hs max on map',fname='NW_Region_2_hs_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 hss max on map',fname='NW_Region_2_hss_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 h min on map',fname='NW_Region_2_min_depth_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 zeta max',fname='NW_Region_2_zeta.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 speed max',fname='NW_Region_2_speed.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 hs max',fname='NW_Region_2_hs.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 hss max',fname='NW_Region_2_hss.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 2 h min',fname='NW_Region_2_min_depth.png')

    otherfigure = plotdata.new_otherfigure(name='NW Region 3 arrival time',fname='NW_Region_3_arrival_times.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 arrival time of max zeta',fname='NW_Region_3_zetatimes.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 zeta max on map',fname='NW_Region_3_zeta_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 speed max on map',fname='NW_Region_3_speed_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 hs max on map',fname='NW_Region_3_hs_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 hss max on map',fname='NW_Region_3_hss_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 h min on map',fname='NW_Region_3_min_depth_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 zeta max',fname='NW_Region_3_zeta.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 speed max',fname='NW_Region_3_speed.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 hs max',fname='NW_Region_3_hs.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 hss max',fname='NW_Region_3_hss.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 3 h min',fname='NW_Region_3_min_depth.png')

    otherfigure = plotdata.new_otherfigure(name='NW Region 4 arrival time',fname='NW_Region_4_arrival_times.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 arrival time of max zeta',fname='NW_Region_4_zetatimes.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 zeta max on map',fname='NW_Region_4_zeta_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 speed max on map',fname='NW_Region_4_speed_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 hs max on map',fname='NW_Region_4_hs_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 hss max on map',fname='NW_Region_4_hss_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 h min on map',fname='NW_Region_4_min_depth_map.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 zeta max',fname='NW_Region_4_zeta.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 speed max',fname='NW_Region_4_speed.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 hs max',fname='NW_Region_4_hs.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 hss max',fname='NW_Region_4_hss.png')
    otherfigure = plotdata.new_otherfigure(name='NW Region 4 h min',fname='NW_Region_4_min_depth.png')

    #---------------------------------------------------------------

    # Parameters used only when creating html and/or latex hardcopy
    # e.g., via clawpack.visclaw.frametools.printframes:

    plotdata.printfigs = True                # print figures
    plotdata.print_format = 'png'            # file format
    plotdata.print_framenos = 'all'          # list of frames to print
    plotdata.print_fignos = 'all'            # list of figures to print
    plotdata.print_gaugenos = 'all'          # list of gauges to print
    plotdata.html = True                     # create html files of plots?
    plotdata.html_homelink = '../README.html'   # pointer for top of index
    plotdata.latex = True                    # create latex file of plots?
    plotdata.latex_figsperline = 2           # layout of plots
    plotdata.latex_framesperline = 1         # layout of plots
    plotdata.latex_makepdf = False           # also run pdflatex?

    return plotdata