source: palm/trunk/SCRIPTS/palm_csd @ 4749

Last change on this file since 4749 was 4749, checked in by maronga, 4 years ago

bugfix in palm_csd

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