Skip to content

Commit

Permalink
bad replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
bnb32 committed Jul 2, 2024
1 parent 4ce19ef commit 8e5573f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 77 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
push:
branches-ignore:
- 'gh-pages'
pull_request:
branches: [main]

jobs:
ruff:
Expand Down
4 changes: 2 additions & 2 deletions nsrdb/tmy/tmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def source_freq(self):

return freq

def get_weighted_FileSystem(self):
def get_weighted_fs(self):
"""Get the FS metric for all datasets and weight and combine.
This is part of STEP #1 of the NSRDB TMY.
Expand Down Expand Up @@ -1234,7 +1234,7 @@ def calculate_tmy_years(self):
Shape is (months, sites).
"""

ws = self.get_weighted_FileSystem()
ws = self.get_weighted_fs()
tmy_years_5, _ = self.select_fs_years(ws)
tmy_years_5, _ = self.sort_years_mm(tmy_years_5)
tmy_years, _, _ = self.persistence_filter(tmy_years_5)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ ignore = [
"N802", # invalid-function-name
"N803", # invalid-argument-name
"N806", # non-lowercase-variable-in-function
"N811", # constant imported as non constant
"PERF102", # incorrect-dict-iterator
"PERF203", # try-except-in-loop
"PERF401", # manual-list-comprehension
"PLC0415", # import not at top of file
"PLR0904", # too-many-public-methods
"PLR0912", # too-many-branches
"PLR0913", # too-many-arguments
Expand Down
186 changes: 113 additions & 73 deletions tests/giffy/lambda_handler.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
"""
GIF ify lambda handler
"""
from cloud_fs import FileSystem

import contextlib
import json
import logging
import os
import sys
import tempfile
import time

import matplotlib as mpl
import matplotlib.cm as cmx
import matplotlib.colors as colors
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
from cloud_fs import FileSystem
from matplotlib import colors
from PIL import Image
from rex import Resource, safe_json_load, init_logger, parse_year
import sys
import tempfile
import time
from rex import Resource, init_logger, parse_year, safe_json_load

logger = logging.getLogger(__name__)

DIR_PATH = NSRDBDIR = os.path.dirname(os.path.realpath(__file__))
LOGO = os.path.join(DIR_PATH, 'nrel.png')
SP_FPATH = os.path.join(DIR_PATH,
'WorldCountries50mShapefile',
'ne_50m_admin_0_countries.shp')
SP_FPATH = os.path.join(
DIR_PATH, 'WorldCountries50mShapefile', 'ne_50m_admin_0_countries.shp'
)


class LambdaArgs(dict):
Expand All @@ -32,8 +35,9 @@ class LambdaArgs(dict):
"""

def __init__(self, event):
self.update({k.lower(): self._parse_env_var(v)
for k, v in os.environ.items()})
self.update(
{k.lower(): self._parse_env_var(v) for k, v in os.environ.items()}
)

if isinstance(event, str):
event = safe_json_load(event)
Expand All @@ -55,10 +59,8 @@ def _parse_env_var(v):
v : obj
ENV variable value converted to proper type
"""
try:
with contextlib.suppress(json.JSONDecodeError):
v = json.loads(v)
except json.JSONDecodeError:
pass

return v

Expand Down Expand Up @@ -115,8 +117,9 @@ def make_images(h5_fpath, img_dir, **kwargs):
fn_base = os.path.join(img_dir, 'image_{}.png')

dset = kwargs.get('dset', 'ghi')
cbar_label = kwargs.get('cbar_label',
'Global Horizontal Irradiance (W/m2)')
cbar_label = kwargs.get(
'cbar_label', 'Global Horizontal Irradiance (W/m2)'
)
counties_fpath = kwargs.get('counties_fpath', SP_FPATH)
logo = kwargs.get('logo', LOGO)
local_timezone = kwargs.get('local_timezone', 0)
Expand Down Expand Up @@ -152,11 +155,17 @@ def make_images(h5_fpath, img_dir, **kwargs):

data = data[mask]
time_index = time_index[mask]
logger.debug('Creating images for {} timesteps:\n{}'
.format(len(time_index), time_index))

logger.info('{} stats: {}, {}, {}'
.format(dset, data.min(), data.mean(), data.max()))
logger.debug(
'Creating images for {} timesteps:\n{}'.format(
len(time_index), time_index
)
)

logger.info(
'{} stats: {}, {}, {}'.format(
dset, data.min(), data.mean(), data.max()
)
)
logger.info('read complete')

buffer = kwargs.get('lim_buffer', 0.001)
Expand All @@ -172,7 +181,6 @@ def make_images(h5_fpath, img_dir, **kwargs):
mpl.rcParams['text.color'] = text_color
mpl.rcParams['axes.labelcolor'] = text_color

i_fname = 0
for ti, timestamp in enumerate(time_index):
logger.info('working on ti {}, timestamp: {}'.format(ti, timestamp))

Expand All @@ -185,35 +193,53 @@ def make_images(h5_fpath, img_dir, **kwargs):

if counties_fpath is not None:
import geopandas as gpd

gdf = gpd.GeoDataFrame.from_file(counties_fpath)
gdf = gdf.to_crs({'init': 'epsg:4326'})
gdf.plot(ax=ax, color=shape_color, edgecolor=shape_edge_color,
linewidth=shape_line_width)
gdf.plot(
ax=ax,
color=shape_color,
edgecolor=shape_edge_color,
linewidth=shape_line_width,
)
else:
gdf = None

col_slice = slice(None)
if resource_map_interval:
col_slice = slice(None, None, resource_map_interval)

ax.scatter(meta.longitude.values[col_slice],
meta.latitude.values[col_slice],
c=data[ti, col_slice],
alpha=alpha, cmap=cmap, s=marker_size,
vmin=min_irrad, vmax=max_irrad, linewidth=0,
marker='s')
ax.scatter(
meta.longitude.values[col_slice],
meta.latitude.values[col_slice],
c=data[ti, col_slice],
alpha=alpha,
cmap=cmap,
s=marker_size,
vmin=min_irrad,
vmax=max_irrad,
linewidth=0,
marker='s',
)

if gdf is not None:
gdf.geometry.boundary.plot(ax=ax, color=None,
edgecolor=shape_edge_color,
linewidth=shape_line_width)
gdf.geometry.boundary.plot(
ax=ax,
color=None,
edgecolor=shape_edge_color,
linewidth=shape_line_width,
)
if shape_aspect:
ax.set_aspect(shape_aspect)

n_cbar = 100
b = ax.scatter([1e6] * n_cbar, [1e6] * n_cbar,
c=np.linspace(min_irrad, max_irrad, n_cbar),
s=0.00001, cmap=cmap)
b = ax.scatter(
[1e6] * n_cbar,
[1e6] * n_cbar,
c=np.linspace(min_irrad, max_irrad, n_cbar),
s=0.00001,
cmap=cmap,
)
cbar = plt.colorbar(b)
ticks = plt.getp(cbar.ax, 'yticklabels')
plt.setp(ticks, color=text_color)
Expand All @@ -226,10 +252,12 @@ def make_images(h5_fpath, img_dir, **kwargs):
if print_timestamp:
local_timestamp = timestamp
if local_timezone != 0:
local_timestamp = (local_timestamp.tz_localize(None)
+ pd.DateOffset(hours=local_timezone))
plt.text(0.12, 0.885, str(local_timestamp),
transform=fig.transFigure)
local_timestamp = local_timestamp.tz_localize(
None
) + pd.DateOffset(hours=local_timezone)
plt.text(
0.12, 0.885, str(local_timestamp), transform=fig.transFigure
)

if logo is not None:
if not isinstance(logo_scale, float):
Expand All @@ -241,8 +269,7 @@ def make_images(h5_fpath, img_dir, **kwargs):
im = np.array(im).astype(float) / 255
fig.figimage(im, 0, 0)

fn = fn_base.format(i_fname)
i_fname += 1
fn = fn_base.format(ti)
fig.savefig(fn, dpi=dpi, bbox_inches='tight', facecolor=face_color)
logger.info('saved: {}'.format(fn))
plt.close()
Expand All @@ -263,15 +290,22 @@ def make_gif(fpath_out, img_dir, **kwargs):
"""
file_tag = kwargs.get('file_tag', 'image_')
duration = kwargs.get('duration', 250)
filenames = [f for f in os.listdir(img_dir)
if f.endswith('.png')
and file_tag in f]

filenames = sorted(filenames,
key=lambda x: int(x.replace('.png', '').split('_')[-1]))
img, *imgs = [Image.open(os.path.join(img_dir, fn)) for fn in filenames]
img.save(fp=fpath_out, format='GIF', append_images=imgs,
save_all=True, duration=duration, loop=0)
filenames = [
f for f in os.listdir(img_dir) if f.endswith('.png') and file_tag in f
]

filenames = sorted(
filenames, key=lambda x: int(x.replace('.png', '').split('_')[-1])
)
img, *imgs = (Image.open(os.path.join(img_dir, fn)) for fn in filenames)
img.save(
fp=fpath_out,
format='GIF',
append_images=imgs,
save_all=True,
duration=duration,
loop=0,
)
logger.info('Saved: {}'.format(fpath_out))


Expand All @@ -291,13 +325,11 @@ def handler(event, context):
h5_path = args.pop('h5_path', None)
if h5_path is None:
s3_obj = args['Records'][0]['s3']
h5_path = ("s3://{}/{}"
.format(s3_obj['bucket']['name'], s3_obj['object']['key']))
h5_path = 's3://{}/{}'.format(
s3_obj['bucket']['name'], s3_obj['object']['key']
)

if args.get('verbose', False):
log_level = 'DEBUG'
else:
log_level = 'INFO'
log_level = 'DEBUG' if args.get('verbose', False) else 'INFO'

logger = init_logger(__name__, log_level=log_level)
out_dir = args['out_dir']
Expand All @@ -306,31 +338,38 @@ def handler(event, context):
if not out_dir.endswith(year):
out_dir = os.path.join(out_dir, year)
except RuntimeError:
msg = ("Cannot parse year from {}, year will not be appended to "
"out_dir".format(h5_path))
msg = (
'Cannot parse year from {}, year will not be appended to '
'out_dir'.format(h5_path)
)
logger.warning(msg)

logger.debug(f'event: {event}')
logger.debug(f'context: {context}')
logger.debug(f'GIFFY inputs:'
f'\nh5_path = {h5_path}'
f'\nout_dir = {out_dir}')
logger.debug(
f'GIFFY inputs:' f'\nh5_path = {h5_path}' f'\nout_dir = {out_dir}'
)
img_dir = args.pop('img_dir', '/tmp')
with tempfile.TemporaryDirectory(prefix='images_',
dir=img_dir) as temp_dir:
with tempfile.TemporaryDirectory(
prefix='images_', dir=img_dir
) as temp_dir:
fname = os.path.basename(h5_path)
h5_local = os.path.join(temp_dir, fname)
logger.debug('Copying {} to {}'.format(h5_path, h5_local))
FileSystem.copy(h5_path, h5_local)

logger.debug('Creating images of {} for all available daylight '
'timesteps and saving to {}'
.format(args.get('dset', 'ghi'), temp_dir))
logger.debug(
'Creating images of {} for all available daylight '
'timesteps and saving to {}'.format(
args.get('dset', 'ghi'), temp_dir
)
)
make_images(h5_local, temp_dir, **args)

gif_fpath = os.path.join(temp_dir, fname.replace('.h5', '.gif'))
logger.debug('Creating {} from images in {}'
.format(gif_fpath, temp_dir))
logger.debug(
'Creating {} from images in {}'.format(gif_fpath, temp_dir)
)
make_gif(gif_fpath, temp_dir, **args)

out_fpath = os.path.join(out_dir, os.path.basename(gif_fpath))
Expand All @@ -343,10 +382,11 @@ def handler(event, context):
return success


if __name__ == "__main__":
if __name__ == '__main__':
if len(sys.argv) > 1:
event = safe_json_load(sys.argv[1])
ts = time.time()
handler(event, None)
logger.info('Giffy runtime: {:.4f} minutes'
.format((time.time() - ts) / 60))
logger.info(
'Giffy runtime: {:.4f} minutes'.format((time.time() - ts) / 60)
)

0 comments on commit 8e5573f

Please sign in to comment.