-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrast-hist
executable file
·58 lines (49 loc) · 1.17 KB
/
rast-hist
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# Author: Daniel Rode
# Created: 01 Nov 2024
# Updated: -
import sys
from sys import exit
import rasterio
import numpy as np
import seaborn as sns
DPI = 300
BINS = 100
YLOG = False
TITLE = "Frequency Histogram"
HELP_TEXT = "Usage: this.py SRC_RAST OUT_PLOT_PNG"
# Parse command line arguments
pos_args = []
args = iter(sys.argv[1:])
for a in args:
if not a.startswith('-'):
pos_args.append(a)
continue
match a:
case '-y' | '--y-axis-log-scale':
YLOG = True
case '-b' | '--bins':
BINS = int(next(args))
case '-d' | '--dpi':
DPI = int(next(args))
case '-t' | '--title':
TITLE = next(args)
case _:
print("error: Invalid flag", a)
exit(1)
try:
IN_PATH, OUT_PATH = pos_args
except ValueError:
print(HELP_TEXT)
exit(1)
# Get values to plot from raster
with rasterio.open(IN_PATH, "r") as f:
nums = f.read()
nums = nums[0] # Choose first layer
nums = nums[~np.isnan(nums)] # Drop NA
# Plot and save histograms
plt = sns.histplot(data=nums, bins=BINS, legend=False)
plt.set_title(TITLE)
if YLOG:
plt.axes.set_yscale('log')
plt.get_figure().savefig(OUT_PATH, bbox_inches='tight', dpi=DPI)