#!/usr/bin/python

import glimcdf
import numpy
import sys
import math

#Parse the config file to determine how to set up the netcdf file
nc, shape = glimcdf.nc_from_config(sys.argv[1])

#Create variables for topography and ice thickness
topg = glimcdf.setup_variable(nc, "topg")
thk = glimcdf.setup_variable(nc, "thk")
beta = glimcdf.setup_variable(nc, "beta", staggered=True)
uvelbc = glimcdf.setup_variable(nc, "uvelbc", staggered=True, useZ=True)
vvelbc = glimcdf.setup_variable(nc, "vvelbc", staggered=True, useZ=True)
#Determine the total length of the domain
L = shape.nx*shape.dx

for i in range(shape.nx):
    xhat = float(i)/(shape.nx-1)
    for j in range(shape.ny):
        yhat = float(j)/(shape.ny-1)
        topg.put_1((0,j,i), -2000) #Everything at sea level
        
        #Kilometer-thick ice in a circle
        if (i > 2 and i < shape.nx-2 and j > 2 and j < shape.ny-2):
            thk.put_1((0,j,i), 1000)
        else:
            thk.put_1((0,i,j), 0)
#!/usr/bin/python

import glimcdf
from math import sin,cos,tan,pi
import numpy
import sys

#Parse the config file to determine how to set up the netcdf file
nc, shape = glimcdf.nc_from_config(sys.argv[1])

#Create variables for topography and ice thickness
topg = glimcdf.setup_variable(nc, "topg")
thk = glimcdf.setup_variable(nc, "thk")
beta = glimcdf.setup_variable(nc, "beta", staggered=True)
#Determine the total length of the domain
L = shape.nx*shape.dx
for j in range(shape.nx):
    #Our surface is a uniform slope.
    x = L*float(j)/(shape.nx - 1)
    z = 2000 - x*tan(.1*pi/180)
    b = 1000 + 1000*sin(2*pi*float(j)/(shape.nx - 1))
    for i in range(shape.ny):
        #Write this data point
        topg.put_1((0,i,j), z - 1000)
        thk.put_1((0,i,j), 1000)
        if j < shape.nx - 1 and i < shape.ny - 1:
           beta.put_1((0,i,j),b)

nc.close()
#Grab from the config file whether or not we are running with periodic boundary conditions
#in the x direction.  If we are, then we will ignore the kinematic boundary condition
#along the vertical edges of the ice shelf (this allows us to simulate a simple 1D
#solution.
parser = ConfigParser.ConfigParser()
parser.read(sys.argv[1])
periodic_ew = int(parser.get("options","periodic_ew"))

#Set the flow law for this experiment in the configuration file and write the file back out
parser.set("parameters", "default_flwa", "4.6e-18")  
f=open(sys.argv[1], "w")
parser.write(f)
f.close()

#Create variables for topography and ice thickness
topg = glimcdf.setup_variable(nc, "topg")
thk = glimcdf.setup_variable(nc, "thk")
uvelbc = glimcdf.setup_variable(nc, "uvelhom", staggered=True, useZ=True)
vvelbc = glimcdf.setup_variable(nc, "vvelhom", staggered=True, useZ=True)
beta = glimcdf.setup_variable(nc, 'beta', staggered=True)
kinbcmask = glimcdf.setup_variable(nc, 'kinbcmask', type=NC.INT)

#Determine the total length of the domain
L = shape.nx*shape.dx

rho_i = 910.0
rho_w = 1028.0

thk[:] = 0
topg[:] = -2000
    return v_nm1 + .5 * (strainRate(h_nm1) + strainRate(h_n))*delta

#Parse the config file to determine how to set up the netcdf file
nc, shape = glimcdf.nc_from_config(sys.argv[1])


#Set arrhenius parameter in the config file
parser = ConfigParser.ConfigParser()
parser.read(sys.argv[1])
parser.set("parameters", "default_flwa", str(A))  
f=open(sys.argv[1], "w")
parser.write(f)
f.close()

