OpticalProperty
1# coding: utf-8 2from Element import Element 3import sqlite3 4 5 6class OpticalItem(object): 7 def __init__(self, op_name="", op_value="", op_type=1, op_model_params={}): 8 self.op_name = op_name 9 self.op_value = op_value 10 self.op_type = op_type 11 self.op_model_params = op_model_params 12 13 def get_op_name(self): 14 return self.op_name 15 16 def set_op_name(self, op_name): 17 self.op_name = op_name 18 19 def get_op_value(self): 20 return self.op_value 21 22 def set_op_value(self, op_value): 23 self.op_value = op_value 24 25 def get_op_type(self): 26 return self.op_type 27 28 def set_op_type(self, op_type): 29 self.op_type = op_type 30 31 def get_op_model_params(self): 32 return self.op_model_params 33 34 def set_op_model_params(self, op_model_params): 35 self.op_model_params = op_model_params 36 37 def get_op_front_reflectance(self): 38 arr = self.op_value.split(";") 39 return list(map(lambda x: float(x), arr[0].split(","))) 40 41 def get_op_back_reflectance(self): 42 arr = self.op_value.split(";") 43 return list(map(lambda x: float(x), arr[1].split(","))) 44 45 def get_op_transmittance(self): 46 arr = self.op_value.split(";") 47 return list(map(lambda x: float(x), arr[2].split(","))) 48 49 def set_op_front_reflectance(self, ref_front: list): 50 if self.op_value == "": 51 self.op_value = ";;" 52 arr = self.op_value.split(";") 53 ref_str = ",".join(list(map(lambda x: str(x), ref_front))) 54 arr[0] = ref_str 55 self.op_value = ";".join(arr) 56 57 def set_op_back_reflectance(self, ref_front: list): 58 if self.op_value == "": 59 self.op_value = ";;" 60 arr = self.op_value.split(";") 61 ref_str = ",".join(list(map(lambda x: str(x), ref_front))) 62 arr[1] = ref_str 63 self.op_value = ";".join(arr) 64 65 def set_op_transmittance(self, transmittance: list): 66 if self.op_value == "": 67 self.op_value = ";;" 68 arr = self.op_value.split(";") 69 t_str = ",".join(list(map(lambda x: str(x), transmittance))) 70 arr[2] = t_str 71 self.op_value = ";".join(arr) 72 73class OpticalProperties(Element): 74 def __init__(self): 75 super().__init__() 76 self.optical_properties = [OpticalItem("birch_branch", "0.105,0.476;0.000,0.000;0.000,0.000", 0), 77 OpticalItem("dark_soil_mollisol", "0.188,0.351;0.000,0.000;0.000,0.000", 0), 78 OpticalItem("birch_leaf_green", "0.058,0.472;0.058,0.472;0.000,0.000", 0)] 79 80 def init_ops_from_json(self, json_object): 81 self.optical_properties.clear() 82 op_node = json_object["scene"]["optical_properties"] 83 for op_name in op_node: 84 op_type = op_node[op_name]["Type"] 85 op_value = op_node[op_name]["value"] 86 op_model_params = {} 87 if op_type == 2: 88 op_model_params = op_node[op_name]["ProspectDParams"] 89 elif op_type == 3: 90 op_model_params = op_node[op_name]["GSVSoilParams"] 91 elif op_type == 4: 92 op_model_params = op_node[op_name]["FluspectParams"] 93 op = OpticalItem(op_name, op_value, op_type, op_model_params) 94 self.optical_properties.append(op) 95 return self 96 97 def to_json_object(self, sensor_bands, less_install_root_path): 98 json_object = {} 99 for op_item in self.optical_properties: 100 json_object_item = {} 101 if op_item.op_type == 0: 102 tmparr = sensor_bands.split(",") 103 center_w_list = [] 104 bandwidth_list = [] 105 cx = sqlite3.connect( 106 less_install_root_path + "\\app\\bin\\scripts\\Lesspy\\SpectralDB\\LambertianDB.db") 107 cu = cx.cursor() 108 for tmp in tmparr: 109 center_w_list.append(float(tmp.split(":")[0])) 110 bandwidth_list.append(float(tmp.split(":")[1])) 111 rfString = [] 112 bfString = [] 113 transString = [] 114 for wi in range(0, len(center_w_list)): 115 center_w = center_w_list[wi] 116 bandwidth = bandwidth_list[wi] 117 left = center_w - 0.5 * bandwidth 118 right = center_w + 0.5 * bandwidth 119 fr = 0 120 br = 0 121 trans = 0 122 num = 0 123 cu.execute("select wavelength, front_ref,back_ref,transmittance from " + op_item.op_name + 124 " where wavelength>=%.13f and wavelength<=%.13f" % (left, right)) 125 rs_set = cu.fetchall() 126 for rs in rs_set: 127 num += 1 128 front_ref = float(rs[1]) 129 fr += front_ref 130 back_ref = float(rs[2]) 131 br += back_ref 132 transmittance = float(rs[3]) 133 trans += transmittance 134 if num != 0: 135 fr /= num 136 br /= num 137 trans /= num 138 else: 139 cu.execute( 140 "SELECT wavelength, front_ref,back_ref,transmittance FROM " + op_item.op_name + 141 " order by abs(wavelength-" + str(center_w) + ") LIMIT 2") 142 rs_set = cu.fetchall() 143 front_ref1 = float(rs_set[0][1]) 144 back_ref1 = float(rs_set[0][2]) 145 transmittance1 = float(rs_set[0][3]) 146 front_ref2 = float(rs_set[1][1]) 147 back_ref2 = float(rs_set[1][2]) 148 transmittance2 = float(rs_set[1][3]) 149 fr = 0.5 * (front_ref1 + front_ref2) 150 br = 0.5 * (back_ref1 + back_ref2) 151 trans = 0.5 * (transmittance1 + transmittance2) 152 rfString.append("%.4f" % fr) 153 bfString.append("%.4f" % br) 154 transString.append("%.4f" % trans) 155 ref = ','.join(rfString) 156 bef = ','.join(bfString) 157 trans = ','.join(transString) 158 op_item.op_value = ref + ";" + bef + ";" + trans 159 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value} 160 elif op_item.op_type == 2: 161 model_params = {"isProsect5": op_item.op_model_params['isProsect5'], 162 "Cw": op_item.op_model_params['Cw'], 163 "opName": op_item.op_model_params['opName'], 164 "Car": op_item.op_model_params['Car'], 165 "Cab": op_item.op_model_params['Cab'], 166 "Cm": op_item.op_model_params['Cm'], 167 "N": op_item.op_model_params['N'], 168 "BP": op_item.op_model_params['BP'], 169 "Anth": op_item.op_model_params['Anth']} 170 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "ProspectDParams": model_params} 171 elif op_item.op_type == 3: 172 model_params = {"c3": op_item.op_model_params['c3'], 173 "opName": op_item.op_model_params['opName'], 174 "cSM": op_item.op_model_params['cSM'], 175 "c1": op_item.op_model_params['c1'], 176 "c2": op_item.op_model_params['c2']} 177 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "GSVSoilParams": model_params} 178 elif op_item.op_type == 4: 179 model_params = {"Cca": op_item.op_model_params['Cca'], 180 "eta_2": op_item.op_model_params['eta_2'], 181 "opName": op_item.op_model_params['opName'], 182 "isFluspectPro": op_item.op_model_params['isFluspectPro'], 183 "eta_1": op_item.op_model_params['eta_1'], 184 "Cab": op_item.op_model_params['Cab'], 185 "Cbc": op_item.op_model_params['Cbc'], 186 "Cdm": op_item.op_model_params['Cdm'], 187 "Cp": op_item.op_model_params['Cp'], 188 "N": op_item.op_model_params['N'], 189 "Cs": op_item.op_model_params['Cs'], 190 "Can": op_item.op_model_params['Can'], 191 "eta": op_item.op_model_params['eta'], 192 "Cw": op_item.op_model_params['Cw'], 193 "V2Z": op_item.op_model_params['V2Z']} 194 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "FluspectParams": model_params} 195 else: 196 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value} 197 198 json_object[op_item.op_name] = json_object_item 199 return json_object 200 201 def is_op_exist(self, op_name): 202 for op_item in self.optical_properties: 203 if op_name == op_item.op_name: 204 return True 205 return False 206 207 def add_optical_item(self, op_item): 208 op_name = op_item.op_name 209 if self.is_op_exist(op_name): 210 self.remove_optical_item(op_item) 211 self.optical_properties.append(op_item) 212 213 def get_optical_item(self, op_name): 214 for op_item in self.optical_properties: 215 if op_name == op_item.op_name: 216 return op_item 217 return None 218 219 def remove_optical_item(self, op_item): 220 op_name = op_item.op_name 221 tmp = None 222 for opi in self.optical_properties: 223 if op_name == opi.op_name: 224 tmp = opi 225 if tmp is not None: 226 self.optical_properties.remove(tmp) 227 228 # # refresh op database before running the simulation 229 # def refresh_op_properties(self): 230 # for op_item in self.optical_properties: 231 # if op_item.op_type == 0: 232 # # get information from databse
class
OpticalItem:
7class OpticalItem(object): 8 def __init__(self, op_name="", op_value="", op_type=1, op_model_params={}): 9 self.op_name = op_name 10 self.op_value = op_value 11 self.op_type = op_type 12 self.op_model_params = op_model_params 13 14 def get_op_name(self): 15 return self.op_name 16 17 def set_op_name(self, op_name): 18 self.op_name = op_name 19 20 def get_op_value(self): 21 return self.op_value 22 23 def set_op_value(self, op_value): 24 self.op_value = op_value 25 26 def get_op_type(self): 27 return self.op_type 28 29 def set_op_type(self, op_type): 30 self.op_type = op_type 31 32 def get_op_model_params(self): 33 return self.op_model_params 34 35 def set_op_model_params(self, op_model_params): 36 self.op_model_params = op_model_params 37 38 def get_op_front_reflectance(self): 39 arr = self.op_value.split(";") 40 return list(map(lambda x: float(x), arr[0].split(","))) 41 42 def get_op_back_reflectance(self): 43 arr = self.op_value.split(";") 44 return list(map(lambda x: float(x), arr[1].split(","))) 45 46 def get_op_transmittance(self): 47 arr = self.op_value.split(";") 48 return list(map(lambda x: float(x), arr[2].split(","))) 49 50 def set_op_front_reflectance(self, ref_front: list): 51 if self.op_value == "": 52 self.op_value = ";;" 53 arr = self.op_value.split(";") 54 ref_str = ",".join(list(map(lambda x: str(x), ref_front))) 55 arr[0] = ref_str 56 self.op_value = ";".join(arr) 57 58 def set_op_back_reflectance(self, ref_front: list): 59 if self.op_value == "": 60 self.op_value = ";;" 61 arr = self.op_value.split(";") 62 ref_str = ",".join(list(map(lambda x: str(x), ref_front))) 63 arr[1] = ref_str 64 self.op_value = ";".join(arr) 65 66 def set_op_transmittance(self, transmittance: list): 67 if self.op_value == "": 68 self.op_value = ";;" 69 arr = self.op_value.split(";") 70 t_str = ",".join(list(map(lambda x: str(x), transmittance))) 71 arr[2] = t_str 72 self.op_value = ";".join(arr)
class
OpticalProperties(Element.Element):
74class OpticalProperties(Element): 75 def __init__(self): 76 super().__init__() 77 self.optical_properties = [OpticalItem("birch_branch", "0.105,0.476;0.000,0.000;0.000,0.000", 0), 78 OpticalItem("dark_soil_mollisol", "0.188,0.351;0.000,0.000;0.000,0.000", 0), 79 OpticalItem("birch_leaf_green", "0.058,0.472;0.058,0.472;0.000,0.000", 0)] 80 81 def init_ops_from_json(self, json_object): 82 self.optical_properties.clear() 83 op_node = json_object["scene"]["optical_properties"] 84 for op_name in op_node: 85 op_type = op_node[op_name]["Type"] 86 op_value = op_node[op_name]["value"] 87 op_model_params = {} 88 if op_type == 2: 89 op_model_params = op_node[op_name]["ProspectDParams"] 90 elif op_type == 3: 91 op_model_params = op_node[op_name]["GSVSoilParams"] 92 elif op_type == 4: 93 op_model_params = op_node[op_name]["FluspectParams"] 94 op = OpticalItem(op_name, op_value, op_type, op_model_params) 95 self.optical_properties.append(op) 96 return self 97 98 def to_json_object(self, sensor_bands, less_install_root_path): 99 json_object = {} 100 for op_item in self.optical_properties: 101 json_object_item = {} 102 if op_item.op_type == 0: 103 tmparr = sensor_bands.split(",") 104 center_w_list = [] 105 bandwidth_list = [] 106 cx = sqlite3.connect( 107 less_install_root_path + "\\app\\bin\\scripts\\Lesspy\\SpectralDB\\LambertianDB.db") 108 cu = cx.cursor() 109 for tmp in tmparr: 110 center_w_list.append(float(tmp.split(":")[0])) 111 bandwidth_list.append(float(tmp.split(":")[1])) 112 rfString = [] 113 bfString = [] 114 transString = [] 115 for wi in range(0, len(center_w_list)): 116 center_w = center_w_list[wi] 117 bandwidth = bandwidth_list[wi] 118 left = center_w - 0.5 * bandwidth 119 right = center_w + 0.5 * bandwidth 120 fr = 0 121 br = 0 122 trans = 0 123 num = 0 124 cu.execute("select wavelength, front_ref,back_ref,transmittance from " + op_item.op_name + 125 " where wavelength>=%.13f and wavelength<=%.13f" % (left, right)) 126 rs_set = cu.fetchall() 127 for rs in rs_set: 128 num += 1 129 front_ref = float(rs[1]) 130 fr += front_ref 131 back_ref = float(rs[2]) 132 br += back_ref 133 transmittance = float(rs[3]) 134 trans += transmittance 135 if num != 0: 136 fr /= num 137 br /= num 138 trans /= num 139 else: 140 cu.execute( 141 "SELECT wavelength, front_ref,back_ref,transmittance FROM " + op_item.op_name + 142 " order by abs(wavelength-" + str(center_w) + ") LIMIT 2") 143 rs_set = cu.fetchall() 144 front_ref1 = float(rs_set[0][1]) 145 back_ref1 = float(rs_set[0][2]) 146 transmittance1 = float(rs_set[0][3]) 147 front_ref2 = float(rs_set[1][1]) 148 back_ref2 = float(rs_set[1][2]) 149 transmittance2 = float(rs_set[1][3]) 150 fr = 0.5 * (front_ref1 + front_ref2) 151 br = 0.5 * (back_ref1 + back_ref2) 152 trans = 0.5 * (transmittance1 + transmittance2) 153 rfString.append("%.4f" % fr) 154 bfString.append("%.4f" % br) 155 transString.append("%.4f" % trans) 156 ref = ','.join(rfString) 157 bef = ','.join(bfString) 158 trans = ','.join(transString) 159 op_item.op_value = ref + ";" + bef + ";" + trans 160 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value} 161 elif op_item.op_type == 2: 162 model_params = {"isProsect5": op_item.op_model_params['isProsect5'], 163 "Cw": op_item.op_model_params['Cw'], 164 "opName": op_item.op_model_params['opName'], 165 "Car": op_item.op_model_params['Car'], 166 "Cab": op_item.op_model_params['Cab'], 167 "Cm": op_item.op_model_params['Cm'], 168 "N": op_item.op_model_params['N'], 169 "BP": op_item.op_model_params['BP'], 170 "Anth": op_item.op_model_params['Anth']} 171 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "ProspectDParams": model_params} 172 elif op_item.op_type == 3: 173 model_params = {"c3": op_item.op_model_params['c3'], 174 "opName": op_item.op_model_params['opName'], 175 "cSM": op_item.op_model_params['cSM'], 176 "c1": op_item.op_model_params['c1'], 177 "c2": op_item.op_model_params['c2']} 178 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "GSVSoilParams": model_params} 179 elif op_item.op_type == 4: 180 model_params = {"Cca": op_item.op_model_params['Cca'], 181 "eta_2": op_item.op_model_params['eta_2'], 182 "opName": op_item.op_model_params['opName'], 183 "isFluspectPro": op_item.op_model_params['isFluspectPro'], 184 "eta_1": op_item.op_model_params['eta_1'], 185 "Cab": op_item.op_model_params['Cab'], 186 "Cbc": op_item.op_model_params['Cbc'], 187 "Cdm": op_item.op_model_params['Cdm'], 188 "Cp": op_item.op_model_params['Cp'], 189 "N": op_item.op_model_params['N'], 190 "Cs": op_item.op_model_params['Cs'], 191 "Can": op_item.op_model_params['Can'], 192 "eta": op_item.op_model_params['eta'], 193 "Cw": op_item.op_model_params['Cw'], 194 "V2Z": op_item.op_model_params['V2Z']} 195 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "FluspectParams": model_params} 196 else: 197 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value} 198 199 json_object[op_item.op_name] = json_object_item 200 return json_object 201 202 def is_op_exist(self, op_name): 203 for op_item in self.optical_properties: 204 if op_name == op_item.op_name: 205 return True 206 return False 207 208 def add_optical_item(self, op_item): 209 op_name = op_item.op_name 210 if self.is_op_exist(op_name): 211 self.remove_optical_item(op_item) 212 self.optical_properties.append(op_item) 213 214 def get_optical_item(self, op_name): 215 for op_item in self.optical_properties: 216 if op_name == op_item.op_name: 217 return op_item 218 return None 219 220 def remove_optical_item(self, op_item): 221 op_name = op_item.op_name 222 tmp = None 223 for opi in self.optical_properties: 224 if op_name == opi.op_name: 225 tmp = opi 226 if tmp is not None: 227 self.optical_properties.remove(tmp) 228 229 # # refresh op database before running the simulation 230 # def refresh_op_properties(self): 231 # for op_item in self.optical_properties: 232 # if op_item.op_type == 0: 233 # # get information from databse
def
init_ops_from_json(self, json_object):
81 def init_ops_from_json(self, json_object): 82 self.optical_properties.clear() 83 op_node = json_object["scene"]["optical_properties"] 84 for op_name in op_node: 85 op_type = op_node[op_name]["Type"] 86 op_value = op_node[op_name]["value"] 87 op_model_params = {} 88 if op_type == 2: 89 op_model_params = op_node[op_name]["ProspectDParams"] 90 elif op_type == 3: 91 op_model_params = op_node[op_name]["GSVSoilParams"] 92 elif op_type == 4: 93 op_model_params = op_node[op_name]["FluspectParams"] 94 op = OpticalItem(op_name, op_value, op_type, op_model_params) 95 self.optical_properties.append(op) 96 return self
def
to_json_object(self, sensor_bands, less_install_root_path):
98 def to_json_object(self, sensor_bands, less_install_root_path): 99 json_object = {} 100 for op_item in self.optical_properties: 101 json_object_item = {} 102 if op_item.op_type == 0: 103 tmparr = sensor_bands.split(",") 104 center_w_list = [] 105 bandwidth_list = [] 106 cx = sqlite3.connect( 107 less_install_root_path + "\\app\\bin\\scripts\\Lesspy\\SpectralDB\\LambertianDB.db") 108 cu = cx.cursor() 109 for tmp in tmparr: 110 center_w_list.append(float(tmp.split(":")[0])) 111 bandwidth_list.append(float(tmp.split(":")[1])) 112 rfString = [] 113 bfString = [] 114 transString = [] 115 for wi in range(0, len(center_w_list)): 116 center_w = center_w_list[wi] 117 bandwidth = bandwidth_list[wi] 118 left = center_w - 0.5 * bandwidth 119 right = center_w + 0.5 * bandwidth 120 fr = 0 121 br = 0 122 trans = 0 123 num = 0 124 cu.execute("select wavelength, front_ref,back_ref,transmittance from " + op_item.op_name + 125 " where wavelength>=%.13f and wavelength<=%.13f" % (left, right)) 126 rs_set = cu.fetchall() 127 for rs in rs_set: 128 num += 1 129 front_ref = float(rs[1]) 130 fr += front_ref 131 back_ref = float(rs[2]) 132 br += back_ref 133 transmittance = float(rs[3]) 134 trans += transmittance 135 if num != 0: 136 fr /= num 137 br /= num 138 trans /= num 139 else: 140 cu.execute( 141 "SELECT wavelength, front_ref,back_ref,transmittance FROM " + op_item.op_name + 142 " order by abs(wavelength-" + str(center_w) + ") LIMIT 2") 143 rs_set = cu.fetchall() 144 front_ref1 = float(rs_set[0][1]) 145 back_ref1 = float(rs_set[0][2]) 146 transmittance1 = float(rs_set[0][3]) 147 front_ref2 = float(rs_set[1][1]) 148 back_ref2 = float(rs_set[1][2]) 149 transmittance2 = float(rs_set[1][3]) 150 fr = 0.5 * (front_ref1 + front_ref2) 151 br = 0.5 * (back_ref1 + back_ref2) 152 trans = 0.5 * (transmittance1 + transmittance2) 153 rfString.append("%.4f" % fr) 154 bfString.append("%.4f" % br) 155 transString.append("%.4f" % trans) 156 ref = ','.join(rfString) 157 bef = ','.join(bfString) 158 trans = ','.join(transString) 159 op_item.op_value = ref + ";" + bef + ";" + trans 160 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value} 161 elif op_item.op_type == 2: 162 model_params = {"isProsect5": op_item.op_model_params['isProsect5'], 163 "Cw": op_item.op_model_params['Cw'], 164 "opName": op_item.op_model_params['opName'], 165 "Car": op_item.op_model_params['Car'], 166 "Cab": op_item.op_model_params['Cab'], 167 "Cm": op_item.op_model_params['Cm'], 168 "N": op_item.op_model_params['N'], 169 "BP": op_item.op_model_params['BP'], 170 "Anth": op_item.op_model_params['Anth']} 171 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "ProspectDParams": model_params} 172 elif op_item.op_type == 3: 173 model_params = {"c3": op_item.op_model_params['c3'], 174 "opName": op_item.op_model_params['opName'], 175 "cSM": op_item.op_model_params['cSM'], 176 "c1": op_item.op_model_params['c1'], 177 "c2": op_item.op_model_params['c2']} 178 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "GSVSoilParams": model_params} 179 elif op_item.op_type == 4: 180 model_params = {"Cca": op_item.op_model_params['Cca'], 181 "eta_2": op_item.op_model_params['eta_2'], 182 "opName": op_item.op_model_params['opName'], 183 "isFluspectPro": op_item.op_model_params['isFluspectPro'], 184 "eta_1": op_item.op_model_params['eta_1'], 185 "Cab": op_item.op_model_params['Cab'], 186 "Cbc": op_item.op_model_params['Cbc'], 187 "Cdm": op_item.op_model_params['Cdm'], 188 "Cp": op_item.op_model_params['Cp'], 189 "N": op_item.op_model_params['N'], 190 "Cs": op_item.op_model_params['Cs'], 191 "Can": op_item.op_model_params['Can'], 192 "eta": op_item.op_model_params['eta'], 193 "Cw": op_item.op_model_params['Cw'], 194 "V2Z": op_item.op_model_params['V2Z']} 195 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value, "FluspectParams": model_params} 196 else: 197 json_object_item = {"Type": op_item.op_type, "value": op_item.op_value} 198 199 json_object[op_item.op_name] = json_object_item 200 return json_object
Inherited Members
- Element.Element
- set_sim
- get_sim