Index: palm/trunk/SCRIPTS/palm_csd
===================================================================
--- palm/trunk/SCRIPTS/palm_csd	(revision 3567)
+++ palm/trunk/SCRIPTS/palm_csd	(revision 3629)
@@ -25,9 +25,8 @@
 # -----------------
 # $Id$
+# Added canopy generator calls. Some improvements
+# 
+# 3567 2018-11-27 13:59:21Z maronga
 # Initial revisions
-#
-#
-# 
-#
 #
 # Description:
@@ -38,8 +37,4 @@
 # @Author Bjoern Maronga (maronga@muk.uni-hannover.de)
 #
-# @todo Remove high vegetation on demand
-# @todo Add vegetation_pars (LAI)
-# @todo Add building_pars (green roofs)
-# @todo Add LAD and BAD arrays (canopy generator)
 # @todo Make input files optional
 # @todo Allow for ASCII input of terrain height and building height
@@ -49,4 +44,5 @@
 from palm_csd_files.palm_csd_netcdf_interface import *
 from palm_csd_files.palm_csd_tools import *
+from palm_csd_files.palm_csd_canopy_generator import *
 import numpy as np
 
@@ -71,6 +67,4 @@
       print ("Error. No configuration file " + input_config + " found.")
       raise SystemExit   
-   else:
-      print(os.path.isfile(input_config))
 
    config.read(input_config)
@@ -79,6 +73,13 @@
 #  Definition of settings
    global settings_filename_out
-   global settings_lai_season
    global settings_bridge_width
+   global settings_lai_roof_intensive
+   global settings_lai_roof_extensive
+   global settings_season
+   global settings_lai_low_default
+   global settings_lai_high_default
+   global settings_patch_height_default
+   global settings_lai_alpha
+   global settings_lai_beta
    global ndomains
 
@@ -113,9 +114,11 @@
    global domain_dz
    global domain_3d
-   global domain_hv
-   global domain_cg
+   global domain_high_vegetation
    global domain_ip
    global domain_za
    global domain_parent
+   global domain_green_roofs
+   global domain_street_trees
+   global domain_canopy_patches
 
 #  Definition of input data parameters
@@ -137,4 +140,5 @@
    global input_file_building_type
    global input_file_building_type
+   global input_file_lai  
    global input_file_vegetation_type
    global input_file_vegetation_height
@@ -144,5 +148,10 @@
    global input_file_street_crossings
    global input_file_soil_type
-
+   global input_file_vegetation_on_roofs
+   global input_file_tree_crown_diameter
+   global input_file_tree_height
+   global input_file_tree_trunk_diameter
+   global input_file_tree_type
+   global input_file_patch_height
 
    global zt_all
@@ -150,6 +159,13 @@
 
    settings_filename_out = "default"
-   settings_lai_season = "summer"
    settings_bridge_width = 3.0
+   settings_lai_roof_intensive = 0.5
+   settings_lai_roof_extensive = 1.0
+   settings_season = "summer"
+   settings_lai_high_default = 6.0
+   settings_lai_low_default = 1.0
+   settings_patch_height_default = 10.0
+   settings_lai_alpha = 5.0
+   settings_lai_beta = 3.0
    ndomains = 0
 
@@ -181,9 +197,11 @@
    domain_dz = []
    domain_3d = []
-   domain_hv = []
-   domain_cg = []
+   domain_high_vegetation = []
    domain_ip = []
    domain_za = []
    domain_parent = []
+   domain_green_roofs = []
+   domain_street_trees = []
+   domain_canopy_patches = []
 
    zt_min = 0.0
@@ -206,4 +224,5 @@
    input_file_bridges_id = []   
    input_file_building_type = []
+   input_file_lai = []
    input_file_vegetation_type = []
    input_file_vegetation_height = []   
@@ -213,5 +232,10 @@
    input_file_street_crossings = []     
    input_file_soil_type = []
-
+   input_file_vegetation_on_roofs = []
+   input_file_tree_crown_diameter = []
+   input_file_tree_height = []
+   input_file_tree_trunk_diameter = []
+   input_file_tree_type = []
+   input_file_patch_height = []
    
 # Load all user parameters from config file
@@ -240,7 +264,14 @@
       if ( read_tmp == 'settings' ):
          settings_filename_out = config.get(read_tmp, 'filename_out')
