source: palm/trunk/SCRIPTS/create_basic_static_driver.py @ 4586

Last change on this file since 4586 was 4370, checked in by raasch, 5 years ago

bugfixes for previous commit: unused variables removed, Temperton-fft usage on GPU, openacc porting of vector version of Obukhov length calculation, collective read switched off on NEC to avoid hanging; some vector directives added in prognostic equations to force vectorization on Intel19 compiler, configuration files for NEC Aurora added

  • Property svn:executable set to *
File size: 13.6 KB
RevLine 
[4072]1#------------------------------------------------------------------------------#
2# This file is part of the PALM model system.
3#
4# PALM is free software: you can redistribute it and/or modify it under the
5# terms of the GNU General Public License as published by the Free Software
6# Foundation, either version 3 of the License, or (at your option) any later
7# version.
8#
9# PALM is distributed in the hope that it will be useful, but WITHOUT ANY
10# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along with
14# PALM. If not, see <http://www.gnu.org/licenses/>.
15#
[4370]16# Copyright 1997-2020 Leibniz Universitaet Hannover
[4072]17#------------------------------------------------------------------------------#
18
19import math
20from   netCDF4 import Dataset
21import numpy as np
22import datetime
23
24
25class StaticDriver:
26    """ This is an example script to generate static drivers for PALM.
27   
28    You can use it as a starting point for creating your setup specific
29    driver. 
30    """
31   
32   
33    def __init__(self):
34        """ Open the static driver as NetCDF4 file. Here, you have to give the
35        full path to the static driver that shall be created. Existing file
36        with same name is deleted.
37        """
38        print('Opening file...')
39        self.nc_file = Dataset('path/to/file.nc', 'w', format='NETCDF4')
40
41
42    def write_global_attributes(self):
43        """ Write global attributes to static driver. """
44        print("Writing global attributes...")
45       
46        # mandatory global attributes
47        self.nc_file.origin_lon = 55.0 # used to initialize coriolis parameter
48        self.nc_file.origin_lat = 0.0 # (overwrite initialization_parameters)
49        self.nc_file.origin_time = '2000-06-21 12:00:00 +00'
50        self.nc_file.origin_x = 308124
51        self.nc_file.origin_y = 6098908
52        self.nc_file.origin_z = 0.0
53        self.nc_file.rotation_angle = 0.0
54       
55        # optional global attributes
56        self.nc_file.author = 'Your Name'
57        self.nc_file.comment = 'Miscellaneous information about the data ' \
58                               'or methods to produce it.'
59        self.nc_file.creation_date = str(datetime.datetime.now())
60        self.nc_file.institution = 'Institut of Meteorology and Climatology,' \
61                                   'Leibniz University Hannover'
62        self.nc_file.history = ''
63        self.nc_file.palm_revision = ''
64        self.nc_file.title = 'Static driver for some arbitrary PALM setup'
65
66
67    def define_dimensions(self):
68        """ Set dimensions on which variables are defined. """
69        print("Writing dimensions...")
70       
71        # specify general grid parameters
72        # these values must equal those set in the initialization_parameters
73        self.nx = 39
74        self.ny = 39
75        self.nz = 40
76        dx = 50
77        dy = 50
78        dz = 50
79       
80        # create soil grid (only relevant if land surface module is used)
81        dz_soil = np.array((0.01, 0.02, 0.04, 0.06, 0.14, 0.26, 0.54, 1.86))
82        zsoil_fullLayers = np.zeros_like(dz_soil)
83        zsoil_fullLayers = np.around([np.sum(dz_soil[:zs]) for zs in np.arange(1,len(dz_soil)+1)],2)
84        zsoil_array = zsoil_fullLayers - dz_soil/2.
85       
86        # create plant canopy grid (only relevant of plant canopy module is used)
87        zlad_array = np.arange(0,10,1)
88       
89        # mandatory dimensions
90        self.nc_file.createDimension('x' ,self.nx+1)
91        self.x = self.nc_file.createVariable('x', 'f8', ('x',))
92        self.x.units = 'm'
93        self.x.standard_name = 'x coordinate of cell centers'
94        self.x[:] = np.arange(0,(self.nx+1)*dx,dx)
95       
96        self.nc_file.createDimension('xu', self.nx)
97        self.xu = self.nc_file.createVariable('xu', 'f8', ('xu',))
98        self.xu.units = 'm'
99        self.xu.standard_name = 'x coordinate of cell edges'
100        self.xu[:] = np.arange(dx/2.,self.nx*dx,dx)
101       
102        self.nc_file.createDimension('y', self.ny+1)
103        self.y = self.nc_file.createVariable('y', 'f8', ('y',))
104        self.y.units = 'm'
105        self.y.standard_name = 'y coordinate of cell centers'
106        self.y[:] = np.arange(0,(self.ny+1)*dy,dy)
107       
108        self.nc_file.createDimension('yv', self.ny)
109        self.yv = self.nc_file.createVariable('yv', 'f8', ('yv',))
110        self.yv.units = 'm'
111        self.yv.standard_name = 'y coordinate of cell edges'
112        self.yv[:] = np.arange(dy/2.,self.ny*dy,dy)
113       
114        # if your simulation uses a stretched vertical grid, you need to
115        # modify the z and zw coordinates,
116        # e.g. z_array = (...) and zw_array = (...)
117        z_array = np.append(0, np.arange(dz/2,(self.nz+1)*dz,dz))
118        self.nc_file.createDimension('z',self.nz+2)
119        self.z = self.nc_file.createVariable('z', 'f8', ('z',))
120        self.z.units = 'm'
121        self.z.standard_name = 'z coordinate of cell centers'
122        self.z[:] = z_array
123
124        zw_array = np.arange(0,(self.nz+2)*dz,dz)
125        self.nc_file.createDimension('zw', self.nz+2)
126        self.zw = self.nc_file.createVariable('zw', 'f8', ('zw',))
127        self.zw.units = 'm'
128        self.zw.standard_name = 'z coordinate of cell edges'
129        self.zw[:] = zw_array
130       
131        # optional dimensions, uncomment if needed
132        #self.nc_file.createDimension('zsoil', len(zsoil_array))
133        #self.zsoil = self.nc_file.createVariable('zsoil', 'f8', ('zsoil',))
134        #self.zsoil.positive = 'down'
135        #self.zsoil.units = 'm'
136        #self.zsoil.standard_name = 'depth below land'
137        #self.zsoil[:] = zsoil_array
138       
139        #self.nc_file.createDimension('zlad', len(zlad_array))
140        #self.zlad = self.nc_file.createVariable('zlad', 'f8', ('zlad',))
141        #self.zlad.units = 'm'
142        #self.zlad.standard_name = 'z coordinate of resolved plant canopy'
143        #self.zlad[:] = zlad_array
144       
145        #self.nc_file.createDimension('nalbedo_pars', 3)
146        #self.nalbedo_pars = self.nc_file.createVariable(
147            #'nalbedo_pars', 'i1', ('nalbedo_pars',))
148        #self.nalbedo_pars[:] = np.arange(0,3)
149       
150        #self.nc_file.createDimension('nbuilding_pars', 46)
151        #self.nbuilding_pars = self.nc_file.createVariable(
152            #'nbuilding_pars', 'i4', ('nbuilding_pars',))
153        #self.nbuilding_pars[:] = np.arange(0,46)
154       
155        #self.nc_file.createDimension('nsoil_pars', 8)
156        #self.nsoil_pars = self.nc_file.createVariable(
157            #'nsoil_pars', 'i1', ('nsoil_pars',))
158        #self.nsoil_pars[:] = np.arange(0,8)
159       
160        #self.nc_file.createDimension('nsurface_fraction', 3)
161        #self.nsurface_fraction = self.nc_file.createVariable(
162            #'nsurface_fraction', 'i1', ('nsurface_fraction',))
163        #self.nsurface_fraction[:] = np.arange(0,3)
164       
165        #self.nc_file.createDimension('nvegetation_pars', 12)
166        #self.nvegetation_pars = self.nc_file.createVariable(
167            #'nvegetation_pars', 'i1', ('nvegetation_pars',))
168        #self.nvegetation_pars[:] = np.arange(0,12)
169       
170        #self.nc_file.createDimension('npavement_pars', 4)
171        #self.npavement_pars = self.nc_file.createVariable(
172            #'npavement_pars', 'i1', ('npavement_pars',))
173        #self.npavement_pars[:] = np.arange(0,4)
174
175        #self.nc_file.createDimension('npavement_subsurface_pars', 2)
176        #self.npavement_subsurface_pars = self.nc_file.createVariable(
177            #'npavement_subsurface_pars', 'i1', ('npavement_subsurface_pars',))
178        #self.npavement_subsurface_pars[:] = np.arange(0,2)
179       
180        #self.nc_file.createDimension('nwater_pars', 6)
181        #self.nwater_pars = self.nc_file.createVariable(
182            #'nwater_pars', 'i1', ('nwater_pars',))
183        #self.nwater_pars[:] = np.arange(0,6)
184       
185
186    def add_variables(self):
187        """ Uncomment variables below as you like.
188       
189        Be aware that some variables depend on others. For a description of
190        each variable, please have a look at the documentation at
191        palm.muk.uni-hannover.de/trac/wiki/doc/app/iofiles/pids/static
192       
193        An example of how you modify the variables is given below:
194       
195        building_2d_array = np.ones((self.ny+1,self.nx+1)) * -9999.9
196        south_wall, north_wall, left_wall, right_wall = 20, 25, 20, 25
197        building_2d_array[south_wall:north_wall,left_wall:right_wall] = 50
198        nc_buildings_2d = self.nc_file.createVariable(
199            'buildings_2d', 'f4', ('y','x'),fill_value=-9999.9)
200        nc_buildings_2d.lod = 1
201        nc_buildings_2d[:,:] = building_2d_array
202        """
203        print("Writing variables...")
204       
205        ## topography variables
206        #nc_buildings_2d = self.nc_file.createVariable(
207            #'buildings_2d', 'f4', ('y','x'), fill_value=-9999.9)
208        #nc_buildings_2d.lod = 1
209        #nc_buildings_2d[:,:] = -9999.9
210       
211        #nc_buildings_3d = self.nc_file.createVariable(
212            #'buildings_3d', 'i1', ('z','y','x'),fill_value=-127)
213        #nc_buildings_3d.lod = 2
214        #nc_buildings_3d[:,:,:] = -127
215       
216        #nc_building_id = self.nc_file.createVariable(
217            #'building_id', 'i1', ('y','x'), fill_value=-127)
218        #nc_building_id[:,:] = -127
219       
220        #nc_zt = self.nc_file.createVariable(
221            #'zt', 'f4', ('y','x'), fill_value=-9999.9)
222        #nc_zt.long_name = 'orography'
223        #nc_zt.units = 'm'
224        #nc_zt[:,:] = -9999.9
225       
226        ## surface variables
227        #nc_albedo_pars = self.nc_file.createVariable(
228            #'albedo_pars', 'f4', ('nalbedo_pars','y','x'), fill_value=-9999.9)
229        #nc_albedo_pars.long_name = 'albedo parameters'
230        #nc_albedo_pars[:,:,:] = -9999.9
231       
232        #nc_albedo_type = self.nc_file.createVariable(
233            #'albedo_type', 'f4', ('y','x'), fill_value=-9999.9)
234        #nc_albedo_type[:,:] = -9999.9
235       
236        #nc_building_pars = self.nc_file.createVariable(
237            #'building_pars', 'f4', ('nbuilding_pars','y','x'), fill_value=-9999.9)
238        #nc_building_pars.long_name = 'building parameters'
239        #nc_building_pars[:,:,:] = -9999.9
240       
241        #nc_building_type = self.nc_file.createVariable(
242            #'building_type', 'i1', ('y','x'), fill_value=-127)
243        #nc_building_type[:,:] = -127
244       
245        #nc_pavement_pars = self.nc_file.createVariable(
246            #'pavement_pars', 'f4', ('npavement_pars','y','x'), fill_value=-9999.9)
247        #nc_pavement_pars[:,:,:] = -9999.9
248       
249        #nc_pavement_subsurface_pars = self.nc_file.createVariable(
250            #'pavement_subsurface_pars', 'i1',
251            #('npavement_subsurface_pars','zsoil','y','x'), fill_value=-9999.9)
252        #nc_pavement_subsurface_pars[:,:,:,:] = -9999.9
253       
254        #nc_pavement_type = self.nc_file.createVariable(
255            #'pavement_type', 'i1', ('y','x'), fill_value=-127)
256        #nc_pavement_type[:,:] = -127
257       
258        #nc_soil_pars = self.nc_file.createVariable(
259            #'soil_pars', 'f', ('nsoil_pars','zsoil','y','x'), fill_value=-9999.9)
260        #nc_soil_pars.long_name = 'soil parameters'
261        #nc_soil_pars.lod = 2 # if set to 1, adjust dimensions of soil_pars
262        #nc_soil_pars[:,:,:,:] = -9999.9
263       
264        #nc_soil_type = self.nc_file.createVariable(
265            #'soil_type', 'i1', ('zsoil','y','x'), fill_value=-127)
266        #nc_soil_type.lod = 2 # if set to 1, adjust dimensions of soil_type
267        #nc_soil_type[:,:,:] = -127
268       
269        ## required, if several surface types are defined at the same location
270        #nc_surface_fraction = self.nc_file.createVariable(
271            #'surface_fraction', 'f', ('nsurface_fraction','y','x'), fill_value=-9999.9)
272        #nc_surface_fraction[0,:,:] = -9999.9 # vegetation fraction
273        #nc_surface_fraction[1,:,:] = -9999.9 # pavement fraction
274        #nc_surface_fraction[2,:,:] = -9999.9 # water fraction
275       
276        #nc_vegetation_pars = self.nc_file.createVariable(
277            #'vegetation_pars', 'f', ('nvegetation_pars','y','x'), fill_value=-9999.9)
278        #nc_vegetation_pars.long_name = 'vegetation parameters'
279        #nc_vegetation_pars[:,:,:] = -9999.9
280       
281        #nc_vegetation_type = self.nc_file.createVariable(
282            #'vegetation_type', 'i1', ('y','x'), fill_value=-127)
283        #nc_vegetation_type[:,:] = -127
284       
285        #nc_street_type = self.nc_file.createVariable(
286            #'street_type', 'i1', ('y','x'),
287            #fill_value=-127)
288        #nc_street_type[:,:] = -127
289       
290        #nc_water_pars = self.nc_file.createVariable(
291            #'water_pars', 'i1', ('nwater_pars','y','x'), fill_value=-9999.9)
292        #nc_water_pars[:,:,:] = -9999.9
293       
294        #nc_water_type = self.nc_file.createVariable(
295            #'water_type', 'i1',('y','x'), fill_value=-127)
296        #nc_water_type[:,:] = -127
297       
298        ## resolved plant canopy
299        #nc_lad = self.nc_file.createVariable(
300            #'lad', 'f4', ('zlad','y','x'), fill_value=-9999.9)
301        #nc_lad.long_name = 'leaf area density'
302        #nc_lad[:,:,:] = -9999.9
303       
304        #nc_root_area_dens_s = self.nc_file.createVariable(
305            #'root_area_dens_s', 'f4', ('zsoil','y','x'), fill_value=-9999.9)
306        #nc_root_area_dens_s.long_name = 'parameterized root area density'
307        #nc_root_area_dens_s.units = '1/m'
308        #nc_root_area_dens_s[:,:,:] = -9999.9
309       
310       
311    def finalize(self):
312        """ Close file """
313        print("Closing file...")
314       
315        self.nc_file.close()
316
317
318if __name__ == '__main__':
319    driver = StaticDriver()
320    driver.write_global_attributes()
321    driver.define_dimensions()
322    driver.add_variables()
323    driver.finalize()
324   
Note: See TracBrowser for help on using the repository browser.