Skip to content

Commit

Permalink
Merge pull request #169 from pacificclimate/bugfix/106
Browse files Browse the repository at this point in the history
Add ensemble name filter to get_units_from_file_object
  • Loading branch information
cairosanders committed Nov 2, 2020
2 parents 71725b9 + 78a7da4 commit ec7261a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ce/api/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def get_time_value(timeset, time_idx):
run_result = result[data_file_variable.file.run.name] = {
"data": {},
"units": get_units_from_run_object(
data_file_variable.file.run, variable
data_file_variable.file.run, variable, ensemble_name
),
"modtime": data_file_variable.file.index_time,
}
Expand Down
12 changes: 8 additions & 4 deletions ce/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
from ce.api.geo import wkt_to_masked_array


def get_files_from_run_variable(run, variable):
def get_files_from_run_variable(run, variable, ensemble_name):
return [
file_
for file_ in run.files
if variable in [dfv.netcdf_variable_name for dfv in file_.data_file_variables]
if any(
variable == dfv.netcdf_variable_name
and any(ensemble.name == ensemble_name for ensemble in dfv.ensembles)
for dfv in file_.data_file_variables
)
]


Expand All @@ -34,8 +38,8 @@ def get_units_from_file_object(file_, varname):
)


def get_units_from_run_object(run, varname):
files = get_files_from_run_variable(run, varname)
def get_units_from_run_object(run, varname, ensemble_name):
files = get_files_from_run_variable(run, varname, ensemble_name)
units = {get_units_from_file_object(file_, varname) for file_ in files}

if len(units) != 1:
Expand Down
47 changes: 44 additions & 3 deletions ce/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ def multitime_db(cleandb,):
sesh = dbcopy.session
now = datetime.utcnow()

ens = Ensemble(name="ce", version=2.0, changes="", description="",)
ce_ens = Ensemble(name="ce", version=2.0, changes="", description="",)
# Create diff ensemble to test unit consistency ensemble filter
p2a_ens = Ensemble(name="p2a", version=2.0, changes="", description="",)

rcp45 = Emission(short_name="rcp45")

Expand Down Expand Up @@ -505,6 +507,12 @@ def multitime_db(cleandb,):
standard_name="air_temperature",
units="degC",
)
# Create variable with different units
tasmax_diff_units = VariableAlias(
long_name="Tasmax with different units",
standard_name="tmax_diff_units",
units="degK",
)

anuspline_grid = Grid(
name="Canada ANUSPLINE",
Expand All @@ -530,11 +538,44 @@ def multitime_db(cleandb,):
)
for file_ in files
]
# Create file with different units
files.append(
DataFile(
filename=resource_filename(
"ce",
"tests/data/"
"tasmax_mClim_BNU-ESM_historical_r1i1p1_19650101-19701230.nc",
),
unique_id="file9",
first_1mib_md5sum="xxxx",
x_dim_name="lon",
y_dim_name="lat",
index_time=now,
run=runs[0],
)
)
# Create dfv with same var name and diff units
dfv_diff_units = DataFileVariableGridded(
netcdf_variable_name="tasmax",
range_min=0,
range_max=50,
file=files[9],
variable_alias=tasmax_diff_units,
grid=anuspline_grid,
)
dfvs.append(dfv_diff_units)

sesh.add_all(files + dfvs + runs + [anuspline_grid, tasmax, bnu_esm, rcp45, ens])
sesh.add_all(
files
+ dfvs
+ runs
+ [anuspline_grid, tasmax, tasmax_diff_units, bnu_esm, rcp45, ce_ens, p2a_ens]
)
sesh.commit()

ens.data_file_variables += dfvs
ce_ens.data_file_variables += dfvs[:-1]
# Add dfv with diff units to diff ensemble
p2a_ens.data_file_variables.append(dfvs.pop())
sesh.add_all(sesh.dirty)

# Create the three timesets, with just one time step per timeset
Expand Down

0 comments on commit ec7261a

Please sign in to comment.