-         settings_lai_season = config.get(read_tmp, 'lai_season') 
-         settings_bridge_width = float(config.get(read_tmp, 'bridge_width'))      
- 
+         settings_lai_roof_intensive = config.get(read_tmp, 'lai_roof_intensive')
+         settings_lai_roof_extensive = config.get(read_tmp, 'lai_roof_extensive') 
+         settings_bridge_width = float(config.get(read_tmp, 'bridge_width'))   
+         settings_season = config.get(read_tmp, 'season')   
+         settings_lai_high_default = float(config.get(read_tmp, 'lai_high_vegetation_default'))
+         settings_lai_low_default = float(config.get(read_tmp, 'lai_low_vegetation_default'))         
+         settings_patch_height_default = float(config.get(read_tmp, 'patch_height_default'))  
+         settings_lai_alpha = float(config.get(read_tmp, 'lai_alpha')) 
+         settings_lai_beta = float(config.get(read_tmp, 'lai_beta')) 
+
       if ( read_tmp.split("_")[0] == 'domain' ):
          ndomains = ndomains + 1
@@ -251,6 +282,6 @@
          domain_dz.append(float(config.get(read_tmp, 'dz')))       
          domain_3d.append(config.getboolean(read_tmp, 'buildings_3d'))
-         domain_hv.append(config.getboolean(read_tmp, 'allow_high_vegetation'))
-         domain_cg.append(config.getboolean(read_tmp, 'generate_vegetation_patches'))       
+         domain_high_vegetation.append(config.getboolean(read_tmp, 'allow_high_vegetation'))
+         domain_canopy_patches.append(config.getboolean(read_tmp, 'generate_vegetation_patches'))       
          domain_ip.append(config.getboolean(read_tmp, 'interpolate_terrain'))
          domain_za.append(config.getboolean(read_tmp, 'use_palm_z_axis')) 
@@ -265,5 +296,7 @@
          domain_x1.append(int(floor(float(config.get(read_tmp, 'origin_x'))/float(config.get(read_tmp, 'pixel_size'))) + int(config.get(read_tmp, 'nx'))))
          domain_y1.append(int(floor(float(config.get(read_tmp, 'origin_y'))/float(config.get(read_tmp, 'pixel_size'))) + int(config.get(read_tmp, 'ny'))))
-
+         domain_green_roofs.append(config.getboolean(read_tmp, 'vegetation_on_roofs')) 
+         domain_street_trees.append(config.getboolean(read_tmp, 'street_trees')) 
+         
       if ( read_tmp.split("_")[0] == 'input' ):
          input_names.append(read_tmp.split("_")[1])
@@ -280,5 +313,6 @@
          input_file_building_id.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_building_id'))  
          input_file_bridges_id.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_bridges_id')) 
-         input_file_building_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_building_type'))  
+         input_file_building_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_building_type'))
+         input_file_lai.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_lai'))         
          input_file_vegetation_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_vegetation_type')) 
          input_file_vegetation_height.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_vegetation_height'))          
@@ -286,5 +320,11 @@
          input_file_water_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_water_type'))
          input_file_street_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_street_type'))    
-         input_file_street_crossings.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_street_crossings'))  
+         input_file_street_crossings.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_street_crossings')) 
+         input_file_patch_height.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_patch_height')) 
+         input_file_tree_crown_diameter.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_tree_crown_diameter'))  
+         input_file_tree_height.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_tree_height'))  
+         input_file_tree_trunk_diameter.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_tree_trunk_diameter'))  
+         input_file_tree_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_tree_type'))  
+         input_file_vegetation_on_roofs.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_vegetation_on_roofs'))           
          #input_file_soil_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_soil_type'))    
    return 0
@@ -317,5 +357,12 @@
    "street_crossings": "b",     
    "soil_type": "b",
-   "surface_fraction": "f4"
+   "surface_fraction": "f4",
+   "building_pars": "f4",
+   "vegetation_pars": "f4",
+   "tree_data": "f4",
+   "tree_type": "b",
+   "nbuilding_pars": "i",
+   "nvegetation_pars": "i",
+   "zlad": "f4"
    }
 
@@ -340,5 +387,9 @@
    "street_crossings": np.byte(-127),   
    "soil_type": np.byte(-127),
-   "surface_fraction": float(-9999.0)
+   "surface_fraction": float(-9999.0),
+   "building_pars": float(-9999.0),
+   "vegetation_pars": float(-9999.0),
+   "tree_data": float(-9999.0),
+   "tree_type": np.byte(-127)
    }
 
