1 from typing
import Dict, List
6 if __name__ ==
"__main__":
8 corr_map: Dict[str, str] = {
"sst2b":
"SST",
"sst3b":
"SST3",
"sst2bmsst3b":
"d_SST_SST3",
"dm3750":
"BT37_MAXMIN",
9 "m3750":
"BT37",
"sd.sst":
"SST_STD",
10 "dm8550":
"BT85_MAXMIN",
"dm11000":
"BT11",
"dm12000":
"BT12",
11 "dm3750m12000":
"d_BT37_BT12",
12 "dm3750m11000":
"d_BT37_BT11",
"dm11000m12000":
"d_BT11_BT12",
"m8550":
"BT85",
13 "lat":
"lat",
"lon":
"lon",
"anc.wv":
"wv",
"sd.m3750":
"BT37_STD",
14 "sd.m11000":
"BT11_STD",
"sd.m12000":
"SST_STD",
"Tdeflong":
"Tdeflong",
15 "maxm8550":
"BT85_MAX",
"minm8550":
"BT85_MIN",
"m.rho1380":
"RHOCIRRUS",
16 "dm.rho678":
"RHOred_MAXMIN",
"m.rho678":
"RHOred",
" minm.rho678":
"RHOred_MIN",
17 "maxm.rho678":
"RHOred_MAX",
"maxm.rho748":
"RHO07_MAX",
"minm.rho748":
"RHO07_MIN",
18 "minm.rho1610":
"RHO16_MIN",
"maxm.rho1610":
"RHO16_MAX",
19 "minm.rho1380":
"RHOCIRRUS_MIN",
"maxm.rho1380":
"RHOCIRRUS_MAX",
"m.rho1610":
"RHO16",
20 "dm.rho1610":
"RHO16_MAXMIN",
21 "dm.rho1380":
"RHOCIRRUS_MAXMIN",
"m.rho748":
"RHO07",
"maxm.rho.1380":
"RHOCIRRUS_MAX"}
55 for i, child
in enumerate(input_tree.children):
56 if child.testname
in names:
57 names[child.testname].append(i)
59 names[child.testname] = [i]
60 new_children: List[Tree] = []
61 for name, indexes
in names.items():
62 new_children.append(
Tree())
63 new_children[-1].testname = name
64 new_children[-1].children = [input_tree.children[indexes[0]], input_tree.children[indexes[1]]]
65 new_children[-1].threshold = input_tree.children[indexes[0]].threshold
66 input_tree.children[indexes[0]].testname =
"true"
67 input_tree.children[indexes[1]].testname =
"false"
68 input_tree.children[indexes[0]].threshold =
None
69 input_tree.children[indexes[1]].threshold =
None
70 input_tree.children = new_children
71 for child
in new_children:
72 for dec
in child.children:
78 pat_len = len(pattern)
81 while conc == line[:count * pat_len]:
91 if (i > len(lines) - 2):
96 while j < len(lines) - 1:
97 next_line = lines[j + 1]
99 if next_order > current_order:
100 tree.children.append(
Tree())
101 test_name = next_line[next_order * len(pattern):].split()[0]
102 test_order = next_line[next_order * len(pattern):].split()[1]
103 test_threshold =
float(next_line[next_order * len(pattern):].split()[2][:-1])
104 test_treesum =
float(next_line[next_order * len(pattern):].split()[3])
108 tree.children[-1].testname = test_name
109 tree.children[-1].threshold = test_threshold
110 tree.children[-1].addTreesum = test_treesum
111 tree.children[-1].node_type =
"threshold_max" if test_order ==
"<" else "threshold_min"
120 index = inp_str.find(
")")
121 return inp_str[index + 1:]
127 for _
in range(len(tree.children)):
129 if tree.testname ==
"decision_tree":
130 test_name =
"decision_tree"
132 json[test_name] = {
"children": arr}
133 elif tree.testname !=
"true" and tree.testname !=
"false":
134 thresholds = [{
"threshold_max": tree.threshold}]
136 if test_name_or
in corr_map:
137 test_name = corr_map[test_name_or]
139 print(f
"{test_name_or} NOT FOUND")
140 raise SystemExit(
'Wrong Keyword')
141 json[test_name] = {
"thresholds": thresholds,
"children": arr}
143 test_name = tree.testname
145 json[test_name] = {
"addTreesum": tree.addTreesum,
"children": arr}
147 json[test_name] = {
"addTreesum": tree.addTreesum}
148 for i, child
in enumerate(tree.children):
157 with open(path_inp_file,
"r")
as file:
158 lines = file.readlines()
167 adt.testname =
"decision_tree"
175 with open(out_path,
"w")
as outfile:
176 json.dump(jsonf, outfile, indent=4)
180 ocdata = os.environ.get(
'OCDATAROOT')
182 raise SystemExit(
'OCDATAROOT not found')
183 path_no_glint = os.path.join(ocdata,
"viirs",
"j1",
"cal",
"NOAA_20_ADtree_no_glint_R2022.0_results.txt")
184 path_glint = os.path.join(ocdata,
"viirs",
"j1",
"cal",
"NOAA_20_ADtree_mod_glint_R2022.0_results.txt")
185 path_night = os.path.join(ocdata,
"viirs",
"j1",
"cal",
"NOAA_20_ADtree_night_R2022.0_results.txt")
188 adt_night = {
"version":
"v6.R2022"}
190 adt_night[
"decision_tree"] = tree_night[
"decision_tree"]
191 adt_full = {
"version":
"v6.R2022",
"decision_tree": {
"children": [
192 {
"solz": {
"thresholds": [{
"threshold_min": 85.0}],
193 "children": [{
"true": tree_night[
"decision_tree"]}, {
"false": {
"children": [
194 {
"glintcoef": {
"thresholds": [{
"threshold_max": 0.005}],
195 "children": [{
"true": tree_no_glint[
"decision_tree"]},
196 {
"false": tree_glint[
"decision_tree"]}]}}]}}]}}]}}
198 with open(
"cloud_mask_sst.json",
"w")
as outfile:
199 json.dump(adt_full, outfile, indent=2)
200 with open(
"cloud_mask_sst3.json",
"w")
as outfile:
201 json.dump(adt_night, outfile, indent=4)