source: palm/trunk/SCRIPTS/palm_csd @ 4459

Last change on this file since 4459 was 4311, checked in by maronga, 5 years ago

bugfix in palm_csd regarding green roofs

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