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

Last change on this file since 4671 was 4641, checked in by suehring, 4 years ago

Virtual measurement output: change content of attribute data_content, minor changes in long-name attributes and global attributes to be in agreement with (UC)2 data standard; driver creation config files: remove dummy input strings and change default values (to be in agreement with (UC)2 data standard); palm_csd_netcdf_interface: correct wrong false_easting attribute in crs variable

File size: 9.0 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-2020  Leibniz Universitaet Hannover
18#--------------------------------------------------------------------------------#
19#
20# Current revisions:
21# -----------------
22#
23#
24# Former revisions:
25# -----------------
26# $Id: palm_csd_netcdf_interface.py 3668 2019-01-14 12:49:24Z maronga $
27# Correct wrong false_easting attribute in crs variable
28#
29# 3668 2019-01-14 12:49:24Z maronga
30# Some improvements and new routines
31#
32# 3629 2018-12-13 12:18:54Z maronga
33# Some new routines
34#
35# 3567 2018-11-27 13:59:21Z maronga
36# Initial revisions
37#
38# Description:
39# ------------
40# NetCDF interface routines for palm_csd
41#
42# @Author Bjoern Maronga (maronga@muk.uni-hannover.de)
43#
44#------------------------------------------------------------------------------#
45
46from netCDF4 import Dataset
47
48def nc_read_from_file_1d_all(filename, varname):
49
50
51   import numpy as np
52   import sys
53   
54   try:
55      f = open(filename)
56      f.close()
57#      print("Load: " + filename + ".")
58   except FileNotFoundError:
59      print("Error: " + filename + ". No such file. Aborting...")
60      sys.exit(1)
61   
62   nc_file = Dataset(filename, "r+", format="NETCDF4")
63   tmp_array = np.array(nc_file.variables[varname][:] , dtype=type(nc_file.variables[varname]))
64
65   return tmp_array
66
67def nc_read_from_file_2d(filename, varname, x0, x1, y0, y1):
68
69
70   import numpy as np
71   import sys
72   
73   try:
74      f = open(filename)
75      f.close()
76#      print("Load: " + filename + ".")
77   except FileNotFoundError:
78      print("Error: " + filename + ". No such file. Aborting...")
79      sys.exit(1)
80   
81   nc_file = Dataset(filename, "r+", format="NETCDF4")
82   tmp_array = np.array(nc_file.variables[varname][y0:y1+1,x0:x1+1] , dtype=type(nc_file.variables[varname]))
83
84   return tmp_array
85
86def nc_read_from_file_2d_all(filename, varname):
87
88
89   import numpy as np
90   import sys
91   
92   try:
93      f = open(filename)
94      f.close()
95#      print("Load: " + filename + ".")
96   except FileNotFoundError:
97      print("Error: " + filename + ". No such file. Aborting...")
98      sys.exit(1)
99   
100   nc_file = Dataset(filename, "r+", format="NETCDF4")
101   tmp_array = np.array(nc_file.variables[varname][:][:], dtype=type(nc_file.variables[varname]))
102
103   return tmp_array
104
105def nc_read_from_file_3d_all(filename, varname):
106
107
108   import numpy as np
109   import sys
110   
111   try:
112      f = open(filename)
113      f.close()
114#      print("Load: " + filename + ".")
115   except FileNotFoundError:
116      print("Error: " + filename + ". No such file. Aborting...")
117      sys.exit(1)
118   
119   nc_file = Dataset(filename, "r+", format="NETCDF4")
120   tmp_array = np.array(nc_file.variables[varname][:][:][:], dtype=type(nc_file.variables[varname]))
121
122   return tmp_array
123
124def nc_read_from_file_1d(filename, varname, x0, x1):
125
126   import numpy as np
127   import sys
128   
129   try:
130      f = open(filename)
131      f.close()
132      #print("Load: " + filename + ".")
133   except FileNotFoundError:
134      print("Error: " + filename + ". No such file. Aborting...")
135      sys.exit(1)
136   
137   nc_file = Dataset(filename, "r+", format="NETCDF4")
138   tmp_array = np.array(nc_file.variables[varname][x0:x1+1] , dtype=type(nc_file.variables[varname]))
139
140   return tmp_array
141
142
143def nc_create_file(filename):
144
145   import datetime
146
147   try:
148      f =  Dataset(filename, "w", format="NETCDF4")
149      f.close()
150      print("Created: " + filename + ".")
151   except FileNotFoundError:
152      print("Error. Could not create file: " + filename + ". Aborting...")
153      sys.exit(1)
154
155   return 0
156
157
158   
159def nc_write_global_attributes(filename,origin_x,origin_y,origin_lat,origin_lon,origin_time, global_acronym,global_angle,
160                               global_author,global_campaign,global_comment,global_contact,global_data_content,
161                               global_dependencies,global_institution,global_keywords,global_location,global_palm_version,
162                               global_references,global_site,global_source,global_version):
163
164   import datetime
165   import os
166
167   print("Writing global attributes to file...")
168
169   f =  Dataset(filename, "a", format="NETCDF4")
170
171   f.setncattr('Conventions',"CF-1.7") 
172   
173   f.origin_x = origin_x
174   f.origin_y = origin_y
175   f.origin_time = origin_time
176   f.origin_lat = origin_lat
177   f.origin_lon = origin_lon
178   
179   f.acronym = global_acronym
180   f.rotation_angle = global_angle
181   f.author = global_author
182   f.campaign = global_campaign
183   f.comment = global_comment
184   f.contact = global_contact
185   f.data_content   = global_data_content
186   f.dependencies = global_dependencies
187   f.institution = global_institution
188   f.keywords = global_keywords
189   f.location = global_location
190   f.palm_version = global_palm_version
191   f.references = global_references
192   f.site = global_site
193   f.source = global_source
194   f.version = global_version
195   
196
197   f.close()
198
199   return 0
200
201def nc_write_global_attribute(filename,attribute,value):
202
203   import datetime
204   import os
205
206   print("Writing attribute " + attribute + " to file...")
207
208   f =  Dataset(filename, "a", format="NETCDF4")
209   
210   f.setncattr(attribute,value)
211
212   f.close()
213
214
215def nc_write_dimension(filename,varname,array,datatype):
216
217   try:
218      f =  Dataset(filename, "a", format="NETCDF4")
219      #print("Opened: " + filename + ".")
220   except FileNotFoundError:
221      print("Error. Could not open file: " + filename + ". Aborting...")
222      sys.exit(1)
223
224   print("Writing dimension " + varname + " to file...")
225   
226   f.createDimension(varname,len(array))
227   temp = f.createVariable(varname,datatype,varname)
228   temp[:] = array
229   
230   f.close()
231   
232   return 0
233
234def nc_write_to_file_2d(filename,varname,array,datatype,dimname1,dimname2,fillvalue):
235
236   try:
237      f =  Dataset(filename, "a", format="NETCDF4")
238      #print("Opened: " + filename + ".")
239   except FileNotFoundError:
240      print("Error. Could not open file: " + filename + ". Aborting...")
241      sys.exit(1)
242
243   print("Writing array " + varname + " to file...")
244   
245   temp = f.createVariable(varname,datatype,(dimname1,dimname2),fill_value=fillvalue)
246   temp[:] = array
247   
248   f.close()
249
250   return 0
251
252def nc_overwrite_to_file_2d(filename,varname,array):
253
254   try:
255      f =  Dataset(filename, "a", format="NETCDF4")
256      #print("Opened: " + filename + ".")
257   except FileNotFoundError:
258      print("Error. Could not open file: " + filename + ". Aborting...")
259      sys.exit(1)
260
261   print("Writing array " + varname + " to file...")
262   
263   temp = f.variables[varname]
264   temp[:,:] = array
265   
266   f.close()
267
268   return 0
269
270def nc_overwrite_to_file_3d(filename,varname,array):
271
272   try:
273      f =  Dataset(filename, "a", format="NETCDF4")
274      #print("Opened: " + filename + ".")
275   except FileNotFoundError:
276      print("Error. Could not open file: " + filename + ". Aborting...")
277      sys.exit(1)
278
279   print("Writing array " + varname + " to file...")
280   
281   temp = f.variables[varname]
282   temp[:,:,:] = array
283   
284   f.close()
285
286   return 0
287
288def nc_write_to_file_3d(filename,varname,array,datatype,dimname1,dimname2,dimname3,fillvalue):
289
290   try:
291      f =  Dataset(filename, "a", format="NETCDF4")
292      #print("Opened: " + filename + ".")
293   except FileNotFoundError:
294      print("Error. Could not open file: " + filename + ". Aborting...")
295      sys.exit(1)
296
297   print("Writing array " + varname + " to file...")
298   
299   temp = f.createVariable(varname,datatype,(dimname1,dimname2,dimname3),fill_value=fillvalue)
300   temp[:,:,:] = array
301   
302   f.close()
303
304   return 0
305
306
307def nc_write_attribute(filename,variable,attribute,value):
308
309   f = Dataset(filename, "a", format="NETCDF4")
310
311   var = f.variables[variable]
312   var.setncattr(attribute,value) 
313   
314   f.close()
315
316   return 0
317
318def nc_write_crs(filename):
319
320   try:
321      f =  Dataset(filename, "a", format="NETCDF4")
322      #print("Opened: " + filename + ".")
323   except FileNotFoundError:
324      print("Error. Could not open file: " + filename + ". Aborting...")
325      sys.exit(1)
326
327   print("Writing crs to file...")
328   
329   temp = f.createVariable("crs","i")
330   
331   temp.long_name = "coordinate reference system"
332   temp.grid_mapping_name = "transverse_mercator"
333   temp.semi_major_axis = 6378137.0
334   temp.inverse_flattening = 298.257222101
335   temp.longitude_of_prime_meridian = 0.0
336   temp.longitude_of_central_meridian = 9.0
337   temp.scale_factor_at_central_meridian = 0.9996
338   temp.latitude_of_projection_origin = 0.0
339   temp.false_easting = 500000.0
340   temp.false_northing = 0.0
341   temp.units = "m"
342   temp.epsg_code = "EPSG:25833"
343   
344   f.close()
345
346   return 0
Note: See TracBrowser for help on using the repository browser.