Introduction
Each of the main four wrapper functions are built off individual functions, which can be found here.
Each of the files are written as so: def_[wrapper function its used within][descriptor][descriptor]
Descriptions of the functions can be found on the right hand side table of contents.
def_open_qf_path
Validate a measurement path and locate its quality raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measurement_path
|
str or Path
|
Measurement raster that must already exist on disk. |
required |
quality_path
|
str or Path
|
Explicit quality raster. When |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
measurement |
Path
|
The validated measurement raster. |
quality |
Path
|
The quality raster to pair with it. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the measurement raster does not exist, or if no quality raster could be found or the one given is missing. |
Examples:
Both paths are checked on disk, so this is shown rather than executed:
>>> resolve_quality_path("KONA/KONA_fluxCo2_20260101.tif")
(PosixPath('KONA/KONA_fluxCo2_20260101.tif'),
PosixPath('KONA/KONA_fluxCo2_20260101_qf.tif'))
Source code in src/fluxmappy/def_open_qf_path.py
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 | |
def_filt_rast_pair
Recursively find and pair measurement and quality TIFFs.
Walks data_dir, parses every filename, and matches each
measurement raster to the quality raster sharing its site, flux type
and date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dir
|
Path
|
Directory to search, including subdirectories. |
required |
flux_type
|
str
|
Flux type to keep, e.g. |
required |
Returns:
| Type | Description |
|---|---|
list of RasterPair
|
One entry per matched measurement/quality pair, ordered by date. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If no measurement rasters of |
Examples:
Requires rasters on disk, so this is shown rather than executed:
>>> from pathlib import Path
>>> pairs = find_raster_pairs(Path("KONA"), "fluxCo2")
>>> len(pairs)
12
Source code in src/fluxmappy/def_filt_rast_pair.py
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 | |
def_filt_rast_name
Parse site, flux type, date and quality suffix from a filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Raster path whose filename follows
|
required |
Returns:
| Type | Description |
|---|---|
RasterRecord or None
|
The parsed record, or |
Examples:
>>> from pathlib import Path
>>> record = parse_raster_path(Path("KONA_fluxCo2_20260101.tif"))
>>> record.site, record.flux_type, record.date
('KONA', 'fluxCo2', '20260101')
>>> record.is_quality
False
A _qf suffix marks the quality-flag raster of the same day:
>>> parse_raster_path(Path("KONA_fluxCo2_20260101_qf.tif")).is_quality
True
Names that do not match return None rather than raising:
>>> parse_raster_path(Path("notes.txt")) is None
True
Source code in src/fluxmappy/def_filt_rast_name.py
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 | |
def_filt_qf_mask
Return True where a measurement pixel passes the quality rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quality_data
|
MaskedArray
|
Quality-flag bands, shaped either like |
required |
measurement_shape
|
tuple of int
|
Shape |
required |
qf_threshold
|
float
|
Quality-flag threshold, on the 0-5 FluxMap scale. |
required |
qf_keep
|
(lte, gte)
|
Which side of the threshold passes. |
"lte"
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Boolean array of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> quality = np.ma.array([[[0, 1], [2, 3]]])
>>> quality_pass_mask(quality, (1, 2, 2), qf_threshold=1, qf_keep="lte")
array([[[ True, True],
[False, False]]])
Keeping the other side of the same threshold inverts the result:
>>> quality_pass_mask(quality, (1, 2, 2), qf_threshold=1, qf_keep="gte")
array([[[False, True],
[ True, True]]])
A single quality band broadcasts across every measurement band:
>>> quality_pass_mask(quality, (48, 2, 2), qf_threshold=1, qf_keep="lte").shape
(48, 2, 2)
>>> quality_pass_mask(quality, (1, 2, 2), qf_threshold=1, qf_keep="eq")
Traceback (most recent call last):
...
ValueError: qf_keep must be either 'lte' or 'gte'
Source code in src/fluxmappy/def_filt_qf_mask.py
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 | |
def_filt_pair
Read and quality-filter one raster pair entirely in memory.
Opens both rasters, checks they share a grid, and masks every measurement pixel whose quality flag fails the rule. Nothing is written to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pair
|
RasterPair
|
Measurement and quality rasters for a single day. |
required |
qf_threshold
|
float
|
Quality-flag threshold, on the 0-5 FluxMap scale. |
1
|
qf_keep
|
(lte, gte)
|
Which side of the threshold to keep. |
"lte"
|
Returns:
| Type | Description |
|---|---|
FilteredFluxRaster
|
Carrying the masked bands, the originating |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the measurement and quality rasters do not share a grid, or if the quality band count is neither 1 nor equal to the measurement band count. |
Notes
A measurement raster that does not hold 48 bands prints a warning to stdout and is filtered anyway.
Examples:
Requires rasters on disk, so this is shown rather than executed:
>>> filtered = filter_raster_pair(pair, qf_threshold=1)
>>> filtered.filtered_bands.shape
(48, 64, 64)
Source code in src/fluxmappy/def_filt_pair.py
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 | |
def_filt_grid_vali
Require measurement and quality rasters to share a spatial grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measurement_path
|
Path
|
Path to the measurement raster, used in the error message. |
required |
measurement_profile
|
dict
|
Rasterio profile of the measurement raster. |
required |
quality_path
|
Path
|
Path to the quality raster, used in the error message. |
required |
quality_profile
|
dict
|
Rasterio profile of the quality raster. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Returns nothing when the grids match. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> from pathlib import Path
>>> grid = {"width": 64, "height": 64, "transform": None, "crs": "EPSG:4326"}
>>> assert_same_grid(Path("a.tif"), grid, Path("a_qf.tif"), dict(grid)) is None
True
>>> other = dict(grid, width=32)
>>> assert_same_grid(Path("a.tif"), grid, Path("a_qf.tif"), other)
Traceback (most recent call last):
...
ValueError: a_qf.tif does not match a.tif for: width
Source code in src/fluxmappy/def_filt_grid_vali.py
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 | |
def_diu_tz_vali
Return an IANA timezone, or raise a clear configuration error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
IANA timezone name, such as |
required |
parameter
|
str
|
Name of the calling argument, used to make the error message point at the right place. |
required |
Returns:
| Type | Description |
|---|---|
ZoneInfo
|
The resolved timezone. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> resolve_timezone("America/Chicago", "out_tz")
zoneinfo.ZoneInfo(key='America/Chicago')
>>> resolve_timezone("Mars/Olympus", "out_tz")
Traceback (most recent call last):
...
ValueError: Unknown out_tz 'Mars/Olympus'; use an IANA timezone name such as 'UTC', 'Europe/Madrid', or 'America/Chicago'
Source code in src/fluxmappy/def_diu_tz_vali.py
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 | |
def_diu_lt
Return the UTC half-hour instants making up one full local day.
Used to decide whether a local day is fully covered by the source rasters once a timezone shift has been applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_date
|
str
|
Local date in |
required |
target_timezone
|
ZoneInfo
|
Timezone the local day is measured in. |
required |
Returns:
| Type | Description |
|---|---|
set of datetime.datetime
|
Every half-hour instant of that local day, in UTC. Normally 48 instants, but 46 or 50 across a daylight-saving transition. |
Examples:
>>> from zoneinfo import ZoneInfo
>>> len(expected_local_day_instants("20260101", ZoneInfo("UTC")))
48
A spring-forward day is an hour shorter, so two slots do not exist:
>>> len(expected_local_day_instants("20260308", ZoneInfo("America/Chicago")))
46
A fall-back day gains an hour:
>>> len(expected_local_day_instants("20261101", ZoneInfo("America/Chicago")))
50
Source code in src/fluxmappy/def_diu_lt.py
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 | |
Regroup daily cycles from source dates into local dates.
Each band is timestamped from its source date and timezone, converted
into output_timezone, and filed under the local date and
half-hour slot it lands in. Slots receiving more than one value, as
happens on a fall-back day, are averaged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dated_cycles
|
list of (str, numpy.ndarray)
|
Source date in |
required |
input_timezone
|
ZoneInfo
|
Timezone in which band 1 of each source day is midnight. |
required |
output_timezone
|
ZoneInfo
|
Timezone to regroup into. |
required |
incomplete_local_days
|
(drop, include, error)
|
What to do with local days the source data only partly covers. |
"drop"
|
Returns:
| Name | Type | Description |
|---|---|---|
dates |
tuple of str
|
Local dates retained, in ascending order. |
daily_cycles |
ndarray
|
Array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> from zoneinfo import ZoneInfo
>>> cycles = [(d, np.arange(48, dtype=float))
... for d in ("20260101", "20260102", "20260103")]
>>> dates, values = localize_daily_cycles(
... cycles, ZoneInfo("UTC"), ZoneInfo("America/Chicago"), "drop"
... )
>>> values.shape
(2, 48)
>>> dates
('20260101', '20260102')
Source code in src/fluxmappy/def_diu_lt.py
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
def_diu_coll
Collect 48 spatial means for every region and source date.
Runs the statistics pass over every raster and files each day's 48
half-hourly means under its (feature_id, site, flux_type) group,
ready for averaging into a diurnal cycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtered_rasters
|
iterable of FilteredFluxRaster
|
Quality-filtered rasters, one per day. |
required |
vector_path
|
Path or None
|
Polygon vector file to summarise within, or |
required |
id_field
|
str or None
|
Vector attribute identifying each polygon, or |
required |
all_touched
|
bool
|
Include every pixel touched by a polygon, not only those whose centre falls inside it. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Keyed by |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any raster does not yield exactly 48 half-hourly means, or if no statistics were available at all. |
Examples:
>>> rasters = [_example_raster(d) for d in ("20260101", "20260102")]
>>> grouped = collect_dated_cycles(rasters, None, None, False)
>>> list(grouped)
[('whole_raster', 'KONA', 'fluxCo2')]
>>> len(grouped[('whole_raster', 'KONA', 'fluxCo2')]["dated_cycles"])
2
Source code in src/fluxmappy/def_diu_coll.py
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 | |
def_diu_calc
Summarise stacked daily cycles into mean, spread, counts and totals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
daily_cycles
|
ndarray
|
Array of shape |
required |
timestep_seconds
|
float or None
|
Seconds represented by each slot, used to scale the cumulative
cycle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mean_cycle |
ndarray
|
Per-slot mean across days, shape |
standard_deviation |
ndarray
|
Per-slot standard deviation across days, not across pixels. |
valid_day_count |
ndarray
|
Number of finite observations contributing to each slot. |
cumulative_cycle |
ndarray
|
Running total of |
Examples:
>>> import numpy as np
>>> days = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> mean, sd, n, cum = summarize_daily_cycles(days, 1800.0)
>>> mean
array([2., 3.])
>>> sd
array([1., 1.])
>>> n
array([2, 2])
>>> cum
array([3600., 9000.])
A day missing one slot is skipped for that slot only:
>>> days = np.array([[1.0, np.nan], [3.0, 4.0]])
>>> mean, sd, n, cum = summarize_daily_cycles(days, None)
>>> mean
array([2., 4.])
>>> n
array([2, 1])
Source code in src/fluxmappy/def_diu_calc.py
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 | |
def_stat_vect
Read polygon features, preserving a unique ID for each.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector_path
|
Path
|
Vector file to read, in any format Fiona can open. |
required |
id_field
|
str
|
Attribute to use as each feature's identifier. When |
None
|
Returns:
| Type | Description |
|---|---|
VectorLayer
|
Carrying the layer |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If the file holds no features, if |
Examples:
Requires a vector file on disk, so this is shown rather than executed:
>>> from pathlib import Path
>>> layer = read_vector_layer(Path("sites.shp"), id_field="name")
>>> len(layer.features)
3
Source code in src/fluxmappy/def_stat_vect.py
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 | |
def_stat_mask
Yield one independently masked raster per vector feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtered_raster
|
FilteredFluxRaster
|
Raster to clip, which must carry |
required |
vector_layer
|
VectorLayer
|
Polygons to clip to, reprojected to the raster CRS as needed. |
required |
all_touched
|
bool
|
Include every pixel touched by a polygon, not only those whose centre falls inside it. |
False
|
Yields:
| Type | Description |
|---|---|
GeofencedRaster
|
One per polygon that overlaps the raster. Polygons with no overlapping pixels are skipped. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the raster lacks the spatial metadata needed to apply a vector. |
Source code in src/fluxmappy/def_stat_mask.py
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 | |
Yield polygon regions, or one whole region per raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtered_rasters
|
iterable of FilteredFluxRaster
|
Rasters to geofence. |
required |
vector_path
|
Path
|
Polygon vector file. When |
None
|
id_field
|
str
|
Vector attribute identifying each polygon. |
None
|
all_touched
|
bool
|
Include every pixel touched by a polygon. |
False
|
Yields:
| Type | Description |
|---|---|
GeofencedRaster
|
One per raster and polygon, or one per raster when no vector is given. |
Examples:
With no vector, each raster passes through as a single whole region:
>>> rasters = [_example_raster("20260101")]
>>> regions = list(iter_geofenced_rasters(rasters))
>>> len(regions)
1
>>> regions[0].feature_id
'whole_raster'
>>> regions[0].masked_bands.shape
(48, 1, 1)
Source code in src/fluxmappy/def_stat_mask.py
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 150 151 152 153 154 | |
def_stat_calc
Calculate spatial statistics for one masked or whole raster.
Reduces a region's pixels to summary values, either per half-hourly band or collapsed to a single daily layer first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
GeofencedRaster
|
Raster already masked to a polygon, or kept whole. |
required |
product
|
(half - hourly, daily)
|
Temporal resolution. |
"half-hourly"
|
Returns:
| Type | Description |
|---|---|
PolygonStatistics
|
Holding |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
standard_deviation is computed across the spatial axes, so it
describes variation between pixels within the region at each
timestep.
Examples:
A 2x2 region with two half-hourly bands:
>>> region = _example_region()
>>> stats = calculate_region_statistics(region, product="half-hourly")
>>> stats.mean
array([2.5, 6.5])
>>> stats.minimum
array([1., 5.])
>>> stats.count
array([4, 4])
Collapsing the bands to one daily layer instead:
>>> daily = calculate_region_statistics(region, product="daily")
>>> float(daily.mean)
4.5
>>> daily.product
'daily'
Source code in src/fluxmappy/def_stat_calc.py
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 | |
def_stat_iter
Yield spatial statistics one region at a time.
The streaming form behind :func:~fluxmappy.wrap_stat, which collects
the results into a list. Unlike wrap_stat, vector must already
be a :class:~pathlib.Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtered_rasters
|
iterable of FilteredFluxRaster
|
Quality-filtered rasters, typically the output of
:func: |
required |
vector
|
Path
|
Polygon vector file to summarise within. When |
None
|
product
|
(half - hourly, daily)
|
Temporal resolution of the statistics. |
"half-hourly"
|
id_field
|
str
|
Vector attribute identifying each polygon. When |
None
|
all_touched
|
bool
|
Include every pixel touched by a polygon, not only those whose centre falls inside it. |
False
|
Yields:
| Type | Description |
|---|---|
PolygonStatistics
|
One entry per raster and region. |
See Also
wrap_stat : Collect the same statistics into a list.
Examples:
>>> from fluxmappy.wrap_stat import _example_raster
>>> rasters = [_example_raster(d) for d in ("20260101", "20260102")]
>>> stats = list(iter_polygon_statistics(rasters, product="daily"))
>>> len(stats)
2
>>> float(stats[0].mean)
23.5
Source code in src/fluxmappy/def_stat_iter.py
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 | |