Unidata / MetPy

MetPy is a collection of tools in Python for reading, visualizing and performing calculations with weather data.
https://unidata.github.io/MetPy/
BSD 3-Clause "New" or "Revised" License
1.25k stars 414 forks source link

Testing thermo calcs #719

Open dopplershift opened 6 years ago

dopplershift commented 6 years ago

We have this old document sitting around that may be a good source for testing/validation. Algorithms_For_Thermo_Calculations.pdf

eliteuser26 commented 6 years ago

What is intended with this issue? Do you need to test values to see if the results are correct? I can help with this and display the result of the tests.

Is there a need to add additional tests?

dopplershift commented 6 years ago

The intention with this issue is to look at that document and turn any example calculations into useful test cases to validate our implementations. It would also be appropriate to implement any missing calculations.

eliteuser26 commented 6 years ago

This is what I thought you wanted to do. So from your answer I should change current test examples with the values in the document. What about in the case of multiple values should I create arrays of values and expected results? I can see that the functions pass one value and expect one result unless I run a for loop over the array. Is that possible?

Also write additional tests for missing calculations.

dopplershift commented 6 years ago

No leave the current tests alone; just add new ones. As far as multiple values are concerned, in general we have functions that can check if arrays of values are equivalent, so no manual looping is needed. In general, I prefer one test per independent logical check. If there's an example in the document that explicitly does multiple values, then an array is appropriate, but I don't think it'd be good to group together logically independent checks into one test.

If it's simple enough, you can use pytest.mark.parametrize to run a test multiple times with different values.

For missing calculations, yes we would want tests based on any relevant examples in the document.

eliteuser26 commented 6 years ago

That is what I wanted to do is to not touch current tests. Create new tests for a single value. I will verify what is needed for the tests in terms of value or array of values. I can still run the test separately outside of the branch. I was going to compare current tests to the ones in the document to see if any tests are missing. Got it.

eliteuser26 commented 6 years ago

@dopplershift It is not define what starting values should be passed to the functions for temperature, relative humidity, pressure or other values when verifying values. Although units are defined in the equation but it doesn't indicate what kind of starting values to use. Unless I misunderstand pint values, no units are defined in the definition in the doc string. Suggestions or comments?

dopplershift commented 6 years ago

Is there a specific function you're looking at?

eliteuser26 commented 6 years ago

As an example, I was looking at saturation_vapor_pressure. I just picked one to compare to the Algorithms_For_Thermo_Calculations.pdf file. In the docstring it indicates that the temperature is in degree Celsius but in the equation it is in units of Kelvin. Should I convert the temperature in degree Kelvin and look at the result or put in temperature in degree Celsius and calculate the result? This is just one example. When reading the docstring it indicates that it accepts temperature as a parameter in Pint quantity but doesn't specify the units. For the parameters and result section in docstring, should it indicate the Pint quantity unit by default? Is there a location where I need to look for the units for each parameter? It would make easier to create the tests. I can search for the calculation in the PDF file and create the tests.

jrleeman commented 6 years ago

Hi Claude,

That's the beauty of using pint. We don't need a specific temperature unit - any one will do! The doc string indicates that the formula was written for degrees Celsius input, but pint takes care of unit conversions so we just check that you really gave us a temperature, not a length for example. Try this:

import metpy.calc as mpcalc
from metpy.units import units

print(mpcalc.saturation_vapor_pressure(25 * units.degC))
print(mpcalc.saturation_vapor_pressure(77 * units.degF))
print(mpcalc.saturation_vapor_pressure(298.15 * units.kelvin))
print(mpcalc.saturation_vapor_pressure(536.67 * units.degR))

and you'll see that they each produce 31.674294 hPa.

eliteuser26 commented 6 years ago

I have read the documentation and tutorial on Pint. It does the conversion on the fly before applying any operation on values. It is becoming clearer for me now when reading this. I noticed that some functions do accept arrays of values for calculation in Metpy.

eliteuser26 commented 6 years ago

@dopplershift What happened if there are slight differences in resulting values compared to the Thermo Algorithm document? I don't expect to find that many errors. It does look good. As an example, I calculated the saturation vapor pressure for -40C and came out with 0.18957612476 millibar instead of 0.51036 millibar in the document. Is that significant?

dopplershift commented 6 years ago

Well 60% difference seems significant to me, but that doesn't mean we're wrong--it largely depends on what formula, approach, and assumptions are being used in that document. Some careful judgement based on meteorological experience will be necessary here.

eliteuser26 commented 6 years ago

So far so good in testing some of the functions. For the saturation vapor pressure most results were very close. I am able to test a suite of values (ndarray) to calculate a suite of results and compare. I will quickly test them one by one so I can figure out which ones are missing. I am starting to understand the structure for the Pint module and you don't have to worry about the conversion so much. This is great.

eliteuser26 commented 6 years ago

I can make a series of tests for values and results by putting all functions in one file. I can add the expected results as information from the Algorithms_For_Thermo_Calculations in it. I am not sure if this file would be useful for other people to use.

eliteuser26 commented 6 years ago

I have found a web site with the same algorithm document but rewritten in text format. Much easier to visualize and read.

https://wahiduddin.net/calc/density_algorithms.htm

dopplershift commented 6 years ago

Excellent, that's good to know.