Skip to content

Commit

Permalink
pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshempelmann committed Jan 7, 2019
1 parent 8988bef commit 46d3c0e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 130 deletions.
160 changes: 80 additions & 80 deletions eggshell/nc/nc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def get_values(resource, variable=None):
# raise Exception(msg)
#
#
#
# #
# def unrotate_pole(resource, write_to_file=False):
# """
# Calculates the unrotatated coordinates for a rotated pole grid
Expand Down Expand Up @@ -703,85 +703,85 @@ def get_values(resource, variable=None):
# ds.close()
#
# return lats, lons


def drs_filename(resource, skip_timestamp=False, skip_format=False,
variable=None, rename_file=False, add_file_path=False):
"""
generates filename according to the data reference syntax (DRS)
based on the metadata in the resource.
http://cmip-pcmdi.llnl.gov/cmip5/docs/cmip5_data_reference_syntax.pdf
https://pypi.python.org/pypi/drslib
:param add_file_path: if add_file_path=True, path to file will be added (default=False)
:param resource: netcdf file
:param skip_timestamp: if True then from/to timestamp != added to the filename
(default: False)
:param variable: appropriate variable for filename, if not set (default), variable will
be determined. For files with more than one data variable,
the variable parameter has to be defined (default: )
example: variable='tas'
:param rename_file: rename the file. (default: False)
:returns str: DRS filename
"""
from os import path, rename

try:
ds = Dataset(resource)
if variable is None:
variable = get_variable(resource)
# CORDEX example: EUR-11_ICHEC-EC-EARTH_historical_r3i1p1_DMI-HIRHAM5_v1_day
cordex_pattern = "{variable}_{domain}_{driving_model}_{experiment}_{ensemble}_{model}_{version}_{frequency}"
# CMIP5 example: tas_MPI-ESM-LR_historical_r1i1p1
cmip5_pattern = "{variable}_{model}_{experiment}_{ensemble}"
filename = resource
if ds.project_id == 'CORDEX' or ds.project_id == 'EOBS':
filename = cordex_pattern.format(
variable=variable,
domain=ds.CORDEX_domain,
driving_model=ds.driving_model_id,
experiment=ds.experiment_id,
ensemble=ds.driving_model_ensemble_member,
model=ds.model_id,
version=ds.rcm_version_id,
frequency=ds.frequency)
elif ds.project_id == 'CMIP5':
# TODO: attributes missing in netcdf file for name generation?
filename = cmip5_pattern.format(
variable=variable,
model=ds.model_id,
experiment=ds.experiment,
ensemble=ds.parent_experiment_rip
)
else:
raise Exception('unknown project %s' % ds.project_id)
ds.close()
except Exception:
LOGGER.exception('Could not read metadata %s', resource)
try:
# add from/to timestamp if not skipped
if skip_timestamp is False:
LOGGER.debug("add timestamp")
from_timestamp, to_timestamp = get_timerange(resource)
LOGGER.debug("from_timestamp %s", from_timestamp)
filename = "%s_%s-%s" % (filename, int(from_timestamp), int(to_timestamp))

# add format extension
if skip_format is False:
filename = filename + '.nc'

pf = path.dirname(resource)
# add file path
if add_file_path is True:
filename = path.join(pf, filename)

# rename the file
if rename_file is True:
if path.exists(path.join(resource)):
rename(resource, path.join(pf, filename))
except Exception:
LOGGER.exception('Could not generate DRS filename for %s', resource)

