def test_execute_hourly_without_sun(self): # Prepare input files, without solar radiation self.timestamp = datetime(2014, 10, 1, 15, 0, tzinfo=senegal_tzinfo) self.setup_input_file('temperature', np.array([[38.0, 28.0]])) self.setup_input_file('humidity', np.array([[52.0, 42.0]])) self.setup_input_file('wind_speed', np.array([[3.3, 2.3]])) self.setup_input_file('pressure', np.array([[1013.0, 1013.0]])) # Configuration with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 nighttime_solar_radiation_ratio = 0.8 elevation = 8 step_length = 60 unit_converter_pressure = x / 10.0 unit_converter_solar_radiation = x * 3600 / 1e6 ''').format(self=self)) application = VaporizeApp() application.read_command_line() application.read_configuration() # Execute and check exception self.assertRaises(RuntimeError, application.run)
def test_albedo_configuration_as_one_number(self): with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} step_length = 60 albedo = 0.23 nighttime_solar_radiation_ratio = 0.8 elevation = 8 ''').format(self=self)) application = VaporizeApp() application.run(dry=True)
def test_seasonal_albedo_configuration_as_12_numbers(self): with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} step_length = 60 albedo = 0.10 0.23 0.34 0.24 0.45 0.46 0.34 0.12 0.14 0.78 0.78 0.12 nighttime_solar_radiation_ratio = 0.8 elevation = 8 ''').format(self=self)) application = VaporizeApp() application.run(dry=True)
def test_run_app_with_seasonal_albedo_with_mix_sample_inputs(self): with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} step_length = 60 albedo = a00.tif 0.23 a00.tif a00.tif a00.tif a00.tif a00.tif a00.tif 0.23 a00.tif 0.23 a00.tif nighttime_solar_radiation_ratio = 0.8 elevation = 8 ''').format(self=self)) application = VaporizeApp() application.run(dry=True)
def test_correct_configuration(self): with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 nighttime_solar_radiation_ratio = 0.8 elevation = 8 step_length = 60 unit_converter_pressure = x / 10.0 unit_converter_solar_radiation = x * 3600 / 1e6 ''').format(self=self)) application = VaporizeApp() application.run(dry=True)
def test_execute_with_nodata(self): """This is essentially the same as test_execute, but the gdal rasters contain cells with nodata.""" # Prepare input files self.timestamp = datetime(2014, 10, 1, 15, 0, tzinfo=senegal_tzinfo) nan = float("nan") self.setup_input_file('temperature', np.array([[38, nan]])) self.setup_input_file('humidity', np.array([[52, nan]])) self.setup_input_file('wind_speed', np.array([[3.3, 2.3]])) self.setup_input_file('pressure', np.array([[1013, 1013]])) self.setup_input_file('solar_radiation', np.array([[681, 403]])) self.setup_input_file('dem', np.array([[8, nan]]), with_date=False) with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 nighttime_solar_radiation_ratio = 0.8 elevation = dem.tif step_length = 60 unit_converter_pressure = x / 10.0 unit_converter_solar_radiation = x * 3600 / 1e6 ''').format(self=self)) application = VaporizeApp() application.read_command_line() application.read_configuration() # Verify the output file doesn't exist yet result_filename = os.path.join( self.tempdir, 'evaporation-{}.tif'.format( self.timestamp.strftime('%Y-%m-%d-%H-%M%z'))) self.assertFalse(os.path.exists(result_filename)) # Execute application.run() # Check that it has created a file self.assertTrue(os.path.exists(result_filename)) # Check that the created file is correct fp = gdal.Open(result_filename) timestamp = fp.GetMetadata()['TIMESTAMP'] self.assertEqual(timestamp, '2014-10-01T15:00:00-01:00') self.assertEqual(fp.RasterXSize, 2) self.assertEqual(fp.RasterYSize, 1) self.assertEqual(fp.GetGeoTransform(), self.geo_transform) # We can't just compare fp.GetProjection() to self.wgs84.ExportToWkt(), # because sometimes there are minor differences in the formatting or in # the information contained in the WKT. self.assertTrue(fp.GetProjection().startswith('GEOGCS["WGS 84",')) self.assertTrue(fp.GetProjection().endswith('AUTHORITY["EPSG","4326"]]' )) nodatavalue = fp.GetRasterBand(1).GetNoDataValue() np.testing.assert_almost_equal(fp.GetRasterBand(1).ReadAsArray(), np.array([[0.63, nodatavalue]]), decimal=2) fp = None
def test_execute_notz(self): # Prepare input files without time zone self.timestamp = datetime(2014, 10, 1, 15, 0) self.setup_input_file('temperature-notz', np.array([[38.0, 28.0]])) self.setup_input_file('humidity-notz', np.array([[52.0, 42.0]])) self.setup_input_file('wind_speed-notz', np.array([[3.3, 2.3]])) self.setup_input_file('pressure-notz', np.array([[1013.0, 1013.0]])) self.setup_input_file('solar_radiation-notz', np.array([[681.0, 403.0]])) with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 nighttime_solar_radiation_ratio = 0.8 elevation = 8 step_length = 60 unit_converter_pressure = x / 10.0 unit_converter_solar_radiation = x * 3600 / 1e6 temperature_prefix = temperature-notz humidity_prefix = humidity-notz wind_speed_prefix = wind_speed-notz solar_radiation_prefix = solar_radiation-notz pressure_prefix = pressure-notz ''').format(self=self)) application = VaporizeApp() application.read_command_line() application.read_configuration() # Verify the output file doesn't exist yet result_filename = os.path.join( self.tempdir, 'evaporation-{}.tif'.format( self.timestamp.strftime('%Y-%m-%d-%H-%M%z'))) self.assertFalse(os.path.exists(result_filename)) # Execute self.assertRaisesRegex(Exception, 'time zone', application.run) # Verify the output file still doesn't exist self.assertFalse(os.path.exists(result_filename))
def test_execute_hourly_no_pressure(self): """Same as test_execute_hourly, but does not have pressure an input; therefore, it will calculate pressure itself.""" # Prepare input files self.timestamp = datetime(2014, 10, 1, 15, 0, tzinfo=senegal_tzinfo) self.setup_input_file('temperature', np.array([[38.0, 28.0]])) self.setup_input_file('humidity', np.array([[52.0, 42.0]])) self.setup_input_file('wind_speed', np.array([[3.3, 2.3]])) self.setup_input_file('solar_radiation', np.array([[681.0, 403.0]])) # Also setup an output file that has no corresponding input files rogue_output_file = os.path.join( self.tempdir, 'evaporation-2013-01-01-15-00-0100.tif') with open(rogue_output_file, 'w') as f: f.write('irrelevant contents') with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 nighttime_solar_radiation_ratio = 0.8 elevation = 8 step_length = 60 unit_converter_solar_radiation = x * 3600 / 1e6 ''').format(self=self)) application = VaporizeApp() application.read_command_line() application.read_configuration() # Verify the output file doesn't exist yet result_filename = os.path.join( self.tempdir, 'evaporation-{}.tif'.format( self.timestamp.strftime('%Y-%m-%d-%H-%M%z'))) self.assertFalse(os.path.exists(result_filename)) # Verify the rogue output file is still here self.assertTrue(os.path.exists(rogue_output_file)) # Execute application.run() # Check that it has created a file self.assertTrue(os.path.exists(result_filename)) # Check that the rogue output file is gone self.assertFalse(os.path.exists(rogue_output_file)) # Check that the created file is correct fp = gdal.Open(result_filename) timestamp = fp.GetMetadata()['TIMESTAMP'] self.assertEqual(timestamp, '2014-10-01T15:00:00-01:00') self.assertEqual(fp.RasterXSize, 2) self.assertEqual(fp.RasterYSize, 1) self.assertEqual(fp.GetGeoTransform(), self.geo_transform) # We can't just compare fp.GetProjection() to self.wgs84.ExportToWkt(), # because sometimes there are minor differences in the formatting or in # the information contained in the WKT. self.assertTrue(fp.GetProjection().startswith('GEOGCS["WGS 84",')) self.assertTrue(fp.GetProjection().endswith('AUTHORITY["EPSG","4326"]]' )) np.testing.assert_almost_equal(fp.GetRasterBand(1).ReadAsArray(), np.array([[0.63, 0.36]]), decimal=2) fp = None
def test_execute_daily_with_radiation(self): """Same as test_execute_daily, except that we use solar radiation instead of sunshine duration.""" # Prepare input files self.timestamp = date(2014, 7, 6) self.setup_input_file('temperature_max', np.array([[21.5, 28]])) self.setup_input_file('temperature_min', np.array([[12.3, 15]])) self.setup_input_file('humidity_max', np.array([[84.0, 70.0]])) self.setup_input_file('humidity_min', np.array([[63.0, 60.0]])) self.setup_input_file('wind_speed', np.array([[2.078, 2.244]])) self.setup_input_file('solar_radiation', np.array([[22.07, 21.62]])) # Also setup an output file that has no corresponding input files rogue_output_file = os.path.join( self.tempdir, 'evaporation-2013-01-01.tif') with open(rogue_output_file, 'w') as f: f.write('irrelevant contents') with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 elevation = 100 step_length = 1440 ''').format(self=self)) application = VaporizeApp() application.read_command_line() application.read_configuration() # Verify the output file doesn't exist yet result_filename = os.path.join( self.tempdir, 'evaporation-{}.tif'.format( self.timestamp.strftime('%Y-%m-%d'))) self.assertFalse(os.path.exists(result_filename)) # Verify the rogue output file is still here self.assertTrue(os.path.exists(rogue_output_file)) # Execute application.run() # Check that it has created a file self.assertTrue(os.path.exists(result_filename)) # Check that the rogue output file is gone self.assertFalse(os.path.exists(rogue_output_file)) # Check that the created file is correct fp = gdal.Open(result_filename) timestamp = fp.GetMetadata()['TIMESTAMP'] self.assertEqual(timestamp, '2014-07-06') self.assertEqual(fp.RasterXSize, 2) self.assertEqual(fp.RasterYSize, 1) self.assertEqual(fp.GetGeoTransform(), self.geo_transform) # We can't just compare fp.GetProjection() to self.wgs84.ExportToWkt(), # because sometimes there are minor differences in the formatting or in # the information contained in the WKT. self.assertTrue(fp.GetProjection().startswith('GEOGCS["WGS 84",')) self.assertTrue(fp.GetProjection().endswith('AUTHORITY["EPSG","4326"]]' )) np.testing.assert_almost_equal(fp.GetRasterBand(1).ReadAsArray(), np.array([[3.9, 4.8]]), decimal=1) fp = None
def test_daily(self): self.setup_input_file('temperature_max', textwrap.dedent("""\ Title=Temperature Max Location=-16.25 16.217 4326 Altitude=100 2014-07-06,21.5, """)) self.setup_input_file('temperature_min', textwrap.dedent("""\ Title=Temperature Min Location=-16.25 16.217 4326 Altitude=100 2014-07-06,12.3, """)) self.setup_input_file('humidity_max', textwrap.dedent("""\ Title=Humidity Max Location=-16.25 16.217 4326 Altitude=100 2014-07-06,84.0, """)) self.setup_input_file('humidity_min', textwrap.dedent("""\ Title=Humidity Min Location=-16.25 16.217 4326 Altitude=100 2014-07-06,63.0, """)) self.setup_input_file('wind_speed', textwrap.dedent("""\ Title=Wind speed Location=-16.25 16.217 4326 Altitude=100 2014-07-06,2.078, """)) self.setup_input_file('sunshine_duration', textwrap.dedent("""\ Title=Sunshine duration Location=-16.25 16.217 4326 Altitude=100 2014-07-06,9.25, """)) with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 step_length = 1440 ''').format(self=self)) application = VaporizeApp() application.read_command_line() application.read_configuration() # Verify the output file doesn't exist yet result_filename = os.path.join(self.tempdir, 'evaporation.hts') self.assertFalse(os.path.exists(result_filename)) # Execute application.run() # Check that it has created a file and that the file is correct t = Timeseries() with open(result_filename) as f: t.read_file(f) self.assertEqual(len(t), 1) adate = datetime(2014, 7, 6) self.assertEqual(t.bounding_dates(), (adate, adate)) self.assertAlmostEqual(t[adate], 3.9)
def test_hourly(self): self.setup_input_file('temperature', textwrap.dedent("""\ Title=Temperature Location=-16.25 16.217 4326 Altitude=8 Timezone=CVT (UTC-0100) 2014-10-01 15:00,38, """)) self.setup_input_file('humidity', textwrap.dedent("""\ Title=Humidity Location=-16.25 16.217 4326 Altitude=8 Timezone=CVT (UTC-0100) 2014-10-01 15:00,52, """)) self.setup_input_file('wind_speed', textwrap.dedent("""\ Title=Wind speed Location=-16.25 16.217 4326 Altitude=8 Timezone=CVT (UTC-0100) 2014-10-01 15:00,3.3, """)) self.setup_input_file('pressure', textwrap.dedent("""\ Title=Pressure Location=-16.25 16.217 4326 Altitude=8 Timezone=CVT (UTC-0100) 2014-10-01 15:00,1013.0, """)) self.setup_input_file('solar_radiation', textwrap.dedent("""\ Title=Solar radiation Location=-16.25 16.217 4326 Altitude=8 Timezone=CVT (UTC-0100) 2014-10-01 15:00,681.0, """)) with open(self.config_file, 'w') as f: f.write(textwrap.dedent('''\ base_dir = {self.tempdir} albedo = 0.23 nighttime_solar_radiation_ratio = 0.8 step_length = 60 unit_converter_pressure = x / 10.0 unit_converter_solar_radiation = x * 3600 / 1e6 ''').format(self=self)) application = VaporizeApp() application.read_command_line() application.read_configuration() # Verify the output file doesn't exist yet result_filename = os.path.join(self.tempdir, 'evaporation.hts') self.assertFalse(os.path.exists(result_filename)) # Execute application.run() # Check that it has created a file and that the file is correct t = Timeseries() with open(result_filename) as f: t.read_file(f) self.assertEqual(len(t), 1) adate = datetime(2014, 10, 1, 15, 0, tzinfo=senegal_tzinfo) self.assertEqual(t.bounding_dates(), (adate, adate)) self.assertAlmostEqual(t[adate], 0.63)