forked from janjaapmeijer/oceanpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadwrite.py
More file actions
158 lines (112 loc) · 4.44 KB
/
readwrite.py
File metadata and controls
158 lines (112 loc) · 4.44 KB
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
import numpy as np
import os
def readxyz(filename,step=None,xy=False):
xyz = open(filename)
if xy is False:
x = []
y = []
z = []
for line in xyz:
x1,y1,z1 = line.split()
x.append(float(x1))
y.append(float(y1))
if z1 == '-9999':
z.append(float('nan'))
else:
z.append(float(z1))
xyz.close()
if step is not None:
xstep = [x[i] for i in range(0,len(x),step)]
ystep = [y[i] for i in range(0,len(y),step)]
zstep = [z[i] for i in range(0,len(z),step)]
return xstep, ystep, zstep
else:
return x, y, z
else:
x = []
y = []
for line in xyz:
x1,y1 = line.split()
x.append(float(x1))
y.append(float(y1))
xyz.close()
return (x, y)
def writexyz(filename, x, y, z=None):
if z is None:
with open(filename, "w") as xy:
for i in range(0,len(x)):
xy.write(str(x[i]) + " " + str(y[i]) + '\n')
else:
with open(filename, "w") as xyz:
if isinstance(x, list):
for i in range(0,len(x)):
xyz.write(str(x[i]) + " " + str(y[i]) + " " + str(z[i]) + '\n')
else:
xyz.write(str(x) + " " + str(y) + " " + str(z))
def addxyz(filenamelst,step=None):
x = []
y = []
z = []
for i in range(0,len(filenamelst)):
xtemp, ytemp, ztemp = readxyz(filenamelst[i],step)
x = x + xtemp
y = y + ytemp
z = z + ztemp
del xtemp, ytemp, ztemp
return x, y, z
def readxytxt(filename):
x = np.genfromtxt(filename, usecols=(0), delimiter=' ', dtype=None) # comma: delimiter=','
y = np.genfromtxt(filename, usecols=(1), delimiter=' ', dtype=None)
return x, y
def write_ascii(filename, array, xll, yll, cellsize, nodata=int(-9999)):
import collections
# header = {'ncols': array.shape[1], 'nrows': array.shape[0], 'xllcorner': xll, 'yllcorner': yll,
# 'cellsize': cellsize, 'NODATA_value': nodata}
header = collections.OrderedDict((('ncols', array.shape[1]),
('nrows', array.shape[0]),
('xllcorner', xll),
('yllcorner', yll),
('cellsize', cellsize),
('NODATA_value', nodata)))
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
array[np.isnan(array)] = nodata
# np.savetxt(filename, np.flipud(array), header=header, comments='', fmt="%1.4f")
delimiter = '\t'
fmt="%1.4f"
with open(filename, 'w') as f:
for key in header.keys():
f.write(key + '\t%s\n' % header[key])
for row in np.flipud(array):
line = delimiter.join("-9999" if value == nodata else fmt % value for value in row)
f.write(line + '\n')
def read_asciiheader(filename, info=None):
import linecache
if info is None:
info = ['ncols', 'nrows', 'xllcorner', 'yllcorner', 'cellsize', 'NODATA_value']
header = {}
for (i, var) in enumerate(info):
var, val = linecache.getline(filename, i+1).split()
header[var] = float(val)
return header
def read_ascii(filename, nodata='-9999'):
header = read_asciiheader(filename)
array = np.flipud(np.genfromtxt(filename, skip_header=6, missing_values=nodata, usemask=True).filled(np.nan))
return array, header
def readxls(filename, colnos, rowstart = 0, sheetno = 0):
import xlrd
workbook = xlrd.open_workbook(filename)
sheet = workbook.sheet_by_index(sheetno)
x = []
y = []
for row in range(rowstart,sheet.nrows):
x.append(sheet.cell_value(row,colnos[0]))
y.append(sheet.cell_value(row,colnos[1]))
return x, y
import pickle
def write_dict(dictionary, path, filename, protocol=pickle.HIGHEST_PROTOCOL):
with open(os.path.join(path, filename + '.pkl'), 'wb') as f:
pickle.dump(dictionary, f, protocol=protocol)
def read_dict(path, filename, encoding='ascii'):
with open(os.path.join(path, filename), 'rb') as f:
return pickle.load(f, encoding=encoding)