-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytools.py
187 lines (146 loc) · 4.75 KB
/
pytools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
"""
Created on Sat May 11 18:06:23 2013
@author: thackray
"""
import os
from csv import DictReader
import numpy as np
import cPickle as pickle
try:
import pylab as pl
PL = True
except ImportError:
PL = False
try:
from mpl_toolkits.basemap import Basemap
BASEMAP = True
except ImportError:
BASEMAP = False
try:
from mpl_toolkits.axes_grid1 import make_axes_locatable as mal
MAL = True
except ImportError:
MAL = False
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec']
def compare(x,y, eps=1e-6):
return abs(x-y) < eps
def savefig(figname):
"""Takes a .png-less fig name and saves it with the .png extension and
redued whitespace around the meat of the figure"""
pl.savefig(figname+'.png', bbox_inches = 'tight')
def pload(filename, asarray = False):
"""Wrapper to make un-pickling friendlier.
Arguments:
filename: string file name to open
Keyword Arguments:
asarray: (Default False) will return pickled object as an array if possible
Returns:
Object that was pickled
"""
f = open(filename, 'rb')
d = pickle.load(f)
if asarray:
return np.array(d)
else:
return d
def pdump(obj, filename):
"""Wrapper to make pickling friendlier.
Arguments:
obj: object to be pickled
filename: name of file to pickle object under
Returns:
Nothing
"""
f = open(filename, 'wb')
pickle.dump(obj,f)
return
def listget(listy, i, default=0):
try:
return listy[i]
except IndexError:
return default
def dict_to_csv(dictionary, csvname):
"""Save a dictionary to csv format.
Arguments:
dictionary: dictionary to unpack into csv
csvname: string filename
Returns:
None
"""
fieldnames = dictionary.keys()
fieldlengths = [len(dictionary[field]) for field in fieldnames]
with open(csvname, 'w') as f:
f.write(','.join(fieldnames)+'\n')
for i in range(max(fieldlengths)):
line = [str(listget(dictionary[fld],i,'')) for fld in fieldnames]
f.write(','.join(line)+'\n')
return
def dictcsv(csvname, fieldnames = None, arrays = False):
"""Reading csv files into a dictionary.
Arguments:
csvname: string filename
Keyword Arguments:
fieldnames: list of csv column names. If none, first column of the file
being read will be used.
arrays: Whether or not to return csv contents as a dict of arrays
Returns:
dictionary of columns as numpy arrays, keys are fieldnames
"""
fileobj = open(csvname, 'rU')
DR = DictReader(fileobj, fieldnames = fieldnames)
fields = DR.fieldnames
l = DR.next()
dicty = {}
for f in fields:
try:
dicty[f] = [float(l[f])]
except (TypeError, ValueError):
dicty[f] = [l[f]]
for row in DR:
for f in fields:
try:
dicty[f].append(float(row[f]))
except (TypeError, ValueError):
dicty[f].append(row[f])
if arrays:
for key in dicty:
dicty[key] = np.array(dicty[key])
return dicty
if PL and BASEMAP:
def plot_map(lat, lon, data, normalized=False):
"""Plots data on a Basemap.
Arguments:
lat: array of lat locations of data
lon: array of lon locations of other axis of data
data: 2D lat-lon grid of data to put on map
Keyword Arguments:
normalized: if True, show a 0:1 map of ratios of max value
Returns:
Nothing
"""
bm = Basemap(projection = 'mill', llcrnrlon=min(lon),
llcrnrlat=min(lat), urcrnrlon=max(lon),
urcrnrlat=max(lat))
lons, lats = np.meshgrid(lon, lat)
x, y = bm(lons, lats)
pl.figure(figsize=(18,14))
ax = pl.gca()
bm.drawcoastlines(linewidth=1.25, color='white')
bm.drawparallels(np.array([-70,-50,-30,-10,10,30,50,70]),
labels=[1,0,0,0])
bm.drawmeridians(np.arange(min(lon),max(lon),60),labels=[0,0,0,1])
if normalized:
bm.pcolor(x,y,data.T/np.abs(np.max(data)))
else:
bm.pcolor(x,y,data.T)
if MAL:
divider = mal(ax)
cax = divider.append_axes("right", size='5%', pad=0.05)
pl.colorbar(cax=cax)
else:
pl.colorbar()
return
if __name__=='__main__':
dict_to_csv({'a':range(6),'b':range(3)},'test_write.csv')