"""
Create fgmax_grid.txt input file
"""

from clawpack.geoclaw import fgmax_tools, topotools
from numpy import sqrt

# NWcoastFGmax1

def make_fgmax_grid1():
    fg = fgmax_tools.FGmaxGrid()
    fg.fname = 'fgmax1.txt'
    fg.point_style = 3       # will specify a 2d grid of points on quadrilateral

    fg.x1= -124.792953
    fg.x2= -124.697762
    fg.x3= -124.367886
    fg.x4= -124.367415
    fg.y1= 48.399016
    fg.y2= 48.44565
    fg.y3= 48.328516
    fg.y4= 48.270112

    # determine number of points so mesh spacing is approximately dx:
    dx = 1.62/3600.    # 1.62 arcssecond grid (~ 50.0 m)
    x12_length = sqrt((fg.x2-fg.x1)**2 + (fg.y2-fg.y1)**2)
    x12_length = max(x12_length, sqrt((fg.x3-fg.x4)**2 + (fg.y3-fg.y4)**2))
    fg.n12 = int(x12_length / dx)  # number of points for corner 1 to 2
    x23_length = sqrt((fg.x2-fg.x3)**2 + (fg.y2-fg.y3)**2)
    x23_length = max(x23_length, sqrt((fg.x1-fg.x4)**2 + (fg.y1-fg.y4)**2))
    fg.n23 = int(x23_length / dx)  # number of points for corner 2 to 3

    fg.tstart_max =  0.*60     # when to start monitoring max values
    fg.tend_max = 1.e10       # when to stop monitoring max values
    fg.dt_check = 10.         # target time (sec) increment between updating max values
    fg.min_level_check = 4    # which levels to monitor max on
    fg.arrival_tol = 1.e-2    # tolerance for flagging arrival
    
    fg.input_file_name = 'fgmax1.txt'
    fg.write_input_data()

# NWcoastFGmax2

def make_fgmax_grid2():
    fg = fgmax_tools.FGmaxGrid()
    fg.fname = 'fgmax2.txt'
    fg.point_style = 3       # will specify a 2d grid of points on quadrilateral

    fg.x1= -124.792953
    fg.x2= -124.61767
    fg.x3= -124.689592
    fg.x4= -124.787063
    fg.y1= 48.399016
    fg.y2= 48.346574
    fg.y3= 48.160762
    fg.y4= 48.182077

    # determine number of points so mesh spacing is approximately dx:
    dx = 1.62/3600.    # 1.62 arcssecond grid (~ 50.0 m)
    x12_length = sqrt((fg.x2-fg.x1)**2 + (fg.y2-fg.y1)**2)
    x12_length = max(x12_length, sqrt((fg.x3-fg.x4)**2 + (fg.y3-fg.y4)**2))
    fg.n12 = int(x12_length / dx)  # number of points for corner 1 to 2
    x23_length = sqrt((fg.x2-fg.x3)**2 + (fg.y2-fg.y3)**2)
    x23_length = max(x23_length, sqrt((fg.x1-fg.x4)**2 + (fg.y1-fg.y4)**2))
    fg.n23 = int(x23_length / dx)  # number of points for corner 2 to 3

    fg.tstart_max =  0.*60     # when to start monitoring max values
    fg.tend_max = 1.e10       # when to stop monitoring max values
    fg.dt_check = 10.         # target time (sec) increment between updating max values
    fg.min_level_check = 4    # which levels to monitor max on
    fg.arrival_tol = 1.e-2    # tolerance for flagging arrival
    
    fg.input_file_name = 'fgmax2.txt'
    fg.write_input_data()

# NWcoastFGmax3

def make_fgmax_grid3():
    fg = fgmax_tools.FGmaxGrid()
    fg.fname = 'fgmax3.txt'
    fg.point_style = 3       # will specify a 2d grid of points on quadrilateral

    fg.x1= -124.787063
    fg.x2= -124.689592
    fg.x3= -124.555294
    fg.x4= -124.723713
    fg.y1= 48.182077
    fg.y2= 48.160762
    fg.y3= 47.89338
    fg.y4= 47.874455

    # determine number of points so mesh spacing is approximately dx:
    dx = 1.62/3600.    # 1.62 arcssecond grid (~ 50.0 m)
    x12_length = sqrt((fg.x2-fg.x1)**2 + (fg.y2-fg.y1)**2)
    x12_length = max(x12_length, sqrt((fg.x3-fg.x4)**2 + (fg.y3-fg.y4)**2))
    fg.n12 = int(x12_length / dx)  # number of points for corner 1 to 2
    x23_length = sqrt((fg.x2-fg.x3)**2 + (fg.y2-fg.y3)**2)
    x23_length = max(x23_length, sqrt((fg.x1-fg.x4)**2 + (fg.y1-fg.y4)**2))
    fg.n23 = int(x23_length / dx)  # number of points for corner 2 to 3

    fg.tstart_max =  0.*60     # when to start monitoring max values
    fg.tend_max = 1.e10       # when to stop monitoring max values
    fg.dt_check = 10.         # target time (sec) increment between updating max values
    fg.min_level_check = 4    # which levels to monitor max on
    fg.arrival_tol = 1.e-2    # tolerance for flagging arrival
    
    fg.input_file_name = 'fgmax3.txt'
    fg.write_input_data()

# NWcoastFGmax4

def make_fgmax_grid4():
    fg = fgmax_tools.FGmaxGrid()
    fg.fname = 'fgmax4.txt'
    fg.point_style = 3       # will specify a 2d grid of points on quadrilateral

    fg.x1= -124.671642
    fg.x2= -124.444442
    fg.x3= -124.322678
    fg.x4= -124.510764
    fg.y1= 47.880717
    fg.y2= 47.905631
    fg.y3= 47.549813
    fg.y4= 47.549986

    # determine number of points so mesh spacing is approximately dx:
    dx = 1.62/3600.    # 1.62 arcssecond grid (~ 50.0 m)
    x12_length = sqrt((fg.x2-fg.x1)**2 + (fg.y2-fg.y1)**2)
    x12_length = max(x12_length, sqrt((fg.x3-fg.x4)**2 + (fg.y3-fg.y4)**2))
    fg.n12 = int(x12_length / dx)  # number of points for corner 1 to 2
    x23_length = sqrt((fg.x2-fg.x3)**2 + (fg.y2-fg.y3)**2)
    x23_length = max(x23_length, sqrt((fg.x1-fg.x4)**2 + (fg.y1-fg.y4)**2))
    fg.n23 = int(x23_length / dx)  # number of points for corner 2 to 3

    fg.tstart_max =  0.*60     # when to start monitoring max values
    fg.tend_max = 1.e10       # when to stop monitoring max values
    fg.dt_check = 10.         # target time (sec) increment between updating max values
    fg.min_level_check = 4    # which levels to monitor max on
    fg.arrival_tol = 1.e-2    # tolerance for flagging arrival
    
    fg.input_file_name = 'fgmax4.txt'
    fg.write_input_data()

if __name__ == "__main__":
    make_fgmax_grid1()
    make_fgmax_grid2()
    make_fgmax_grid3()
    make_fgmax_grid4()
