def thorn(year, latitude, df, model): data = df.loc[df['yil'] == year][model].values # lat = 37 lat = deg2rad(latitude) mmdlh = monthly_mean_daylight_hours(lat, year) evapo = np.array(thornthwaite(data, mmdlh)) return evapo
def main(ref_raster_filepath, year, dst_filepath, align, buffer_dist): logger = logging.getLogger(__name__) suhi.settings.METEOSWISS_S3_CLIENT_KWARGS = { 'endpoint_url': environ.get('S3_ENDPOINT_URL') } with rio.open(ref_raster_filepath) as src: # get the (valid data) extent of the raster ref_raster_extent_geom = [ geometry.shape(geom) for geom, val in features.shapes(src.dataset_mask(), transform=src.transform) if val != 0 ][0] # get the monthly mean temperature data array of the reference year for # the raster extent monthly_T_da = suhi.open_meteoswiss_s3_ds( year, 'TabsD', geom=ref_raster_extent_geom.buffer(buffer_dist), crs=src.crs.to_string(), roi=False)['TabsD'] attrs = monthly_T_da.attrs.copy() monthly_T_da = monthly_T_da.resample(time='1MS').mean(dim='time') # select the hottest month hottest_month_i = monthly_T_da.mean(dim=['x', 'y']).argmax() hottest_month_T_da = monthly_T_da.isel(time=hottest_month_i) # hottest_month_T_da.attrs['pyproj_srs'] = attrs['pyproj_srs'] # potential evapotranspiration (Thornthwaite, 1948) year_heat_index = monthly_T_da.groupby('time').apply( lambda temp: (temp / 5)**1.514).sum(dim='time') year_alpha = .49239 + .01792 * year_heat_index - \ .0000771771 * year_heat_index**2 + .000000675 * year_heat_index**3 pe_da = xr.full_like(hottest_month_T_da, np.nan) pe_da = pe_da.where( ~(hottest_month_T_da >= 26.5), -415.85 + 32.24 * hottest_month_T_da - .43 * hottest_month_T_da**2) pe_da = pe_da.where( ~((hottest_month_T_da > 0) & (hottest_month_T_da < 26.5)), 16 * ((10 * (hottest_month_T_da / year_heat_index))**year_alpha)) pe_da = pe_da.where(~(hottest_month_T_da <= 0), 0) pe_da *= pyeto.monthly_mean_daylight_hours(JONCTION_LAT)[ hottest_month_i.item()] / 12 pe_da.attrs['pyproj_srs'] = attrs['pyproj_srs'] if align: # align it to the reference raster ref_ds = salem.open_xr_dataset(ref_raster_filepath) pe_da = suhi.align_ds(pe_da, ref_ds) # write the raster pe_da.rio.set_crs(pe_da.attrs['pyproj_srs']).rio.to_raster(dst_filepath) logger.info("dumped reference evapotranspiration raster to %s", dst_filepath)
def thorn_clima(year, latitude, df, model, senario): data = df.loc[(df['Yıl'] == year) & (df['Model'] == model) & (df['Senaryo'] == senario), 'Ortalama_Sıcaklık'].values # lat = 37 if len(data) != 12: data = data[0:12] lat = deg2rad(latitude) mmdlh = monthly_mean_daylight_hours(lat, year) evapo = np.array(thornthwaite(data, mmdlh)) return data, evapo
def main(lat, temp_mean, precip_mean, max_tsmd): '''get evapotranspiration''' # taken from Richards pyeto, see doc in same folder for more informatios pyeto_lat = pyeto.deg2rad(lat) # converts the degree to radians mean_month_list = [temp_mean[i] for i in temp_mean] monthly_mean_daylight = pyeto.monthly_mean_daylight_hours(pyeto_lat) eto = pyeto.thornthwaite(mean_month_list, monthly_mean_daylight) '''make a "month_number" : "evapo" dic''' eto_dict = {} for n,i in enumerate(eto): eto_dict[n+1] = eto[n] print "\nWaterbalance with maximum deficientcy of", max_tsmd acc_TSMD = {} acc_factor = 0 budget = {} '''The following part is still a bit hacky. Two for loops that first calculates the the water buget for every mongth and then starts a second loop to substract the value of the month before from the current month ''' print "month \t rain \t eto \t\t TSMD" for month, rain, etom in zip(range(1,13),precip_mean, eto_dict): TSMD = precip_mean[rain] - ( eto_dict[etom] * 0.75 ) if TSMD <= max_tsmd: TSMD = max_tsmd print month,"\t", precip_mean[rain], "\t", eto_dict[etom],"\t", TSMD budget[month] = TSMD if TSMD < 0 else 0 for month, rain, etom in zip(range(1,13),precip_mean, eto_dict): TSMD = precip_mean[rain] - ( eto_dict[etom] * 0.75 ) if month == 1: acc_TSMD[month] = TSMD + budget[12] else: acc_TSMD[month] = TSMD + budget[month -1] if acc_TSMD[month] > 0 : acc_TSMD[month] = 0 if acc_TSMD[month] < max_tsmd: acc_TSMD[month] = max_tsmd print "\nWater budget" p(budget) print "" return acc_TSMD
import pandas as pd import numpy as np import os from pyeto import thornthwaite, monthly_mean_daylight_hours, deg2rad os.chdir(r"C:\Users\PC3\Desktop\Hydro") df = pd.read_csv('data.csv') df['Date'] = pd.to_datetime(df['Date']) df['Year'] = df['Date'].dt.year df = df.set_index('Date') df_m = df.resample("M").mean() data = [] lat = deg2rad(41.3) for i in range(2015,2019): mmdlh = monthly_mean_daylight_hours(lat, i) monthly_t = df_m['Temp'][(df_m.Year == i)] PET = thornthwaite(monthly_t.values.tolist(), mmdlh) data.append(PET) print("a")
def test_monthly_mean_daylight_hours(self): # Test against values for latitude 20 deg N from Bautista et al (2009) # Calibration of the equations of Hargreaves and Thornthwaite to # estimate the potential evapotranspiration in semi-arid and subhumid # tropical climates for regional applications. Atmosfera 22(4), 331- # 348. test_mmdlh = [ 10.9, # Jan 11.3, # Feb 11.9, # Mar 12.5, # Apr 12.9, # May 13.2, # Jun 13.1, # Jul 12.7, # Aug 12.1, # Sep 11.5, # Oct 11.0, # Nov 10.8, # Dec ] mmdlh = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(20.0)) # Values were only quoted to 1 decimal place so check they are accurate # to within 12 minutes (0.2 hours) for m in range(12): self.assertAlmostEqual(mmdlh[m], test_mmdlh[m], delta=0.15) # Test against values for latitude 46 deg N from Mimikou M. and # Baltas E., Technical hydrology, Second edition, NTUA, 2002. # cited in PAPADOPOULOU E., VARANOU E., BALTAS E., DASSAKLIS A., and # MIMIKOU M. (2003) ESTIMATING POTENTIAL EVAPOTRANSPIRATION AND ITS # SPATIAL DISTRIBUTION IN GREECE USING EMPIRICAL METHODS. test_mmdlh = [ 8.9, # Jan 10.1, # Feb 11.6, # Mar 13.3, # Apr 14.7, # May 15.5, # Jun 15.2, # Jul 13.9, # Aug 12.3, # Sep 10.7, # Oct 9.2, # Nov 8.5, # Dec ] mmdlh = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(46.0)) # Values were only quoted to 1 decimal place so check they are accurate # to within 12 minutes (0.2 hours) for m in range(12): self.assertAlmostEqual(mmdlh[m], test_mmdlh[m], delta=0.15) # Test against values obtained for Los Angles, California, # latitude 34 deg 05' N, from # http://aa.usno.navy.mil/data/docs/Dur_OneYear.php latitude = pyeto.deg2rad(34.0833333) la_mmdlh = [ 10.182, # Jan 10.973, # Feb 11.985, # Mar 13.046, # Apr 13.940, # May 14.388, # Jun 14.163, # Jul 13.404, # Aug 12.374, # Sep 11.320, # Oct 10.401, # Nov 9.928, # Dec ] mmdlh = pyeto.monthly_mean_daylight_hours(latitude) # Check that the 2 methods are almost the same (within 15 minutes) for m in range(12): self.assertAlmostEqual(mmdlh[m], la_mmdlh[m], delta=0.25) # Test with year set to a non-leap year non_leap = pyeto.monthly_mean_daylight_hours(latitude, 2015) for m in range(12): self.assertEqual(mmdlh[m], non_leap[m]) # Test with year set to a leap year leap = pyeto.monthly_mean_daylight_hours(latitude, 2016) for m in range(12): if m == 0: self.assertEqual(leap[m], non_leap[m]) elif m == 1: # Feb # Because Feb extends further into year in a leap year it # should have a slightly longer mean day length in northern # hemisphere self.assertGreater(leap[m], non_leap[m]) else: # All months after Feb in a lieap year will be composed of # diffent Julian days (days of the year) compared to a # non-leap year so will have different mean daylengths. self.assertNotEqual(leap[m], non_leap[m]) # Test with bad latitude with self.assertRaises(ValueError): _ = pyeto.monthly_mean_daylight_hours( pyeto.deg2rad(90.01)) with self.assertRaises(ValueError): _ = pyeto.monthly_mean_daylight_hours( pyeto.deg2rad(-90.01)) # Test limits of latitude _ = pyeto.monthly_mean_daylight_hours( pyeto.deg2rad(90.0)) _ = pyeto.monthly_mean_daylight_hours( pyeto.deg2rad(-90.0))
def test_monthly_mean_daylight_hours(self): # Test against values for latitude 20 deg N from Bautista et al (2009) # Calibration of the equations of Hargreaves and Thornthwaite to # estimate the potential evapotranspiration in semi-arid and subhumid # tropical climates for regional applications. Atmosfera 22(4), 331- # 348. test_mmdlh = [ 10.9, # Jan 11.3, # Feb 11.9, # Mar 12.5, # Apr 12.9, # May 13.2, # Jun 13.1, # Jul 12.7, # Aug 12.1, # Sep 11.5, # Oct 11.0, # Nov 10.8, # Dec ] mmdlh = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(20.0)) # Values were only quoted to 1 decimal place so check they are accurate # to within 12 minutes (0.2 hours) for m in range(12): self.assertAlmostEqual(mmdlh[m], test_mmdlh[m], delta=0.15) # Test against values for latitude 46 deg N from Mimikou M. and # Baltas E., Technical hydrology, Second edition, NTUA, 2002. # cited in PAPADOPOULOU E., VARANOU E., BALTAS E., DASSAKLIS A., and # MIMIKOU M. (2003) ESTIMATING POTENTIAL EVAPOTRANSPIRATION AND ITS # SPATIAL DISTRIBUTION IN GREECE USING EMPIRICAL METHODS. test_mmdlh = [ 8.9, # Jan 10.1, # Feb 11.6, # Mar 13.3, # Apr 14.7, # May 15.5, # Jun 15.2, # Jul 13.9, # Aug 12.3, # Sep 10.7, # Oct 9.2, # Nov 8.5, # Dec ] mmdlh = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(46.0)) # Values were only quoted to 1 decimal place so check they are accurate # to within 12 minutes (0.2 hours) for m in range(12): self.assertAlmostEqual(mmdlh[m], test_mmdlh[m], delta=0.15) # Test against values obtained for Los Angles, California, # latitude 34 deg 05' N, from # http://aa.usno.navy.mil/data/docs/Dur_OneYear.php latitude = pyeto.deg2rad(34.0833333) la_mmdlh = [ 10.182, # Jan 10.973, # Feb 11.985, # Mar 13.046, # Apr 13.940, # May 14.388, # Jun 14.163, # Jul 13.404, # Aug 12.374, # Sep 11.320, # Oct 10.401, # Nov 9.928, # Dec ] mmdlh = pyeto.monthly_mean_daylight_hours(latitude) # Check that the 2 methods are almost the same (within 15 minutes) for m in range(12): self.assertAlmostEqual(mmdlh[m], la_mmdlh[m], delta=0.25) # Test with year set to a non-leap year non_leap = pyeto.monthly_mean_daylight_hours(latitude, 2015) for m in range(12): self.assertEqual(mmdlh[m], non_leap[m]) # Test with year set to a leap year leap = pyeto.monthly_mean_daylight_hours(latitude, 2016) for m in range(12): if m == 0: self.assertEqual(leap[m], non_leap[m]) elif m == 1: # Feb # Because Feb extends further into year in a leap year it # should have a slightly longer mean day length in northern # hemisphere self.assertGreater(leap[m], non_leap[m]) else: # All months after Feb in a lieap year will be composed of # diffent Julian days (days of the year) compared to a # non-leap year so will have different mean daylengths. self.assertNotEqual(leap[m], non_leap[m]) # Test with bad latitude with self.assertRaises(ValueError): _ = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(90.01)) with self.assertRaises(ValueError): _ = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(-90.01)) # Test limits of latitude _ = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(90.0)) _ = pyeto.monthly_mean_daylight_hours(pyeto.deg2rad(-90.0))