from mpl_toolkits.basemap import Basemap
import numpy as np
import csv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import glob
import os
from matplotlib.ticker import MaxNLocator
from matplotlib import rc, rcParams
from numpy.random import uniform, seed
from matplotlib.mlab import griddata
from numpy import arange,array,ones#,random,linalg
from pylab import plot,show

os.chdir('C:/Documents and Settings/Alex/Documents/Research/Delaware/APEX/datafiles/data/type')
infonames = glob.glob('Text*')
arraylen =  len(infonames)
floatdate = np.zeros(arraylen)
lat = [None]*arraylen
lon = [None]*arraylen
day = np.zeros(arraylen)
month=[None]*arraylen
year=[None]*arraylen
daystr=[None]*arraylen

for d in range(0,int(arraylen)):
	f = open(infonames[d], 'r')
	while 1:
		line = f.readline()
		if line[:4] == "Date":
			month[d] = str(line[5:7])
			day[d] = int(line[8:10])
			daystr[d] = str(line[8:10])
			year[d] = str(line[11:15])
			break
	f = open(infonames[d], 'r')
	while 1:
		line = f.readline()
		if line[:3] == "Lat":
			lat[d] = str(line[5:12])
			break
	f = open(infonames[d], 'r')
	while 1:
		line = f.readline()
		if line[:3] == "Lon":
			lon[d] = str(line[5:12])
			break
	
	if (year[d] == '2013'):
		yr = 0
	elif (year[d] == '2014'):
		yr = 365
	elif (year[d] == '2015'):
		yr = 365*2


	if(month[d] == '01'):
		mon = 0
	elif(month[d] == '02'):
		mon = 31
	elif(month[d] == '03'):
		mon = 31 + 28
	elif(month[d] == '04'):
		mon = (31 + 28 + 31)
	elif(month[d] == '05'):
		mon = (31 + 28 + 31 + 30)
	elif(month[d] == '06'):
		mon = (31 + 28 + 31 + 30 + 31)
	elif(month[d] == '07'):
		mon = (31 + 28 + 31 + 30 + 31 + 30)
	elif(month[d] == '08'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31)
	elif(month[d] == '09'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31)
	elif(month[d] == '10'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30)
	elif(month[d] == '11'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
	elif(month[d] == '12'):
		mon = (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
	else:
		print 'invalid month'

	floatdate[d] = yr + mon + day[d]

os.chdir('C:/Documents and Settings/Alex/Documents/Research/Delaware/APEX/datafiles')

#from netCDF4 import Dataset
#from numpy import arange # array module from http://numpy.scipy.org
#from numpy.testing import assert_array_equal, assert_array_almost_equal
#
## open a the netCDF file for reading.
#ncfile = Dataset('data/seasonal/S19973552010079.L3b_SCWI_CHL.main','r') 
## read the data in variable named 'data'.
#data = ncfile.variables['data'][:]
#nx,ny = data.shape
## check the data.
#data_check = arange(nx*ny) # 1d array
#data_check.shape = (nx,ny) # reshape to 2d array


m = Basemap(width=1500000, height=1500000,
            resolution='i',projection='lcc',\
            lat_1=-68.,lat_2=-56,lat_0=-60,lon_0=-63.)
m.drawcoastlines()
m.fillcontinents(color='gray',lake_color='white')
# draw parallels and meridians.
m.drawparallels(np.arange(-70,-50,5))
m.drawmeridians(np.arange(0,360,5))
m.drawmapboundary(fill_color='white')
ax = plt.gca()
lonflt = map(float,lon)
latflt = map(float,lat)
x, y  = m(lonflt,latflt)
cs =m.scatter(x,y)
#for y in np.linspace(m.ymax/20,19*m.ymax/20,9):
#    for x in np.linspace(m.xmax/20,19*m.xmax/20,12):
#        lon, lat = m(x,y,inverse=True)
#        poly = m.tissot(lon,lat,1.5,100,\
#                        facecolor='green',zorder=10,alpha=0.5)
plt.title("Current Float Location")
plt.show()