-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnppy.radar.create_header.py
139 lines (121 loc) · 5.37 KB
/
dnppy.radar.create_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
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
__author__ = ["Daniel Jensen, [email protected]",
"Scott Baron",
"Jwely"]
import os
import re
import codecs
def create_header(folder):
"""
Builds a header file for the input UAVSAR .grd file,
allowing the data to be read as a raster dataset.
:param folder: the folder containing the UAVSAR .grd and .ann files
"""
os.chdir(folder)
# Empty lists to put information that will be recalled later.
Lines_list = []
Samples_list = []
Latitude_list = []
Longitude_list = []
Files_list = []
# Step 1: Look through folder and determine how many different flights there are
# by looking at the HDR files.
for files in os.listdir(folder):
if files [-4:] == ".grd":
newfile = open(files[0:-4] + ".hdr", 'w')
newfile.write(
"""
ENVI
description = {DESCFIELD}
samples = NSAMP
lines = NLINE
bands = 1
header offset = 0
file type = ENVI Standard
data type = DATTYPE
interleave = bsq
sensor type = Unknown
byte order = 0
map info = {
Geographic Lat/Lon,
1.5000, 1.5000,
LONGITUDE,
LATITUDE,
5.5560000000e-05,
5.5560000000e-05,
WGS-84,
units=Degrees
}
coordinate system string = {
GEOGCS["GCS_WGS_1984",
DATUM["D_WGS_1984",
SPHEROID["WGS_1984",
6378137.0,298.257223563]],
PRIMEM["Greenwich",0],
UNIT["Degree",0.017453292519943295]]
}
wavelength units = Unknown
"""
)
newfile.close()
if files[0:18] not in Files_list:
Files_list.append(files[0:18])
#Variables used to recall indexed values.
var1 = 0
#Step 2: Look through the folder and locate the annotation file(s).
# These can be in either .txt or .ann file types.
for files in os.listdir(folder):
if Files_list[var1] and files[-4:] == ".txt" or files[-4:] == ".ann":
#Step 3: Once located, find the info we are interested in and append it to
# the appropriate list. We limit the variables to <=1 so that they only
# return two values (one for each polarization of
# searchfile = with codecs.open(files, 'r', encoding='utf-8',errors='ignore') as fdata:
# searchfile = open(files, "r")
with open(files, encoding="latin-1") as datafile:
for line in files:
if "Ground Range Data Latitude Lines" in line:
Lines = line[67:72]
if Lines not in Lines_list:
Lines_list.append(Lines)
elif "Ground Range Data Longitude Samples" in line:
Samples = line[67:72]
if Samples not in Samples_list:
Samples_list.append(Samples)
elif "grd_mag.row_addr" in line:
Latitude = line.split()[3]
print(Latitude)
if Latitude not in Latitude_list:
Latitude_list.append(Latitude)
elif "grd_mag.col_addr" in line:
Longitude = line.split()[3]
print(Longitude)
if Longitude not in Longitude_list:
Longitude_list.append(Longitude)
#Reset the variables to zero for each different flight date.
var1 = 0
searchfile.close()
var6 = 0
# Step 3: Open HDR file and replace data.
for files in os.listdir(folder):
if files[-4:] == ".hdr":
with open(files, "r") as sources:
lines = sources.readlines()
with open(files, "w") as sources:
for line in lines:
if "data type = DATTYPE" in line:
sources.write(re.sub(line[12:19], "4", line))
elif "DESCFIELD" in line:
sources.write(re.sub(line[2:11], "File Imported into ENVI.", line))
elif "lines" in line:
sources.write(re.sub(line[10:15], Lines_list[Files_list.index(files[0:18])], line))
elif "samples" in line:
sources.write(re.sub(line[10:15], Samples_list[Files_list.index(files[0:18])], line))
elif "map info" in line:
sources.write(re.sub(line[47:66], "{lon}, {lat}".format(
lon=Longitude_list[Files_list.index(files[0:18])],
lat=Latitude_list[Files_list.index(files[0:18])]), line))
else:
sources.write(re.sub(line, line, line))
print("Finished creating hdrs")
return
test_data = '/Volumes/JT/projects/uavsar/jemez/test'
create_header(test_data)