#Create variables for topography and ice thickness
topg = glimcdf.setup_variable(nc, "topg")
thk  = glimcdf.setup_variable(nc, "thk")
beta = glimcdf.setup_variable(nc, "beta", staggered=True)
kinbcu = glimcdf.setup_variable(nc, "uvelbc",  staggered=True, useZ=True)
kinbcv = glimcdf.setup_variable(nc, "vvelbc",  staggered=True, useZ=True)
guessu = glimcdf.setup_variable(nc, "uvelhom", staggered=True, useZ=True)
guessv = glimcdf.setup_variable(nc, "vvelhom", staggered=True, useZ=True)

#Determine the total length of the domain
L = shape.nx*shape.dx

rho_i = 910.0
rho_w = 1028.0

q_0 = 4e5 #Ice flux at the grounding line, cannot be zero!!
h_0 = 1000
#!/usr/bin/python

#Run with a command line argument that is the config file to read filenames and grid data from

import glimcdf
from math import sin,cos,tan,pi
import numpy
import sys

#Parse the config file to determine how to set up the netcdf file
nc, shape = glimcdf.nc_from_config(sys.argv[1])

#Create variables for topography and ice thickness
topg = glimcdf.setup_variable(nc, "topg")
thk = glimcdf.setup_variable(nc, "thk")

#Set this on a flat surface
topg[:] = 0

#Create a hump of ice in the middle third of the domain, with a maximum heigt of 1000 m.
cenx = shape.nx / 2
ceny = shape.ny / 2

i = numpy.indices((shape.nx, shape.ny)).astype("float32")

#Create a field with the normalized distance from the center of the domain
r_squared = 1.0/8.0 - ((i[1,:] - cenx)/shape.nx)**2 - ((i[0,:] - ceny)/shape.ny)**2 

H = (2000.0 * numpy.sqrt( numpy.where(r_squared > 0, r_squared, 0)))

thk[0,:,:] = H
for line in inputfile:
    i, j, mag, azimuth = line.strip().split()
    kinematic_bc_mask[i,j] = 1
    kinematic_bc_mask = numpy.array(kinematic_bc_mask,'int32')
    ice_vel_azimuth[i,j] = azimuth
    ice_vel_magnitude[i,j] = mag



#Create a NetCDF with the raw data fields.  This will be a useful
#debugging tool
nc = pycdf.CDF("ross-raw.nc", NC.WRITE | NC.CREATE | NC.TRUNC)
nc.automode()
glimcdf.setup_dimensions(nc, cols, rows, 1, 6822, 6822)

existency_var = glimcdf.setup_variable(nc, "existency", type=NC.INT)
azimuth_var = glimcdf.setup_variable(nc, "velo_azimuth", type=NC.DOUBLE)
magnitude_var = glimcdf.setup_variable(nc, "velo_mag", type=NC.DOUBLE)
thck_var = glimcdf.setup_variable(nc, "thk", type=NC.DOUBLE)
topg_var = glimcdf.setup_variable(nc, "topg", type=NC.DOUBLE)
reliable_var = glimcdf.setup_variable(nc, "velo_reliable", type=NC.INT)
fake_shelf_var = glimcdf.setup_variable(nc, "fake_shelf_mask", type=NC.INT)
real_shelf_mask_var = glimcdf.setup_variable(nc, "real_shelf_mask", type=NC.INT)
acab_var = glimcdf.setup_variable(nc, "acab", type=NC.DOUBLE)
bbar_var = glimcdf.setup_variable(nc, "bbar", type=NC.DOUBLE)
surface_temp_var = glimcdf.setup_variable(nc, "surface_temp", type=NC.DOUBLE)
kinbc_var = glimcdf.setup_variable(nc, "kinematic_bc_mask", type=NC.INT)
land_ice_mask_var = glimcdf.setup_variable(nc, "land_ice_mask", type=NC.INT)
shelf_front_mask_var = glimcdf.setup_variable(nc, "shelf_front_mask", type=NC.INT)

existency_var[0,:,:] = existency.tolist()