Coverage for pyguymer3/osterrain/findExtent.py: 4%
24 statements
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-16 08:31 +0000
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-16 08:31 +0000
1#!/usr/bin/env python3
3# Define function ...
4def findExtent(
5 fname0,
6 /,
7):
8 """Find the extent of the dataset
10 Parameters
11 ----------
12 fname0 : str
13 the path to the dataset
15 Returns
16 -------
17 minX : int
18 the left edge
19 maxX : int
20 the right edge
21 minY : int
22 the bottom edge
23 maxY : int
24 the top edge
26 Notes
27 -----
29 Copyright 2017 Thomas Guymer [1]_
31 References
32 ----------
33 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
34 """
36 # Import standard modules ...
37 import io
38 import re
39 import zipfile
41 # Import sub-functions ...
42 from .loadASCIIheader import loadASCIIheader
44 # **************************************************************************
46 # Initialize limits ...
47 maxX = -2**31
48 maxY = -2**31
49 minX = 2**31
50 minY = 2**31
52 # Compile regex to save time ...
53 pattern = re.compile(r"data/[a-z]+/[a-z]+[0-9]+_OST50GRID_[0-9]+.zip")
55 # Load dataset ...
56 with zipfile.ZipFile(fname0, "r") as fObj0:
57 # Loop over members ...
58 for fname1 in fObj0.namelist():
59 # Skip this member if it is not a sub-dataset ...
60 if pattern.match(fname1) is None:
61 continue
63 # Determine sub-dataset key ...
64 key = fname1.split("/")[-1].split("_")[0].upper()
66 # Read sub-dataset into RAM so that it becomes seekable ...
67 # NOTE: https://stackoverflow.com/a/12025492
68 zipObj = io.BytesIO(fObj0.read(fname1))
70 # Load sub-dataset ...
71 with zipfile.ZipFile(zipObj, "r") as fObj1:
72 # Read ASCII dataset into RAM so that it becomes seekable ...
73 # NOTE: https://stackoverflow.com/a/12025492
74 ascObj = io.BytesIO(fObj1.read(f"{key}.asc"))
76 # Load header of ASCII dataset ...
77 hdr = loadASCIIheader(ascObj)
79 # Increment limits ...
80 maxX = max(maxX, hdr["xllcorner"] // hdr["cellsize"] + hdr["ncols"])
81 maxY = max(maxY, hdr["yllcorner"] // hdr["cellsize"] + hdr["nrows"])
82 minX = min(minX, hdr["xllcorner"] // hdr["cellsize"])
83 minY = min(minY, hdr["yllcorner"] // hdr["cellsize"])
85 # Return answers ...
86 return minX, maxX, minY, maxY