-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_file.py
149 lines (125 loc) · 5.95 KB
/
test_file.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
import os
import unittest
from datetime import datetime, timezone
from pathlib import Path
import ukis_pysat.file as psf
path_testfiles = Path(__file__).parents[0] / "testfiles"
str_path = os.path.join(os.path.dirname(__file__), "testfiles")
class FileTest(unittest.TestCase):
def test_env(self):
with self.assertRaises(KeyError, msg=f"No environment variable Key found"):
psf.env_get("Key")
os.environ["FUN"] = "True"
self.assertTrue(psf.env_get("FUN", boolean=True))
os.environ["ISITWEEKENDYET"] = "0"
self.assertFalse(psf.env_get("ISITWEEKENDYET", boolean=True))
def test_get_sentinel_scene_from_path_testfiles(self):
with psf.get_sentinel_scene_from_dir(path_testfiles) as (full_path, ident):
self.assertEqual("S1M_hello_from_inside", ident)
def test_get_sentinel_scene_from_str_path(self):
with psf.get_sentinel_scene_from_dir(str_path) as (full_path, ident):
self.assertEqual("S1M_hello_from_inside", ident)
def test_get_sentinel_scene_from_dir_joinpath(self):
with psf.get_sentinel_scene_from_dir(path_testfiles.joinpath("another_scene")) as (full_path, ident):
self.assertEqual("S2__IN_FOLDER", ident)
def test_get_polarization_from_s1_filename_SDH(self):
self.assertEqual(
psf.get_polarization_from_s1_filename(
"MMM_BB_TTTR_1SDH_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
),
"HH",
)
def test_get_polarization_from_s1_filename_SSH(self):
self.assertEqual(
psf.get_polarization_from_s1_filename(
"MMM_BB_TTTR_1SSH_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
),
"HH",
)
def test_get_polarization_from_s1_filename_SSV(self):
self.assertEqual(
psf.get_polarization_from_s1_filename(
"MMM_BB_TTTR_2SSV_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
),
"VV",
)
def test_get_polarization_from_s1_filename_SDV(self):
self.assertEqual(
psf.get_polarization_from_s1_filename(
"MMM_BB_TTTR_1SDV_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip",
True,
),
"VV,VH",
)
def test_get_ts_from_sentinel_filename_S1(self):
self.assertEqual(
psf.get_ts_from_sentinel_filename(
"S1M_BB_TTTR_LFPP_YYYYMMDDTHHMMSS_20200113T002219_OOOOOO_DDDDDD_CCCC.SAFE.zip",
False,
),
datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
)
self.assertEqual(
psf.get_ts_from_sentinel_filename(
"S1M_BB_TTTR_LFPP_20200113T074619_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
),
datetime(2020, 1, 13, 7, 46, 19, tzinfo=timezone.utc),
)
def test_get_ts_from_sentinel_filename_S2(self):
self.assertEqual(
psf.get_ts_from_sentinel_filename(
"S2AM_MSIXXX_20200113T002219_Nxxyy_ROOO_Txxxxx_<Product Discriminator>.SAFE"
),
datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
)
def test_get_ts_from_sentinel_filename_S3(self):
self.assertEqual(
psf.get_ts_from_sentinel_filename(
"S3M_OL_L_TTT____20200113T002219_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3"
),
datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
)
self.assertEqual(
psf.get_ts_from_sentinel_filename(
"S3M_OL_L_TTTTTT_yyyymmddThhmmss_20200113T002219_YYYYMMDDTHHMMSS_i_GGG_c.SEN3", False
),
datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
)
def test_get_ESA_date_from_datetime(self):
self.assertEqual(
psf.get_sat_ts_from_datetime(datetime(2020, 1, 13, 7, 46, 19, tzinfo=timezone.utc)), "20200113T074619"
)
def test_get_footprint_from_manifest(self):
self.assertEqual(
psf.get_footprint_from_manifest(path_testfiles.joinpath("manifest.safe")).wkt,
"POLYGON ((149.766922 -24.439564, 153.728622 -23.51771, 154.075058 -24.737713, 150.077042 "
"-25.668921, 149.766922 -24.439564))",
)
with self.assertRaises(KeyError, msg="Footprint not found"):
psf.get_footprint_from_manifest(os.path.join(path_testfiles, "manifest_bad.safe"))
def test_get_origin_from_manifest(self):
self.assertEqual(
psf.get_origin_from_manifest(path_testfiles.joinpath("manifest.safe")),
"United Kingdom",
)
with self.assertRaises(KeyError, msg="Country of origin not found."):
psf.get_footprint_from_manifest(os.path.join(path_testfiles, "manifest_bad.safe"))
def test_get_ipf_from_manifest(self):
self.assertEqual(
psf.get_ipf_from_manifest(path_testfiles.joinpath("manifest.safe")),
2.82,
)
with self.assertRaises(KeyError, msg="IPF Version not found."):
psf.get_footprint_from_manifest(os.path.join(path_testfiles, "manifest_bad.safe"))
def test_get_pixel_spacing(self):
self.assertEqual(psf.get_pixel_spacing(path_testfiles), (40.0, 0.0003593261136478086))
with self.assertRaises(KeyError, msg="Pixel Spacing not found."):
psf.get_pixel_spacing(path_testfiles, polarization="FF")
self.assertEqual(psf.get_pixel_spacing(str_path), (40.0, 0.0003593261136478086))
def test_get_proj_string(self):
self.assertEqual(
psf.get_proj_string(psf.get_footprint_from_manifest(path_testfiles.joinpath("manifest.safe"))),
r"+proj=utm +zone=56J, +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
)
if __name__ == "__main__":
unittest.main()