Changeset 4793 for palm/trunk/SCRIPTS/palm_csd_files
- Timestamp:
- Nov 24, 2020 9:48:21 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
palm/trunk/SCRIPTS/palm_csd_files/palm_csd_canopy_generator.py
r4481 r4793 20 20 # Current revisions: 21 21 # ----------------- 22 # 23 # 22 # 23 # 24 24 # Former revisions: 25 25 # ----------------- 26 26 # $Id: palm_csd_canopy_generator.py 3773 2019-03-01 08:56:57Z maronga $ 27 # Bugfix: do not allow BAD < 0, revised treatment of tree trunks 28 # 29 # 3773 2019-03-01 08:56:57Z maronga 27 30 # Added support for tree shape 28 31 # … … 46 49 import math 47 50 import scipy.integrate as integrate 51 import scipy.ndimage as nd 48 52 49 53 def generate_single_tree_lad(x,y,dz,max_tree_height,max_patch_height,tree_type,tree_height,tree_dia,trunk_dia,tree_lai,season,fill): … … 64 68 ids = np.ones((len(zlad),len(y),len(x))) 65 69 ids[:,:,:] = fill 66 70 71 types = np.ones((len(zlad),len(y),len(x))) 72 types[:,:,:] = np.byte(-127) 67 73 68 74 # Calculating the number of trees in the arrays and a boolean array storing the location of trees which is used for convenience in the following loop environment … … 107 113 bad[0:lad_loc_nz,ind_l_y:ind_r_y+1,ind_l_x:ind_r_x+1] = np.where(bad_loc[0:lad_loc_nz,out_l_y:out_r_y+1,out_l_x:out_r_x+1] != fill,bad_loc[0:lad_loc_nz,out_l_y:out_r_y+1,out_l_x:out_r_x+1],bad[0:lad_loc_nz,ind_l_y:ind_r_y+1,ind_l_x:ind_r_x+1]) 108 114 ids[0:lad_loc_nz,ind_l_y:ind_r_y+1,ind_l_x:ind_r_x+1] = np.where(lad_loc[0:lad_loc_nz,out_l_y:out_r_y+1,out_l_x:out_r_x+1] != fill,tree_id_counter,ids[0:lad_loc_nz,ind_l_y:ind_r_y+1,ind_l_x:ind_r_x+1]) 115 types[0:lad_loc_nz,ind_l_y:ind_r_y+1,ind_l_x:ind_r_x+1] = np.where(lad_loc[0:lad_loc_nz,out_l_y:out_r_y+1,out_l_x:out_r_x+1] != fill,np.byte(tree_type[j,i]),types[0:lad_loc_nz,ind_l_y:ind_r_y+1,ind_l_x:ind_r_x+1]) 109 116 110 117 … … 116 123 117 124 del lad_loc, x_loc, y_loc, z_loc, status 118 return lad, bad, ids, zlad125 return lad, bad, ids, types, zlad 119 126 120 127 … … 230 237 fillvalues = { 231 238 "tree_data": float(-9999.0), 239 "tree_type": np.byte(-127) 232 240 } 233 241 234 242 235 243 # Check for missing data in the input and set default values if needed 236 if ( tree_type == fillvalues["tree_ data"] ):244 if ( tree_type == fillvalues["tree_type"] ): 237 245 tree_type = int(0) 238 246 else: … … 295 303 nx = int(tree_dia / dx) + 2 296 304 nz = int(tree_height / dz) + 2 297 305 306 307 298 308 # Add one grid point if diameter is an odd value 299 309 if ( (tree_dia % 2.0) != 0.0 ): 300 310 nx = nx + 1 301 302 311 303 312 # Create local domain of the tree's LAD … … 459 468 460 469 # Create BAD array and populate. TODO: revise as low LAD inside the foliage does not result in low BAD values. 461 bad_loc = np.where(lad_loc != fillvalues["tree_data"],(1.0 - lad_loc)*0.1,lad_loc)470 bad_loc = np.where(lad_loc != fillvalues["tree_data"],(1.0 - (lad_loc/(max(lad_loc.flatten())+0.01)))*0.1,lad_loc) 462 471 463 472 … … 469 478 if ( z[k] <= crown_center ): 470 479 r_test = np.sqrt( (x[i] - tree_location_x)**(2) + (y[j] - tree_location_y)**(2) ) 471 if ( r_test <= radius ): 472 bad_loc[k,j,i] = 1.0 473 if ( (r_test == 0.0) & (trunk_dia <= dx) ): 474 bad_loc[k,j,i] = radius**(2) * 3.14159265359 480 if ( r_test == 0.0 ): 481 if ( trunk_dia <= dx ): 482 bad_loc[k,j,i] = radius**2 * 3.14159265359 483 else: 484 # WORKAROUND: divide remaining circle area over the 8 surrounding valid_pixels 485 bad_loc[k,j-1:j+2,i-1:i+2] = radius**2 * 3.14159265359 / 8.0 486 # for the central pixel fill the pixel 487 bad_loc[k,j,i] = dx**2 488 #elif ( r_test <= radius ): 489 # TODO: calculate circle segment of grid points cut by the grid 490 475 491 476 492 return lad_loc, bad_loc, x, y, z, 0 477 493 478 494 479 def process_patch(dz,patch_height, max_height_lad,patch_lai,alpha,beta):495 def process_patch(dz,patch_height,vegetation_type,max_height_lad,patch_lai,alpha,beta): 480 496 481 497 # Define fill values 482 498 fillvalues = { 483 499 "tree_data": float(-9999.0), 484 "pch_index": int(-9999), 500 "tree_id": int(-9999), 501 "tree_type": np.byte(-127), 502 "pch_index": int(-9999) 485 503 } 486 504 505 487 506 phdz = patch_height[:,:] / dz 488 507 pch_index = np.where( (patch_height[:,:] != fillvalues["tree_data"]),phdz.astype(int)+1,int(-1)) … … 520 539 lad_loc[k,j,i] = 0.5 * ( pre_lad[k-1] + pre_lad[k] ) 521 540 522 return lad_loc, nz, 0 541 542 patch_id_2d = np.where(lad_loc[0,:,:] != fillvalues["tree_data"], 1, 0) 543 patch_id = np.where(lad_loc != fillvalues["tree_data"], 1, 0) 544 patch_type = np.where(lad_loc != fillvalues["tree_data"], np.byte(1), np.byte(-127)) 545 546 num_ids = nd.label(patch_id_2d, output=patch_id_2d) 547 548 for k in range(0,nz): 549 550 vegetation_type_int = vegetation_type.astype(int) 551 patch_id[k,:,:] = np.where((patch_id_2d != 0) & (lad_loc[k,:,:] != fillvalues["tree_data"]), patch_id_2d, fillvalues["tree_id"]) 552 patch_type[k,:,:] = np.where((patch_id_2d != 0) & (lad_loc[k,:,:] != fillvalues["tree_data"]), np.byte(100+vegetation_type), fillvalues["tree_type"]) 553 554 return lad_loc, patch_id, patch_type, nz, 0 523 555 524 556 … … 559 591 self.bad_scale = bad_scale 560 592 self.dbh = dbh 593
Note: See TracChangeset
for help on using the changeset viewer.