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

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

added output of theta_2m_av and minor revisions in palm_csd

  • Property svn:keywords set to Id
File size: 5.3 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 3773 2019-03-01 08:56:57Z raasch $
27# Unspecificed changes
28#
29# 3726 2019-02-07 18:22:49Z maronga
30# Index bound bugfix
31#
32# 3668 2019-01-14 12:49:24Z maronga
33# Minor changes
34#
35# 3567 2018-11-27 13:59:21Z maronga
36# Initial revisions
37#
38#
39# Description:
40# ------------
41# Support routines for palm_csd
42#
43# @Author Bjoern Maronga (maronga@muk.uni-hannover.de)
44#
45#------------------------------------------------------------------------------#
46import numpy as np
47from scipy.interpolate import interp2d
48
49def blend_array_2d(array1,array2,radius):
50#  Blend over the parent and child terrain height within a radius of 50 px
51   
52   gradient_matrix = np.copy(array1)
53   gradient_matrix[:,:] = 1.0
54   gradient_px     = 50
55
56   for i in range(0,gradient_px):
57      gradient_matrix[:,i] = float(i)/float(gradient_px)
58
59
60   for i in range(len(gradient_matrix[0,:])-gradient_px,len(gradient_matrix[0,:])):
61      gradient_matrix[:,i] = float(len(gradient_matrix[0,:])-i)/float(gradient_px) 
62     
63
64   for j in range(0,gradient_px):
65      for i in range(0,len(gradient_matrix[0,:])):
66         if  gradient_matrix[j,i] == 1.0:
67            gradient_matrix[j,i] = float(j)/float(gradient_px) 
68         else:
69            gradient_matrix[j,i] = (gradient_matrix[j,i] + float(j)/float(gradient_px))/2.0
70
71
72   for j in range(len(gradient_matrix[:,0])-gradient_px,len(gradient_matrix[:,0])):
73      for i in range(0,len(gradient_matrix[0,:])):       
74         if  gradient_matrix[j,i] == 1.0:
75            gradient_matrix[j,i] = (len(gradient_matrix[:,0])-j)/float(gradient_px)
76         else:
77            gradient_matrix[j,i] = (gradient_matrix[j,i] + (len(gradient_matrix[:,0])-j)/float(gradient_px))/2.0
78
79   array_blended = array1 * gradient_matrix + (1.0 - gradient_matrix ) * array2
80       
81   return array_blended
82
83
84def interpolate_2d(array,x1,y1,x2,y2):
85   tmp_int2d = interp2d(x1,y1,array,kind='linear')
86   array_ip = tmp_int2d(x2.astype(float), y2.astype(float)) 
87
88   return array_ip
89   
90   
91def bring_to_palm_grid(array,x,y,dz):
92   
93#     Bring the parent terrain height to the child grid 
94      k_tmp = np.arange(0,max(array.flatten())+dz*2,dz)
95      k_tmp[1:] = k_tmp[1:] - dz * 0.5
96      for l in range(0,len(x)):
97         for m in range(0,len(y)):
98            for k in range(1,len(k_tmp+1)):
99               if k_tmp[k] > array[m,l]:
100                  array[m,l] = k_tmp[k-1] + dz * 0.5
101                  break
102
103      return array 
104
105
106def make_3d_from_2d(array_2d,x,y,dz):
107   
108      k_tmp = np.arange(0,max(array_2d.flatten())+dz*2,dz)
109 
110      k_tmp[1:] = k_tmp[1:] - dz * 0.5
111      array_3d = np.ones((len(k_tmp),len(y),len(x)))
112     
113      for l in range(0,len(x)):
114         for m in range(0,len(y)):
115            for k in range(0,len(k_tmp)):
116               if k_tmp[k] > array_2d[m,l]:
117                  array_3d[k,m,l] = 0
118
119      return array_3d.astype(np.byte), k_tmp
120
121
122def make_3d_from_bridges_2d(array_3d,array_2d,x,y,dz,width,fill):
123     
124      for l in range(0,len(x)-1):
125         for m in range(0,len(y)-1):
126            if array_2d[m,l] != fill:
127               k_min = max( int(array_2d[m,l] - width)/dz, 0 )
128               k_max = int(round(array_2d[m,l]/dz))
129               array_3d[k_min:k_max+1,m,l] = 1
130
131
132      return array_3d.astype(np.byte)
133
134
135def check_arrays_2(array1,array2,fill1,fill2):
136
137   missing1 = np.where(array1 == fill1,1,0)
138   missing2 = np.where(array2 == fill2,1,0)
139   result = np.array_equal(missing1,missing2)
140   
141   return result
142
143def check_consistency_3(array1,array2,array3,fill1,fill2,fill3):
144
145   tmp_array = np.where(array1 != fill1,1,0) + np.where(array2 != fill2,1,0) + np.where(array3 != fill3,-1,0)
146   
147   test = np.any(tmp_array.flatten() != 0)
148   if test:
149      print("soil_type array is not consistent!")
150      print("max: " + str(max(tmp_array.flatten())) + ", min: " + str(min(tmp_array.flatten())))
151   else:
152      print("soil_type array is consistent!")     
153   return tmp_array, test
154
155
156
157
158def check_consistency_4(array1,array2,array3,array4,fill1,fill2,fill3,fill4):
159
160   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)
161   
162   test = np.any(tmp_array.flatten() != 1)
163   if test:
164      print("*_type arrays are not consistent!")
165      print("max: " + str(max(tmp_array.flatten())) + ", min: " + str(min(tmp_array.flatten())))
166   else:
167      print("*_type arrays are consistent!")     
168   return tmp_array, test
169
Note: See TracBrowser for help on using the repository browser.