Skip to content

Commit 2ca3454

Browse files
Apply suggestions from Sarah's code review
Co-authored-by: SarahAlidoost <55081872+SarahAlidoost@users.noreply.github.com>
1 parent 515017f commit 2ca3454

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

PyStemmusScope/forcing_io.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import hdf5storage
33
import numpy as np
44
import xarray as xr
5-
from PyStemmusScope import variable_conversion as vc
5+
from . import variable_conversion as vc
66

77

88
def _write_matlab_ascii(fname, data, ncols):
@@ -38,6 +38,7 @@ def read_forcing_data(forcing_file):
3838
ds_forcing = ds_forcing.squeeze(['x', 'y'])
3939

4040
data = {}
41+
4142
# Expected time format is days (as floating point) since Jan 1st 00:00.
4243
data['doy_float'] = (
4344
ds_forcing['time'].dt.dayofyear - 1 +
@@ -59,7 +60,7 @@ def read_forcing_data(forcing_file):
5960
data['ea'] = vc.kpa_to_hpa(vc.calculate_ea(data['t_air_celcius'], data['rh']))
6061

6162
# Load in non-timedependent variables
62-
data['sitename'] = forcing_file.name[:6]
63+
data['sitename'] = forcing_file.name.split('_')[0]
6364
# Forcing data timestep size in seconds
6465
time_delta = (ds_forcing.time.values[1] -
6566
ds_forcing.time.values[0]) / np.timedelta64(1, 's')
@@ -166,6 +167,7 @@ def prepare_forcing(input_dir, forcing_file, config):
166167
config (dict): The PyStemmusScope configuration dictionary.
167168
"""
168169
input_path = Path(input_dir)
170+
169171
# Read the required data from the forcing file into a dictionary
170172
data = read_forcing_data(forcing_file)
171173

tests/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44

55
test_folder = Path(__file__).resolve().parent
6-
data_folder = Path(test_folder, 'test_data')
6+
data_folder = test_folder / 'test_data'
7+
forcing_data_folder = test_folder / 'test_data_forcing'

tests/test_forcing_io.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pandas as pd
44
import pytest
55
from PyStemmusScope import forcing_io
6-
6+
from . import forcing_data_folder
77

88
numbers = '0123456789'
99

@@ -28,7 +28,7 @@ def eval_str(fdata):
2828

2929
@pytest.fixture(scope='session', autouse=True)
3030
def forcing_data():
31-
forcing_file = (Path(__file__).parent / "test_data_forcing" /
31+
forcing_file = (forcing_data_folder /
3232
"FI-Hyy_1996-2014_FLUXNET2015_Met.nc")
3333
return forcing_io.read_forcing_data(forcing_file)
3434

@@ -58,7 +58,7 @@ def dat_files(tmpdir_factory, forcing_data):
5858

5959

6060
def test_mdata(mdata_file):
61-
fn_expected = './tests/test_data_forcing/Mdata.txt'
61+
fn_expected = forcing_data_folder / 'Mdata.txt'
6262

6363
df_expected = pd.read_fwf(fn_expected)
6464
df_written = pd.read_fwf(mdata_file)
@@ -68,7 +68,7 @@ def test_mdata(mdata_file):
6868

6969
# Verifying that the actual data file passes the evaluation
7070
def test_true_mdata_format():
71-
with open('./tests/test_data_forcing/Mdata.txt', encoding='utf-8') as f:
71+
with open(forcing_data_folder / 'Mdata.txt', encoding='utf-8') as f:
7272
content_exp = f.read()
7373
assert eval_str(content_exp)
7474

@@ -81,7 +81,7 @@ def test_mdata_format(mdata_file):
8181

8282

8383
def test_lai_file(lai_file):
84-
fn_expected = './tests/test_data_forcing/LAI_.dat'
84+
fn_expected = forcing_data_folder / 'LAI_.dat'
8585

8686
df_expected = pd.read_fwf(fn_expected)
8787
df_written = pd.read_fwf(lai_file)
@@ -96,7 +96,7 @@ def test_lai_file_format(lai_file):
9696

9797

9898
def test_dat_files(dat_files):
99-
expected_path = './tests/test_data_forcing/'
99+
expected_path = forcing_data_folder
100100

101101
fnames, write_dir = dat_files
102102

@@ -115,7 +115,7 @@ def test_dat_file_format(dat_files):
115115

116116

117117
def test_full_routine(tmp_path, dat_files):
118-
forcing_file = (Path(__file__).parent / "test_data_forcing" /
118+
forcing_file = (forcing_data_folder /
119119
"FI-Hyy_1996-2014_FLUXNET2015_Met.nc")
120120
config = {'NumberOfTimeSteps': 5}
121121

0 commit comments

Comments
 (0)