-
Notifications
You must be signed in to change notification settings - Fork 5
/
header.py
63 lines (49 loc) · 1.82 KB
/
header.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
import csv
from optparse import OptionParser
constants = {
'NODATA': 'NODATA_VALUE',
'NCOLS': 'NCOLS',
'NROWS': 'NROWS',
'XDIM': 'CELLSIZE',
'ULXMAP': 'XLLCORNER',
'ULYMAP': 'YLLCORNER'
}
conversion_map = {
'NODATA': float,
'NCOLS': int,
'NROWS': int,
'XDIM': float,
'ULXMAP': float,
'ULYMAP': float
}
def main(bil_hdr_file, flt_hdr_file):
bil_hdr = {}
with open(bil_hdr_file, 'r') as csvfile:
csvreader = csv.reader(csvfile)
for l in csvreader:
k, v = l[0].split()
if k in constants:
bil_hdr[k] = conversion_map[k](v)
flt_hdr = {}
with open(flt_hdr_file, 'w') as csvfile2:
csvwriter = csv.writer(csvfile2, delimiter=' ')
csvwriter.writerow(['BYTEORDER', 'LSBFIRST'])
for k, v in bil_hdr.items():
if k == 'ULYMAP':
# TODO: can improve `yllcorner` by using gdal corner coords
v = float(v) - bil_hdr['NROWS'] * bil_hdr['XDIM']
flt_hdr[constants[k]] = v
csvwriter.writerow([constants[k], v])
return bil_hdr, flt_hdr
if __name__ == '__main__':
parser = OptionParser(usage='%prog -b bil_hdr_file -f flt_hdr_file')
parser.add_option('-b', '--bil_hdr_file', type=str, dest='bil_hdr_file',
help='name of input .bil header file')
parser.add_option('-f', '--flt_hdr_file', type=str, dest='flt_hdr_file',
help='name of output .flt header file')
options, args = parser.parse_args()
if not options.bil_hdr_file: # if filename is not given
parser.error('Input .bil header filename not given.')
if not options.flt_hdr_file: # if filename is not given
parser.error('Output .flt filename not given.')
main(options.bil_hdr_file, options.flt_hdr_file)