source: palm/trunk/SCRIPTS/palm_csd_files/palm_csd_tools.py @ 4022

Last change on this file since 4022 was 4021, checked in by maronga, 5 years ago

minor changes in palm_csd

File size: 5.4 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#--------------------------------------------------------------------------------#
4# This file is part of the PALM model system.
5#
6# PALM is free software: you can redistribute it and/or modify it under the terms
7# of the GNU General Public License as published by the Free Software Foundation,
8# either version 3 of the License, or (at your option) any later version.
9#
10# PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along with
15# PALM. If not, see <http://www.gnu.org/licenses/>.
16#
17# Copyright 1997-2018  Leibniz Universitaet Hannover
18#--------------------------------------------------------------------------------#
19#
20# Current revisions:
21# -----------------
22# Bugfix: missing conversion to integer
23#
24#
25# Former revisions:
26# -----------------
27# $Id: palm_csd_tools.py 3773 2019-03-01 08:56:57Z maronga $
28# Unspecified changes
29#
30# 3726 2019-02-07 18:22:49Z maronga
31# Index bound bugfix
32#
33# 3668 2019-01-14 12:49:24Z maronga
34# Minor changes
35#
36# 3567 2018-11-27 13:59:21Z maronga
37# Initial revisions
38#
39#
40# Description:
41# ------------
42# Support routines for palm_csd
43#
44# @Author Bjoern Maronga (maronga@muk.uni-hannover.de)
45#
46#------------------------------------------------------------------------------#
47import numpy as np
48from scipy.interpolate import interp2d
49
50def blend_array_2d(array1,array2,radius):
51#  Blend over the parent and child terrain height within a radius of 50 px
52   
53   gradient_matrix = np.copy(array1)
54   gradient_matrix[:,:] = 1.0
55   gradient_px     = 50
56
57   for i in range(0,gradient_px):
58      gradient_matrix[:,i] = float(i)/float(gradient_px)
59
60
61   for i in range(len(gradient_matrix[0,:])-gradient_px,len(gradient_matrix[0,:])):
62      gradient_matrix[:,i] = float(len(gradient_matrix[0,:])-i)/float(gradient_px) 
63     
64
65   for j in range(0,gradient_px):
66      for i in range(0,len(gradient_matrix[0,:])):
67         if  gradient_matrix[j,i] == 1.0:
68            gradient_matrix[j,i] = float(j)/float(gradient_px) 
69         else:
70            gradient_matrix[j,i] = (gradient_matrix[j,i] + float(j)/float(gradient_px))/2.0
71
72
73   for j in range(len(gradient_matrix[:,0])-gradient_px,len(gradient_matrix[:,0])):
74      for i in range(0,len(gradient_matrix[0,:])):       
75         if  gradient_matrix[j,i] == 1.0:
76            gradient_matrix[j,i] = (len(gradient_matrix[:,0])-j)/float(gradient_px)
77         else:
78            gradient_matrix[j,i] = (gradient_matrix[j,i] + (len(gradient_matrix[:,0])-j)/float(gradient_px))/2.0
79
80   array_blended = array1 * gradient_matrix + (1.0 - gradient_matrix ) * array2
81       
82   return array_blended
83
84
85def interpolate_2d(array,x1,y1,x2,y2):
86   tmp_int2d = interp2d(x1,y1,array,kind='linear')
87   array_ip = tmp_int2d(x2.astype(float), y2.astype(float)) 
88
89   return array_ip
90   
91   
92def bring_to_palm_grid(array,x,y,dz):
93   
94#     Bring the parent terrain height to the child grid 
95      k_tmp = np.arange(0,max(array.flatten())+dz*2,dz)
96      k_tmp[1:] = k_tmp[1:] - dz * 0.5
97      for l in range(0,len(x)):
98         for m in range(0,len(y)):
99            for k in range(1,len(k_tmp+1)):
100               if k_tmp[k] > array[m,l]:
101                  array[m,l] = k_tmp[k-1] + dz * 0.5
102                  break
103
104      return array 
105
106
107def make_3d_from_2d(array_2d,x,y,dz):
108   
109      k_tmp = np.arange(0,max(array_2d.flatten())+dz*2,dz)
110 
111      k_tmp[1:] = k_tmp[1:] - dz * 0.5
112      array_3d = np.ones((len(k_tmp),len(y),len(x)))
113     
114      for l in range(0,len(x)):
115         for m in range(0,len(y)):
116            for k in range(0,len(k_tmp)):
117               if k_tmp[k] > array_2d[m,l]:
118                  array_3d[k,m,l] = 0
119
120      return array_3d.astype(np.byte), k_tmp
121
122
123def make_3d_from_bridges_2d(array_3d,array_2d,x,y,dz,width,fill):
124     
125      for l in range(0,len(x)-1):
126         for m in range(0,len(y)-1):
127            if array_2d[m,l] != fill:
128               k_min = max( int((array_2d[m,l] - width)/dz), 0 )
129               k_max = int(round(array_2d[m,l]/dz))
130               array_3d[k_min:k_max+1,m,l] = 1
131
132
133      return array_3d.astype(np.byte)
134
135
136def check_arrays_2(array1,array2,fill1,fill2):
137
138   missing1 = np.where(array1 == fill1,1,0)
139   missing2 = np.where(array2 == fill2,1,0)
140   result = np.array_equal(missing1,missing2)
141   
142   return result
143
144def check_consistency_3(array1,array2,array3,fill1,fill2,fill3):
145
146   tmp_array = np.where(array1 != fill1,1,0) + np.where(array2 != fill2,1,0) + np.where(array3 != fill3,-1,0)
147   
148   test = np.any(tmp_array.flatten() != 0)
149   if test:
150      print("soil_type array is not consistent!")
151      print("max: " + str(max(tmp_array.flatten())) + ", min: " + str(min(tmp_array.flatten())))
152   else:
153      print("soil_type array is consistent!")     
154   return tmp_array, test
155
156
157
158
159def check_consistency_4(array1,array2,array3,array4,fill1,fill2,fill3,fill4):
160
161   tmp_array = np.where(array1 != fill1,1,0) + np.where(array2 != fill2,1,0) + np.where(array3 != fill3,1,0) + np.where(array4 != fill4,1,0)
162   
163   test = np.any(tmp_array.flatten() != 1)
164   if test:
165      print("*_type arrays are not consistent!")
166      print("max: " + str(max(tmp_array.flatten())) + ", min: " + str(min(tmp_array.flatten())))
167   else:
168      print("*_type arrays are consistent!")     
169   return tmp_array, test
170
Note: See TracBrowser for help on using the repository browser.