@@ -363,5 +414,9 @@
    "street_crossings": 0,    
    "soil_type": 1,
-   "surface_fraction": float(0.0)
+   "surface_fraction": float(0.0),
+   "buildings_pars": float(-9999.0),
+   "tree_data": float(-9999.0),
+   "tree_type": 0,
+   "vegetation_pars": float(-9999.0)   
    }
   
@@ -606,5 +661,5 @@
 
 
-      del bridges_2d, bridges_id, building_id
+      del bridges_2d, bridges_id, building_id, buildings_2d
 
 
@@ -684,6 +739,6 @@
    vegetation_height = nc_read_from_file_2d(input_file_vegetation_height[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
          
-   vegetation_type = np.where((vegetation_height != fillvalues["vegetation_height"]) & (vegetation_height == 0.0) & ((vegetation_type == 4) | (vegetation_type == 7) | (vegetation_type == 17)), 3, vegetation_type)
-   vegetation_height = np.where((vegetation_height != fillvalues["vegetation_height"]) & (vegetation_height == 0.0) & ((vegetation_type == 4) | (vegetation_type == 7) | (vegetation_type == 17)), fillvalues["vegetation_height"],vegetation_height)
+   vegetation_type = np.where((vegetation_height != fillvalues["vegetation_height"]) & (vegetation_height == 0.0) & ((vegetation_type == 4) | (vegetation_type == 5) | (vegetation_type == 6) |(vegetation_type == 7) | (vegetation_type == 17) | (vegetation_type == 18)), 3, vegetation_type)
+   vegetation_height = np.where((vegetation_height != fillvalues["vegetation_height"]) & (vegetation_height == 0.0) & ((vegetation_type == 4) | (vegetation_type == 5) | (vegetation_type == 6) |(vegetation_type == 7) | (vegetation_type == 17) | (vegetation_type == 18)), fillvalues["vegetation_height"],vegetation_height)
 
 
@@ -720,7 +775,6 @@
    del soil_type
 
-
-
-
+   del x
+   del y
 
 #  pixels with bridges get building_type = 7 = bridge. This does not change the _type setting for the under-bridge
@@ -737,5 +791,5 @@
          del bridges_2d
 
-# Read/Write street type and street crossings
+# Read/write street type and street crossings
 for i in range(0,ndomains):  
 
@@ -763,2 +817,203 @@
    nc_write_attribute(filename[i], 'street_crossings', 'grid_mapping', 'E_UTM N_UTM lon lat')   
    del street_crossings
+
+
+# Read/write vegetation on roofs
+for i in range(0,ndomains):  
+   if domain_green_roofs[i]: 
+      green_roofs = nc_read_from_file_2d(input_file_vegetation_on_roofs[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
+      buildings_2d = nc_read_from_file_2d_all(filename[i], 'buildings_2d') 
+
+
+      x = nc_read_from_file_2d_all(filename[i], 'x')
+      y = nc_read_from_file_2d_all(filename[i], 'y')  
+      nbuilding_pars = np.arange(0,46)
+      building_pars = np.ones((len(nbuilding_pars),len(y),len(x)))
+      building_pars[:,:,:] = fillvalues["building_pars"]
+
+#     assign green fraction on roofs
+      building_pars[3,:,:] = np.where( (buildings_2d != fillvalues["buildings_2d"] ) & ( green_roofs != fillvalues["building_pars"] ),1.0,fillvalues["building_pars"])
+
+#     assign leaf area index for vegetation on roofs
+      building_pars[4,:,:] = np.where( ( buildings_2d != fillvalues["buildings_2d"] ) & ( green_roofs == 1.0 ),settings_lai_roof_intensive,fillvalues["building_pars"])
+      building_pars[4,:,:] = np.where( ( buildings_2d != fillvalues["buildings_2d"] ) & ( green_roofs == 2.0 ),settings_lai_roof_extensive,building_pars[4,:,:])
+   
+   
+      nc_write_dimension(filename[i], 'nbuilding_pars', nbuilding_pars, datatypes["nbuilding_pars"]) 
+      nc_write_to_file_3d(filename[i], 'building_pars', building_pars, datatypes["building_pars"],'nbuilding_pars','y','x',fillvalues["building_pars"]) 
+      nc_write_attribute(filename[i], 'building_pars', 'long_name', 'building_pars')
+      nc_write_attribute(filename[i], 'building_pars', 'units', '')
+      nc_write_attribute(filename[i], 'building_pars', 'res_orig', domain_px[i])  
+      nc_write_attribute(filename[i], 'building_pars', 'coordinates', 'E_UTM N_UTM lon lat') 
+      nc_write_attribute(filename[i], 'building_pars', 'grid_mapping', 'E_UTM N_UTM lon lat')   
+         
+      del building_pars, buildings_2d, x, y
+
+
+# Read tree data and create LAD and BAD arrays using the canopy generator
+for i in range(0,ndomains):  
+   if domain_street_trees[i]: 
+      lai = nc_read_from_file_2d(input_file_lai[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
+      
+      
+#     Save lai data as default for low and high vegetation
+      lai_low = lai
+      lai_high = lai
+      
+      x = nc_read_from_file_2d_all(filename[i], 'x')
+      y = nc_read_from_file_2d_all(filename[i], 'y')  
+      nvegetation_pars = np.arange(0,11)
+      vegetation_pars = np.ones((len(nvegetation_pars),len(y),len(x)))
+      vegetation_pars[:,:,:] = fillvalues["vegetation_pars"]  
+      
+      vegetation_pars[1,:,:] = lai
+      
+
+#     Write out first version of LAI. Will later be overwritten.
+      nc_write_dimension(filename[i], 'nvegetation_pars', nvegetation_pars, datatypes["nvegetation_pars"]) 
+      nc_write_to_file_3d(filename[i], 'vegetation_pars', vegetation_pars, datatypes["vegetation_pars"],'nvegetation_pars','y','x',fillvalues["vegetation_pars"]) 
+      nc_write_attribute(filename[i], 'vegetation_pars', 'long_name', 'vegetation_pars')
+      nc_write_attribute(filename[i], 'vegetation_pars', 'units', '')
+      nc_write_attribute(filename[i], 'vegetation_pars', 'res_orig', domain_px[i])  
+      nc_write_attribute(filename[i], 'vegetation_pars', 'coordinates', 'E_UTM N_UTM lon lat') 
+      nc_write_attribute(filename[i], 'vegetation_pars', 'grid_mapping', 'E_UTM N_UTM lon lat')   
+
+
+#     Read all tree parameters from file
+      tree_height = nc_read_from_file_2d(input_file_tree_height[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
+      tree_crown_diameter = nc_read_from_file_2d(input_file_tree_crown_diameter[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
+      tree_trunk_diameter = nc_read_from_file_2d(input_file_tree_trunk_diameter[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])         
+      tree_type = nc_read_from_file_2d(input_file_tree_type[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
+      patch_height = nc_read_from_file_2d(input_file_patch_height[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
+      
+#     Remove missing values from the data. Reasonable values will be set by the tree generator    
+      tree_height = np.where( (tree_height == 0.0) | (tree_height == -1.0) ,fillvalues["tree_data"],tree_height)
+      tree_crown_diameter = np.where( (tree_crown_diameter == 0.0) | (tree_crown_diameter == -1.0) ,fillvalues["tree_data"],tree_crown_diameter)
+      tree_trunk_diameter = np.where( (tree_trunk_diameter == 0.0) | (tree_trunk_diameter == -1.0) ,fillvalues["tree_data"],tree_trunk_diameter)
+      tree_type = np.where( (tree_type == 0.0) | (tree_type == -1.0) ,fillvalues["tree_data"],tree_type)
+      patch_height = np.where( patch_height == -1.0 ,fillvalues["tree_data"],patch_height)     
+
+#     Convert trunk diameter from cm to m
+      tree_trunk_diameter = np.where(tree_trunk_diameter != fillvalues["tree_data"], tree_trunk_diameter * 0.01,tree_trunk_diameter)
+
+
+#     Temporarily change missing value for tree_type
+      tree_type = np.where( (tree_type == fillvalues["tree_type"]),fillvalues["tree_data"],tree_type)    
+
+#     Compare patch height array with vegetation type and correct accordingly
+      vegetation_type = nc_read_from_file_2d_all(filename[i], 'vegetation_type') 
+
+
+#     For zero-height patches, set vegetation_type to short grass and remove these pixels from the patch height array
+      vegetation_type = np.where( (patch_height == 0.0) & ( (vegetation_type == 4) | (vegetation_type == 5) | (vegetation_type == 6) |(vegetation_type == 7) | (vegetation_type == 17) | (vegetation_type == 18) ),3,vegetation_type)
+      patch_type = np.where( (patch_height == 0.0) & ( (vegetation_type == 4) | (vegetation_type == 5) | (vegetation_type == 6) |(vegetation_type == 7) | (vegetation_type == 17) | (vegetation_type == 18) ),fillvalues["tree_data"],patch_height)      
+      nc_overwrite_to_file_2d(filename[i], 'vegetation_type', vegetation_type)  
+
+      max_tree_height = max(tree_height.flatten())
+      max_patch_height = max(patch_height.flatten())
+      
+      if ( (max_tree_height != fillvalues["tree_data"]) | (max_patch_height == fillvalues["tree_data"]) ):
+           
+         lad, bad, tree_ids, zlad = generate_single_tree_lad(x,y,domain_dz[i],max_tree_height,max_patch_height,tree_type,tree_height,tree_crown_diameter,tree_trunk_diameter,lai,settings_season,fillvalues["tree_data"])
+  
+  
+#        Remove LAD volumes that are inside buildings
+         buildings_2d = nc_read_from_file_2d_all(filename[i], 'buildings_2d') 
+         for k in range(0,len(zlad)-1):
+  
+            lad[k,:,:] = np.where(buildings_2d == fillvalues["buildings_2d"],lad[k,:,:],fillvalues["tree_data"])
+            bad[k,:,:] = np.where(buildings_2d == fillvalues["buildings_2d"],bad[k,:,:],fillvalues["tree_data"])
+            tree_ids[k,:,:] = np.where(buildings_2d == fillvalues["buildings_2d"],tree_ids[k,:,:],fillvalues["tree_data"])      
+  
+  
+         nc_write_dimension(filename[i], 'zlad', zlad, datatypes["tree_data"]) 
+         nc_write_to_file_3d(filename[i], 'lad', lad, datatypes["tree_data"],'zlad','y','x',fillvalues["tree_data"]) 
+         nc_write_attribute(filename[i], 'lad', 'long_name', 'leaf area density')
+         nc_write_attribute(filename[i], 'lad', 'units', '')
+         nc_write_attribute(filename[i], 'lad', 'res_orig', domain_px[i])  
+         nc_write_attribute(filename[i], 'lad', 'coordinates', 'E_UTM N_UTM lon lat') 
+         nc_write_attribute(filename[i], 'lad', 'grid_mapping', 'E_UTM N_UTM lon lat') 
+  
+         nc_write_to_file_3d(filename[i], 'bad', bad, datatypes["tree_data"],'zlad','y','x',fillvalues["tree_data"]) 
+         nc_write_attribute(filename[i], 'bad', 'long_name', 'basal area density')
+         nc_write_attribute(filename[i], 'bad', 'units', '')
+         nc_write_attribute(filename[i], 'bad', 'res_orig', domain_px[i])  
+         nc_write_attribute(filename[i], 'bad', 'coordinates', 'E_UTM N_UTM lon lat') 
+         nc_write_attribute(filename[i], 'bad', 'grid_mapping', 'E_UTM N_UTM lon lat')
+            
+         nc_write_to_file_3d(filename[i], 'tree_id', tree_ids, datatypes["tree_data"],'zlad','y','x',fillvalues["tree_data"]) 
+         nc_write_attribute(filename[i], 'tree_id', 'long_name', 'tree id')
+         nc_write_attribute(filename[i], 'tree_id', 'units', '')
+         nc_write_attribute(filename[i], 'tree_id', 'res_orig', domain_px[i])  
+         nc_write_attribute(filename[i], 'tree_id', 'coordinates', 'E_UTM N_UTM lon lat') 
+         nc_write_attribute(filename[i], 'tree_id', 'grid_mapping', 'E_UTM N_UTM lon lat')           
+            
+         del buildings_2d, lai, lad, bad, tree_ids, zlad
+
+      del vegetation_pars, tree_height, tree_crown_diameter, tree_trunk_diameter, tree_type, patch_height, vegetation_type, x, y
+
+
+# Create vegetation patches for locations with high vegetation type
+for i in range(0,ndomains):  
+   if domain_canopy_patches[i]: 
+
+#     Load vegetation_type and lad array (at level z = 0) for re-processing
+      vegetation_type = nc_read_from_file_2d_all(filename[i], 'vegetation_type') 
+      lad = nc_read_from_file_3d_all(filename[i], 'lad') 
+      patch_height = nc_read_from_file_2d(input_file_patch_height[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
+      vegetation_pars = nc_read_from_file_3d_all(filename[i], 'vegetation_pars') 
+      lai = vegetation_pars[1,:,:]
+
+#     For all pixels where the LAD array does not contains a street tree and for which a high vegetation was set and for which a patch height is either missing of larger than one grid spacing
+#     the lai_high is reset to a low vegetation value
+      lai_high = np.where( (lad[0,:,:] == fillvalues["tree_data"]) & ( ( (vegetation_type == 4) | (vegetation_type == 5) | (vegetation_type == 6) |(vegetation_type == 7) | (vegetation_type == 17) | (vegetation_type == 18) ) & ( (patch_height == fillvalues["tree_data"]) | (patch_height >= domain_dz[i])) ),settings_lai_low_default,fillvalues["tree_data"])
+
+#     For all pixels where lai_high is set but no lai is available, set the default high vegetation lai TODO: check if this makes sense bacauce lai_high = lai anway?!
+      lai_high = np.where( (lai_high != fillvalues["tree_data"]) & (lai == fillvalues["tree_data"]), settings_lai_high_default, lai_high)
+
+#     Define a patch height whereever it is missing, but where a high vegetation LAI was set
+      patch_height = np.where ( (lai_high != fillvalues["tree_data"]) & (patch_height == fillvalues["tree_data"]), settings_patch_height_default, patch_height)
+
+#     Remove pixels where street trees were already set
+      patch_height = np.where ( (lad[0,:,:] != fillvalues["tree_data"]), fillvalues["tree_data"], patch_height)
+
+#     For missing LAI values, set either the high vegetation default or the low vegetation default    
+      lai_high = np.where( (patch_height > 2.0) & (lai_high == fillvalues["tree_data"]),settings_lai_high_default,lai_high)
+      lai_high = np.where( (patch_height <= 2.0) & (lai_high == fillvalues["tree_data"]),settings_lai_low_default,lai_high)
+    
+    
+      if ( max(patch_height.flatten()) >= (2.0 * domain_dz[i]) ):
+         print("    start calculating LAD (this might take some time)") 
+         lad_patch, patch_nz, status = process_patch(domain_dz[i],patch_height,lai_high,settings_lai_alpha,settings_lai_beta)
+
+         lad[0:patch_nz+1,:,:] = np.where( (lad[0:patch_nz+1,:,:] == fillvalues["tree_data"]),lad_patch[0:patch_nz+1,:,:], lad[0:patch_nz+1,:,:] )
+
+#     Remove high vegetation wherever it is replaced by a leaf area density. This should effectively remove all high vegetation pixels
+      vegetation_type = np.where((lad[0,:,:] != fillvalues["tree_data"]) & (vegetation_type != fillvalues["vegetation_type"]),3,vegetation_type)
+  
+ 
+#     If desired, remove all high vegetation. TODO: check if this is still necessary
+      if domain_high_vegetation[i]:
+         vegetation_type = np.where((vegetation_type != fillvalues["vegetation_type"]) & ( (vegetation_type == 4) | (vegetation_type == 5) | (vegetation_type == 6) |(vegetation_type == 7) | (vegetation_type == 17) | (vegetation_type == 18) ),3,vegetation_type)   
+
+  
+#     Remove LAI from pixels where a LAD was set
+      lai_low = np.where( (lad[0,:,:] == fillvalues["tree_data"]), lai,fillvalues["tree_data"])
+    
+#     Fill low vegetation pixels without LAI set with default value
+      lai_low = np.where( (lai == fillvalues["tree_data"]) & (vegetation_type != fillvalues["vegetation_type"] ), settings_lai_low_default, lai)
+
+    
+#     Overwrite lai in vegetation_parameters  
+
+      vegetation_pars[1,:,:] = lai_low
+      nc_overwrite_to_file_3d(filename[i], 'vegetation_pars', vegetation_pars) 
+
+#     Overwrite lad array
+      nc_overwrite_to_file_3d(filename[i], 'lad', lad) 
+      
+#     Overwrite vegetation_type
+      nc_overwrite_to_file_2d(filename[i], 'vegetation_type', vegetation_type)   
+      
+      
+      del vegetation_type, lad, lai, patch_height, vegetation_pars
