Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gb/bias nc #181

Merged
merged 15 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
moved special imports into methods
  • Loading branch information
grantbuster committed Dec 11, 2023
commit 9888f05a1e3f7090fd0e2c52e52e64016bb47ca0
3 changes: 2 additions & 1 deletion sup3r/bias/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"""Bias calculation and correction modules."""
from .bias_transforms import (global_linear_bc, local_linear_bc,
monthly_local_linear_bc)
from .bias_calc import LinearCorrection, MonthlyLinearCorrection
from .bias_calc import (LinearCorrection, MonthlyLinearCorrection,
SkillAssessment)
6 changes: 5 additions & 1 deletion sup3r/bias/bias_correct_means.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import numpy as np
import pandas as pd
import rioxarray
import xarray as xr
from rex import Resource
from scipy.interpolate import interp1d
Expand Down Expand Up @@ -116,6 +115,11 @@ def convert_month_height_tif(self, month, height):
os.remove(outfile)

if not os.path.exists(outfile) or self.overwrite:
try:
import rioxarray
except ImportError as e:
msg = 'Need special installation of "rioxarray" to run this!'
raise ImportError(msg) from e
tmp = rioxarray.open_rasterio(infile)
ds = tmp.to_dataset("band")
ds = ds.rename(
Expand Down
37 changes: 22 additions & 15 deletions sup3r/utilities/era_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,12 @@

logger = logging.getLogger(__name__)

try:
import cdsapi

CDS_API_CLIENT = cdsapi.Client()
except ImportError as e:
msg = f'Could not import cdsapi package. {e}'
logger.error(msg)


class EraDownloader:
"""Class to handle ERA5 downloading, variable renaming, file combination,
and interpolation.
"""

msg = ('To download ERA5 data you need to have a ~/.cdsapirc file '
'with a valid url and api key. Follow the instructions here: '
'https://cds.climate.copernicus.eu/api-how-to')
req_file = os.path.join(os.path.expanduser('~'), '.cdsapirc')
assert os.path.exists(req_file), msg

# variables available on a single level (e.g. surface)
SFC_VARS: ClassVar[list] = [
'10m_u_component_of_wind', '10m_v_component_of_wind',
Expand Down Expand Up @@ -275,6 +261,26 @@ def prep_var_lists(self, variables):
logger.warning(msg)
warn(msg)

@staticmethod
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you went on a clean up spree huh? nice!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha yeah i thought some of the sphinx issues were being caused by these special imports that weren't being installed... That's not the issue but oh well now it's cleaner.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it did turn out to be some weird import thing!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha yeah just not the few that i tried 🤦

def get_cds_client():
"""Get the copernicus climate data store (CDS) API object for ERA
downloads."""
try:
import cdsapi
cds_api_client = cdsapi.Client()
except ImportError as e:
msg = f'Could not import cdsapi package. {e}'
logger.error(msg)
raise ImportError(msg) from e

msg = ('To download ERA5 data you need to have a ~/.cdsapirc file '
'with a valid url and api key. Follow the instructions here: '
'https://cds.climate.copernicus.eu/api-how-to')
req_file = os.path.join(os.path.expanduser('~'), '.cdsapirc')
assert os.path.exists(req_file), msg

return cds_api_client

def download_process_combine(self):
"""Run the download routine."""
sfc_check = len(self.sfc_file_variables) > 0
Expand Down Expand Up @@ -338,7 +344,8 @@ def download_file(cls, variables, time_dict, area, out_file, level_type,
if level_type == 'pressure':
entry['pressure_level'] = levels
logger.info(f'Calling CDS-API with {entry}.')
CDS_API_CLIENT.retrieve(
cds_api_client = cls.get_cds_client()
cds_api_client.retrieve(
f'reanalysis-era5-{level_type}-levels',
entry, out_file)
else:
Expand Down
7 changes: 6 additions & 1 deletion sup3r/utilities/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Utilities module for plotting data
"""

import imageio
import matplotlib
from matplotlib import cm
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -170,6 +169,12 @@ def make_movie(ntime, movieDir, movieName, fps=24):
number of frame per second for the movie, by default 24

"""
try:
import imageio
except ImportError as e:
msg = f'Need extra installation to make movie "imageio": {e}'
raise ImportError(msg) from e

# initiate an empty list of "plotted" images
myimages = []
# loops through available pngs
Expand Down
4 changes: 2 additions & 2 deletions sup3r/utilities/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,12 +1460,12 @@ def get_input_handler_class(file_paths, input_handler_name):


def np_to_pd_times(times):
"""Convert np.bytes_ times to DatetimeIndex
"""Convert `np.bytes_` times to DatetimeIndex

Parameters
----------
times : ndarray | list
List of np.bytes_ objects for time indices
List of `np.bytes_` objects for time indices

Returns
-------
Expand Down