source: palm/trunk/SCRIPTS/palm_csd @ 3775

Last change on this file since 3775 was 3773, checked in by maronga, 6 years ago

added output of theta_2m_av and minor revisions in palm_csd

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