source: palm/trunk/SCRIPTS/palm_csd @ 4874

Last change on this file since 4874 was 4843, checked in by raasch, 4 years ago

local namelist parameter added to switch off the module although the respective module namelist appears in the namelist file, further copyright updates

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 61.8 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#--------------------------------------------------------------------------------#
4# This file is part of the PALM model system.
5#
6# PALM is free software: you can redistribute it and/or modify it under the terms
7# of the GNU General Public License as published by the Free Software Foundation,
8# either version 3 of the License, or (at your option) any later version.
9#
10# PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along with
15# PALM. If not, see <http://www.gnu.org/licenses/>.
16#
17# Copyright 1997-2021  Leibniz Universitaet Hannover
18#--------------------------------------------------------------------------------#
19#
20# Current revisions:
21# -----------------
22#
23#
24# Former revisions:
25# -----------------
26# $Id: palm_csd 4843 2021-01-15 15:22:11Z raasch $
27# Removed debug output; assign negative ids for tree patches
28#
29# 4793 2020-11-24 09:48:21Z maronga
30# Added output of tree_type (species or classification). Added tree_id for
31# vegetation patches. Allow for overlapping vegetation with buildings
32#
33# 4749 2020-10-16 11:21:27Z maronga
34# Bugfix: processing of patch height faulty for multiple domains
35#
36# 4746 2020-10-15 17:16:30Z maronga
37# Add support for backyard high vegetation, which is commonly classified as
38# short grass
39#
40# 4490 2020-04-10 05:32:45Z maronga
41# Bugfix: call of canopy generator was not made if tree heights were only
42# available via patch_height, but not in tree_height
43#
44# 4481 2020-03-31 18:55:54Z maronga
45# Bugfix: green roofs were assigned to all available roofs when switched on
46#
47# 4149 2019-08-08 14:02:18Z suehring
48# Bugfix, nbuilding_pars was 1 element to small
49#
50# 3955 2019-05-07 09:55:25Z maronga
51# Bugfix in preparaing of green roofs
52# Bugfix: remove LAI for bare soils
53#
54# 3859 2019-04-03 20:30:31Z maronga
55# Bugfix: wrong variable naming for 'y'
56#
57# 3773 2019-03-01 08:56:57Z maronga
58# Unspecificed changes
59#
60# 3726 2019-02-07 18:22:49Z maronga
61# Removed some more bugs
62#
63# 3688 2019-01-22 10:44:20Z maronga
64# Some unspecified bugfixes
65#
66# 3668 2019-01-14 12:49:24Z maronga
67# Various improvements and bugfixes
68#
69# 3629 2018-12-13 12:18:54Z maronga
70# Added canopy generator calls. Some improvements
71#
72# 3567 2018-11-27 13:59:21Z maronga
73# Initial revisions
74#
75# Description:
76# ------------
77# Processing tool for creating PIDS conform static drivers from rastered NetCDF
78# input
79#
80# @Author Bjoern Maronga (maronga@muk.uni-hannover.de)
81#
82# @todo Make input files optional
83# @todo Allow for ASCII input of terrain height and building height
84# @todo Modularize reading config file
85# @todo Convert to object-oriented treatment (buidings, trees)
86# @todo Automatically shift child domains so that their origin lies intersects
87#       a edge note of the parent grid
88#------------------------------------------------------------------------------#
89
90from palm_csd_files.palm_csd_netcdf_interface import *
91from palm_csd_files.palm_csd_tools import *
92from palm_csd_files.palm_csd_canopy_generator import *
93import numpy as np
94
95   
96def read_config_file():
97   
98   import configparser
99   from math import floor
100   import numpy as np
101   import os
102   import subprocess as sub
103   import sys
104
105#  Check if configuration files exists and quit otherwise
106   input_config = ".csd.config"
107   for i in range(1,len(sys.argv)): 
108      input_config = str(sys.argv[i])
109       
110   config = configparser.RawConfigParser(allow_no_value=True)
111
112   if ( os.path.isfile(input_config) == False ):
113      print ("Error. No configuration file " + input_config + " found.")
114      raise SystemExit   
115
116   config.read(input_config)
117
118
119#  Definition of settings
120
121   global settings_bridge_width
122   global settings_lai_roof_intensive
123   global settings_lai_roof_extensive
124   global settings_season
125   global settings_lai_low_default
126   global settings_lai_high_default
127   global settings_patch_height_default
128   global settings_lai_alpha
129   global settings_lai_beta
130   global settings_veg_type_below_trees
131   global ndomains
132
133#  Definition of global configuration parameters
134   global global_acronym
135   global global_angle
136   global global_author
137   global global_campaign
138   global global_comment
139   global global_contact
140   global global_data_content 
141   global global_dependencies
142   global global_institution
143   global global_keywords
144   global global_location
145   global global_palm_version
146   global global_references
147   global global_site
148   global global_source
149
150   global  path_out
151   global filename_out
152   global version_out
153
154
155#  Definition of domain parameters
156   global domain_names
157   global domain_px
158   global domain_x0
159   global domain_y0
160   global domain_x1
161   global domain_y1
162   global domain_nx
163   global domain_ny
164   global domain_dz
165   global domain_3d
166   global domain_high_vegetation
167   global domain_ip
168   global domain_za
169   global domain_parent
170   global domain_green_roofs
171   global domain_street_trees
172   global domain_canopy_patches
173   global domain_overhanging_trees
174
175#  Definition of input data parameters
176   global input_names
177   global input_px
178 
179       
180   global input_file_x
181   global input_file_y
182   global input_file_x_UTM
183   global input_file_y_UTM   
184   global input_file_lat
185   global input_file_lon 
186   global input_file_zt
187   global input_file_buildings_2d
188   global input_file_bridges_2d
189   global input_file_building_id
190   global input_file_bridges_id   
191   global input_file_building_type
192   global input_file_building_type
193   global input_file_lai 
194   global input_file_vegetation_type
195   global input_file_vegetation_height
196   global input_file_pavement_type
197   global input_file_water_type
198   global input_file_street_type
199   global input_file_street_crossings
200   global input_file_soil_type
201   global input_file_vegetation_on_roofs
202   global input_file_tree_crown_diameter
203   global input_file_tree_height
204   global input_file_tree_trunk_diameter
205   global input_file_tree_type
206   global input_file_patch_height
207
208   global zt_all
209   global zt_min
210
211   settings_bridge_width = 3.0
212   settings_lai_roof_intensive = 2.5
213   settings_lai_roof_extensive = 0.8
214   settings_season = "summer"
215   settings_lai_high_default = 6.0
216   settings_lai_low_default = 1.0
217   settings_patch_height_default = 10.0
218   settings_lai_alpha = 5.0
219   settings_lai_beta = 3.0
220   settings_veg_type_below_trees = 3
221   ndomains = 0
222
223   global_acronym = ""
224   global_angle = ""
225   global_author = ""
226   global_campaign = ""
227   global_comment = ""
228   global_contact = ""
229   global_data_content = ""
230   global_dependencies = ""
231   global_institution = ""
232   global_keywords = ""
233   global_location = ""
234   global_palm_version = 6.0
235   global_references = ""
236   global_site = ""
237   global_source = ""
238
239   path_out = ""
240   version_out = 1
241   filename_out = "default"
242   
243   domain_names = []
244   domain_px = []
245   domain_x0 = []
246   domain_y0 = []
247   domain_x1 = []
248   domain_y1 = []
249   domain_nx = []
250   domain_ny = []
251   domain_dz = []
252   domain_3d = []
253   domain_high_vegetation = []
254   domain_ip = []
255   domain_za = []
256   domain_parent = []
257   domain_green_roofs = []
258   domain_street_trees = []
259   domain_canopy_patches = []
260   domain_overhanging_trees = []
261
262   zt_min = 0.0
263   zt_all = []
264
265   input_names = []
266   input_px = []
267
268   input_file_x = []
269   input_file_y = [] 
270   input_file_x_UTM = []
271   input_file_y_UTM = [] 
272   input_file_lat = []
273   input_file_lon = [] 
274   
275   input_file_zt = []
276   input_file_buildings_2d = []
277   input_file_bridges_2d = []
278   input_file_building_id = []
279   input_file_bridges_id = []   
280   input_file_building_type = []
281   input_file_lai = []
282   input_file_vegetation_type = []
283   input_file_vegetation_height = []   
284   input_file_pavement_type = []
285   input_file_water_type = []
286   input_file_street_type = [] 
287   input_file_street_crossings = []     
288   input_file_soil_type = []
289   input_file_vegetation_on_roofs = []
290   input_file_tree_crown_diameter = []
291   input_file_tree_height = []
292   input_file_tree_trunk_diameter = []
293   input_file_tree_type = []
294   input_file_patch_height = []
295   
296# Load all user parameters from config file
297   for i in range(0,len(config.sections())):
298
299      read_tmp = config.sections()[i]
300     
301      if ( read_tmp == 'global' ):
302         global_acronym = config.get(read_tmp, 'acronym')
303         global_angle = float(config.get(read_tmp, 'rotation_angle'))
304         global_author = config.get(read_tmp, 'author')
305         global_campaign = config.get(read_tmp, 'campaign')
306         global_comment = config.get(read_tmp, 'comment')
307         global_contact = config.get(read_tmp, 'contact_person')
308         global_data_content = config.get(read_tmp, 'data_content')   
309         global_dependencies = config.get(read_tmp, 'dependencies')
310         global_institution = config.get(read_tmp, 'institution')
311         global_keywords = config.get(read_tmp, 'keywords')
312         global_location = config.get(read_tmp, 'location')
313         global_palm_version = float(config.get(read_tmp, 'palm_version'))
314         global_references = config.get(read_tmp, 'references')
315         global_site = config.get(read_tmp, 'site')
316         global_source = config.get(read_tmp, 'source')
317
318
319      if ( read_tmp == 'settings' ):
320         settings_lai_roof_intensive = config.get(read_tmp, 'lai_roof_intensive')
321         settings_lai_roof_extensive = config.get(read_tmp, 'lai_roof_extensive') 
322         settings_bridge_width = float(config.get(read_tmp, 'bridge_width'))   
323         settings_season = config.get(read_tmp, 'season')   
324         settings_lai_high_default = float(config.get(read_tmp, 'lai_high_vegetation_default'))
325         settings_lai_low_default = float(config.get(read_tmp, 'lai_low_vegetation_default'))         
326         settings_patch_height_default = float(config.get(read_tmp, 'patch_height_default')) 
327         settings_lai_alpha = float(config.get(read_tmp, 'lai_alpha')) 
328         settings_lai_beta = float(config.get(read_tmp, 'lai_beta')) 
329         settings_veg_type_below_trees = config.get(read_tmp, 'vegetation_type_below_trees') 
330
331      if ( read_tmp == 'output' ):
332         path_out = config.get(read_tmp, 'path')         
333         filename_out = config.get(read_tmp, 'file_out')
334         version_out = float(config.get(read_tmp, 'version'))
335
336      if ( read_tmp.split("_")[0] == 'domain' ):
337         ndomains = ndomains + 1
338         domain_names.append(read_tmp.split("_")[1])
339         domain_px.append(float(config.get(read_tmp, 'pixel_size')))
340         domain_nx.append(int(config.get(read_tmp, 'nx')))
341         domain_ny.append(int(config.get(read_tmp, 'ny')))
342         domain_dz.append(float(config.get(read_tmp, 'dz')))       
343         domain_3d.append(config.getboolean(read_tmp, 'buildings_3d'))
344         domain_high_vegetation.append(config.getboolean(read_tmp, 'allow_high_vegetation'))
345         domain_canopy_patches.append(config.getboolean(read_tmp, 'generate_vegetation_patches'))       
346         domain_ip.append(config.getboolean(read_tmp, 'interpolate_terrain'))
347         domain_za.append(config.getboolean(read_tmp, 'use_palm_z_axis')) 
348         if domain_ip[ndomains-1] and not domain_za[ndomains-1]:
349            domain_za[ndomains-1] = True
350            print("+++ Overwrite user setting for use_palm_z_axis")
351         
352         domain_parent.append(config.get(read_tmp, 'domain_parent'))
353
354         domain_x0.append(int(floor(float(config.get(read_tmp, 'origin_x'))/float(config.get(read_tmp, 'pixel_size')))))
355         domain_y0.append(int(floor(float(config.get(read_tmp, 'origin_y'))/float(config.get(read_tmp, 'pixel_size')))))       
356         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'))))
357         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'))))
358         domain_green_roofs.append(config.getboolean(read_tmp, 'vegetation_on_roofs')) 
359         domain_street_trees.append(config.getboolean(read_tmp, 'street_trees')) 
360         domain_overhanging_trees.append(config.getboolean(read_tmp, 'overhanging_trees')) 
361                 
362      if ( read_tmp.split("_")[0] == 'input' ):
363         input_names.append(read_tmp.split("_")[1])
364         input_px.append(float(config.get(read_tmp, 'pixel_size')))
365         input_file_x.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_x'))
366         input_file_y.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_y')) 
367         input_file_lat.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_lat'))
368         input_file_lon.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_lon'))   
369         input_file_x_UTM.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_x_UTM'))
370         input_file_y_UTM.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_y_UTM')) 
371         input_file_zt.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_zt'))
372         input_file_buildings_2d.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_buildings_2d'))
373         input_file_bridges_2d.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_bridges_2d'))
374         input_file_building_id.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_building_id')) 
375         input_file_bridges_id.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_bridges_id')) 
376         input_file_building_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_building_type'))
377         input_file_lai.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_lai'))         
378         input_file_vegetation_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_vegetation_type')) 
379         input_file_vegetation_height.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_vegetation_height'))         
380         input_file_pavement_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_pavement_type'))   
381         input_file_water_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_water_type'))
382         input_file_street_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_street_type'))   
383         input_file_street_crossings.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_street_crossings')) 
384         input_file_patch_height.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_patch_height')) 
385         
386         tmp = config.get(read_tmp, 'file_tree_crown_diameter')
387         if tmp is not None:
388            input_file_tree_crown_diameter.append(config.get(read_tmp, 'path') + "/" + tmp) 
389         else:
390            input_file_tree_crown_diameter.append(None)
391         input_file_tree_height.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_tree_height')) 
392         input_file_tree_trunk_diameter.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_tree_trunk_diameter')) 
393         input_file_tree_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_tree_type')) 
394         input_file_vegetation_on_roofs.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_vegetation_on_roofs'))           
395         #input_file_soil_type.append(config.get(read_tmp, 'path') + "/" + config.get(read_tmp, 'file_soil_type'))   
396   return 0
397
398
399############################################################
400
401# Start of main program
402datatypes = {
403   "x": "f4", 
404   "y": "f4", 
405   "z": "f4", 
406   "lat": "f4", 
407   "lon": "f4",   
408   "E_UTM": "f4", 
409   "N_UTM": "f4", 
410   "zt": "f4",
411   "buildings_2d": "f4",
412   "buildings_3d": "b",
413   "bridges_2d": "f4",
414   "building_id": "i",
415   "bridges_id": "i",
416   "building_type": "b",
417   "nsurface_fraction": "i",
418   "vegetation_type": "b",
419   "vegetation_height": "f4",
420   "pavement_type": "b", 
421   "water_type": "b", 
422   "street_type": "b", 
423   "street_crossings": "b",     
424   "soil_type": "b",
425   "surface_fraction": "f4",
426   "building_pars": "f4",
427   "vegetation_pars": "f4",
428   "tree_data": "f4",
429   "tree_id": "i", 
430   "tree_type": "f4",
431   "tree_types": "b", 
432   "nbuilding_pars": "i",
433   "nvegetation_pars": "i",
434   "zlad": "f4"
435   }
436
437fillvalues = {
438   "lat": float(-9999.0),
439   "lon": float(-9999.0),
440   "E_UTM": float(-9999.0),
441   "N_UTM": float(-9999.0),   
442   "zt": float(-9999.0),
443   "buildings_2d": float(-9999.0),
444   "buildings_3d": np.byte(-127),
445   "bridges_2d": float(-9999.0),
446   "building_id": int(-9999),
447   "bridges_id": int(-9999),
448   "building_type": np.byte(-127),
449   "nsurface_fraction": int(-9999),
450   "vegetation_type": np.byte(-127),
451   "vegetation_height": float(-9999.0),
452   "pavement_type": np.byte(-127),
453   "water_type": np.byte(-127),
454   "street_type": np.byte(-127), 
455   "street_crossings": np.byte(-127),   
456   "soil_type": np.byte(-127),
457   "surface_fraction": float(-9999.0),
458   "building_pars": float(-9999.0),
459   "vegetation_pars": float(-9999.0),
460   "tree_data": float(-9999.0),
461   "tree_id": int(-9999),
462   "tree_type": float(-9999.0),
463   "tree_types": np.byte(-127)
464   }
465
466defaultvalues = {
467   "lat": float(-9999.0),
468   "lon": float(-9999.0),
469   "E_UTM": float(-9999.0),
470   "N_UTM": float(-9999.0), 
471   "zt": float(0.0),
472   "buildings_2d": float(0.0),
473   "buildings_3d": 0,   
474   "bridges_2d": float(0.0),
475   "building_id": int(0),
476   "bridges_id": int(0),
477   "building_type": 1,
478   "nsurface_fraction": int(-9999),
479   "vegetation_type": 3,
480   "vegetation_height": float(-9999.0),
481   "pavement_type": 1, 
482   "water_type": 1, 
483   "street_type": 1, 
484   "street_crossings": 0,   
485   "soil_type": 1,
486   "surface_fraction": float(0.0),
487   "buildings_pars": float(-9999.0),
488   "tree_data": float(-9999.0),
489   "tree_type": np.byte(-127),
490   "vegetation_pars": float(-9999.0)   
491   }
492 
493# Read configuration file and set parameters accordingly
494read_config_file()
495
496
497filename = []
498ii = []
499ii_parent = []
500# Define indices and filenames for all domains and create netCDF files
501for i in range(0,ndomains):
502
503#  Calculate indices and input files
504   ii.append(input_px.index(domain_px[i]))
505   filename.append(path_out + "/" + filename_out + "_" + domain_names[i])
506   if domain_parent[i] is not None:
507      ii_parent.append(input_px.index(domain_px[domain_names.index(domain_parent[i])]))
508   else:
509      ii_parent.append(None)
510
511
512   x_UTM = nc_read_from_file_2d(input_file_x[ii[i]], "Band1", domain_x0[i], domain_x0[i]+1, domain_y0[i], domain_y0[i]+1)
513   y_UTM = nc_read_from_file_2d(input_file_y[ii[i]], "Band1", domain_x0[i], domain_x0[i]+1, domain_y0[i], domain_y0[i]+1)
514   lat = nc_read_from_file_2d(input_file_lat[ii[i]], "Band1", domain_x0[i], domain_x0[i]+1, domain_y0[i], domain_y0[i]+1)
515   lon = nc_read_from_file_2d(input_file_lon[ii[i]], "Band1", domain_x0[i], domain_x0[i]+1, domain_y0[i], domain_y0[i]+1) 
516   
517#  Calculate position of origin
518   x_UTM_origin = float(x_UTM[0,0]) - 0.5 * (float(x_UTM[0,1]) - float(x_UTM[0,0]))
519   y_UTM_origin = float(y_UTM[0,0]) - 0.5 * (float(y_UTM[1,0]) - float(y_UTM[0,0])) 
520   x_origin = float(lon[0,0]) - 0.5 * (float(lon[0,1]) - float(lon[0,0]))
521   y_origin = float(lat[0,0]) - 0.5 * (float(lat[1,0]) - float(lat[0,0]))   
522   
523#  Create NetCDF output file and set global attributes
524   nc_create_file(filename[i])
525   nc_write_global_attributes(filename[i],x_UTM_origin,y_UTM_origin,y_origin,x_origin,"",global_acronym,global_angle,global_author,global_campaign,global_comment,global_contact,global_data_content,global_dependencies,global_institution,global_keywords,global_location,global_palm_version,global_references,global_site,global_source,version_out)
526
527   del x_UTM, y_UTM, lat, lon
528   
529# Process terrain height
530for i in range(0,ndomains):
531
532#  Read and write terrain height (zt)
533   zt = nc_read_from_file_2d(input_file_zt[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])
534
535#  Final step: add zt array to the global array   
536   zt_all.append(zt)
537   del zt
538 
539#  Calculate the global (all domains) minimum of the terrain height. This value will be substracted for all
540#  data sets
541zt_min = min(zt_all[0].flatten()) 
542for i in range(0,ndomains):
543   zt_min = min(zt_min,min(zt_all[i].flatten()))
544   
545del zt_all[:]
546
547print( "Shift terrain heights by -" + str(zt_min))
548for i in range(0,ndomains):
549
550#  Read and write terrain height (zt)
551   zt = nc_read_from_file_2d(input_file_zt[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])
552   x = nc_read_from_file_1d(input_file_x[ii[i]], "x", domain_x0[i], domain_x1[i])
553   y = nc_read_from_file_1d(input_file_y[ii[i]], "y", domain_y0[i], domain_y1[i])
554
555 
556   zt = zt - zt_min
557   
558   nc_write_global_attribute(filename[i], 'origin_z', float(zt_min))
559
560#  If necessary, interpolate parent domain terrain height on child domain grid and blend the two
561   if domain_ip[i]:
562      parent_id = domain_names.index(domain_parent[i])
563      tmp_x0 = int( domain_x0[i] * domain_px[i] / domain_px[parent_id] ) - 1
564      tmp_y0 = int( domain_y0[i] * domain_px[i] / domain_px[parent_id] ) - 1
565      tmp_x1 = int( domain_x1[i] * domain_px[i] / domain_px[parent_id] ) + 1
566      tmp_y1 = int( domain_y1[i] * domain_px[i] / domain_px[parent_id] ) + 1
567
568      tmp_x = nc_read_from_file_1d(input_file_x[ii_parent[i]], "x", tmp_x0, tmp_x1)
569      tmp_y = nc_read_from_file_1d(input_file_y[ii_parent[i]], "y", tmp_y0, tmp_y1)
570
571      zt_parent = nc_read_from_file_2d(input_file_zt[ii_parent[i]], 'Band1', tmp_x0, tmp_x1, tmp_y0, tmp_y1)
572
573      zt_parent = zt_parent - zt_min
574
575#     Interpolate array and bring to PALM grid of child domain
576      zt_ip = interpolate_2d(zt_parent,tmp_x,tmp_y,x,y)
577      zt_ip = bring_to_palm_grid(zt_ip,x,y,domain_dz[parent_id])
578     
579     
580#     Shift the child terrain height according to the parent mean terrain height
581      print("shifting: -" + str(np.mean(zt)) + " +" + str(np.mean(zt_ip)))
582      #zt = zt - np.min(zt) + np.min(zt_ip)
583      zt = zt - np.mean(zt) + np.mean(zt_ip)
584 
585 
586#     Blend over the parent and child terrain height within a radius of 50 px 
587      zt = blend_array_2d(zt,zt_ip,50)
588     # zt = zt_ip
589   
590#  Final step: add zt array to the global array   
591
592   zt_all.append(zt)
593   del zt
594 
595
596# Read and shift x and y coordinates, shift terrain height according to its minimum value and write all data
597# to file 
598for i in range(0,ndomains):   
599#  Read horizontal grid variables from zt file and write them to output file
600   x = nc_read_from_file_1d(input_file_x[ii[i]], "x", domain_x0[i], domain_x1[i])
601   y = nc_read_from_file_1d(input_file_y[ii[i]], "y", domain_y0[i], domain_y1[i]) 
602   x = x - min(x.flatten()) + domain_px[i]/2.0
603   y = y - min(y.flatten()) + domain_px[i]/2.0 
604   nc_write_dimension(filename[i], 'x', x, datatypes["x"])
605   nc_write_dimension(filename[i], 'y', y, datatypes["y"]) 
606   nc_write_attribute(filename[i], 'x', 'long_name', 'x')
607   nc_write_attribute(filename[i], 'x', 'standard_name','projection_x_coordinate')   
608   nc_write_attribute(filename[i], 'x', 'units', 'm')
609   nc_write_attribute(filename[i], 'y', 'long_name', 'y')
610   nc_write_attribute(filename[i], 'y', 'standard_name', 'projection_y_coordinate')   
611   nc_write_attribute(filename[i], 'y', 'units', 'm')
612
613   lat = nc_read_from_file_2d(input_file_lat[ii[i]], "Band1", domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])
614   lon = nc_read_from_file_2d(input_file_lon[ii[i]], "Band1", domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
615
616   nc_write_to_file_2d(filename[i], 'lat', lat, datatypes["lat"],'y','x',fillvalues["lat"])
617   nc_write_to_file_2d(filename[i], 'lon', lon, datatypes["lon"],'y','x',fillvalues["lon"]) 
618   
619   nc_write_attribute(filename[i], 'lat', 'long_name', 'latitude')
620   nc_write_attribute(filename[i], 'lat', 'standard_name','latitude')   
621   nc_write_attribute(filename[i], 'lat', 'units', 'degrees_north')
622 
623   nc_write_attribute(filename[i], 'lon', 'long_name', 'longitude')
624   nc_write_attribute(filename[i], 'lon', 'standard_name','longitude')   
625   nc_write_attribute(filename[i], 'lon', 'units', 'degrees_east') 
626   
627   x_UTM = nc_read_from_file_2d(input_file_x_UTM[ii[i]], "Band1", domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])
628   y_UTM = nc_read_from_file_2d(input_file_y_UTM[ii[i]], "Band1", domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
629
630   
631   nc_write_to_file_2d(filename[i], 'E_UTM', x_UTM, datatypes["E_UTM"],'y','x',fillvalues["E_UTM"])
632   nc_write_to_file_2d(filename[i], 'N_UTM', y_UTM, datatypes["N_UTM"],'y','x',fillvalues["N_UTM"]) 
633   
634   nc_write_attribute(filename[i], 'E_UTM', 'long_name', 'easting')
635   nc_write_attribute(filename[i], 'E_UTM', 'standard_name','projection_x_coorindate')   
636   nc_write_attribute(filename[i], 'E_UTM', 'units', 'm')
637 
638   nc_write_attribute(filename[i], 'N_UTM', 'long_name', 'northing')
639   nc_write_attribute(filename[i], 'N_UTM', 'standard_name','projection_y_coorindate')   
640   nc_write_attribute(filename[i], 'N_UTM', 'units', 'm') 
641 
642   nc_write_crs(filename[i])
643   
644 
645 
646#  If necessary, bring terrain height to PALM's vertical grid. This is either forced by the user or implicitly
647#  by using interpolation for a child domain       
648   if domain_za[i]:
649      zt_all[i] = bring_to_palm_grid(zt_all[i],x,y,domain_dz[i])
650
651   nc_write_to_file_2d(filename[i], 'zt', zt_all[i], datatypes["zt"],'y','x',fillvalues["zt"])
652   nc_write_attribute(filename[i], 'zt', 'long_name', 'orography')
653   nc_write_attribute(filename[i], 'zt', 'units', 'm')
654   nc_write_attribute(filename[i], 'zt', 'res_orig', domain_px[i]) 
655   nc_write_attribute(filename[i], 'zt', 'coordinates', 'E_UTM N_UTM lon lat') 
656   nc_write_attribute(filename[i], 'zt', 'grid_mapping', 'E_UTM N_UTM lon lat')   
657
658del zt_all
659
660
661#  Process building height, id, and type
662for i in range(0,ndomains): 
663   buildings_2d = nc_read_from_file_2d(input_file_buildings_2d[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])
664
665   building_id = nc_read_from_file_2d(input_file_building_id[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
666 
667   building_type = nc_read_from_file_2d(input_file_building_type[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
668   building_type[building_type >= 254] = fillvalues["building_type"]
669   building_type = np.where(building_type < 1,defaultvalues["building_type"],building_type)
670 
671   check = check_arrays_2(buildings_2d,building_id,fillvalues["buildings_2d"],fillvalues["building_id"])
672   if not check:
673      buildings_2d = np.where(building_id != fillvalues["building_id"],buildings_2d,fillvalues["buildings_2d"])
674      building_id = np.where(buildings_2d == fillvalues["buildings_2d"],fillvalues["building_id"],building_id)
675      print("Data check #1 " + str(check_arrays_2(buildings_2d,building_id,fillvalues["buildings_2d"],fillvalues["building_id"])))
676
677   check = check_arrays_2(buildings_2d,building_type,fillvalues["buildings_2d"],fillvalues["building_type"])
678   if not check:
679      building_type = np.where(buildings_2d == fillvalues["buildings_2d"],fillvalues["building_type"],building_type)
680      building_type = np.where((building_type == fillvalues["building_type"]) & (buildings_2d != fillvalues["buildings_2d"]),defaultvalues["building_type"],building_type)
681      print("Data check #2 " + str(check_arrays_2(buildings_2d,building_type,fillvalues["buildings_2d"],fillvalues["building_type"])))
682 
683
684   nc_write_to_file_2d(filename[i], 'buildings_2d', buildings_2d, datatypes["buildings_2d"],'y','x',fillvalues["buildings_2d"])
685   nc_write_attribute(filename[i], 'buildings_2d', 'long_name', 'buildings')
686   nc_write_attribute(filename[i], 'buildings_2d', 'units', 'm')
687   nc_write_attribute(filename[i], 'buildings_2d', 'res_orig', domain_px[i]) 
688   nc_write_attribute(filename[i], 'buildings_2d', 'lod', 1) 
689   nc_write_attribute(filename[i], 'buildings_2d', 'coordinates', 'E_UTM N_UTM lon lat') 
690   nc_write_attribute(filename[i], 'buildings_2d', 'grid_mapping', 'E_UTM N_UTM lon lat')   
691   
692   nc_write_to_file_2d(filename[i], 'building_id', building_id, datatypes["building_id"],'y','x',fillvalues["building_id"])
693   nc_write_attribute(filename[i], 'building_id', 'long_name', 'building id')
694   nc_write_attribute(filename[i], 'building_id', 'units', '')
695   nc_write_attribute(filename[i], 'building_id', 'res _orig', domain_px[i]) 
696   nc_write_attribute(filename[i], 'building_id', 'coordinates', 'E_UTM N_UTM lon lat') 
697   nc_write_attribute(filename[i], 'building_id', 'grid_mapping', 'E_UTM N_UTM lon lat')   
698   
699   nc_write_to_file_2d(filename[i], 'building_type', building_type, datatypes["building_type"],'y','x',fillvalues["building_type"])
700   nc_write_attribute(filename[i], 'building_type', 'long_name', 'building type')
701   nc_write_attribute(filename[i], 'building_type', 'units', '')
702   nc_write_attribute(filename[i], 'building_type', 'res_orig', domain_px[i]) 
703   nc_write_attribute(filename[i], 'building_type', 'coordinates', 'E_UTM N_UTM lon lat') 
704   nc_write_attribute(filename[i], 'building_type', 'grid_mapping', 'E_UTM N_UTM lon lat')   
705 
706del buildings_2d
707del building_id
708del building_type
709
710# Create 3d buildings if necessary. In that course, read bridge objects and add them to building layer
711for i in range(0,ndomains): 
712   
713   if domain_3d[i]:
714      x = nc_read_from_file_2d_all(filename[i], 'x')
715      y = nc_read_from_file_2d_all(filename[i], 'y') 
716      buildings_2d = nc_read_from_file_2d_all(filename[i], 'buildings_2d') 
717      building_id = nc_read_from_file_2d_all(filename[i], 'building_id') 
718     
719      bridges_2d = nc_read_from_file_2d(input_file_bridges_2d[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])
720      bridges_id = nc_read_from_file_2d(input_file_bridges_id[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])
721     
722      bridges_2d = np.where(bridges_2d == 0.0,fillvalues["bridges_2d"],bridges_2d)
723      building_id = np.where(bridges_2d == fillvalues["bridges_2d"],building_id,bridges_id)
724     
725      if np.any(buildings_2d != fillvalues["buildings_2d"]):
726         buildings_3d, z = make_3d_from_2d(buildings_2d,x,y,domain_dz[i])
727         if np.any(bridges_2d != fillvalues["bridges_2d"]):
728            buildings_3d = make_3d_from_bridges_2d(buildings_3d,bridges_2d,x,y,domain_dz[i],settings_bridge_width,fillvalues["bridges_2d"])
729         else:
730            print("Skipping creation of 3D bridges (no bridges in domain)")
731           
732           
733         nc_write_dimension(filename[i], 'z', z, datatypes["z"]) 
734         nc_write_attribute(filename[i], 'z', 'long_name', 'z') 
735         nc_write_attribute(filename[i], 'z', 'units', 'm')
736         
737         nc_overwrite_to_file_2d(filename[i], 'building_id', building_id) 
738 
739         nc_write_to_file_3d(filename[i], 'buildings_3d', buildings_3d, datatypes["buildings_3d"],'z','y','x',fillvalues["buildings_3d"])   
740         nc_write_attribute(filename[i], 'buildings_3d', 'long_name', 'buildings 3d')
741         nc_write_attribute(filename[i], 'buildings_3d', 'units', '')
742         nc_write_attribute(filename[i], 'buildings_3d', 'res_orig', domain_px[i]) 
743         nc_write_attribute(filename[i], 'buildings_3d', 'lod', 2) 
744         
745         del buildings_3d
746         
747      else:
748         print("Skipping creation of 3D buildings (no buildings in domain)")
749
750
751      del bridges_2d, bridges_id, building_id, buildings_2d
752
753
754
755# Read vegetation type, water_type, pavement_type, soil_type and make fields consistent
756for i in range(0,ndomains): 
757
758   building_type = nc_read_from_file_2d_all(filename[i], 'building_type') 
759   
760   vegetation_type = nc_read_from_file_2d(input_file_vegetation_type[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
761   vegetation_type[vegetation_type == 255] = fillvalues["vegetation_type"]
762   vegetation_type = np.where((vegetation_type < 1) & (vegetation_type != fillvalues["vegetation_type"]),defaultvalues["vegetation_type"],vegetation_type)
763   
764   pavement_type = nc_read_from_file_2d(input_file_pavement_type[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
765   pavement_type[pavement_type == 255] = fillvalues["pavement_type"]
766   pavement_type = np.where((pavement_type < 1) & (pavement_type != fillvalues["pavement_type"]),defaultvalues["pavement_type"],pavement_type)
767
768   water_type = nc_read_from_file_2d(input_file_water_type[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
769   water_type[water_type == 255] = fillvalues["water_type"]
770   water_type = np.where((water_type < 1) & (water_type != fillvalues["water_type"]),defaultvalues["water_type"],water_type)
771 
772#  to do: replace by real soil input data
773   soil_type = nc_read_from_file_2d(input_file_vegetation_type[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i]) 
774   soil_type[soil_type == 255] = fillvalues["soil_type"]
775   soil_type = np.where((soil_type < 1) & (soil_type != fillvalues["soil_type"]),defaultvalues["soil_type"],soil_type)
776
777#  Make arrays consistent
778#  #1 Set vegetation type to missing for pixel where a pavement type is set
779   vegetation_type = np.where((vegetation_type != fillvalues["vegetation_type"]) & (pavement_type != fillvalues["pavement_type"]),fillvalues["vegetation_type"],vegetation_type)
780
781#  #2 Set vegetation type to missing for pixel where a building type is set
782   vegetation_type = np.where((vegetation_type != fillvalues["vegetation_type"]) & (building_type != fillvalues["building_type"]) ,fillvalues["vegetation_type"],vegetation_type)
783
784#  #3 Set vegetation type to missing for pixel where a building type is set
785   vegetation_type = np.where((vegetation_type != fillvalues["vegetation_type"]) & (water_type != fillvalues["water_type"]),fillvalues["vegetation_type"],vegetation_type)   
786
787#  #4 Remove pavement for pixels with buildings
788   pavement_type = np.where((pavement_type != fillvalues["pavement_type"]) & (building_type != fillvalues["building_type"]),fillvalues["pavement_type"],pavement_type) 
789
790#  #5 Remove pavement for pixels with water.
791   pavement_type = np.where((pavement_type != fillvalues["pavement_type"]) & (water_type != fillvalues["water_type"]),fillvalues["pavement_type"],pavement_type) 
792   
793#  #6 Remove water for pixels with buildings
794   water_type = np.where((water_type != fillvalues["water_type"]) & (building_type != fillvalues["building_type"]),fillvalues["water_type"],water_type) 
795
796#  Correct vegetation_type when a vegetation height is available and is indicative of low vegeetation
797   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])   
798         
799   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)
800   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)
801 
802#  Check for consistency and fill empty fields with default vegetation type
803   consistency_array, test = check_consistency_4(vegetation_type,building_type,pavement_type,water_type,fillvalues["vegetation_type"],fillvalues["building_type"],fillvalues["pavement_type"],fillvalues["water_type"])
804   
805   if test:
806      vegetation_type = np.where(consistency_array == 0,defaultvalues["vegetation_type"],vegetation_type)
807      consistency_array, test = check_consistency_4(vegetation_type,building_type,pavement_type,water_type,fillvalues["vegetation_type"],fillvalues["building_type"],fillvalues["pavement_type"],fillvalues["water_type"])
808
809#  #7 to be removed: set default soil type everywhere
810   soil_type = np.where((vegetation_type != fillvalues["vegetation_type"]) | (pavement_type != fillvalues["pavement_type"]),defaultvalues["soil_type"],fillvalues["soil_type"]) 
811       
812   
813#  Check for consistency and fill empty fields with default vegetation type
814   consistency_array, test = check_consistency_3(vegetation_type,pavement_type,soil_type,fillvalues["vegetation_type"],fillvalues["pavement_type"],fillvalues["soil_type"])
815       
816#  Create surface_fraction array
817   x = nc_read_from_file_2d_all(filename[i], 'x')
818   y = nc_read_from_file_2d_all(filename[i], 'y') 
819   nsurface_fraction = np.arange(0,3)
820   surface_fraction = np.ones((len(nsurface_fraction),len(y),len(x)))
821   
822   surface_fraction[0,:,:] = np.where(vegetation_type != fillvalues["vegetation_type"], 1.0, 0.0)
823   surface_fraction[1,:,:] = np.where(pavement_type != fillvalues["pavement_type"], 1.0, 0.0)
824   surface_fraction[2,:,:] = np.where(water_type != fillvalues["water_type"], 1.0, 0.0)
825   
826   nc_write_dimension(filename[i], 'nsurface_fraction', nsurface_fraction, datatypes["nsurface_fraction"]) 
827   nc_write_to_file_3d(filename[i], 'surface_fraction', surface_fraction, datatypes["surface_fraction"],'nsurface_fraction','y','x',fillvalues["surface_fraction"])     
828   nc_write_attribute(filename[i], 'surface_fraction', 'long_name', 'surface fraction')
829   nc_write_attribute(filename[i], 'surface_fraction', 'units', '')
830   nc_write_attribute(filename[i], 'surface_fraction', 'res_orig', domain_px[i])     
831   del surface_fraction   
832
833   nc_write_to_file_2d(filename[i], 'vegetation_type', vegetation_type, datatypes["vegetation_type"],'y','x',fillvalues["vegetation_type"])     
834   nc_write_attribute(filename[i], 'vegetation_type', 'long_name', 'vegetation type')
835   nc_write_attribute(filename[i], 'vegetation_type', 'units', '')
836   nc_write_attribute(filename[i], 'vegetation_type', 'res_orig', domain_px[i]) 
837   nc_write_attribute(filename[i], 'vegetation_type', 'coordinates', 'E_UTM N_UTM lon lat') 
838   nc_write_attribute(filename[i], 'vegetation_type', 'grid_mapping', 'E_UTM N_UTM lon lat')   
839   del vegetation_type
840
841   nc_write_to_file_2d(filename[i], 'pavement_type', pavement_type, datatypes["pavement_type"],'y','x',fillvalues["pavement_type"])   
842   nc_write_attribute(filename[i], 'pavement_type', 'long_name', 'pavement type')
843   nc_write_attribute(filename[i], 'pavement_type', 'units', '')
844   nc_write_attribute(filename[i], 'pavement_type', 'res_orig', domain_px[i]) 
845   nc_write_attribute(filename[i], 'pavement_type', 'coordinates', 'E_UTM N_UTM lon lat') 
846   nc_write_attribute(filename[i], 'pavement_type', 'grid_mapping', 'E_UTM N_UTM lon lat')   
847   del pavement_type
848
849   nc_write_to_file_2d(filename[i], 'water_type', water_type, datatypes["water_type"],'y','x',fillvalues["water_type"]) 
850   nc_write_attribute(filename[i], 'water_type', 'long_name', 'water type')
851   nc_write_attribute(filename[i], 'water_type', 'units', '')
852   nc_write_attribute(filename[i], 'water_type', 'res_orig', domain_px[i]) 
853   nc_write_attribute(filename[i], 'water_type', 'coordinates', 'E_UTM N_UTM lon lat') 
854   nc_write_attribute(filename[i], 'water_type', 'grid_mapping', 'E_UTM N_UTM lon lat')   
855   del water_type
856
857   nc_write_to_file_2d(filename[i], 'soil_type', soil_type, datatypes["soil_type"],'y','x',fillvalues["soil_type"]) 
858   nc_write_attribute(filename[i], 'soil_type', 'long_name', 'soil type')
859   nc_write_attribute(filename[i], 'soil_type', 'units', '')
860   nc_write_attribute(filename[i], 'soil_type', 'res_orig', domain_px[i])   
861   nc_write_attribute(filename[i], 'soil_type', 'coordinates', 'E_UTM N_UTM lon lat') 
862   nc_write_attribute(filename[i], 'soil_type', 'grid_mapping', 'E_UTM N_UTM lon lat')   
863   del soil_type
864
865   del x
866   del y
867
868#  pixels with bridges get building_type = 7 = bridge. This does not change the _type setting for the under-bridge
869#  area NOTE: when bridges are present the consistency check will fail at the moment
870   if domain_3d[i]:
871      if np.any(building_type != fillvalues["building_type"]):
872 
873         bridges_2d = nc_read_from_file_2d(input_file_bridges_2d[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])         
874         bridges_2d = np.where(bridges_2d == 0.0,fillvalues["bridges_2d"],bridges_2d)
875         building_type = np.where(bridges_2d != fillvalues["bridges_2d"],7,building_type)
876         nc_overwrite_to_file_2d(filename[i], 'building_type', building_type) 
877 
878         del building_type 
879         del bridges_2d
880
881# Read/write street type and street crossings
882for i in range(0,ndomains): 
883
884   street_type = nc_read_from_file_2d(input_file_street_type[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
885   street_type[street_type == 255] = fillvalues["street_type"]
886   street_type = np.where((street_type < 1) & (street_type != fillvalues["street_type"]),defaultvalues["street_type"],street_type)
887
888   pavement_type = nc_read_from_file_2d_all(filename[i], 'pavement_type') 
889   street_type = np.where((pavement_type == fillvalues["pavement_type"]),fillvalues["street_type"],street_type) 
890
891   nc_write_to_file_2d(filename[i], 'street_type', street_type, datatypes["street_type"],'y','x',fillvalues["street_type"]) 
892   nc_write_attribute(filename[i], 'street_type', 'long_name', 'street type')
893   nc_write_attribute(filename[i], 'street_type', 'units', '')
894   nc_write_attribute(filename[i], 'street_type', 'res_orig', domain_px[i]) 
895   nc_write_attribute(filename[i], 'street_type', 'coordinates', 'E_UTM N_UTM lon lat') 
896   nc_write_attribute(filename[i], 'street_type', 'grid_mapping', 'E_UTM N_UTM lon lat')   
897   del street_type
898   
899   street_crossings = nc_read_from_file_2d(input_file_street_crossings[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
900   street_crossings[street_crossings == 255] = fillvalues["street_crossings"]
901   street_crossings = np.where((street_crossings < 1) & (street_crossings != fillvalues["street_crossings"]),defaultvalues["street_crossings"],street_crossings)
902   
903   nc_write_to_file_2d(filename[i], 'street_crossing', street_crossings, datatypes["street_crossings"],'y','x',fillvalues["street_crossings"]) 
904   nc_write_attribute(filename[i], 'street_crossing', 'long_name', 'street crossings')
905   nc_write_attribute(filename[i], 'street_crossing', 'units', '')
906   nc_write_attribute(filename[i], 'street_crossing', 'res_orig', domain_px[i]) 
907   nc_write_attribute(filename[i], 'street_crossing', 'coordinates', 'E_UTM N_UTM lon lat') 
908   nc_write_attribute(filename[i], 'street_crossing', 'grid_mapping', 'E_UTM N_UTM lon lat')   
909   del street_crossings
910
911
912# Read/write vegetation on roofs
913for i in range(0,ndomains): 
914   if domain_green_roofs[i]: 
915      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])   
916      buildings_2d = nc_read_from_file_2d_all(filename[i], 'buildings_2d') 
917
918
919      x = nc_read_from_file_2d_all(filename[i], 'x')
920      y = nc_read_from_file_2d_all(filename[i], 'y') 
921      nbuilding_pars = np.arange(0,47)
922      building_pars = np.ones((len(nbuilding_pars),len(y),len(x)))
923      building_pars[:,:,:] = fillvalues["building_pars"]
924
925#     assign green fraction on roofs
926      building_pars[3,:,:] = np.where( ( buildings_2d != fillvalues["buildings_2d"] ) & ( green_roofs != fillvalues["building_pars"] ) & ( green_roofs != 0.0 ),1,fillvalues["building_pars"] )
927
928#     assign leaf area index for vegetation on roofs
929      building_pars[4,:,:] = np.where( ( building_pars[3,:,:] != fillvalues["building_pars"] ) & ( green_roofs >= 0.5 ),settings_lai_roof_intensive,fillvalues["building_pars"])
930      building_pars[4,:,:] = np.where( ( building_pars[3,:,:] != fillvalues["building_pars"] ) & ( green_roofs < 0.5 ),settings_lai_roof_extensive,building_pars[4,:,:])
931   
932   
933      nc_write_dimension(filename[i], 'nbuilding_pars', nbuilding_pars, datatypes["nbuilding_pars"]) 
934      nc_write_to_file_3d(filename[i], 'building_pars', building_pars, datatypes["building_pars"],'nbuilding_pars','y','x',fillvalues["building_pars"]) 
935      nc_write_attribute(filename[i], 'building_pars', 'long_name', 'building_pars')
936      nc_write_attribute(filename[i], 'building_pars', 'units', '')
937      nc_write_attribute(filename[i], 'building_pars', 'res_orig', domain_px[i]) 
938      nc_write_attribute(filename[i], 'building_pars', 'coordinates', 'E_UTM N_UTM lon lat') 
939      nc_write_attribute(filename[i], 'building_pars', 'grid_mapping', 'E_UTM N_UTM lon lat')   
940         
941      del building_pars, buildings_2d, x, y
942
943
944# Read tree data and create LAD and BAD arrays using the canopy generator
945for i in range(0,ndomains): 
946   lai = nc_read_from_file_2d(input_file_lai[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
947     
948   vegetation_type = nc_read_from_file_2d_all(filename[i], 'vegetation_type')
949     
950   lai = np.where(vegetation_type == fillvalues["vegetation_type"],fillvalues["vegetation_pars"],lai)
951
952     
953   x = nc_read_from_file_2d_all(filename[i], 'x')
954   y = nc_read_from_file_2d_all(filename[i], 'y') 
955   nvegetation_pars = np.arange(0,12)
956   vegetation_pars = np.ones((len(nvegetation_pars),len(y),len(x)))
957   vegetation_pars[:,:,:] = fillvalues["vegetation_pars"] 
958     
959   vegetation_pars[1,:,:] = lai
960     
961
962#  Write out first version of LAI. Will later be overwritten.
963   nc_write_dimension(filename[i], 'nvegetation_pars', nvegetation_pars, datatypes["nvegetation_pars"]) 
964   nc_write_to_file_3d(filename[i], 'vegetation_pars', vegetation_pars, datatypes["vegetation_pars"],'nvegetation_pars','y','x',fillvalues["vegetation_pars"]) 
965   nc_write_attribute(filename[i], 'vegetation_pars', 'long_name', 'vegetation_pars')
966   nc_write_attribute(filename[i], 'vegetation_pars', 'units', '')
967   nc_write_attribute(filename[i], 'vegetation_pars', 'res_orig', domain_px[i]) 
968   nc_write_attribute(filename[i], 'vegetation_pars', 'coordinates', 'E_UTM N_UTM lon lat') 
969   nc_write_attribute(filename[i], 'vegetation_pars', 'grid_mapping', 'E_UTM N_UTM lon lat')   
970
971   del lai, vegetation_pars, vegetation_type
972
973# Read tree data and create LAD and BAD arrays using the canopy generator
974for i in range(0,ndomains): 
975   if domain_street_trees[i]: 
976
977      vegetation_pars =  nc_read_from_file_2d_all(filename[i], 'vegetation_pars')
978
979      lai = nc_read_from_file_2d(input_file_lai[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
980
981      x = nc_read_from_file_2d_all(filename[i], 'x')
982      y = nc_read_from_file_2d_all(filename[i], 'y') 
983
984#     Save lai data as default for low and high vegetation
985      lai_low = lai
986      lai_high = lai
987   
988#     Read all tree parameters from file
989      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])   
990
991      if (input_file_tree_crown_diameter[ii[i]] is not None):
992         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])   
993         tree_crown_diameter = np.where( (tree_crown_diameter == 0.0) | (tree_crown_diameter == -1.0) ,fillvalues["tree_data"],tree_crown_diameter)
994      else:
995         tree_crown_diameter = np.ones((len(y),len(x)))
996         tree_crown_diameter[:,:] = fillvalues["tree_data"]
997
998
999      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])         
1000      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]) 
1001      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]) 
1002     
1003#     Remove missing values from the data. Reasonable values will be set by the tree generator   
1004      tree_height = np.where( (tree_height == 0.0) | (tree_height == -1.0) ,fillvalues["tree_data"],tree_height)
1005      tree_trunk_diameter = np.where( (tree_trunk_diameter == 0.0) | (tree_trunk_diameter == -1.0) ,fillvalues["tree_data"],tree_trunk_diameter)
1006      tree_type = np.where( (tree_type == 0.0) | (tree_type == -1.0) ,fillvalues["tree_data"],tree_type)
1007     
1008#     For vegetation pixel with missing height information (-1), set default patch height   
1009      patch_height = np.where( patch_height == -1.0 ,settings_patch_height_default,patch_height)     
1010
1011#     Convert trunk diameter from cm to m
1012      tree_trunk_diameter = np.where(tree_trunk_diameter != fillvalues["tree_data"], tree_trunk_diameter * 0.01,tree_trunk_diameter)
1013
1014
1015#     Temporarily change missing value for tree_type
1016      tree_type = np.where( (tree_type == fillvalues["tree_type"]),fillvalues["tree_data"],tree_type)   
1017
1018#     Compare patch height array with vegetation type and correct accordingly
1019      vegetation_type = nc_read_from_file_2d_all(filename[i], 'vegetation_type') 
1020
1021
1022#     For zero-height patches, set vegetation_type to short grass and remove these pixels from the patch height array
1023      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) 
1024      patch_height = 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)       
1025
1026      max_tree_height = max(tree_height.flatten())
1027      max_patch_height = max(patch_height.flatten())
1028     
1029#     Call canopy generator for single trees only if there is any tree height available in the domain.
1030#     This does not guarantee that there are street trees that can be processed. This is checked in the
1031#     canopy generator.
1032      if ( (max_tree_height != fillvalues["tree_data"]) | (max_patch_height != fillvalues["tree_data"]) ):
1033
1034         lad, bad, tree_ids, tree_types, 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"])
1035 
1036#        Remove LAD volumes that are inside buildings
1037         if not domain_overhanging_trees[i]:
1038            buildings_2d = nc_read_from_file_2d_all(filename[i], 'buildings_2d') 
1039            for k in range(0,len(zlad)-1):
1040 
1041               lad[k,:,:] = np.where(buildings_2d == fillvalues["buildings_2d"],lad[k,:,:],fillvalues["tree_data"])
1042               bad[k,:,:] = np.where(buildings_2d == fillvalues["buildings_2d"],bad[k,:,:],fillvalues["tree_data"])
1043               tree_ids[k,:,:] = np.where(buildings_2d == fillvalues["buildings_2d"],tree_ids[k,:,:],fillvalues["tree_id"])     
1044
1045            del buildings_2d
1046 
1047 
1048         nc_write_dimension(filename[i], 'zlad', zlad, datatypes["tree_data"]) 
1049
1050         nc_write_to_file_3d(filename[i], 'lad', lad, datatypes["tree_data"],'zlad','y','x',fillvalues["tree_data"]) 
1051         nc_write_attribute(filename[i], 'lad', 'long_name', 'leaf area density')
1052         nc_write_attribute(filename[i], 'lad', 'units', '')
1053         nc_write_attribute(filename[i], 'lad', 'res_orig', domain_px[i]) 
1054         nc_write_attribute(filename[i], 'lad', 'coordinates', 'E_UTM N_UTM lon lat') 
1055         nc_write_attribute(filename[i], 'lad', 'grid_mapping', 'E_UTM N_UTM lon lat') 
1056 
1057         nc_write_to_file_3d(filename[i], 'bad', bad, datatypes["tree_data"],'zlad','y','x',fillvalues["tree_data"]) 
1058         nc_write_attribute(filename[i], 'bad', 'long_name', 'basal area density')
1059         nc_write_attribute(filename[i], 'bad', 'units', '')
1060         nc_write_attribute(filename[i], 'bad', 'res_orig', domain_px[i]) 
1061         nc_write_attribute(filename[i], 'bad', 'coordinates', 'E_UTM N_UTM lon lat') 
1062         nc_write_attribute(filename[i], 'bad', 'grid_mapping', 'E_UTM N_UTM lon lat')
1063           
1064         nc_write_to_file_3d(filename[i], 'tree_id', tree_ids, datatypes["tree_id"],'zlad','y','x',fillvalues["tree_id"]) 
1065         nc_write_attribute(filename[i], 'tree_id', 'long_name', 'tree id')
1066         nc_write_attribute(filename[i], 'tree_id', 'units', '')
1067         nc_write_attribute(filename[i], 'tree_id', 'res_orig', domain_px[i]) 
1068         nc_write_attribute(filename[i], 'tree_id', 'coordinates', 'E_UTM N_UTM lon lat') 
1069         nc_write_attribute(filename[i], 'tree_id', 'grid_mapping', 'E_UTM N_UTM lon lat')           
1070
1071         nc_write_to_file_3d(filename[i], 'tree_type', tree_types, datatypes["tree_types"],'zlad','y','x',fillvalues["tree_types"]) 
1072         nc_write_attribute(filename[i], 'tree_type', 'long_name', 'tree type')
1073         nc_write_attribute(filename[i], 'tree_type', 'units', '')
1074         nc_write_attribute(filename[i], 'tree_type', 'res_orig', domain_px[i]) 
1075         nc_write_attribute(filename[i], 'tree_type', 'coordinates', 'E_UTM N_UTM lon lat') 
1076         nc_write_attribute(filename[i], 'tree_type', 'grid_mapping', 'E_UTM N_UTM lon lat')   
1077           
1078         del lai, lad, bad, tree_ids, tree_types, zlad
1079
1080      else:
1081         print('No street trees generated in domain ' + str(i))
1082         
1083      del vegetation_pars, tree_height, tree_crown_diameter, tree_trunk_diameter, tree_type, patch_height, x, y
1084
1085
1086# Create vegetation patches for locations with high vegetation type
1087for i in range(0,ndomains): 
1088   if domain_canopy_patches[i]: 
1089
1090      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]) 
1091
1092#     For vegetation pixel with missing height information (-1), set default patch height     
1093      patch_height = np.where( patch_height == -1.0 ,settings_patch_height_default,patch_height)
1094     
1095      max_patch_height = max(patch_height.flatten())
1096      if ( max_patch_height != fillvalues["tree_data"] ):
1097#        Call canopy generator for single trees only if there is any tree height available in the domain.
1098#        This does not guarantee that there are street trees that can be processed. This is checked in the
1099#        canopy generator.
1100
1101#        Load vegetation_type and lad array (at level z = 0) for re-processing
1102         vegetation_type = nc_read_from_file_2d_all(filename[i], 'vegetation_type') 
1103         lad = nc_read_from_file_3d_all(filename[i], 'lad') 
1104         tree_id = nc_read_from_file_3d_all(filename[i], 'tree_id') 
1105         tree_types = nc_read_from_file_3d_all(filename[i], 'tree_type') 
1106         zlad = nc_read_from_file_1d_all(filename[i], 'zlad') 
1107         vegetation_pars = nc_read_from_file_3d_all(filename[i], 'vegetation_pars') 
1108         lai = nc_read_from_file_2d(input_file_lai[ii[i]], 'Band1', domain_x0[i], domain_x1[i], domain_y0[i], domain_y1[i])   
1109   
1110
1111#        Determine all pixels that do not already have an LAD but which are high vegetation to a dummy value of 1.0 and remove all other pixels
1112         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])) ),1.0,fillvalues["tree_data"])
1113
1114
1115#        Treat all pixels where short grass is defined, but where a patch_height >= dz is found, as high vegetation (often the case in backyards)
1116         lai_high = np.where( (lai_high == fillvalues["tree_data"]) & (patch_height >= domain_dz[i]) & (vegetation_type == 3), 1.0, lai_high)
1117                         
1118#        If overhanging trees are allowed, assign pixels with patch_height > dz that are not included in vegetation_type to high
1119         if domain_overhanging_trees[i]:
1120            lai_high = np.where( (lai_high == fillvalues["tree_data"]) & (patch_height >= domain_dz[i]), 1.0, lai_high)
1121
1122#        Now, assign either the default LAI for high vegetation or keep 1.0 from the lai_high array.
1123         lai_high = np.where( (lai_high != fillvalues["tree_data"]) & (lai == fillvalues["tree_data"]), settings_lai_high_default, lai_high)
1124
1125#        If LAI values are available in the LAI array, write them on the lai_high array
1126         lai_high = np.where( (lai_high != fillvalues["tree_data"]) & (lai != fillvalues["tree_data"]), lai, lai_high)
1127
1128
1129#        Define a patch height wherever it is missing, but where a high vegetation LAI was set
1130         patch_height = np.where ( (lai_high != fillvalues["tree_data"]) & (patch_height == fillvalues["tree_data"]), settings_patch_height_default, patch_height)
1131
1132
1133#        Remove pixels where street trees were already se
1134         patch_height = np.where ( (lad[0,:,:] != fillvalues["tree_data"]), fillvalues["tree_data"], patch_height)
1135
1136#        Remove patch heights that have no lai_high value
1137         patch_height = np.where ( (lai_high == fillvalues["tree_data"]), fillvalues["tree_data"], patch_height)
1138
1139
1140
1141
1142#        For missing LAI values, set either the high vegetation default or the low vegetation default   
1143         lai_high = np.where( (patch_height > 2.0) & (patch_height != fillvalues["tree_data"]) & (lai_high == fillvalues["tree_data"]),settings_lai_high_default,lai_high)
1144         lai_high = np.where( (patch_height <= 2.0) & (patch_height != fillvalues["tree_data"]) & (lai_high == fillvalues["tree_data"]),settings_lai_low_default,lai_high)
1145         
1146         if ( max(patch_height.flatten()) >= (2.0 * domain_dz[i]) ):
1147            print("    start calculating LAD (this might take some time)")       
1148
1149         
1150            lad_patch, patch_id, patch_types, patch_nz, status = process_patch(domain_dz[i],patch_height,vegetation_type,max(zlad),lai_high,settings_lai_alpha,settings_lai_beta)
1151
1152#           Set negative ids for patches
1153            patch_id = np.where( patch_id != fillvalues["tree_id"], -patch_id, patch_id)
1154
1155#           2D loop in order to avoid memory problems with large arrays
1156            for iii in range(0,domain_nx[i]):
1157               for jj in range(0,domain_ny[i]):           
1158
1159                  tree_id[0:patch_nz+1,jj,iii] = np.where( (lad[0:patch_nz+1,jj,iii] == fillvalues["tree_data"]),patch_id[0:patch_nz+1,jj,iii], tree_id[0:patch_nz+1,jj,iii] )
1160                  tree_types[0:patch_nz+1,jj,iii] = np.where( (lad[0:patch_nz+1,jj,iii] == fillvalues["tree_data"]),patch_types[0:patch_nz+1,jj,iii], tree_types[0:patch_nz+1,jj,iii] )                 
1161                  lad[0:patch_nz+1,jj,iii]     = np.where( (lad[0:patch_nz+1,jj,iii] == fillvalues["tree_data"]),lad_patch[0:patch_nz+1,jj,iii], lad[0:patch_nz+1,jj,iii] )
1162
1163#        Remove high vegetation wherever it is replaced by a leaf area density. This should effectively remove all high vegetation pixels
1164         vegetation_type = np.where((lad[0,:,:] != fillvalues["tree_data"]) & (vegetation_type != fillvalues["vegetation_type"]),settings_veg_type_below_trees,vegetation_type)
1165             
1166#        If desired, remove all high vegetation. TODO: check if this is still necessary
1167         if not domain_high_vegetation[i]:
1168            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)   
1169
1170 
1171#        Set default low LAI for pixels with an LAD (short grass below trees)
1172         lai_low = np.where( (lad[0,:,:] == fillvalues["tree_data"]), lai, settings_lai_low_default)
1173   
1174#        Fill low vegetation pixels without LAI set or with LAI = 0 with default value
1175         lai_low = np.where( ( (lai_low == fillvalues["tree_data"]) | (lai_low == 0.0) ) & (vegetation_type != fillvalues["vegetation_type"] ), settings_lai_low_default, lai_low)
1176
1177#        Remove lai for pixels that have no vegetation_type
1178         lai_low = np.where( ( (vegetation_type != fillvalues["vegetation_type"]) & (vegetation_type != 1) ), lai_low, fillvalues["tree_data"])
1179       
1180#        Overwrite lai in vegetation_parameters 
1181         vegetation_pars[1,:,:] = np.copy(lai_low)
1182         nc_overwrite_to_file_3d(filename[i], 'vegetation_pars', vegetation_pars) 
1183
1184#        Overwrite lad and id arrays
1185         nc_overwrite_to_file_3d(filename[i], 'lad', lad) 
1186         nc_overwrite_to_file_3d(filename[i], 'tree_id', tree_id) 
1187         nc_overwrite_to_file_3d(filename[i], 'tree_type', tree_types) 
1188
1189         nc_overwrite_to_file_2d(filename[i], 'vegetation_type', vegetation_type) 
1190
1191
1192         del vegetation_type, lad, lai, patch_height, vegetation_pars, zlad
1193      else:
1194         print('No tree patches found in domain ' + str(i))
1195
1196# Final adjustment of vegetation parameters: remove LAI where a bare soil was set
1197for i in range(0,ndomains): 
1198   
1199      vegetation_type = nc_read_from_file_2d_all(filename[i], 'vegetation_type') 
1200      vegetation_pars = nc_read_from_file_3d_all(filename[i], 'vegetation_pars') 
1201      lai = vegetation_pars[1,:,:]   
1202     
1203     
1204#     Remove lai for pixels that have no vegetation_type
1205      lai = np.where( ( (vegetation_type != fillvalues["vegetation_type"]) & (vegetation_type != 1) ), lai, fillvalues["tree_data"])
1206       
1207#     Overwrite lai in vegetation_parameters 
1208      vegetation_pars[1,:,:] = np.copy(lai)
1209      nc_overwrite_to_file_3d(filename[i], 'vegetation_pars', vegetation_pars) 
1210     
1211      del vegetation_type, lai, vegetation_pars
1212
1213
1214# Final consistency check
1215for i in range(0,ndomains): 
1216   vegetation_type = nc_read_from_file_2d_all(filename[i], 'vegetation_type') 
1217   pavement_type = nc_read_from_file_2d_all(filename[i], 'pavement_type') 
1218   building_type = nc_read_from_file_2d_all(filename[i], 'building_type') 
1219   water_type = nc_read_from_file_2d_all(filename[i], 'water_type')     
1220   soil_type = nc_read_from_file_2d_all(filename[i], 'soil_type') 
1221
1222#  Check for consistency and fill empty fields with default vegetation type
1223   consistency_array, test = check_consistency_4(vegetation_type,building_type,pavement_type,water_type,fillvalues["vegetation_type"],fillvalues["building_type"],fillvalues["pavement_type"],fillvalues["water_type"])
1224
1225#  Check for consistency and fill empty fields with default vegetation type
1226   consistency_array, test = check_consistency_3(vegetation_type,pavement_type,soil_type,fillvalues["vegetation_type"],fillvalues["pavement_type"],fillvalues["soil_type"])
1227
1228   surface_fraction = nc_read_from_file_3d_all(filename[i], 'surface_fraction')       
1229   surface_fraction[0,:,:] = np.where(vegetation_type != fillvalues["vegetation_type"], 1.0, 0.0)
1230   surface_fraction[1,:,:] = np.where(pavement_type != fillvalues["pavement_type"], 1.0, 0.0)
1231   surface_fraction[2,:,:] = np.where(water_type != fillvalues["water_type"], 1.0, 0.0)
1232   nc_overwrite_to_file_3d(filename[i], 'surface_fraction', surface_fraction) 
1233       
1234   del vegetation_type, pavement_type, building_type, water_type, soil_type
Note: See TracBrowser for help on using the repository browser.