return filename
#
#
# def drs_filename(resource, skip_timestamp=False, skip_format=False,
# variable=None, rename_file=False, add_file_path=False):
# """
# generates filename according to the data reference syntax (DRS)
# based on the metadata in the resource.
# http://cmip-pcmdi.llnl.gov/cmip5/docs/cmip5_data_reference_syntax.pdf
# https://pypi.python.org/pypi/drslib
# :param add_file_path: if add_file_path=True, path to file will be added (default=False)
# :param resource: netcdf file
# :param skip_timestamp: if True then from/to timestamp != added to the filename
# (default: False)
# :param variable: appropriate variable for filename, if not set (default), variable will
# be determined. For files with more than one data variable,
# the variable parameter has to be defined (default: )
# example: variable='tas'
# :param rename_file: rename the file. (default: False)
# :returns str: DRS filename
# """
# from os import path, rename
#
# try:
# ds = Dataset(resource)
# if variable is None:
# variable = get_variable(resource)
# # CORDEX example: EUR-11_ICHEC-EC-EARTH_historical_r3i1p1_DMI-HIRHAM5_v1_day
# cordex_pattern = "{variable}_{domain}_{driving_model}_{experiment}_{ensemble}_{model}_{version}_{frequency}"
# # CMIP5 example: tas_MPI-ESM-LR_historical_r1i1p1
# cmip5_pattern = "{variable}_{model}_{experiment}_{ensemble}"
# filename = resource
# if ds.project_id == 'CORDEX' or ds.project_id == 'EOBS':
# filename = cordex_pattern.format(
# variable=variable,
# domain=ds.CORDEX_domain,
# driving_model=ds.driving_model_id,
# experiment=ds.experiment_id,
# ensemble=ds.driving_model_ensemble_member,
# model=ds.model_id,
# version=ds.rcm_version_id,
# frequency=ds.frequency)
# elif ds.project_id == 'CMIP5':
# # TODO: attributes missing in netcdf file for name generation?
# filename = cmip5_pattern.format(
# variable=variable,
# model=ds.model_id,
# experiment=ds.experiment,
# ensemble=ds.parent_experiment_rip
# )
# else:
# raise Exception('unknown project %s' % ds.project_id)
# ds.close()
# except Exception:
# LOGGER.exception('Could not read metadata %s', resource)
# try:
# # add from/to timestamp if not skipped
# if skip_timestamp is False:
# LOGGER.debug("add timestamp")
# from_timestamp, to_timestamp = get_timerange(resource)
# LOGGER.debug("from_timestamp %s", from_timestamp)
# filename = "%s_%s-%s" % (filename, int(from_timestamp), int(to_timestamp))
#
# # add format extension
# if skip_format is False:
# filename = filename + '.nc'
#
# pf = path.dirname(resource)
# # add file path
# if add_file_path is True:
# filename = path.join(pf, filename)
#
# # rename the file
# if rename_file is True:
# if path.exists(path.join(resource)):
# rename(resource, path.join(pf, filename))
# except Exception:
# LOGGER.exception('Could not generate DRS filename for %s', resource)
#
# return filename


def sort_by_time(resource):
Expand Down
1 change: 0 additions & 1 deletion eggshell/plot/plt_eodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ def plot_products(products, extend=[10, 20, 5, 15], dir_output='.'):
# return imagefile



# def plot_band(source, file_extension='PNG', colorscheem=None):
# """
# plots the first band of a geotif file
Expand Down
17 changes: 9 additions & 8 deletions eggshell/plot/plt_ncdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.patches as mpatches
import cartopy.crs as ccrs

