Browse Source

better arguments handling, some fixes

master
putro 2 years ago
parent
commit
8f461db1c0
  1. 37
      openlamb.py

37
openlamb.py

@ -80,6 +80,9 @@ def merge_df(dataframes, sensori):
df = dataframes[sensori[0]]
for sensore in sensori[1:]:
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
@ -90,13 +93,13 @@ def get_dataframes(dati_csv, dati, sensori):
if dati_csv:
df = process(dati_csv[0], sensore, True)
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)
dataframes[sensore + "-csv"] = df
if dati:
df = process(dati[0], sensore, False)
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
return dataframes
@ -153,7 +156,8 @@ def check_csv(args, filelist, csv_dict):
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))
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
@ -178,6 +182,16 @@ def download_csv(filename, id, path):
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():
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", nargs='+', required=False,
@ -193,20 +207,18 @@ def main():
csv_files = list_of_csv_files(path_to_csv_files)
dati_csv = []
if args.csv:
if not re.search("\\d{4}-\\d{4}", args.csv[0]):
if not re.search("\\d{4}", args.csv[0]):
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)
check_year_range(args.csv[0])
dati_csv = check_csv(args.csv[0], csv_files, csv_dict)
dati = []
if args.dataset:
if "all" in args.dataset:
for k in datasets_ambiente.keys():
dati.append(datasets_ambiente[k])
else:
for d in args.dataset:
dati.append(datasets_ambiente[d])
check_year_range(args.dataset[0])
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)
datamerged = merge_df(dataframes, list(dataframes.keys()))
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)))
plot_dataframe(datamerged)
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()
except KeyboardInterrupt:
print("program terminated by user")

Loading…
Cancel
Save