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

Last change on this file since 3638 was 3629, checked in by maronga, 6 years ago

palm_csd improvements

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