-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_uncertainty_data.py
145 lines (115 loc) · 4.71 KB
/
gather_uncertainty_data.py
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
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
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
"""Gather data to characterize uncertainty within each tile including cloud persistence, bowtie trim, no decision, etc."""
import argparse
import logging
import os
from dask.distributed import Client
from config import SNOW_YEAR, preprocessed_dir, uncertainty_dir
from luts import inv_cgf_codes
from shared_utils import (
open_preprocessed_dataset,
fetch_raster_profile,
write_tagged_geotiff,
)
def count_no_decision_occurence(ds_chunked):
"""Count the per-pixel occurrence of "no decision" in the snow year.
Args:
ds_chunked (xarray.Dataset): The chunked dataset.
Returns:
xarray.DataArray: count of "No decision" values".
"""
logging.info(f"Counting occurence of `No decision` values...")
no_decision_count = (ds_chunked == inv_cgf_codes["No decision"]).sum(dim="time")
return no_decision_count
def count_missing_l1b_occurence(ds_chunked):
"""Count the per-pixel occurrence of "Missing L1B data" in the snow year.
Args:
ds_chunked (xarray.Dataset): The chunked dataset.
Returns:
xarray.DataArray: count of "Missing L1B data" values".
"""
logging.info(f"Counting occurence of `Missing L1B data` values...")
missing_l1b_count = (ds_chunked == inv_cgf_codes["Missing L1B data"]).sum(
dim="time"
)
return missing_l1b_count
def count_l1b_calibration_fail(ds_chunked):
"""Count the per-pixel occurrence of "L1B data failed calibration" in the snow year.
Args:
ds_chunked (xarray.Dataset): The chunked dataset.
Returns:
xarray.DataArray: count of "L1B data failed calibration" values".
"""
logging.info(f"Counting occurence of `L1B data failed calibration` values...")
l1b_fail_count = (ds_chunked == inv_cgf_codes["L1B data failed calibration"]).sum(
dim="time"
)
return l1b_fail_count
def count_bowtie_trim(ds_chunked):
"""Count the per-pixel occurrence of "Onboard VIIRS bowtie trim" in the snow year.
Args:
ds_chunked (xarray.Dataset): The chunked dataset.
Returns:
xarray.DataArray: count of "Onboard VIIRS bowtie trim" values".
"""
logging.info(f"Counting occurence of `Onboard VIIRS bowtie trim` values...")
bowtie_trim_count = (ds_chunked == inv_cgf_codes["Onboard VIIRS bowtie trim"]).sum(
dim="time"
)
return bowtie_trim_count
def get_max_cloud_persistence(ds_chunked):
"""Determine maximum per-pixel cloud persistence value.
Args:
ds_chunked (xarray.Dataset): The chunked dataset of cloud persistence.
Returns:
xarray.DataArray: max cloud persistence value".
"""
logging.info(f"Finding maximum cloud persistence value...")
max_cloud_persist = ds_chunked.max(dim="time")
return max_cloud_persist
if __name__ == "__main__":
log_file_path = os.path.join(os.path.expanduser("~"), "source_data_uncertainty.log")
logging.basicConfig(filename=log_file_path, level=logging.INFO)
parser = argparse.ArgumentParser(
description="Script to Fetch Data For Uncertainty Analysis"
)
parser.add_argument("tile_id", type=str, help="VIIRS Tile ID (ex. h11v02)")
args = parser.parse_args()
tile_id = args.tile_id
logging.info(
f"Gathering uncertainty data for tile {tile_id} for snow year {SNOW_YEAR}."
)
client = Client()
uncertainty_data = dict()
fp = preprocessed_dir / f"snow_year_{SNOW_YEAR}_{tile_id}.nc"
cgf_snow_ds = open_preprocessed_dataset(
fp, {"x": "auto", "y": "auto"}, "CGF_NDSI_Snow_Cover"
)
uncertainty_data.update({"no decision": count_no_decision_occurence(cgf_snow_ds)})
uncertainty_data.update({"missing L1B": count_missing_l1b_occurence(cgf_snow_ds)})
uncertainty_data.update({"L1B fail": count_l1b_calibration_fail(cgf_snow_ds)})
uncertainty_data.update({"bowtie trim": count_bowtie_trim(cgf_snow_ds)})
cgf_snow_ds.close()
cloud_ds = open_preprocessed_dataset(
fp, {"x": "auto", "y": "auto"}, "Cloud_Persistence"
)
uncertainty_data.update(
{"max_cloud_persistence": get_max_cloud_persistence(cgf_snow_ds)}
)
cloud_ds.close()
out_profile = fetch_raster_profile(tile_id, {"dtype": "int16", "nodata": 0})
for uncertainty_name, uncertainty_array in uncertainty_data.items():
if uncertainty_array.sum().compute() == 0:
logging.info(
f"No occurences found for {uncertainty_name}. A GeoTIFF will not be written."
)
continue
write_tagged_geotiff(
uncertainty_dir,
tile_id,
"",
uncertainty_name,
out_profile,
uncertainty_array.compute().values.astype("int16"),
)
client.close()
print("Uncertainty Data Fetch Complete.")