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

Last change on this file since 3681 was 3668, checked in by maronga, 6 years ago

removed most_methods circular and lookup. added improved version of palm_csd

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