source: palm/trunk/SCRIPTS/palm_csd @ 4254

Last change on this file since 4254 was 4149, checked in by suehring, 5 years ago

palm_csd: bugfix in dimension of building_pars

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