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

Last change on this file since 4185 was 4023, checked in by maronga, 6 years ago

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