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
21 stars 2 forks source link

Irradiance Units #366

Closed nathancoliver closed 5 months ago

nathancoliver commented 5 months ago

I recently ran into an issue defining units in the climakitae.Select object. Previously, I was able to use the units W m-2 for the irradiance variables. The irradiance variables I have been using have the display_name Instantaneous downwelling longwave flux at bottom, Instantaneous downwelling shortwave flux at bottom, Shortwave surface downward direct normal irradiance, and Shortwave surface downward diffuse irradiance. I have also tried using the units W/m2, but that has not worked either. The error that I am receiving is as follows:

KeyError: 'W/m2' - When I set Select.variable equal to Shortwave surface downward direct normal irradiance KeyError: 'W m-2' - When I set Select.variable equal to the other three aforementioned variables

What is the canonical reference for what units are allowed in a selection? I found the varaible_descriptions.csv. The listed variables are working for me, but the listed units are not working.

I created a minimum working example below for you to try out:

import climakitae as ck
from climakitae.util.utils import convert_to_local_time

## Setting ck.Select.variable equal to any of the following variables will produce the KeyError.
weather_variable = "Instantaneous downwelling longwave flux at bottom" # KeyError: 'W m-2'
# weather_variable = "Shortwave surface downward direct normal irradiance" # KeyError: 'W/m2'
# weather_variable = "Instantaneous downwelling shortwave flux at bottom" # KeyError: 'W m-2'
# weather_variable = "Shortwave surface downward diffuse irradiance" # KeyError: 'W m-2'

## Setting ck.Select.units to either of the following units produces the KeyError
units = "W m-2"
# units = "W/m2"

STATION_LONGITUDE = -118.36543
STATION_LATITUDE = 34.19966
selections = ck.Select()
selections.data_type = "Gridded"
selections.area_average = "Yes"
selections.scenario_historical = ["Historical Climate"]
selections.scenario_ssp = ["SSP 3-7.0 -- Business as Usual"]
selections.time_slice = (2030,2059)
selections.timescale = "hourly"
selections.resolution = "9 km"
selections.cached_area = ["coordinate selection"]
selections.latitude = (
    STATION_LATITUDE - 0.08,
    STATION_LATITUDE + 0.08,
)
selections.longitude = (
    STATION_LONGITUDE - 0.08,
    STATION_LONGITUDE + 0.08,
)
selections.variable = weather_variable
selections.units = units
data = selections.retrieve()
data = convert_to_local_time(data, selections)  # convert to local timezone
data = data.sel(simulation="WRF_MIROC6_r1i1p1f1")

I was wondering if you could help me resolve this issue.

elehmer commented 5 months ago

@nicolejkeeney This could be related to #358 since it is a recent issue?

elehmer commented 5 months ago

I am seeing an inconsistency in units for these datasets. When you first select the data its units are "W/m2" and then when make changes in the Select GUI they change to "W m-2". Need to look over the unit conversion code.

elehmer commented 5 months ago

@nathancoliver Thanks for this bug report! Looks like it will be fixed with #367, We will merge that in a few minutes.

elehmer commented 5 months ago

Fixed in #367. Merged to main @ c04b497

nathancoliver commented 5 months ago

@elehmer @nicolejkeeney It appears to be working now. Thank you very much.

elehmer commented 5 months ago

@elehmer @nicolejkeeney It appears to be working now. Thank you very much.

Thank you so much for reporting the bug! We will also look into the inconsistent formatting of the units so that there is just one way it is written out.