Skip to content

Commit

Permalink
mhm-trunk: updated modules in pre-proc and post_proc
Browse files Browse the repository at this point in the history
  • Loading branch information
schaefed committed Jun 13, 2018
1 parent a9b843b commit c540279
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 90 deletions.
28 changes: 18 additions & 10 deletions post-proc/date2dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def date2dec(calendar = 'standard', units=None,
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the UFZ makefile project (cf. gpl.txt and lgpl.txt).
along with the UFZ Python package (cf. gpl.txt and lgpl.txt).
If not, see <http://www.gnu.org/licenses/>.
Copyright 2010-2013 Arndt Piayda, Matthias Cuntz
Expand All @@ -294,27 +294,35 @@ def date2dec(calendar = 'standard', units=None,
MC, Nov 2013 - removed remnant of time treatment before time check in eng keyword
MC, Jun 2015 - adapted to new netCDF4/netcdftime (>= v1.0) and datetime (>= Python v2.7.9)
MC, Oct 2015 - call date2num with list instead of single netCDF4.datetime objects
DS, Dec 2016 - fixed import problem with newer versions of netcdftime
"""

#
# Checks
# Constants
calendars = ['standard', 'gregorian', 'julian', 'proleptic_gregorian',
'excel1900', 'excel1904', '365_day', 'noleap', '366_day',
'all_leap', '360_day', 'decimal', 'decimal360']
is1 = False
#
# Checks
try:
import netcdftime as nt
testit = nt.__version__
except:
is1 = True
if not is1:
nt.date2dec # check if method exists
if ((nt.__version__ <= '0.9.2') & (calendar == '360_day')):
raise ValueError("date2dec error: Your version of netcdftime.py is equal"
" or below 0.9.2. The 360_day calendar does not work with"
" arrays here. Please download a newer one.")
else:
except (ImportError, AttributeError):
import netCDF4 as nt
#
try:
nt.datetime
except AttributeError:
# the date functions in netCDF (e.g. date2num) are only forwarded from a module
# called cftime. At one point in time the implementation of netCDF4 changed and
# the function datetime is not explicitly propagated anymore, so let's monkey
# patch that here
import cftime
nt.datetime = cftime.datetime

calendar = calendar.lower()
if (calendar not in calendars):
raise ValueError("date2dec error: Wrong calendar!"
Expand Down Expand Up @@ -501,7 +509,7 @@ def date2dec(calendar = 'standard', units=None,
# depending on chosen calendar and optional set of the time units
# decimal date is calculated
output = np.zeros(outsize)
t0 = nt.datetime(1582, 10, 05, 00, 00, 00)
t0 = nt.datetime(1582, 10, 5, 00, 00, 00)
t1 = nt.datetime(1582, 10, 15, 00, 00, 00)
is121 = True if (min(timeobj)<t0) and (max(timeobj)>=t1) else False
if (calendar == 'standard') or (calendar == 'gregorian'):
Expand Down
22 changes: 17 additions & 5 deletions post-proc/readnetcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def readnetcdf(file, var='', code=-1, reform=False, squeeze=False,
units get list of units of variables from attribute units
longnames get list of long names of variables from
attribute long_name
attributes get dictionary of all attributes of specific variable
attributes get dictionary of all attributes of specific variable or of file if variable is omitted
sort sort variable names. Codes, units and longnames will be
sorted accoringly so that indeces still match.
pointer if True, (return file pointer, variable pointer); only for reading
overwrite if True, (return file pointer, variable pointer); modification of file/variable possible
overwrite if True, (return file pointer, variable pointer); modification of file/variable possible if
file contains only one variable
Output
------
Expand Down Expand Up @@ -144,7 +144,7 @@ def readnetcdf(file, var='', code=-1, reform=False, squeeze=False,
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the UFZ makefile project (cf. gpl.txt and lgpl.txt).
along with the UFZ Python package (cf. gpl.txt and lgpl.txt).
If not, see <http://www.gnu.org/licenses/>.
Copyright 2009-2014 Matthias Cuntz, Stephan Thober
Expand All @@ -158,6 +158,8 @@ def readnetcdf(file, var='', code=-1, reform=False, squeeze=False,
MC, Oct 2013 - netcdfread, ncread, readnc
ST, Apr 2014 - added overwrite flag
ST, May 2014 - added dims flag
ST, Jun 2016 - added read of file attributes
ST, Aug 2016 - restricted use of overwrite option
"""
try:
import netCDF4 as nc
Expand All @@ -167,6 +169,9 @@ def readnetcdf(file, var='', code=-1, reform=False, squeeze=False,
try:
if overwrite:
f = nc.Dataset(file, 'a')
if len(f.variables) > 1:
f.close()
raise ValueError('ERROR: only use the overwrite option for files with one variable.')
else:
f = nc.Dataset(file, 'r')
except IOError:
Expand Down Expand Up @@ -255,7 +260,14 @@ def readnetcdf(file, var='', code=-1, reform=False, squeeze=False,
return slongs
# Get attributes
if attributes:
if var not in vars:
if var == '':
attrs = dict()
attr = f.ncattrs()
for a in attr:
attrs[a] = getattr(f, a)
f.close()
return attrs
elif var not in vars:
f.close()
raise ValueError('Variable '+var+' not in file '+file)
attrs = dict()
Expand Down
Loading

0 comments on commit c540279

Please sign in to comment.