Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit f86dae2

Browse files
tfuhrmannMatt Garthwaite
authored andcommitted
added unit tests for new functionality, minor changes to read/write functions for CR coordinate file
1 parent 28a99f3 commit f86dae2

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

coral/dataio.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import rasterio
77
from rasterio.windows import Window
8-
import os
8+
import sys, os
99

1010

1111
def readfile(file, sub_im, cr):
@@ -85,10 +85,11 @@ def read_radar_coords(filename):
8585
# get site name
8686
line = line.strip("\n")
8787
site.append(line.split("\t")[0])
88-
az.append(line.split("\t")[6])
89-
rg.append(line.split("\t")[7])
90-
idx = idx + 1
88+
az.append(line.split("\t")[-2])
89+
rg.append(line.split("\t")[-1])
90+
idx = idx + 1
9191
print("Radar coordinates at %d sites read" % (idx))
92+
f.close()
9293
else:
9394
print("ERROR: Can't read file", filename)
9495
sys.exit()
@@ -116,6 +117,8 @@ def write_radar_coords(filename_init, filename, sites, geom):
116117
out_line = temp2 + "\t" + str(cr[1][1]) + \
117118
"\t" + str(cr[1][0]) + "\n"
118119
fout.write(out_line)
120+
f.close()
121+
fout.close()
119122
else:
120123
print("ERROR: Can't read file", filename_init)
121124
sys.exit()

data/site_az_rg.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SERF 110 87
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SERF -12.34 123.45 67.89 20180701 99999999 110 87

tests/test_coral.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os.path
66
import numpy as np
77
from coral.corner_reflector import *
8-
from coral.dataio import readpar, readmli
8+
from coral.dataio import readpar, readmli, read_radar_coords, write_radar_coords
99

1010
class TestCoral(unittest.TestCase):
1111
@classmethod
@@ -157,6 +157,59 @@ def test_loop(self):
157157

158158
# test the mean value of the average intensity image
159159
self.assertEqual(round(np.mean(avgI), 6), -11.385613) # -11.385613198856245
160+
161+
162+
class TestShift(unittest.TestCase):
163+
@classmethod
164+
def setUpClass(cls):
165+
cls.files = []
166+
for file in glob.glob("data/*.mli"):
167+
cls.files.append(file)
168+
169+
cls.files.sort()
170+
cls.sub_im = 51
171+
cls.targ_win_sz = 5
172+
cls.clt_win_sz = 9
173+
cls.cr = [ 86, 111] # coordinates changed to check shift
174+
175+
def test_loop(self):
176+
'''test the coordinate shift'''
177+
avgI, rcs, scr, Avg_clt, t, cr_new, cr_pos = loop(self.files, self.sub_im, self.cr, self.targ_win_sz, self.clt_win_sz)
178+
179+
# test array containing values of shift to be applied to radar coordinates
180+
self.assertEqual(cr_pos[0], 52)
181+
self.assertEqual(cr_pos[1], 50)
182+
183+
184+
class TestCRfiles(unittest.TestCase):
185+
@classmethod
186+
def setUpClass(cls):
187+
cls.file_in1 = "data/site_az_rg.txt"
188+
cls.file_in2 = "data/site_lat_lon_hgt_date1_date2_az_rg.txt"
189+
cls.file_out1 = "data/site_az_rg_new.txt"
190+
cls.file_out2 = "data/site_lat_lon_hgt_date1_date2_az_rg_new.txt"
191+
cls.sites = {'SERF' : np.array([[-999, -999],[ 87, 110]])}
192+
cls.geom = "desc"
193+
194+
def test_cr_file(self):
195+
'''test the calculation loop function'''
196+
# open reduced CR coordinate file
197+
site, az, rg = read_radar_coords(self.file_in1)
198+
self.assertEqual(int(rg[0]), 87)
199+
self.assertEqual(int(az[0]), 110)
200+
# write to new file
201+
write_radar_coords(self.file_in1, self.file_out1, self.sites, self.geom)
202+
assert os.path.exists(self.file_out1) == 1
203+
os.remove(self.file_out1)
204+
205+
# open full CR coordinate file
206+
site, az, rg = read_radar_coords(self.file_in2)
207+
self.assertEqual(int(rg[0]), 87)
208+
self.assertEqual(int(az[0]), 110)
209+
# write to new file
210+
write_radar_coords(self.file_in2, self.file_out2, self.sites, self.geom)
211+
assert os.path.exists(self.file_out2) == 1
212+
os.remove(self.file_out2)
160213

161214

162215
if __name__ == '__main__':

0 commit comments

Comments
 (0)