"""

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'
lon_0  = '0'
height = 3333500*2
#db2.data['mask'][db2.data['mask'] == 1] = 0

# plot to check :
#db2.data['mask'][db2.data['mask'] == 1] = 0
#imshow(db2.data['mask'][::-1,:])
#colorbar()
#tight_layout()
#show()

imshow(db2.data['B'][::-1])
colorbar()
#show()
print db2.data['B'].max()

# generate the contour :
m = MeshGenerator(db2, 'mesh', '')

# Manually create a circular contour
angles = linspace(0, 1, 1000)*2*pi
xs = 2999000*cos(angles)
ys = 2999000*sin(angles)
contour = array(zip(xs, ys))

m.set_contour(contour)

m.write_gmsh_contour(1000, boundary_extend=False)
m.add_edge_attractor(1)
#field, ifield, lcMin, lcMax, distMin, distMax
m.add_threshold(2, 1, 5000, 5000, 1, 100000)
m.finish(4)
m.close_file()
# -*- 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')
from numpy import *
from pylab import *

thklm = 200.0
bm2 = DataFactory.get_bedmap2(thklm)
bedmap2 = DataInput(bm2)

# Load the domain coordinates
domain_coordinates = loadtxt('domain_coordinates1.out')

mesh_name = 'ant_mesh'

# Create a contour for the domain
#=======================================================
# Create a mesh file in the current directory 
m = MeshGenerator(bedmap2, mesh_name, '')
# Manually set the countour instead of calculating it automatically
m.set_contour(domain_coordinates)
# Write the contour points to the mesh file
m.write_gmsh_contour(100000, boundary_extend = False)
# Extrude the flat mesh 10,000 meters in the z dimension. The height of the 
# mesh can be automatically scaled to the proper height by the model object
m.extrude(10000, 14)
# We're finished with the flat mesh!
m.close_file()


# Refine the mesh based on ice thickness
#=======================================================
ref_bm = MeshRefiner(bedmap2, 'H', gmsh_file_name = mesh_name) 
# Refine the mesh based on the refinement radius
@author: jake
"""

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(thklim = 200)

# Create a countour of the continent - without the ice shelves
db2 = DataInput(bedmap2)
db2.set_data_val('mask',1,127)
mg = MeshGenerator(db2, 'mesh', '')
mg.create_contour('mask', 0, skip_pts=4)
mg.eliminate_intersections(dist=40)
# Get the longest contour, which will be the coastline 
cont = mg.longest_cont

# Load the glacier data
glacier_data = loadtxt('glacier_data.out', delimiter = '|', dtype = 'str')
names = array(glacier_data[:,0], dtype = 'str')
lons = array(glacier_data[:,1], dtype = 'f')
lats = array(glacier_data[:,2], dtype = 'f')

# Convert lons and lats to x, y coordinates
x, y = db2.p(lons,lats)

# Next, eliminate glaciers that are too far from the coast
  [xmax, 0],
  [xmax, 100000],
  [xmin, 100000]
])

# Name of refined mesh
mesh_name = 'transect_mesh'

# Get the Antarctica data sets
bedmap2   = DataFactory.get_bedmap2(thklim=200)
# Load in a DataInput object. We won't use it, but we need to pass a DataInput
# to the MeshGenerator, and we have to fake it out.
db2 = DataInput(None, bedmap2)

# Create a mesh file in the current directory 
m = MeshGenerator(db2, mesh_name, '')
# Manually set the countour instead of calculating it automatically
m.set_contour(cont)
# Write the contour points to the mesh file
m.write_gmsh_contour(100000, boundary_extend = False)
# Extrude the flat mesh 10,000 meters in the z dimension. The height of the 
# mesh can be automatically scaled to the proper height by the model object
m.extrude(20000, 10)

# We're finished with the flat mesh!
m.close_file()

# Refine the mesh based on thickness

# Wrap the bed spline in a function that takes two arguments
def bed(x,y) :