Skip to content

Commit

Permalink
Bugfix regresion spectrum calculation effective area arguments (josep…
Browse files Browse the repository at this point in the history
…hhardinee#99)

* Added some fixes for passing in effective sampling area as a function not being handled appropriately. Also added regressions tests to make sure this doesn't become issue in future

* Added test data for arm ld tests

* Ran test_DropSizeDistribution.py through black for formatting
  • Loading branch information
josephhardinee authored Sep 3, 2020
1 parent c0eeb17 commit 8a8ca66
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
8 changes: 6 additions & 2 deletions pydsd/DropSizeDistribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,19 @@ def calculate_dsd_from_spectrum(self, effective_sampling_area=None, replace=True
Parameters
----------
effective_sampling_area: function
Function that returns the effective sampling area as a function of diameter.
Function that returns the effective sampling area as a function of diameter. Optionally
a array with effective sampling area matched to diameter dimension can be provided as an array.
replace: boolean
Whether to replace Nd with the newly calculated one. If true, no return value to save memory.
"""

D = self.diameter["data"]

if effective_sampling_area is not None:
A = effective_sampling_area
if callable(effective_sampling_area):
A = effective_sampling_area(D)
else:
A = np.array(effective_sampling_area)
elif self.effective_sampling_area is not None:
A = self.effective_sampling_area["data"]
else:
Expand Down
53 changes: 50 additions & 3 deletions pydsd/tests/test_DropSizeDistribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
import copy

from ..aux_readers import ARM_Vdis_Reader
from ..io import ARM_vdisdrops_reader
from ..io.NetCDFWriter import write_netcdf
from .. import DropSizeDistribution
from ..aux_readers import ARM_APU_reader
from ..utility import filter


@pytest.fixture
def two_dvddrops_open_test_file(tmpdir):
filename_in = "testdata/corvdisdropsM1.b1.20181214.020816.cdf"
filename_out = tmpdir + "test_vdisdrops.nc"
dsd = ARM_vdisdrops_reader.read_arm_vdisdrops_netcdf(filename_in)
return dsd


@pytest.fixture
Expand All @@ -16,7 +27,15 @@ def two_dvd_open_test_file(tmpdir):
return dsd


class TestNetCDFWriter(object):
@pytest.fixture
def parsivel_open_test_file(tmpdir):
filename_in = "testdata/corldM1.b1.20181214.000000.cdf"
filename_out = tmpdir + "test_pars.nc"
dsd = ARM_APU_reader.read_parsivel_arm_netcdf(filename_in)
return dsd


class TestDropSizeDistribution(object):
"""
Test module for the DropSizeDistribution"
"""
Expand Down Expand Up @@ -49,10 +68,9 @@ def test_calculating_D0_returns_correct_values(self, two_dvd_open_test_file):
two_dvd_open_test_file.fields["Nd"]["data"][0, 1] = 2
two_dvd_open_test_file.calculate_dsd_parameterization()
assert np.isclose(
two_dvd_open_test_file.fields["D0"]["data"][0], .198, rtol=.01
two_dvd_open_test_file.fields["D0"]["data"][0], 0.198, rtol=0.01
) # One percent tolerance is closer than my hand calcs.

@pytest.mark.dependency()
def test_saving_of_scatter_table(self, two_dvd_open_test_file, tmpdir):
two_dvd_open_test_file.calculate_radar_parameters()
two_dvd_open_test_file.save_scattering_table(tmpdir + "/test_scatter.scatter")
Expand All @@ -66,3 +84,32 @@ def test_reading_of_scatter_table(self, two_dvd_open_test_file, tmpdir):
two_dvd_open_test_file.calculate_radar_parameters(
scatter_table_filename=tmpdir + "/test_scatter.scatter"
)

def test_calculate_spectrum_accepts_function(self, parsivel_open_test_file):
parsivel_open_test_file.calculate_dsd_from_spectrum(
effective_sampling_area=filter.parsivel_sampling_area
)
assert (
parsivel_open_test_file.fields["Nd"]["source"]
== "Calculated from spectrum."
)

def test_calculate_spectrum_no_optional_args_with_effective_area_on_object_set(
self, two_dvddrops_open_test_file
):
two_dvddrops_open_test_file.calculate_dsd_from_spectrum()
assert (
two_dvddrops_open_test_file.fields["Nd"]["source"]
== "Calculated from spectrum."
)

def test_calculate_spectrum_accepts_array_arg(self, two_dvddrops_open_test_file):
two_dvddrops_open_test_file.calculate_dsd_from_spectrum(
effective_sampling_area=two_dvddrops_open_test_file.effective_sampling_area[
"data"
]
)
assert (
two_dvddrops_open_test_file.fields["Nd"]["source"]
== "Calculated from spectrum."
)
File renamed without changes.
Binary file added testdata/corldM1.b1.20181214.000000.cdf
Binary file not shown.

0 comments on commit 8a8ca66

Please sign in to comment.