2121from datetime import datetime
2222from enum import Enum
2323from pathlib import Path
24+ from zipfile import ZipFile
2425
2526import numpy as np
2627import pytest
2728import yaml
29+ from pytest_lazy_fixtures import lf
30+ from upath import UPath
31+
32+ from satpy .readers .core .remote import FSFile
2833
2934geotiepoints = pytest .importorskip ("geotiepoints" , "1.7.5" )
3035
4348END_TIME = datetime (2019 , 2 , 1 , 2 , 47 , 20 )
4449
4550@pytest .fixture (scope = "module" )
46- def granule_directory (tmp_path_factory ):
51+ def data_directory (tmp_path_factory ):
52+ """Create a granule directory."""
53+ return tmp_path_factory .mktemp ("data" )
54+
55+
56+ @pytest .fixture (scope = "module" )
57+ def granule_directory (data_directory ):
4758 """Create a granule directory."""
48- data_dir = tmp_path_factory .mktemp ("data" )
49- gdir = data_dir / f"S1A_IW_GRDH_1SDV_{ dirname_suffix } .SAFE"
59+ gdir = data_directory / f"S1A_IW_GRDH_1SDV_{ dirname_suffix } .SAFE"
5060 os .mkdir (gdir )
5161 return gdir
5262
@@ -62,6 +72,29 @@ def annotation_file(granule_directory):
6272 return annotation_file
6373
6474
75+ @pytest .fixture (scope = "module" )
76+ def zipped_annotation_file (annotation_file , data_directory ):
77+ """Create a zipped annotation file."""
78+ zip_file = data_directory / "annotation.zip"
79+ with ZipFile (zip_file , mode = "w" ) as archive :
80+ basename = os .path .join (* os .path .normpath (annotation_file ).split (os .path .sep )[- 3 :])
81+ archive .write (annotation_file , basename )
82+ fname = f"zip://{ basename } ::file://{ zip_file .as_posix ()} "
83+ return fname
84+
85+
86+ @pytest .fixture (scope = "module" )
87+ def upath_annotation_file (zipped_annotation_file ):
88+ """Create a upath of a zipped annotation file."""
89+ return UPath (zipped_annotation_file )
90+
91+
92+ @pytest .fixture (scope = "module" )
93+ def fs_annotation_file (upath_annotation_file ):
94+ """Create an FSFile of a zipped annotation file."""
95+ return FSFile (upath_annotation_file )
96+
97+
6598@pytest .fixture (scope = "module" )
6699def annotation_filehandler (annotation_file ):
67100 """Create an annotation filehandler."""
@@ -161,13 +194,35 @@ def measurement_file(granule_directory):
161194
162195
163196@pytest .fixture (scope = "module" )
164- def measurement_filehandler (measurement_file , noise_filehandler , calibration_filehandler ):
197+ def zipped_measurement_file (measurement_file , data_directory ):
198+ """Create a zipped measurement file."""
199+ zip_file = data_directory / "measurement.zip"
200+ with ZipFile (zip_file , mode = "w" ) as archive :
201+ basename = os .path .join (* os .path .normpath (measurement_file ).split (os .path .sep )[- 3 :])
202+ archive .write (measurement_file , basename )
203+ return f"zip://{ basename } ::file://{ zip_file .as_posix ()} "
204+
205+
206+ @pytest .fixture (scope = "module" )
207+ def upath_measurement_file (zipped_measurement_file ):
208+ """Create a upath of a zipped measurement file."""
209+ return UPath (zipped_measurement_file )
210+
211+
212+ @pytest .fixture (scope = "module" )
213+ def fsfile_measurement_file (upath_measurement_file ):
214+ """Create an FSFile of a zipped measurement file."""
215+ return FSFile (upath_measurement_file )
216+
217+
218+ @pytest .fixture (scope = "module" , params = ("measurement_file" , "upath_measurement_file" , "fsfile_measurement_file" ))
219+ def measurement_filehandler (request , noise_filehandler , calibration_filehandler ):
165220 """Create a measurement filehandler."""
166221 filename_info = {"mission_id" : "S1A" , "dataset_name" : "foo" , "start_time" : START_TIME , "end_time" : END_TIME ,
167222 "polarization" : "vv" }
168223 filetype_info = None
169224 from satpy .readers .sar_c_safe import SAFEGRD
170- filehandler = SAFEGRD (measurement_file ,
225+ filehandler = SAFEGRD (request . getfixturevalue ( request . param ) ,
171226 filename_info ,
172227 filetype_info ,
173228 calibration_filehandler ,
@@ -782,8 +837,16 @@ def test_get_calibration_constant(self, calibration_filehandler):
782837 assert type (res ) is np .float32
783838
784839
785- def test_incidence_angle (annotation_filehandler ):
840+ @pytest .mark .parametrize (("annotation_file_thing" ), [
841+ lf ("annotation_file" ),
842+ lf ("zipped_annotation_file" ),
843+ lf ("upath_annotation_file" ),
844+ lf ("fs_annotation_file" )
845+ ])
846+ def test_incidence_angle (annotation_file_thing ):
786847 """Test reading the incidence angle in an annotation file."""
848+ filename_info = dict (start_time = START_TIME , end_time = END_TIME , polarization = "vv" )
849+ annotation_filehandler = SAFEXMLAnnotation (annotation_file_thing , filename_info , None )
787850 query = DataQuery (name = "incidence_angle" , polarization = "vv" )
788851 res = annotation_filehandler .get_dataset (query , {})
789852 np .testing .assert_allclose (res , 19.18318046 )
0 commit comments