from cartopy.util import add_cyclic_point
from eggshell.nc.calculation import fieldmean
Expand All @@ -30,7 +31,6 @@ def plot_extend(resource, file_extension='png'):
:return graphic: graphic in specified format
"""
import matplotlib.patches as mpatches
lats, lons = get_coordinates(resource, unrotate=True)

# box_top = 45
Expand All @@ -51,7 +51,7 @@ def plot_extend(resource, file_extension='png'):
ax = plt.axes(projection=projection)
ax.stock_img()
ax.coastlines()
ax.add_patch(mpatches.Polygon(xy, closed=True, transform=ccrs.PlateCarree(), color='coral', alpha=0.6))
ax.add_patch(mpatches.Polygon(xy, closed=True, transform=ccrs.PlateCarree(), color='coral', alpha=0.6))
# ccrs.Geodetic()
ax.gridlines()
plt.show()
Expand Down Expand Up @@ -90,8 +90,8 @@ def spaghetti(resouces, variable=None, title=None, file_extension='png'):
title = "Field mean of %s " % variable

LOGGER.info('plot values preparation done')
except:
msg = "plot values preparation failed"
except Exception as ex:
msg = "plot values preparation failed {}".format(ex)
LOGGER.exception(msg)
raise Exception(msg)
try:
Expand All @@ -113,8 +113,8 @@ def spaghetti(resouces, variable=None, title=None, file_extension='png'):

plt.close()
LOGGER.info('timeseries spaghetti plot done for %s with %s lines.' % (variable, c))
except Exception as e:
msg = 'matplotlib spaghetti plot failed: {}'.format(e)
except Exception as ex:
msg = 'matplotlib spaghetti plot failed: {}'.format(ex)
LOGGER.exception(msg)
return output_png

Expand Down Expand Up @@ -149,7 +149,7 @@ def uncertainty(resouces, variable=None, ylim=None, title=None, file_extension='
title = "Field mean of %s " % variable

try:
fig = plt.figure(figsize=(20, 10), facecolor='w', edgecolor='k') # dpi=600,
fig = plt.figure(figsize=(20, 10), facecolor='w', edgecolor='k')
# variable = get_variable(resouces[0])
df = pd.DataFrame()

Expand Down Expand Up @@ -194,7 +194,8 @@ def uncertainty(resouces, variable=None, ylim=None, title=None, file_extension='
ha='right', va='bottom', alpha=0.5)

try:
rmean = df_smooth.quantile([0.5], axis=1,) # df_smooth.median(axis=1)
rmean = df_smooth.quantile([0.5], axis=1,)
# df_smooth.median(axis=1)
# skipna=False quantile([0.5], axis=1, numeric_only=False )
q05 = df_smooth.quantile([0.10], axis=1,) # numeric_only=False)
q33 = df_smooth.quantile([0.33], axis=1,) # numeric_only=False)
Expand Down
4 changes: 2 additions & 2 deletions eggshell/plot/plt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def concat_images(images, orientation='v'):
cw = oi.size[0]
ch = oi.size[1]
cp = h * i
box = [0, cp, cw, ch+cp]
box = [0, cp, cw, ch + cp]
result.paste(oi, box=box)

if orientation == 'h':
Expand All @@ -158,7 +158,7 @@ def concat_images(images, orientation='v'):
cw = oi.size[0]
ch = oi.size[1]
cp = w * i
box = [cp, 0, cw+cp, ch]
box = [cp, 0, cw + cp, ch]
result.paste(oi, box=box)

ip, image = mkstemp(dir=work_dir, suffix='.png')
Expand Down
79 changes: 40 additions & 39 deletions eggshell/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import eggshell as eg
from eggshell.config import Paths
from eggshell.nc.nc_utils import get_variable

paths = Paths(eg)

Expand Down Expand Up @@ -163,45 +164,45 @@ def extract_archive(resources, output_dir=None):
return files


def get_coordinates(resource, variable=None, unrotate=False):
"""
reads out the coordinates of a variable
:param resource: netCDF resource file
:param variable: variable name
:param unrotate: If True the coordinates will be returned for unrotated pole
:returns list, list: latitudes , longitudes
"""
if type(resource) != list:
resource = [resource]

if variable is None:
variable = get_variable(resource)

if unrotate is False:
try:
if len(resource) > 1:
ds = MFDataset(resource)
else:
ds = Dataset(resource[0])

var = ds.variables[variable]
dims = list(var.dimensions)
if 'time' in dims:
dims.remove('time')
# TODO: find position of lat and long in list and replace dims[0] dims[1]
lats = ds.variables[dims[0]][:]
lons = ds.variables[dims[1]][:]
ds.close()
LOGGER.info('got coordinates without pole rotation')
except Exception as e:
msg = 'failed to extract coordinates: {}'.format(e)
raise Exception(msg)
else:
lats, lons = unrotate_pole(resource)
LOGGER.info('got coordinates with pole rotation')
return lats, lons
# def get_coordinates(resource, variable=None, unrotate=False):
# """
# reads out the coordinates of a variable
#
# :param resource: netCDF resource file
# :param variable: variable name
# :param unrotate: If True the coordinates will be returned for unrotated pole
#
# :returns list, list: latitudes , longitudes
# """
# if type(resource) != list:
# resource = [resource]
#
# if variable is None:
# variable = get_variable(resource)
#
# if unrotate is False:
# try:
# if len(resource) > 1:
# ds = MFDataset(resource)
# else:
# ds = Dataset(resource[0])
#
# var = ds.variables[variable]
# dims = list(var.dimensions)
# if 'time' in dims:
# dims.remove('time')
# # TODO: find position of lat and long in list and replace dims[0] dims[1]
# lats = ds.variables[dims[0]][:]
# lons = ds.variables[dims[1]][:]
# ds.close()
# LOGGER.info('got coordinates without pole rotation')
# except Exception as e:
# msg = 'failed to extract coordinates: {}'.format(e)
# raise Exception(msg)
# else:
# lats, lons = unrotate_pole(resource)
# LOGGER.info('got coordinates with pole rotation')
# return lats, lons


def rename_complexinputs(complexinputs):
Expand Down

0 comments on commit 46d3c0e

Please sign in to comment.