Commit 2ba941b6 authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

improved error handling when reading files

parent ae389441
......@@ -34,6 +34,8 @@ def plant_dk(ffargs):
# Gather default data on plants
plant_dir = r'W:\PROJECTS\MNP2020\MNP_2020\c_fases\f7_draagkracht\a_source_data\plant'
if not os.path.isdir(plant_dir):
raise AssertionError('Please connect to W:\\projects\MNP2020')
# Vertaling associatie <-> beheertype. Add generalized syntaxon code and repair beheertypen ("01.02" -> "N01.02.00")
plant_associaties_src = 'vertaling_IPO_ASSO_2009_ps.xls' # Vertaling Associatie --> Beheertype
......@@ -48,19 +50,24 @@ def plant_dk(ffargs):
# Get master list of beheertypen
bt_master_src = r'c:\apps\proj_code\benb_utils\resources\snl_beheertypen.csv'
bt_master = pd.read_csv(bt_master_src, sep=',', comment='#', quotechar='"')
try:
bt_master = pd.read_csv(bt_master_src, sep=',', comment='#', quotechar='"')
except FileNotFoundError:
raise AssertionError("please instal BenB repository and update local path to \\resources\snl_beheertypen.csv")
btcode2omschrijving = dict(zip(bt_master.LAND_TYPE_CODE.apply(fix_bt, as_mnp=True), bt_master.LST_NAME))
# Get default mnp draagkracht values
mnp_dk_src = r'w:\PROJECTS\qmar\MNP-SNL-ParameterSet\Parameters_v06_2019_12_09\03_MNP_versie6_par_density_factors_BT2019_v2.csv'
mnp_dk = pd.read_csv(mnp_dk_src, sep=',', usecols=['Species_code', 'Land_type_code', 'Land_type_quality'])
try:
mnp_dk = pd.read_csv(mnp_dk_src, sep=',', usecols=['Species_code', 'Land_type_code', 'Land_type_quality'])
except FileNotFoundError:
raise AssertionError('Please connect to w:\\projects\qmar')
# Read list of equested beheertypen to analyse, add mnp-style code
try:
bts = pd.read_csv(args.bt_src, sep=',', usecols=['Land_type_code'])
except ValueError as e:
print('Error: beheertypen list must contain column "Land_type_code"')
raise
except (ValueError, FileNotFoundError) as e:
raise AssertionError('cannot read beheertypen source: {}. Check for column Land_type_code!'.format(e))
bts['bt_code'] = bts.Land_type_code.apply(fix_bt, as_mnp=True) # Add MNP-style Beheertype codes
bts.drop_duplicates(subset='bt_code', inplace=True)
......@@ -72,10 +79,11 @@ def plant_dk(ffargs):
# Read list of equested species, add syntax group and assert all plants
try:
species = pd.read_csv(args.sp_src, sep=',', usecols=['Species_code', 'Local_name', 'Scientific_name'])
except ValueError:
print('Error: species list must contain columns "Species_code" (S09xxxxxx) "Local_name" "Scientific_name"')
except (ValueError, FileNotFoundError) as e:
raise AssertionError('Cannot find species list, or species list is missing "Species_code" (S09xxxxxx) '
'"Local_name" "Scientific_name" columns: {}'.format(e))
species.drop_duplicates(subset='Species_code', inplace=True)
assert all(species.Species_code.apply(code2tax) == 'P'), "Error: non plants found in requested species list."
assert all(species.Species_code.apply(code2tax) == 'P'), "non plants found in requested species list."
# species dicts
code2scientific = dict(zip(species.Species_code, species.Scientific_name))
......@@ -88,8 +96,8 @@ def plant_dk(ffargs):
print('Doing {0}-{1}'.format(sp_code, bt))
# Dictionary to hold results
d = dict.fromkeys(['Scientific_name', 'Land_type_code', 'mean_trouw', 'mean_freq', 'hits', 'hits_gte_th', 'dk_ww'],
np.nan)
d = dict.fromkeys(['Scientific_name', 'Land_type_code', 'mean_trouw', 'mean_freq', 'hits', 'hits_gte_th',
'dk_ww'], np.nan)
# Get initial selection from plant response data
d['hits'] = responses.loc[(responses.Soortnaam == code2scientific[sp_code]) &
......@@ -107,10 +115,13 @@ def plant_dk(ffargs):
d['dk_ww'] = dk_ww(frequentie=d['mean_freq'], trouw=d['mean_trouw'], th=args.th_dk, tuning=args.A)
d['Species_code'] = sp_code
d['Scientific_name'] = code2scientific[sp_code]
d['Local_name'] = code2local[sp_code]
d['Land_type_code'] = bt
d['Land_type_omschrijving'] = btcode2omschrijving[bt]
try:
d['Scientific_name'] = code2scientific[sp_code]
d['Local_name'] = code2local[sp_code]
d['Land_type_omschrijving'] = btcode2omschrijving[bt]
except KeyError as e:
raise AssertionError("{} is not recognized".format( e))
# Wrap up results and append to list
dk_lst.append(pd.DataFrame(d, index=pd.RangeIndex(1)))
......@@ -197,10 +208,12 @@ def plant_dk(ffargs):
dkww_df.to_csv(index=False, sep='\t', line_terminator='\n'),
pd.pivot_table(dk.assign(Soort=dk.Scientific_name.str.cat(dk.Local_name, sep="_"),
BT=dk.Land_type_code.str.cat(dk.Land_type_omschrijving, sep='_')),
index='Soort', columns='BT', values='dk_ww', aggfunc='mean')\
index='Soort', columns='BT', values='dk_ww', aggfunc='mean')
.to_csv(sep=',', line_terminator='\n'))
# Prepare output
basename = '03PlantDensityFactors{0}'.format(ts_full)
assert os.path.isdir(args.out_dir), "{} is not a valid output directory".format(args.out_dir)
# Write report
with open(os.path.join(args.out_dir, '{}.txt'.format(basename)), 'w') as f:
......@@ -208,11 +221,11 @@ def plant_dk(ffargs):
# MNP style CSV output
dk.dropna(axis=0, subset=['dk_ww'])\
.rename(mapper={'dk_ww': 'Land_type_quality'}, axis=1)\
.loc[:, ['Species_code', 'Land_type_code', 'Land_type_quality', 'Local_name', 'Scientific_name',
'Land_type_omschrijving']]\
.round({'Land_type_quality': 2})\
.to_csv(os.path.join(args.out_dir, '{}.csv'.format(basename)), sep=',', header=True, index=False)
.rename(mapper={'dk_ww': 'Land_type_quality'}, axis=1)\
.loc[:, ['Species_code', 'Land_type_code', 'Land_type_quality', 'Local_name', 'Scientific_name',
'Land_type_omschrijving']]\
.round({'Land_type_quality': 2})\
.to_csv(os.path.join(args.out_dir, '{}.csv'.format(basename)), sep=',', header=True, index=False)
print('...done')
......@@ -232,7 +245,7 @@ if __name__ == '__main__':
try:
plant_dk(fargs)
except AssertionError as e:
print(e)
print('Error: {}'.format(e))
sys.exit(1)
def full(fargs):
......@@ -240,7 +253,7 @@ if __name__ == '__main__':
try:
plant_dk(fargs)
except AssertionError as e:
print(e)
print('Error: {}'.format(e))
sys.exit(1)
model_desc = \
......
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