diff --git a/tests/clm_mocking_files/drv_clmin.dat b/tests/clm_mocking_files/drv_clmin.dat new file mode 100644 index 0000000..0142d5c --- /dev/null +++ b/tests/clm_mocking_files/drv_clmin.dat @@ -0,0 +1,115 @@ +!========================================================================= +! +! CLMCLMCLMCLMCLMCLMCLMCLMCL A community developed and sponsored, freely +! L M available land surface process model. +! M --COMMON LAND MODEL-- C +! C L CLM WEB INFO: http://www.clm.org? +! LMCLMCLMCLMCLMCLMCLMCLMCLM CLM ListServ/Mailing List: +! +!========================================================================= +! drv_clmin.dat: Read by drv_readclmin.f90 +! +! DESCRIPTION: +! 1-D user defined CLM parameters. +! NOTE: For 2-D runs, it is recommended that readclm_in.f be +! modified to allow the specification of spatially-variable parameters. +! This file can still be used for all spatially-constant variabiles when +! running CLM in 2-D. +! +! NOTE on INDEXES: There are several index soil and vegetation values. These +! are followedby several parameters that are defined by the index. If a +! -999.0 is placed in the fields after the index, then the default index +! value will be used. However, if it is desired to override this default +! value, simply replace the -999.0 with the value you desire. +! +! INPUT DATA FORMAT: +! FORTRAN PARAMETER NAME, VALUE, description (not read in) +! This is fixed format, in any order. readclm_in.f skips any comment lines +! +! REVISION HISTORY: +! 12 April 1999: Paul Houser; initial code +!========================================================================= +! FILE FORMAT: +!Variable Name Variable Value Description (not read in) +!-------------!!--------------------------------------!!------> +!========================================================================= +! +! CLM Domain (Read into 1D drv_module variables) : +! +maxt 1 Maximum tiles per grid (originally 3; changed it, becasue we have one type per cell) +mina 0.05 Min grid area for tile (%) +udef -9999. Undefined value +vclass 2 Vegetation Classification Scheme (1=UMD,2=IGBP,etc.) NOT the index +! +! CLM Files (Read into 1D drv_module variables): +! +vegtf drv_vegm.fixed.dat Vegetation Tile Specification File +vegpf drv_vegp.dat Vegetation Type Parameter +metf1d narr_1hr.dat Meterologic input file- valdai 3 hr +outf1d clm_out.txt CLM output file +poutf1d clm_para_out.dat CLM 1D Parameter Output File +rstf clm.rst. CLM active restart file +! +!!! LDAS Run Timing Parameters (Read into 1D drv_module variables): +! +startcode 2 1=restart file,2=defined! +sss 00 Starting Second +smn 00 Starting Minute +shr 00 Starting Hour +sda 01 Starting Day +smo 10 Starting Month +syr 2002 Starting Year +! +ess 00 Ending Second +emn 00 Ending Minute +ehr 23 Ending Hour +eda 30 Ending Day +emo 09 Ending Month +eyr 2003 Ending Year +! +! IC Source: (1) restart file, (2) drv_clmin.dat (this file) +! +clm_ic 2 1=restart file,2=defined +! CLM Initial Condition Source +! +! CLM initial conditions (1-D : used in drv_clmini.f90_ +! +t_ini 300. Initial temperature [K] +h2osno_ini 0. Initial snow cover, water equivalent [mm] +! +! Diagnostic CLM output variable specification +! +surfind 2 Number of surface diagnostic variables to output +soilind 1 Number of soil layer diagnostic variables to output +snowind 0 Number of snow layer diagnostic variables to output +! +!=== NOTE: The variables below are assigned to the ENTIRE grid domain, +!=== and then are translated into tilespace. To read in grid or +!=== tilespace variable information, you must modify the driver code. +! +!===CLM Forcingparameters (read into 2-D grid module variables) +! +forc_hgt_u 10.0 Observational height of wind [m] +forc_hgt_t 2.0 Observational height of temperature [m] +forc_hgt_q 2.0 Observational height of humidity [m] +! +!===CLM Vegetation parameters (read into 2-D grid module variables) +! +dewmx 0.1 Maximum allowed dew [mm] +qflx_tran_vegmx-9999.0 Maximum possible transpiration rate [mm/s] +rootfr -9999.0 Root Fraction (depth average) +! +! +!===Roughness lengths (read into 2-D grid module variables) +! +zlnd 0.01 Roughness length for soil [m] +zsno 0.0024 Roughness length for snow [m] +csoilc 0.0025 Drag coefficient for soil under canopy [-] +! +!=== Numerical finite-difference parameters (read into 2-D grid module variables) +! +capr 0.34 Tuning factor to turn first layer T into surface T +cnfac 0.5 Crank Nicholson factor between 0 and 1 +smpmin -1.e8 Restriction for min of soil poten. (mm) +ssi 0.033 Irreducible water saturation of snow +wimp 0.05 Water impremeable if porosity < wimp diff --git a/tests/clm_mocking_files/drv_vegp.dat b/tests/clm_mocking_files/drv_vegp.dat new file mode 100644 index 0000000..e3b8971 --- /dev/null +++ b/tests/clm_mocking_files/drv_vegp.dat @@ -0,0 +1,111 @@ +!========================================================================= +! +! CLMCLMCLMCLMCLMCLMCLMCLMCL A community developed and sponsored, freely +! L M available land surface process model. +! M --COMMON LAND MODEL-- C +! C L CLM WEB INFO: http://clm.gsfc.nasa.gov +! LMCLMCLMCLMCLMCLMCLMCLMCLM CLM ListServ/Mailing List: +! +!========================================================================= +! clm_vegp.dat: +! +! DESCRIPTION: +! Vegetation parameter data file for IGBP classification. +! Please note that any other classification can be used with CLM +! provided that the following parameter values are given. +! This data file is read in by drv_getvegp.f90 - this program expects +! the format given below, but does not assume an exact parameter order. +! +! REVISION HISTORY: +! 15 September 1999: Yongjiu Dai; Initial code +! 15 December 1999: Paul Houser; F90 Revision +! 25 November 2000: Mariana Vertenstein +!========================================================================= +!IGBP Land Cover Types (other classes can be used by changing this file) +! 1 evergreen needleleaf forests +! 2 evergreen broadleaf forests +! 3 deciduous needleleaf forests +! 4 deciduous broadleaf forests +! 5 mixed forests +! 6 closed shrublands +! 7 open shrublands +! 8 woody savannas +! 9 svannas +! 10 grasslands +! 11 permanent wetlands +! 12 croplands +! 13 urban and built-up lands +! 14 cropland / natural vegetation mosaics +! 15 snow and ice +! 16 barren or sparsely vegetated +! 17 water bodies +! 18 bare soil +!========================================================================= +! +itypwat (1-soil, 2-land ice, 3-deep lake, 4-shallow lake, 5-wetland: swamp, marsh) +1 1 1 1 1 1 1 1 1 1 5 1 1 1 2 1 3 1 +! +lai Maximum leaf area index [-] +6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 2.00 6.00 6.00 5.00 6.00 0.00 6.00 0.00 0.00 +! +lai0 Minimum leaf area index [-] +5.00 5.00 1.00 1.00 3.00 2.00 1.00 2.00 1.00 0.50 0.50 0.50 1.00 2.00 0.00 0.50 0.00 0.00 +! +sai Stem area index [-] +2.00 2.00 2.00 2.00 2.00 2.00 2.00 2.00 2.00 4.00 2.00 0.50 2.00 2.00 2.00 2.00 2.00 0.00 +! +z0m Aerodynamic roughness length [m] +1.00 2.20 1.00 0.80 0.80 0.10 0.10 0.70 0.10 0.03 0.03 0.06 0.50 0.06 0.01 0.05 0.002 0.01 +! +displa Displacement height [m] +11.0 23.00 11.0 13.0 13.0 0.30 0.30 6.50 0.70 0.30 0.30 0.30 3.00 0.30 0.00 0.10 0.00 0.00 +! +dleaf Leaf dimension [m] +0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.04 0.00 +! +roota Fitted numerical index of rooting distribution +7.00 7.00 7.00 6.00 5.00 6.00 5.00 6.00 5.00 1.00 6.00 6.00 5.00 5.00 0.00 5.00 0.00 0.00 +! +rootb Fitted numerical index of rooting distribution +2.00 1.00 2.00 2.00 1.50 1.50 2.50 2.50 1.00 2.50 2.00 2.50 2.00 2.00 0.00 2.00 0.00 0.00 +! +rhol_vis !leaf reflectance vis +0.07 0.10 0.07 0.10 0.08 0.08 0.08 0.09 0.11 0.11 0.11 0.11 0.09 0.09 -99. 0.09 -99. -99. +! +rhol_nir !leaf reflectance nir +0.35 0.45 0.35 0.45 0.40 0.40 0.40 0.49 0.58 0.58 0.35 0.58 0.47 0.47 -99. 0.47 -99. -99. +! +rhos_vis !stem reflectance vis +0.16 0.16 0.16 0.16 0.16 0.16 0.16 0.26 0.36 0.36 0.36 0.36 0.24 0.24 -99. 0.24 -99. -99. +! +rhos_nir !stem reflectance nir +0.39 0.39 0.39 0.39 0.39 0.39 0.39 0.48 0.58 0.58 0.39 0.58 0.47 0.47 -99. 0.47 -99. -99. +! +taul_vis !leaf transmittance vis +0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.06 0.07 0.07 0.07 0.07 0.06 0.06 -99. 0.06 -99. -99. +! +taul_nir !leaf transmittance nir +0.10 0.25 0.10 0.25 0.17 0.17 0.17 0.21 0.25 0.25 0.10 0.25 0.20 0.20 -99. 0.20 -99. -99. +! +taus_vis !stem transmittance vis +0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.11 0.22 0.22 0.22 0.22 0.09 0.09 -99. 0.09 -99. -99. +! +taus_nir !stem transmittance nir +0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.19 0.38 0.38 0.001 0.38 0.15 0.15 -99. 0.15 -99. -99. +! +xl !leaf/stem orientation index +0.01 0.10 0.01 0.25 0.13 0.13 0.13 -0.08 -0.3 -0.3 -0.3 -0.3 -0.07 -0.07 -99. -0.07 -99. -99. +! +vw !btran exponent:[(h2osoi_vol-watdry)/(watopt-watdry)]**vw +1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. -99. 1. -99. -99. +! +irrig !irrigation flag for each PFT +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +! + + + + + + + diff --git a/tests/test_clm.py b/tests/test_clm.py new file mode 100644 index 0000000..7108b19 --- /dev/null +++ b/tests/test_clm.py @@ -0,0 +1,95 @@ +import os +import pytest +import numpy as np +import subsettools as st +import hf_hydrodata +import shutil +from test_subsetting import mock_hf_data + +@pytest.fixture +def mock_hf_get_file(monkeypatch): + def mock_get_raw_file(filepath, **kwargs): + file_type = kwargs.get("file_type") + write_dir = filepath + if file_type == "vegp": + file_path = os.path.join(os.getcwd(), "tests/clm_mocking_files/drv_vegp.dat") + shutil.copy(file_path, write_dir) + elif file_type == "drv_clm": + file_path = os.path.join(os.getcwd(), "tests/clm_mocking_files/drv_clmin.dat") + shutil.copy(file_path, write_dir) + monkeypatch.setattr(hf_hydrodata, "get_raw_file", mock_get_raw_file) + + + +def test_config_clm_filepaths(mock_hf_get_file, mock_hf_data, tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + file_paths = st.config_clm( + ij_bounds=(0,0,10,10), + start="2005-10-01", + end="2006-10-01", + dataset="conus1_baseline_mod", + write_dir=write_dir + ) + assert file_paths["vegp"] == f"{write_dir}/drv_vegp.dat" + assert file_paths["pfb"] == f"{write_dir}/drv_vegm.dat" + assert file_paths["drv_clm"] == f"{write_dir}/drv_clmin.dat" + +def test_config_clm_data_vegm(mock_hf_get_file, mock_hf_data, tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + file_paths = st.config_clm( + ij_bounds=(0,0,10,10), + start="2005-10-01", + end="2006-10-01", + dataset="conus1_baseline_mod", + write_dir=write_dir + ) + with open(f"{write_dir}/drv_vegm.dat") as my_file: + file_lines = my_file.readlines() + assert file_lines[2] == '1 1 1.000000 1.000000 1.00 1.00 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n' + assert len(file_lines) == 3 + + +@pytest.mark.parametrize( + "start, end, expected_start, expected_end", + [ + pytest.param("2005-10-01","2005-10-06", "2005101", "2005105", id="time window of one year"), + pytest.param("2005-10-01", "2006-10-01", "2005101", "2006930", id="time window of 5 days") + ], +) +def test_config_clm_data_drv_clm(start, end, expected_start, expected_end, mock_hf_get_file, mock_hf_data, tmp_path): + write_dir = tmp_path / "write" + write_dir.mkdir() + file_paths = st.config_clm( + ij_bounds=(0,0,10,10), + start=start, + end=end, + dataset="conus1_baseline_mod", + write_dir=write_dir + ) + # open file and save lines + with open(f"{write_dir}/drv_clmin.dat") as my_file: + file_lines = my_file.readlines() + # start and end dates are written as three different lines + # so I use this function to pull out the numbers from each line and put them together in an array + start_numbers = [] + end_numbers = [] + def pull_numbers(line_no, my_list): + line=file_lines[line_no] + stripped = line.replace(" ", "") + for char in stripped: + if char.isdigit(): + my_list.append(char) + # pull out the start date + pull_numbers(60, start_numbers) + pull_numbers(59, start_numbers) + pull_numbers(58, start_numbers) + # pull out the end date + pull_numbers(67, end_numbers) + pull_numbers(66, end_numbers) + pull_numbers(65, end_numbers) + + # check that it matches up with provided expected values + assert expected_start == "".join(start_numbers) + assert expected_end == "".join(end_numbers) \ No newline at end of file diff --git a/tests/test_subsetting.py b/tests/test_subsetting.py index e81bb3b..3be80df 100644 --- a/tests/test_subsetting.py +++ b/tests/test_subsetting.py @@ -31,7 +31,10 @@ def mock_get_data(options): else: shape = (nt, nj, ni) elif resolution == "static": - shape = (_DUMMY_NZ, nj, ni) + if variable == "clm_run": + shape = (23, 1, 1) + else: + shape = (_DUMMY_NZ, nj, ni) return np.ones(shape) monkeypatch.setattr(hf_hydrodata, "get_gridded_data", mock_get_data)