File size: 1,207 Bytes
939693f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Write a FireWx-FM probability NPY to a georeferenced GeoTIFF."""

from __future__ import annotations

import argparse
from pathlib import Path

import numpy as np
import rasterio


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--reference-stack", type=Path, required=True)
    parser.add_argument("--prediction-npy", type=Path, required=True)
    parser.add_argument("--output-tif", type=Path, required=True)
    args = parser.parse_args()

    pred = np.load(args.prediction_npy).astype(np.float32)
    with rasterio.open(args.reference_stack) as src:
        if (src.height, src.width) != pred.shape:
            raise ValueError(f"reference grid {(src.height, src.width)} does not match prediction {pred.shape}")
        profile = src.profile.copy()
    profile.update(count=1, dtype="float32", nodata=0.0, compress="deflate", predictor=2)

    args.output_tif.parent.mkdir(parents=True, exist_ok=True)
    with rasterio.open(args.output_tif, "w", **profile) as dst:
        dst.write(pred.astype(np.float32), 1)
    print(f"wrote {args.output_tif}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())