Skip to content
Snippets Groups Projects
Commit 6499858e authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

Merge branch 'csv_tools' into 'master'

csv tools in dedicated file

See merge request !8
parents 11c9db32 019d30d0
No related branches found
No related tags found
1 merge request!8csv tools in dedicated file
......@@ -27,68 +27,6 @@ def rand_web_color_hex():
return "#{}".format(rgb)
def fix_bt(code_in, as_mnp=False):
"""
Parser for Beheertype codes to repair abbreviated codes and/or MNP extension
eg. 02.01 --> N02.01 if as_mnp==False
02.01 --> N01.01.00 if as_mnp==True
N05.01 --> N05.01 if as_mnp==False
N05.01 --> N05.01.00 if as_mnp==True
N04.02.01 --> N04.02.01 if as_mnp==False
N04.02.01 --> N04.02.01 if as_mnp==True
BT Code is as follows <N><AA>.<BB>.<CC>
N = letter, may be missing
A = two digits indicating top level beheertype "TOP"
B = two digits indicating sub level beheertype "SUB"
C = two digits indicating neergeschaald beheertype if > 00. Else "00" as used in MNP param notation "NEER"
N04.02.00 is equivalent to N04.02
:param code_in: beheertypecode in any format
:param as_mnp: return MNP style beheertypecode, default=False
:return: beheertypecode
"""
if isinstance(code_in, numbers.Number):
code_in = str(code_in)
# Identify parts of code
parts = code_in.split(".")
if len(parts) == 1:
raise Exception("Unexpected BT code: {}".format(code_in))
elif len(parts) == 2:
top, sub = parts
neer = None
elif len(parts) == 3:
top, sub, neer = parts
# Verify TOP and add "N" is required
if top.isdigit():
top = "N{0}".format(top.zfill(2))
else:
assert top[0].isupper(), "Unexpected BT Code format: {}".format(code_in)
# Verify SUB
assert sub.isdigit and len(sub) == 2, "Unexpected BT Code format: {}".format(
code_in
)
# Verify NEER and check if it is neergeschaald or not
keep_neer = False
if neer:
assert neer.isdigit and len(neer) == 2, "Unexpected BT Code format: {}".format(
code_in
)
if int(neer) > 0:
keep_neer = True
# Construct output
head = "{}.{}".format(top, sub)
if keep_neer:
return "{}.{}".format(head, neer)
elif as_mnp:
return "{}.00".format(head)
else:
return head
def pnt2square_id(east_north, size, of="full"):
"""
Used to slot a point into a geographical square based on its coordinates.
......@@ -129,21 +67,6 @@ def pnt2square_id(east_north, size, of="full"):
raise
def draagkracht_ww(frequentie, trouw, th):
"""
Draagkracht formule volgens Wieger Wamelink. Zie MS Teams discussie 16-07-2020 MNP2020/Fase 7 Objectivering Draagk..
:param frequentie: gemiddelde frequentie van soort X binnen beheertype Y
:param trouw: gemiddelde trouw van soort X binnen beheertype Y
:param th: minumum draagkracht value
:return: draagkracht soort X - beheertype Y combinatie, gemaximaliseerd tot 1.
"""
dk = np.min([np.multiply(np.divide(np.max([frequentie, trouw]), 100), 5), 1])
if dk >= th:
return dk
else:
return np.nan
def gen_squares(x_ul, y_ul, nrow, ncol, size):
"""
Generate geodataframe with square polygons
......@@ -225,64 +148,6 @@ def colrow2xy(colrow, rio_object=None, affine_in=None):
return x, y
def csv_combine(files, identifiers, identifiercolumn="df_id"):
"""
appends one table to another while adding a column to identify which entries came from each table
:param files: list of csv files
:param identifiers: list of labels to identify original tables
:param: identifiercolumn: name of identifiercolumn
:return
"""
if not isinstance(files, list) or not isinstance(identifiers, list):
raise Exception("Either files or identifiers is not a list")
for file in files:
if not os.path.exists(file):
raise Exception(file + " was not found!")
df_combined = pd.read_csv(files[0])
df_combined[identifiercolumn] = identifiers[0]
for file, identifier in zip(files[1:], identifiers[1:]):
nextfile = pd.read_csv(file)
nextfile[identifiercolumn] = identifier
df_combined = df_combined.append(nextfile)
return df_combined
def mnp_csvlister(folder, notwanted=False, extension=False):
"""
list all species output csv files in an MNP result folder
you can provida a list of species that you want to exclude from te returned list
:param folder: MNP result folder
:param notwanted: list of species to exclude
:param extension: return file extensions?
:return: list containing filenames or species codes
"""
if type(notwanted) != bool:
try:
file_list = [
file
for file in os.listdir(folder)
if not (np.isin(file[0:9], notwanted))
]
file_list.sort()
except Exception as e:
print(e)
next
else:
file_list = [file for file in os.listdir(folder)]
file_list.sort()
if not extension:
file_list = [f[0:9] for f in file_list]
file_list = list(set([s for s in file_list if s[:2] == "S0"]))
file_list.sort()
return file_list
def progress_bar(iterable, prefix='', suffix='', decimals=1, length=100, fill='#', printEnd="\r"):
"""
......
import os
import pandas as pd
import numpy as np
def csv_combine(files, identifiers, identifiercolumn="df_id"):
"""
appends one table to another while adding a column to identify which entries came from each table
:param files: list of csv files
:param identifiers: list of labels to identify original tables
:param: identifiercolumn: name of identifiercolumn
:return
"""
if not isinstance(files, list) or not isinstance(identifiers, list):
raise Exception("Either files or identifiers is not a list")
for file in files:
if not os.path.exists(file):
raise Exception(file + " was not found!")
df_combined = pd.read_csv(files[0])
df_combined[identifiercolumn] = identifiers[0]
for file, identifier in zip(files[1:], identifiers[1:]):
nextfile = pd.read_csv(file)
nextfile[identifiercolumn] = identifier
df_combined = df_combined.append(nextfile)
return df_combined
def mnp_csvlister(folder, notwanted=False, extension=False):
"""
list all species output csv files in an MNP result folder
you can provida a list of species that you want to exclude from te returned list
:param folder: MNP result folder
:param notwanted: list of species to exclude
:param extension: return file extensions?
:return: list containing filenames or species codes
"""
if type(notwanted) != bool:
try:
file_list = [
file
for file in os.listdir(folder)
if not (np.isin(file[0:9], notwanted))
]
file_list.sort()
except Exception as e:
print(e)
next
else:
file_list = [file for file in os.listdir(folder)]
file_list.sort()
if not extension:
file_list = [f[0:9] for f in file_list]
file_list = list(set([s for s in file_list if s[:2] == "S0"]))
file_list.sort()
return file_list
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment