"""
Create a grounding line contour.
"""

from varglas.data.data_factory import DataFactory
from varglas.utilities import DataInput, MeshGenerator
from numpy import *
from mpl_toolkits.basemap import Basemap

# Get the Antarctica data sets
bedmap2 = DataFactory.get_bedmap2()
db2 = DataInput(bedmap2)

# Get the grounding line by eliminating the shelves
db2.set_data_val('mask',1,127)

# Create a grounding line countour
mg = MeshGenerator(db2, 'mesh', '')
mg.create_contour('mask', 10, skip_pts=2)
mg.eliminate_intersections(dist=20)
cont = mg.longest_cont

# Convert (x,y) coordinates to (lon,lat)
cont_lons, cont_lats = db2.p(cont[:,0], cont[:,1], inverse = True)

# Convert (x,y) coordinates to (lon,lat)
cont_lons, cont_lats = db2.p(cont[:,0], cont[:,1], inverse = True)

# Convert to basemap coordinates
lat_0  = '-90'
lat_ts = '-71'
# -*- coding: utf-8 -*-
"""
Create a uniform mesh of Antarctica for plotting. 
"""

from varglas.utilities         import DataInput, MeshGenerator
from varglas.data.data_factory import DataFactory
from pylab                     import *

thklim = 0
# create meshgrid for contour :
bedmap2 = DataFactory.get_bedmap2()
# process the data :
dbm = DataInput(bedmap2, gen_space=False)
dbm.set_data_val("H", 32767, thklim)

m = MeshGenerator(dbm, 'mesh', '')

m.create_contour('H', 0.0, 5)
m.eliminate_intersections(dist=20)

m.write_gmsh_contour(1000, boundary_extend=False)
m.add_edge_attractor(1)

#field, ifield, lcMin, lcMax, distMin, distMax
m.add_threshold(2, 1, 3000, 3000, 1, 100000)
m.finish(4)

#m.create_2D_mesh('mesh') #FIXME: fails
#m.convert_msh_to_xml('mesh', 'mesh')
mesh = Mesh('data/meshes/ant_mesh.xml')

# Get a bunch of data to use in the simulation 
thklim = 100.0
measures  = DataFactory.get_ant_measures(res=900)
bedmap1   = DataFactory.get_bedmap1(thklim=thklim)
bedmap2   = DataFactory.get_bedmap2(thklim=thklim)

dm  = DataInput(measures, mesh=mesh)
db1 = DataInput(bedmap1,  mesh=mesh)
db2 = DataInput(bedmap2,  mesh=mesh)

# Fix some stuff?
db2.data['B'] = db2.data['S'] - db2.data['H']
db2.set_data_val('H', 32767, thklim)
db2.data['S'] = db2.data['B'] + db2.data['H']

S      = db2.get_spline_expression("S")
B      = db2.get_spline_expression("B")
T_s    = db1.get_spline_expression("srfTemp")
q_geo  = db1.get_spline_expression("q_geo")
adot   = db1.get_spline_expression("adot")

# Create a model for the sole purpose of deforming the full continent mesh
# Get the full continent mesh
full_mesh = MeshFactory.get_antarctica_3D_gradS_detailed()
mesh_model = model.Model()
mesh_model.set_mesh(full_mesh)
mesh_model.set_geometry(S, B, deform=True)
"""
Create contours for the Antarctic ice shelves so we can plot them. 
"""

from pylab import *
from varglas.data.data_factory import DataFactory
from varglas.utilities import DataInput, MeshGenerator
from numpy import *

# Get the Antarctica data sets
bedmap2 = DataFactory.get_bedmap2()
db2 = DataInput(bedmap2)
# Just get the ice shelves
db2.set_data_val('mask',127,0)

mg = MeshGenerator(db2, 'mesh', '')
mg.create_contour('mask', 0, skip_pts=2)
mg.eliminate_intersections(dist=20)

# Get the two longest contours, which I'm assuming are the two major ice shelves
cl = mg.c.allsegs[0]
contour_lens = array(map(len, cl))
sorted_indexes = contour_lens.argsort()

shelf1 = cl[sorted_indexes[-1]]
shelf2 = cl[sorted_indexes[-2]]

# Convert to (lon, lat coordinates)
shelf1_lons, shelf1_lats = db2.p(shelf1[:,0], shelf1[:,1], inverse = True)
shelf2_lons, shelf2_lats = db2.p(shelf2[:,0], shelf2[:,1], inverse = True)