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

Last change on this file since 3784 was 3668, checked in by maronga, 6 years ago

removed most_methods circular and lookup. added improved version of palm_csd

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