source: palm/trunk/SCRIPTS/palm_csd_files/palm_csd_netcdf_interface.py @ 3567

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

added first version of palm_csd (preprocessing tool for creating static drivers)

  • Property svn:keywords set to Id
File size: 7.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_netcdf_interface.py 3567 2018-11-27 13:59:21Z maronga $
27# Initial revisions
28#
29#
30#
31#
32#
33# Description:
34# ------------
35# NetCDF interface routines for palm_csd
36#
37# @Author Bjoern Maronga (maronga@muk.uni-hannover.de)
38#
39#------------------------------------------------------------------------------#
40
41from netCDF4 import Dataset
42
43def nc_read_from_file_2d(filename, varname, x0, x1, y0, y1):
44
45
46   import numpy as np
47   import sys
48   
49   try:
50      f = open(filename)
51      f.close()
52#      print("Load: " + filename + ".")
53   except FileNotFoundError:
54      print("Error: " + filename + ". No such file. Aborting...")
55      sys.exit(1)
56   
57   nc_file = Dataset(filename, "r+", format="NETCDF4")
58   tmp_array = np.array(nc_file.variables[varname][y0:y1+1,x0:x1+1] , dtype=type(nc_file.variables[varname]))
59
60   return tmp_array
61
62def nc_read_from_file_2d_all(filename, varname):
63
64
65   import numpy as np
66   import sys
67   
68   try:
69      f = open(filename)
70      f.close()
71#      print("Load: " + filename + ".")
72   except FileNotFoundError:
73      print("Error: " + filename + ". No such file. Aborting...")
74      sys.exit(1)
75   
76   nc_file = Dataset(filename, "r+", format="NETCDF4")
77   tmp_array = np.array(nc_file.variables[varname][:][:], dtype=type(nc_file.variables[varname]))
78
79   return tmp_array
80
81def nc_read_from_file_1d(filename, varname, x0, x1):
82
83   import numpy as np
84   import sys
85   
86   try:
87      f = open(filename)
88      f.close()
89      #print("Load: " + filename + ".")
90   except FileNotFoundError:
91      print("Error: " + filename + ". No such file. Aborting...")
92      sys.exit(1)
93   
94   nc_file = Dataset(filename, "r+", format="NETCDF4")
95   tmp_array = np.array(nc_file.variables[varname][x0:x1+1] , dtype=type(nc_file.variables[varname]))
96
97   return tmp_array
98
99
100def nc_create_file(filename):
101
102   import datetime
103
104   try:
105      f =  Dataset(filename, "w", format="NETCDF4")
106      f.close()
107      print("Created: " + filename + ".")
108   except FileNotFoundError:
109      print("Error. Could not create file: " + filename + ". Aborting...")
110      sys.exit(1)
111
112   return 0
113
114
115   
116def nc_write_global_attributes(filename,origin_x,origin_y,origin_lat,origin_lon,origin_time, global_acronym,global_angle,
117                               global_author,global_campaign,global_comment,global_contact,global_data_content,
118                               global_dependencies,global_institution,global_keywords,global_location,global_palm_version,
119                               global_references,global_site,global_source,global_version):
120
121   import datetime
122   import os
123
124   print("Writing global attributes to file...")
125
126   f =  Dataset(filename, "a", format="NETCDF4")
127
128   f.setncattr('Conventions',"CF-1.7") 
129   
130   f.origin_x = origin_x
131   f.origin_y = origin_y
132   f.origin_time = origin_time
133   f.origin_lat = origin_lat
134   f.origin_lon = origin_lon
135   
136   f.acronym = global_acronym
137   f.rotation_angle = global_angle
138   f.author = global_author
139   f.campaign = global_campaign
140   f.comment = global_comment
141   f.contact = global_contact
142   f.data_content   = global_data_content
143   f.dependencies = global_dependencies
144   f.institution = global_institution
145   f.keywords = global_keywords
146   f.location = global_location
147   f.palm_version = global_palm_version
148   f.references = global_references
149   f.site = global_site
150   f.source = global_source
151   f.version = global_version
152   
153
154   f.close()
155
156   return 0
157
158
159def nc_write_dimension(filename,varname,array,datatype):
160
161   try:
162      f =  Dataset(filename, "a", format="NETCDF4")
163      #print("Opened: " + filename + ".")
164   except FileNotFoundError:
165      print("Error. Could not open file: " + filename + ". Aborting...")
166      sys.exit(1)
167
168   print("Writing dimension " + varname + " to file...")
169   
170   f.createDimension(varname,len(array))
171   temp = f.createVariable(varname,datatype,varname)
172   temp[:] = array
173   
174   f.close()
175   
176   return 0
177
178def nc_write_to_file_2d(filename,varname,array,datatype,dimname1,dimname2,fillvalue):
179
180   try:
181      f =  Dataset(filename, "a", format="NETCDF4")
182      #print("Opened: " + filename + ".")
183   except FileNotFoundError:
184      print("Error. Could not open file: " + filename + ". Aborting...")
185      sys.exit(1)
186
187   print("Writing array " + varname + " to file...")
188   
189   temp = f.createVariable(varname,datatype,(dimname1,dimname2),fill_value=fillvalue)
190   temp[:] = array
191   
192   f.close()
193
194   return 0
195
196def nc_overwrite_to_file_2d(filename,varname,array):
197
198   try:
199      f =  Dataset(filename, "a", format="NETCDF4")
200      #print("Opened: " + filename + ".")
201   except FileNotFoundError:
202      print("Error. Could not open file: " + filename + ". Aborting...")
203      sys.exit(1)
204
205   print("Writing array " + varname + " to file...")
206   
207   temp = f.variables[varname]
208   temp[:,:] = array
209   
210   f.close()
211
212   return 0
213
214def nc_write_to_file_3d(filename,varname,array,datatype,dimname1,dimname2,dimname3,fillvalue):
215
216   try:
217      f =  Dataset(filename, "a", format="NETCDF4")
218      #print("Opened: " + filename + ".")
219   except FileNotFoundError:
220      print("Error. Could not open file: " + filename + ". Aborting...")
221      sys.exit(1)
222
223   print("Writing array " + varname + " to file...")
224   
225   temp = f.createVariable(varname,datatype,(dimname1,dimname2,dimname3),fill_value=fillvalue)
226   temp[:,:,:] = array
227   
228   f.close()
229
230   return 0
231
232
233def nc_write_attribute(filename,variable,attribute,value):
234
235   f = Dataset(filename, "a", format="NETCDF4")
236
237   var = f.variables[variable]
238   var.setncattr(attribute,value) 
239   
240   f.close()
241
242   return 0
243
244def nc_write_crs(filename):
245
246   try:
247      f =  Dataset(filename, "a", format="NETCDF4")
248      #print("Opened: " + filename + ".")
249   except FileNotFoundError:
250      print("Error. Could not open file: " + filename + ". Aborting...")
251      sys.exit(1)
252
253   print("Writing crs to file...")
254   
255   temp = f.createVariable("crs","i")
256   
257   temp.long_name = "coordinate reference system"
258   temp.grid_mapping_name = "transverse_mercator"
259   temp.semi_major_axis = 6378137.0
260   temp.inverse_flattening = 298.257222101
261   temp.longitude_of_prime_meridian = 0.0
262   temp.longitude_of_central_meridian = 9.0
263   temp.scale_factor_at_central_meridian = 0.9996
264   temp.latitude_of_projection_origin = 0.0
265   temp.false_easting = 50000.0
266   temp.false_northing = 0.0
267   temp.units = "m"
268   temp.epsg_code = "EPSG:25833"
269   
270   f.close()
271
272   return 0
Note: See TracBrowser for help on using the repository browser.