Commit 3a9e02dc authored by roelo008's avatar roelo008
Browse files

small updates to classes

parent 8154eace
import datetime
import pickle
from utils import doren_classes as dc
doren = dc.Doren(header_src=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\EVA\EVA_Doren_header.csv',
sp_src=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\EVA\EVA_Doren_species.csv')
param_header_src = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\EVA\EVA_Doren_header.csv'
param_sp_src = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\EVA\EVA_Doren_species.csv'
param_aoi_src = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\geodata\eva_aoi_diss_fin_3035.shp'
param_dem_src = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\DEM\DTM_3035.tif'
param_posch = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\POSCH_dep\v2'
cv_soil_dir = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\soil\b_processed'
cv_soil_src = 'WRBLEV1_laea.tif'
cv_cntr_dir = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\geodata\countries'
cv_cntr_src = 'ne_50m_cntrs_sel_buff_diss_3035.shp'
cv_precp_dir = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\EObs\2_compiled'
cv_precp_src = "EObs_v200e_rr_5yrmean"
cv_temp_dir = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\EObs\2_compiled'
cv_temp_src = "EObs_v200e_tg_5yrmean"
sp_req_src = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\z_scratch\soortenlijst.csv'
doren = dc.Doren(header_src= param_header_src, sp_src=param_sp_src)
doren.initiate()
doren.apply_requirements('req1', 'req2', 'req3', 'req4', 'req8', 'req9', 'req10',
aoi_src=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\geodata\eva_aoi_fin_3035.shp',
dem_src=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\DEM\DTM_3035.tif')
doren.add_posch(posch_src_dir=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\POSCH_dep\v2')
doren.add_covar(covar_dir=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\soil\b_processed',
covar_src='WRBLEV1_laea.tif', covar_name='soil_type', nominal=True)
doren.add_covar(covar_dir=r'c:\Users\roelo008\OneDrive - WageningenUR\b_geodata\eur_cntrs_3035',
covar_src='esri_eur_cntrs_3035.shp', covar_name='country', raster=False, column='CNTRYNAME',
keep_all=True)
doren.add_yearly_covar(covar_dir=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\EObs\2_compiled',
covar_src_basename="EObs_v200e_rr_5yrmean", covar_name='5_yearly_precip')
doren.add_yearly_covar(covar_dir=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_brondata\covariables\EObs\2_compiled',
covar_src_basename="EObs_v200e_tg_5yrmean", covar_name='5_yearly_temp')
doren.get_requested_species(src=r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\z_scratch\soortenlijst.csv')
doren.apply_requirements('req1', 'req2', 'req3', 'req4', 'req8', 'req9', 'req10', aoi_src=param_aoi_src,
dem_src=param_dem_src)
doren.add_posch(posch_src_dir=param_posch)
doren.add_covar(covar_dir=cv_soil_dir, covar_src=cv_soil_src, covar_name='soil_type', nominal=True)
doren.add_covar(covar_dir=cv_cntr_dir, covar_src=cv_cntr_src, covar_name='country', raster=False, column='SOVEREIGNT')
doren.add_yearly_covar(covar_dir=cv_precp_dir, covar_src_basename=cv_precp_src, covar_name='5_yearly_precip')
doren.add_yearly_covar(covar_dir=cv_temp_dir, covar_src_basename=cv_temp_src, covar_name='5_yearly_temp')
doren.get_requested_species(src=sp_req_src)
doren.write_stuff('species_list')
doren.write_stuff('typische_soorten')
doren.write_stuff('report')
# doren.write_stuff('headers_shp')
with open(r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\b_compiled_data\a_pkl\doren_{}.pkl'.
format(doren.timestamp), 'wb') as handle:
......
......@@ -16,12 +16,12 @@ batch_nr = int(args.batch_nr)
n_batches = int(args.n_batches)
# Recover the Pickled doren object
pkl_src = './pkl_src/doren_20200611.pkl'
pkl_src = './pkl_src/doren_20200617.pkl'
with open(pkl_src, 'rb') as handle:
doren = pickle.load(handle)
doren.base_out_dir = r'./c_out'
doren.get_requested_species(src='./req_species/soortenlijst.csv')
# doren.get_requested_species(src='./req_species/soortenlijst.csv')
# Determine subset of requested species to proces
sel = np.array_split(np.arange(0, len(doren.req_sp)), n_batches)[batch_nr].tolist()
......
......@@ -145,11 +145,12 @@ class Doren:
self.req_found = self.species.intersection(req_sp_set)
self.req_not_found = req_sp_set.difference(self.species)
msg = 'Read {0} species requested for processing, of which {1} match a species in EVA and {2} do not.'.format(
len(self.req_sp), len(self.req_found), len(self.req_not_found))
self.report += msg
msg1 = '\nRead {0} species requested for processing, of which {1} match a species in EVA and {2} do ' \
'not:\n'.format(len(self.req_sp), len(self.req_found), len(self.req_not_found))
msg2 = '\n'.join(self.req_not_found)
self.report += msg1 + msg2 + '\n'
if self.verbose:
print(msg)
print(msg1, msg2)
def apply_requirements(self, *reqs, **kwargs):
"""
......@@ -305,9 +306,10 @@ class Doren:
# Get the values from a column in a polygon shapefile
column = kwargs.get('column', Exception('Provide column keyword argument'))
covar_gdf = gp.read_file(os.path.join(covar_dir, covar_src))
joined = gp.sjoin(left_df=self.eva, right_df=covar_gdf, how='left', op='within', lsuffix='eva_',
rsuffix='covar_')
cov_df = pd.DataFrame(data={'label': joined.loc[:, column]}, index=self.eva.index)
joined = gp.sjoin(left_df=self.eva, right_df=covar_gdf.loc[:, [column, 'geometry']], how='left',
op='within', lsuffix='eva_', rsuffix='covar_')
assert joined.shape[0] == self.eva.shape[0], ' oh ffs'
cov_df = pd.DataFrame(data={'label': joined.loc[:, column].values}, index=self.eva.index)
# drop NA, rename columns to reflect covariable name
cov_df.dropna(axis=0, how='any', inplace=True)
......@@ -362,11 +364,16 @@ class Doren:
:return: self.positive_plots: plot IDs of all plots containing species_name
self.negative_plots: plot IDs of all plots not containing species_name
"""
if species_name not in self.species:
msg = ' species {0} not found in EVA database\n'.format(species_name)
if self.verbose:
print(msg)
self.report += msg
if species_name in self.species:
msg, proceed = ' succes: {0} present in EVA database\n'.format(species_name), True
else:
msg, proceed = ' fail: {0} absent in EVA database\n'.format(species_name), False
if self.verbose:
print(msg)
self.report += msg
if not proceed:
raise OSError(msg)
self.reset_species_sel()
......@@ -453,17 +460,16 @@ class Doren:
'''Write processing report'''
report_name = '{0}_{1}_processing_report.txt'.format(self.basename, self.timestamp)
self.report += 'Written processing report to file: {0}\n\n'.format(os.path.join(out_dir, report_name))
header = 'Processing report for {0} created {1}'.format(self.basename, self.timestamp)
footer = '\nMade with Python 3.5 using Pandas by Hans Roelofsen, WEnR team B&B'
source = 'See git for source script: https://git.wur.nl/roelo008/doren_2019'
footer = '\nMade with Python 3.5 using Pandas by Hans Roelofsen, WEnR team B&B.'
source = '\nSee git for source script: https://git.wur.nl/roelo008/doren_2019.'
with open(os.path.join(out_dir, report_name), 'w') as f:
f.write(header)
f.write(self.report)
f.write(source)
f.write(footer)
self.report += 'Written processing report to file: {0}\n\n'.format(os.path.join(out_dir,report_name))
f.write(source)
elif what == 'eva_headers':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment