better arguments handling, some fixes

This commit is contained in:
putro 2020-04-16 19:12:57 +02:00
parent 783a8a9057
commit 8f461db1c0

View File

@ -80,6 +80,9 @@ def merge_df(dataframes, sensori):
df = dataframes[sensori[0]] df = dataframes[sensori[0]]
for sensore in sensori[1:]: for sensore in sensori[1:]:
df = pd.merge(df, dataframes[sensore]) df = pd.merge(df, dataframes[sensore])
if len(df) == 0:
print('\nERRORE: dati non disponibili per il sensore nel periodo considerato\n')
sys.exit(-1)
return df return df
@ -90,13 +93,13 @@ def get_dataframes(dati_csv, dati, sensori):
if dati_csv: if dati_csv:
df = process(dati_csv[0], sensore, True) df = process(dati_csv[0], sensore, True)
for d in dati_csv[1:]: for d in dati_csv[1:]:
df = pd.concat([df, process(d, sensore, True)], axis=0) df = pd.concat([df, process(d, sensore, True)], axis=0, ignore_index=True)
df.rename(columns={sensore: sensore + "-csv"}, inplace=True) df.rename(columns={sensore: sensore + "-csv"}, inplace=True)
dataframes[sensore + "-csv"] = df dataframes[sensore + "-csv"] = df
if dati: if dati:
df = process(dati[0], sensore, False) df = process(dati[0], sensore, False)
for d in dati[1:]: for d in dati[1:]:
df = pd.concat([df, process(d, sensore, False)], axis=0) df = pd.concat([df, process(d, sensore, False)], axis=0, ignore_index=True)
dataframes[sensore] = df dataframes[sensore] = df
return dataframes return dataframes
@ -153,7 +156,8 @@ def check_csv(args, filelist, csv_dict):
if csv_dict[y][0] not in filelist: if csv_dict[y][0] not in filelist:
print("file %s for year %s is not available in folder %s" % (csv_dict[y][0], y, path_to_csv_files)) print("file %s for year %s is not available in folder %s" % (csv_dict[y][0], y, path_to_csv_files))
download_csv(csv_dict[y][0], csv_dict[y][1], path_to_csv_files) download_csv(csv_dict[y][0], csv_dict[y][1], path_to_csv_files)
f.append(csv_dict[y][0]) if csv_dict[y][0] not in f:
f.append(csv_dict[y][0])
return f return f
@ -178,6 +182,16 @@ def download_csv(filename, id, path):
pass pass
def check_year_range(arg):
"""check if arg is a year or a year range"""
if not re.search("\\d{4}-\\d{4}", arg):
if not re.search("\\d{4}", arg):
print("\nError: syntax for --csv and --dataset parameter: "
"NNNN single year or NNNN-NNNN for years range\n")
sys.exit(-1)
return True
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--dataset", nargs='+', required=False, parser.add_argument("--dataset", nargs='+', required=False,
@ -193,20 +207,18 @@ def main():
csv_files = list_of_csv_files(path_to_csv_files) csv_files = list_of_csv_files(path_to_csv_files)
dati_csv = [] dati_csv = []
if args.csv: if args.csv:
if not re.search("\\d{4}-\\d{4}", args.csv[0]): check_year_range(args.csv[0])
if not re.search("\\d{4}", args.csv[0]): dati_csv = check_csv(args.csv[0], csv_files, csv_dict)
print("Error: syntax for --csv parameter: year for single year or year1-year2 for years range")
else:
dati_csv = check_csv(args.csv[0], csv_files, csv_dict)
print("daty csv = %s" % dati_csv)
dati = [] dati = []
if args.dataset: if args.dataset:
if "all" in args.dataset: if "all" in args.dataset:
for k in datasets_ambiente.keys(): for k in datasets_ambiente.keys():
dati.append(datasets_ambiente[k]) dati.append(datasets_ambiente[k])
else: else:
for d in args.dataset: check_year_range(args.dataset[0])
dati.append(datasets_ambiente[d]) for d in parse_range(args.dataset[0]): # args.dataset:
if datasets_ambiente[str(d)] not in dati:
dati.append(datasets_ambiente[str(d)])
dataframes = get_dataframes(dati_csv, dati, args.sensori) dataframes = get_dataframes(dati_csv, dati, args.sensori)
datamerged = merge_df(dataframes, list(dataframes.keys())) datamerged = merge_df(dataframes, list(dataframes.keys()))
datamerged.to_csv("export.csv") datamerged.to_csv("export.csv")
@ -217,7 +229,8 @@ def main():
print('Valore medio per il sensore %s %s: %s' % (sensore, location, datamerged[sensore].mean().round(1))) print('Valore medio per il sensore %s %s: %s' % (sensore, location, datamerged[sensore].mean().round(1)))
plot_dataframe(datamerged) plot_dataframe(datamerged)
except KeyError: except KeyError:
print("\nKeyError: forse hai specificato un dataset che non esiste ?") print("\nKeyError: forse hai specificato un dataset che non esiste ?\n"
"i dataset sono disponibili per gli anni %s\n " % list(datasets_ambiente.keys()))
traceback.print_exc() traceback.print_exc()
except KeyboardInterrupt: except KeyboardInterrupt:
print("program terminated by user") print("program terminated by user")