"""
Create fgmax_grid.txt input file
"""

from clawpack.geoclaw import fgmax_tools, topotools
from numpy import sqrt

# Create the dictionaries for the (x,y) positions of the quadrilateral vertices

# Create the lists of the x and y coordinates for the vertices of each Fixed Grid (FG)
# For a rectangle, e.g., to set the corners clockwise, starting with most
 # SouthEasterly corner, order the lists as
# xs[.] = (East,East,West,West)
# ys[.] = (South,North,North,South)

xs = {}
ys = {}

# NeahMakah
xs[1] = [-124.576000,-124.601000,-124.692000,-124.678400]
ys[1] = [48.375900,48.396500,48.340200,48.271000]


def make_fgmax_grid(FGno):
    
    fg = fgmax_tools.FGmaxGrid()
    fg.fname = 'fgmax' + '%s.txt' % FGno
    # point_style = point_styles[FGno-1]
    # fg.point_style = point_style      # will specify a 2d grid of points on quadrilateral
    fg.point_style = 3      # will specify a 2d grid of points on quadrilateral
    x=xs.get(FGno)
    y=ys.get(FGno)
    FGname = 'fgmax' + '%s.txt' % FGno
    
    print
    print '---------------------------------------------- '
    print 'FGno, FGname, point_style = ', FGno, FGname, fg.point_style
    print 'x = ', x
    print 'y = ', y
    
    fg.x1 = x[0]
    fg.y1 = y[0]
    fg.x2 = x[1]
    fg.y2 = y[1]
    fg.x3 = x[2]
    fg.y3 = y[2]
    fg.x4 = x[3]
    fg.y4 = y[3]
    
    # determine number of points so mesh spacing is approximately dx:
    dx = 1./(3*3600.)    # 1/3 arcssecond grid
    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 =  5.*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 = FGname
    fg.write_input_data()

if __name__ == "__main__":
    
    nFG = len(xs)
    FGnos = range(1,nFG+1)
    print
    print '====================================================='
    print 'Call make_fgmax_grid for nFG = ', nFG, ' Fixed Grids'
    print 'FGnos = ', FGnos
    print '====================================================='
    
    for FGno in FGnos:
        make_fgmax_grid(FGno)