def test_refet_daily_output(daily_params): """Test daily RefET calculation at a single point and time""" # If I don't copy, the pop changes the test values in daily_data() inputs = daily_params.copy() surface = inputs.pop('surface') expected = inputs.pop('expected') # print('ETr: {}'.format(expected)) # ETr/ETo values only have 4 significant figures # Small number of days don't match if difference is set < 0.008 diff = 0.05 if expected >= 10.0 else 0.008 # Cast all numeric inputs to ee.Number type except tmin (for now) adj_inputs = {} for k, v in inputs.items(): if k == 'rso_type': adj_inputs[k] = v elif k in ['tmin', 'tmax', 'ea', 'rs', 'uz']: adj_inputs[k] = ee.Image.constant(v) else: adj_inputs[k] = ee.Number(v) if surface.lower() == 'etr': refet = Daily(**adj_inputs).etr() elif surface.lower() == 'eto': refet = Daily(**adj_inputs).eto() output = refet\ .reduceRegion(ee.Reducer.first(), geometry=constant_geom, scale=1)\ .getInfo() assert float(output[surface.lower()]) == pytest.approx(expected, abs=diff)
def test_refet_daily_surface_eto(): refet = Daily(tmax=ee.Image.constant(d_args['tmax']), tmin=ee.Image.constant(d_args['tmin']), ea=ee.Image.constant(d_args['ea']), rs=ee.Image.constant(d_args['rs']), uz=ee.Image.constant(d_args['uz']), zw=ee.Number(s_args['zw']), elev=ee.Number(s_args['elev']), lat=ee.Number(s_args['lat']), doy=ee.Number(d_args['doy']), method='refet') output = refet.eto()\ .reduceRegion(ee.Reducer.first(), geometry=constant_geom, scale=1)\ .getInfo() assert float(output['eto']) == pytest.approx(d_args['eto_refet'])
def test_refet_daily_nldas_etr(): """Generate a fake NLDAS image from the test values Convert the test Rs from MJ m-2 d-1 to W m-2, then allocate half to each image """ band_names = [ 'temperature', 'specific_humidity', 'shortwave_radiation', 'wind_u', 'wind_v' ] wind_u = d_args['uz'] / (2**0.5) nldas_coll = ee.ImageCollection.fromImages([ ee.Image.constant([d_args['tmin'], d_args['q_asce'], 0.0, wind_u, wind_u]) \ .double().rename(band_names) \ .set({'system:time_start': ee.Date('2015-07-01T00:00:00', 'UTC').millis()}), ee.Image.constant([d_args['tmax'], d_args['q_asce'], d_args['rs'] / 0.0036, wind_u, wind_u]) \ .double().rename(band_names) \ .set({'system:time_start': ee.Date('2015-07-01T12:00:00', 'UTC').millis()}) ]) refet = Daily.nldas(nldas_coll, elev=ee.Number(s_args['elev']), lat=ee.Number(s_args['lat']), zw=ee.Number(s_args['zw']), method='asce') output = refet.etr()\ .reduceRegion(ee.Reducer.first(), geometry=constant_geom, scale=1)\ .getInfo() assert float(output['etr']) == pytest.approx(d_args['etr_asce'])
def test_refet_daily_etsz(surface, expected): refet = Daily(tmax=ee.Image.constant(d_args['tmax']), tmin=ee.Image.constant(d_args['tmin']), ea=ee.Image.constant(d_args['ea']), rs=ee.Image.constant(d_args['rs']), uz=ee.Image.constant(d_args['uz']), zw=ee.Number(s_args['zw']), elev=ee.Number(s_args['elev']), lat=ee.Number(s_args['lat']), doy=ee.Number(d_args['doy']), method='refet') output = refet.etsz(surface).rename(['etsz']).reduceRegion( reducer=ee.Reducer.first(), geometry=ee.Geometry.Rectangle([0, 0, 10, 10], 'EPSG:32613', False), scale=1).getInfo() assert float(output['etsz']) == pytest.approx(expected)
def test_refet_daily_rso_type_exception(): with pytest.raises(ValueError): refet = Daily(tmax=ee.Image.constant(d_args['tmax']), tmin=ee.Image.constant(d_args['tmin']), ea=ee.Image.constant(d_args['ea']), rs=ee.Image.constant(d_args['rs']), uz=ee.Image.constant(d_args['uz']), zw=ee.Number(s_args['zw']), elev=ee.Number(s_args['elev']), lat=ee.Number(s_args['lat']), doy=ee.Number(d_args['doy']), rso_type='nonsense', method='refet')
def test_refet_daily_cfsv2_etr(): """Generate a fake CFSv2 image from the test values Convert the test Rs from MJ m-2 d-1 to W m-2 """ band_names = [ 'Maximum_temperature_height_above_ground_6_Hour_Interval', 'Minimum_temperature_height_above_ground_6_Hour_Interval', 'Specific_humidity_height_above_ground', 'Downward_Short-Wave_Radiation_Flux_surface_6_Hour_Average', 'u-component_of_wind_height_above_ground', 'v-component_of_wind_height_above_ground', ] wind_u = d_args['uz'] / (2**0.5) cfsv2_coll = ee.ImageCollection.fromImages([ ee.Image.constant([d_args['tmax'] + 273.15, d_args['tmin'] + 273.15, d_args['q_asce'], d_args['rs'] / 0.0864, wind_u, wind_u]) \ .double().rename(band_names) \ .set({'system:time_start': ee.Date('2015-07-01T00:00:00', 'UTC').millis()}), ee.Image.constant([d_args['tmax'] + 273.15, d_args['tmin'] + 273.15, d_args['q_asce'], d_args['rs'] / 0.0864, wind_u, wind_u]) \ .double().rename(band_names) \ .set({'system:time_start': ee.Date('2015-07-01T06:00:00', 'UTC').millis()}), ee.Image.constant([d_args['tmax'] + 273.15, d_args['tmin'] + 273.15, d_args['q_asce'], d_args['rs'] / 0.0864, wind_u, wind_u]) \ .double().rename(band_names) \ .set({'system:time_start': ee.Date('2015-07-01T12:00:00', 'UTC').millis()}), ee.Image.constant([d_args['tmax'] + 273.15, d_args['tmin'] + 273.15, d_args['q_asce'], d_args['rs'] / 0.0864, wind_u, wind_u]) \ .double().rename(band_names) \ .set({'system:time_start': ee.Date('2015-07-01T18:00:00', 'UTC').millis()}), ]) refet = Daily.cfsv2(cfsv2_coll, elev=ee.Number(s_args['elev']), lat=ee.Number(s_args['lat']), zw=ee.Number(s_args['zw']), method='asce') output = refet.etr()\ .reduceRegion(ee.Reducer.first(), geometry=constant_geom, scale=1)\ .getInfo() assert float(output['etr']) == pytest.approx(d_args['etr_asce'])
def test_refet_daily_nldas_eto(): """Compare values to previous calculations""" test_point = ee.Geometry.Point(-120.113, 36.336) nldas_coll = ee.ImageCollection('NASA/NLDAS/FORA0125_H002')\ .filterDate('2017-07-01T06:00:00', '2017-07-02T06:00:00') output = Daily.nldas(nldas_coll, method='asce').eto() \ .reduceRegion(ee.Reducer.first(), geometry=test_point, scale=1)\ .getInfo()['eto'] expected = ee.Image('projects/eddi-noaa/nldas/daily/20170701')\ .select(['ETo'])\ .reduceRegion(ee.Reducer.first(), geometry=test_point, scale=1)\ .getInfo()['ETo'] assert output == pytest.approx(expected, rel=0.001)
def test_refet_daily_gridmet_etr(): """Generate a fake GRIDMET image from the test values""" gridmet_img = ee.Image.constant([ d_args['tmax'] + 273.15, d_args['tmin'] + 273.15, d_args['q_asce'], d_args['rs'] / 0.0864, d_args['uz']])\ .rename(['tmmx', 'tmmn', 'sph', 'srad', 'vs'])\ .set('system:time_start', ee.Date('2015-07-01').millis()) refet = Daily.gridmet(ee.Image(gridmet_img), elev=ee.Number(s_args['elev']), lat=ee.Number(s_args['lat']), zw=ee.Number(s_args['zw']), method='asce') output = refet.etr()\ .reduceRegion(ee.Reducer.first(), geometry=constant_geom, scale=1)\ .getInfo() assert float(output['etr']) == pytest.approx(d_args['etr_asce'])
def test_refet_daily_maca_etr(): """Generate a fake MACA image from the test values""" maca_img = ee.Image.constant([ d_args['tmax'] + 273.15, d_args['tmin'] + 273.15, d_args['q_asce'], d_args['rs'] / 0.0864, d_args['uz'] / (2 ** 0.5), d_args['uz'] / (2 ** 0.5)])\ .rename(['tasmax', 'tasmin', 'huss', 'rsds', 'uas', 'vas'])\ .set('system:time_start', ee.Date('2015-07-01').millis()) refet = Daily.maca(ee.Image(maca_img), elev=ee.Number(s_args['elev']), lat=ee.Number(s_args['lat']), zw=ee.Number(s_args['zw']), method='asce') output = refet.etr()\ .reduceRegion(ee.Reducer.first(), geometry=constant_geom, scale=1)\ .getInfo() assert float(output['etr']) == pytest.approx(d_args['etr_asce'])