cal-adapt / climakitae

A Python toolkit for retrieving, visualizing, and performing scientific analyses with data from the Cal-Adapt Analytics Engine.
https://climakitae.readthedocs.io
BSD 3-Clause "New" or "Revised" License
19 stars 2 forks source link

Pilot Phase 2 - Warming Levels Integration #356

Closed claalmve closed 2 months ago

claalmve commented 2 months ago

Description of PR

Summary of changes and related issue Integrating warming levels option into phase 2 of SCE CAVA Pilot.

Relevant motivation and context We want users to be able to toggle between time and warming level approaches when retrieving data for their CAVA.

Type of change

How Has This Been Tested?

Not tested, just on inputs from a notebook. Will link specific inputs here later.

Checklist:

vicford commented 2 months ago

Okay getting this on running a test on GWL option:

function call

data = cava_data(
    example_locs.iloc[:1],
    variable='Air Temperature at 2m',
    time_start_year=2030, # shouldn't matter
    time_end_year=2060, # shouldn't matter
    approach='warming_level',
    warming_level='3.0',
    units='degC', # shouldnt matter
    threshold = None, # no
    # percentile = None, # no
    metric_calc='max', # yes i think
    ssp_data=["SSP3-7.0"], # no
)

gives this after loading data and calculating metrics

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [6], line 1
----> 1 data = cava_data(
      2     example_locs.iloc[:1],
      3     variable='Air Temperature at 2m',
      4     time_start_year=2030, # shouldn't matter
      5     time_end_year=2060, # shouldn't matter
      6     approach='warming_level',
      7     warming_level='3.0',
      8     units='degC', # shouldnt matter
      9     threshold = None, # no
     10     # percentile = None, # no
     11     metric_calc='max', # yes i think
     12     ssp_data=["SSP3-7.0"], # no
     13 )

File ~/src/climakitae/climakitae/explore/vulnerability.py:407, in cava_data(input_locations, time_start_year, time_end_year, units, variable, metric_calc, threshold, downscaling_method, approach, warming_level, historical_data, ssp_data, export_method, separate_files)
    405 # Calculate and export into separate files
    406 for point in loaded_data:
--> 407     calc_val = metric_agg(point, approach, metric_calc, threshold)
    408     calc_val.name = f"Days per year with {metric_calc.capitalize()} Daily {variable} above threshold of {threshold} {units}"
    409     metric_data.append(calc_val)

File ~/src/climakitae/climakitae/explore/vulnerability.py:77, in metric_agg(da, approach, metric, threshold)
     72 da = da.assign_coords(hours_from_center=timestamps).rename(
     73     hours_from_center="time"
     74 )
     76 # Computing metric
---> 77 metric_data = apply_metric(da)
     79 # Reverting time dimension back to WL framework and rename dimension
     80 metric_data = metric_data.assign_coords(
     81     time=np.concatenate(
     82         [
   (...)
     86     )
     87 ).rename(time="years_from_center")

File ~/src/climakitae/climakitae/explore/vulnerability.py:48, in metric_agg.<locals>.apply_metric(da)
     45 def apply_metric(da):
     46     """Applies the hard-coded metric to the DataArray"""
     47     return (
---> 48         (da.resample(time="1D").reduce(metric_map[metric]) > threshold)
     49         .resample(time="1Y")
     50         .sum()
     51     )

File /srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/_typed_ops.py:242, in DataArrayOpsMixin.__gt__(self, other)
    241 def __gt__(self, other):
--> 242     return self._binary_op(other, operator.gt)

File /srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/dataarray.py:3530, in DataArray._binary_op(self, other, f, reflexive)
   3526 other_variable = getattr(other, "variable", other)
   3527 other_coords = getattr(other, "coords", None)
   3529 variable = (
-> 3530     f(self.variable, other_variable)
   3531     if not reflexive
   3532     else f(other_variable, self.variable)
   3533 )
   3534 coords, indexes = self.coords._merge_raw(other_coords, reflexive)
   3535 name = self._result_name(other)

File /srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/_typed_ops.py:432, in VariableOpsMixin.__gt__(self, other)
    431 def __gt__(self, other):
--> 432     return self._binary_op(other, operator.gt)

File /srv/conda/envs/notebook/lib/python3.9/site-packages/xarray/core/variable.py:2519, in Variable._binary_op(self, other, f, reflexive)
   2516 attrs = self._attrs if keep_attrs else None
   2517 with np.errstate(all="ignore"):
   2518     new_data = (
-> 2519         f(self_data, other_data) if not reflexive else f(other_data, self_data)
   2520     )
   2521 result = Variable(dims, new_data, attrs=attrs)
   2522 return result

TypeError: '>' not supported between instances of 'float' and 'NoneType'
vicford commented 2 months ago

Getting a pieces mismatches error in testing this branch in WL notebook too