ukis_pysat.file

ukis_pysat.file.env_get(key: str, boolean: bool = False) → Union[str, bool]

get an environment variable or fail with a meaningful error message

Parameters:
  • key – name of environment variable
  • boolean – bool (default: False), optional. Returns true if key in [“true”, “y”, “yes”, “1”].
Returns:

str or boolean

ukis_pysat.file.get_footprint_from_manifest(xml_path: Union[str, pathlib.Path]) → Any

Return a shapely polygon with footprint of scene, tested for Sentinel-1.

Parameters:xml_path – path to manifest.safe
Returns:shapely polygon
>>> get_footprint_from_manifest(Path(__file__).parents[1] / "tests/testfiles/manifest.safe").wkt
'POLYGON ((149.766922 -24.439564, 153.728622 -23.51771, 154.075058 -24.737713, 150.077042 -25.668921, 149.766922 -24.439564))'
ukis_pysat.file.get_ipf_from_manifest(xml_path: Union[str, pathlib.Path]) → float

Get IPF version from manifest file, tested for Sentinel-1.

Parameters:xml_path – path to manifest.safe
Returns:ipf version (float)
>>> get_ipf_from_manifest(Path(__file__).parents[1] / "tests/testfiles/manifest.safe")
2.82
ukis_pysat.file.get_origin_from_manifest(xml_path: Union[str, pathlib.Path]) → str

Get origin from manifest file, tested for Sentinel-1.

Parameters:xml_path – path to manifest.safe
Returns:country of origin
>>> get_origin_from_manifest(Path(__file__).parents[1] / "tests/testfiles/manifest.safe")
'United Kingdom'
ukis_pysat.file.get_pixel_spacing(scenedir: Union[str, pathlib.Path], polarization: str = 'HH') → Tuple[float, float]

Get pixel spacing, tested for Sentinel-1.

Parameters:
  • scenedir – path to unzipped SAFE-directory of scene
  • polarization – str (default: ‘HH’)
Returns:

tuple with pixel spacing in meters and degrees as floats

>>> get_pixel_spacing(Path(__file__).parents[1] / "tests/testfiles")
(40.0, 0.0003593261136478086)
ukis_pysat.file.get_polarization_from_s1_filename(filename: str, dual: bool = False) → str

Get polarization from the filename of a Sentinel-1 scene. https://sentinel.esa.int/web/sentinel/user-guides/sentinel-1-sar/naming-conventions.

Parameters:
  • filename – top-level SENTINEL-1 product folder name
  • dual – boolean (default: True), optional
Returns:

str

>>> get_polarization_from_s1_filename("MMM_BB_TTTR_1SDH_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip")
'HH'
>>> get_polarization_from_s1_filename("MMM_BB_TTTR_1SSH_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip")
'HH'
>>> get_polarization_from_s1_filename("MMM_BB_TTTR_2SSV_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip")
'VV'
>>> get_polarization_from_s1_filename("MMM_BB_TTTR_1SDV_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip", True)
'VV,VH'
ukis_pysat.file.get_proj_string(footprint: Any) → str

Get UTM projection string the centroid of footprint is located in. Footprint itself might cover multiple UTM zones.

Parameters:footprint – shapely polygon
Returns:string with information about projection
>>> get_proj_string(get_footprint_from_manifest(Path(__file__).parents[1] / "tests/testfiles/manifest.safe"))
'+proj=utm +zone=56J, +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
ukis_pysat.file.get_sat_ts_from_datetime(dt: datetime.datetime, dformat: str = '%Y%m%dT%H%M%S') → str

Get ESA timestamp string (used in their filenames) from datetime object.

Parameters:
  • dt – datetime.datetime object
  • dformat – : str, (default: %Y%m%dT%H%M%S)
Returns:

ESA timestamp as string

>>> get_sat_ts_from_datetime(datetime(2020, 1, 13, 7, 46, 19, tzinfo=timezone.utc)
'20200113T074619'
ukis_pysat.file.get_sentinel_scene_from_dir(indir: Union[str, pathlib.Path]) → Iterator[Tuple[pathlib.Path, str]]

Scan directory for s1 scenes, unzips them if necessary. Tested with Sentinel-1, -2 & -3.

Parameters:indir – path to zipped S1 scene or directory with S1 scene
Yields:full_path (directory with scene, str), ident (filename of scene, str)
>>> with get_sentinel_scene_from_dir(Path(__file__).parents[1] / "tests/testfiles") as (fp, name):
...     print(name)
S1M_hello_from_inside
ukis_pysat.file.get_ts_from_sentinel_filename(filename: str, start_date: bool = True, dformat: str = '%Y%m%dT%H%M%S') → datetime.datetime

Get timestamp from the filename of a Sentinel scene, according to naming conventions. Currently works for S1, S2 & S3.

Parameters:
  • filename – top-level SENTINEL product folder or file name
  • start_date – boolean (default: True), False is Stop Date, optional
  • dformat – str, (default: %Y%m%dT%H%M%S)
Returns:

datetime.datetime object with timezone information

>>> get_ts_from_sentinel_filename("S1M_BB_TTTR_LFPP_20200113T074619_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip")
datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)
>>> get_ts_from_sentinel_filename("S1M_BB_TTTR_LFPP_YYYYMMDDTHHMMSS_20200113T002219_OOOOOO_DDDDDD_CCCC.SAFE.zip", False)
datetime.datetime(2020, 1, 13, 0, 22, 19, tzinfo=datetime.timezone.utc)
>>> get_ts_from_sentinel_filename("S3M_OL_L_TTT____20200113T074619_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3")
datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)
>>> get_ts_from_sentinel_filename("S3M_OL_L_TTTTTT_yyyymmddThhmmss_20200113T074619_YYYYMMDDTHHMMSS_i_GGG_c.SEN3", False)
datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)
>>> get_ts_from_sentinel_filename("S2AM_MSIXXX_20200113T074619_Nxxyy_ROOO_Txxxxx_<Product Discriminator>.SAFE")
datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)