Commit 87a7c8ce authored by Lucia Rinchiuso's avatar Lucia Rinchiuso
Browse files

Refinments, fixed a few issues

parent 8c7f8105
...@@ -19,7 +19,10 @@ stationsconfig = configparser.ConfigParser() ...@@ -19,7 +19,10 @@ stationsconfig = configparser.ConfigParser()
stationsconfig.read("stations.def") stationsconfig.read("stations.def")
GRDCFile=stationsconfig.get("OverAll","GRDCFile",fallback=None) GRDCFile=stationsconfig.get("OverAll","GRDCFile",fallback=None)
StationList=stationsconfig.get("OverAll","StationList",fallback=[]).split(',') StationList=stationsconfig.get("OverAll","StationList",fallback=[]).split(',')
ExcludedList=stationsconfig.get("OverAll","ExcludedList",fallback=[]).split(',') if stationsconfig.has_option("OverAll","ExcludedList"):
ExcludedList=stationsconfig.get("OverAll","ExcludedList",fallback=[]).split(',')
else:
ExcludedList=[]
GraphFile=stationsconfig.get("OverAll","GraphFile",fallback=None) GraphFile=stationsconfig.get("OverAll","GraphFile",fallback=None)
DiagFile=stationsconfig.get("OverAll","DiagFile",fallback="StationDiag.nc") DiagFile=stationsconfig.get("OverAll","DiagFile",fallback="StationDiag.nc")
StructureFile = stationsconfig.get("OverAll","StructuresFile",fallback=None) StructureFile = stationsconfig.get("OverAll","StructuresFile",fallback=None)
...@@ -37,10 +40,11 @@ if len(StationList) < 1 : ...@@ -37,10 +40,11 @@ if len(StationList) < 1 :
sys.exit() sys.exit()
print('Station list:') print('Station list:')
print(StationList) print(StationList)
if len(StationList) < 1 : if len(ExcludedList) < 1 :
print("No excluded stations") print("No excluded stations")
print('Exclusion list:') else:
print(ExcludedList) print('Exclusion list:')
print(ExcludedList)
# #
# Some extra modules so we can start to work # Some extra modules so we can start to work
# #
...@@ -61,27 +65,35 @@ A.fetchnetcdf(GRDCFile, metaonly=True, region=[L.lonrange,L.latrange]) ...@@ -61,27 +65,35 @@ A.fetchnetcdf(GRDCFile, metaonly=True, region=[L.lonrange,L.latrange])
# Excluded stations/regions # Excluded stations/regions
EX=[] EX=[]
for teststn in ExcludedList : for teststn in ExcludedList :
EX.extend(A.FindID(teststn)) sublist = A.FindID(teststn)
print('Excluded '+str(A.FindID(teststn))) print("With ", teststn," exclude stations: ",str(sublist))
if EX[-1] < 0 : EX.extend(sublist)
if not sublist :
print("Excluded station selection ", teststn," was already not present in the GRDC file over your regions.") print("Excluded station selection ", teststn," was already not present in the GRDC file over your regions.")
EX=list(set(EX)) #avoid duplicates
#Selected stations/regions #Selected stations/regions
IDs=[] IDs=[]
initial_calls=[] initial_calls=[]
for teststn in StationList : for teststn in StationList :
IDs.extend(A.FindID(teststn)) sublist = A.FindID(teststn)
for i in A.FindID(teststn): print("With ", teststn," select stations: ",str(sublist))
for i in sublist: #avoid duplicates and save the initial call(s)
if i in IDs:
sublist.remove(i)
index=IDs.index(i)
initial_calls[index]=initial_calls[index]+', '+teststn
continue
initial_calls.append(teststn) initial_calls.append(teststn)
print('Selected '+str(A.FindID(teststn))) IDs.extend(sublist)
if IDs[-1] < 0 : if not sublist :
print("Station selection ", teststn," could not be found in the GRDC file over your regions.") print("Station selection ", teststn," could not be found in the GRDC file over your regions.")
if len(IDs)!=len(initial_calls): print('WARNING') if len(IDs)!=len(initial_calls): print('WARNING')
#Subtraction of ALL recurrences of the excluded stations #Subtraction of ALL recurrences of the excluded stations
for i in set(EX): for i in EX:
for index, elem in enumerate(IDs): if i in IDs:
if elem==i: index=IDs.index(i)
del initial_calls[index] del initial_calls[index]
del IDs[index] del IDs[index]
# Get additional information from the GRDC file. # Get additional information from the GRDC file.
Rivers=[] Rivers=[]
Upstream=[] Upstream=[]
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import numpy as np import numpy as np
import datetime import datetime
import sys import sys
import re
from netCDF4 import Dataset from netCDF4 import Dataset
from netCDF4 import stringtochar, chartostring, stringtoarr from netCDF4 import stringtochar, chartostring, stringtoarr
# #
...@@ -49,6 +50,11 @@ class AllStations : ...@@ -49,6 +50,11 @@ class AllStations :
return False return False
else: else:
return float(n).is_integer() return float(n).is_integer()
def findWholeWord(self,w,s):
if re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search(s):
return True
else:
return False
def FindIDfromName(self, name) : def FindIDfromName(self, name) :
ID = -1 ID = -1
for i,stn in enumerate(self.stations) : for i,stn in enumerate(self.stations) :
...@@ -57,7 +63,7 @@ class AllStations : ...@@ -57,7 +63,7 @@ class AllStations :
else : else :
stnname=stn.Name.tolist() stnname=stn.Name.tolist()
# #
if stnname.lower().find(name.lower()) >= 0 : if self.findWholeWord(name.strip(),stnname) :
ID = stn.Number ID = stn.Number
return ID return ID
def FindIDfromCountry(self, river, country) : def FindIDfromCountry(self, river, country) :
...@@ -74,7 +80,7 @@ class AllStations : ...@@ -74,7 +80,7 @@ class AllStations :
stnriver=i.River.tolist() stnriver=i.River.tolist()
else: else:
stnriver='all' stnriver='all'
if stncountry==country and stnriver==river: if self.findWholeWord(country.strip(),stncountry) and self.findWholeWord(river.strip(),stnriver):
ID.append(i.Number) ID.append(i.Number)
return ID return ID
def FindIDfromRiver(self, river) : def FindIDfromRiver(self, river) :
...@@ -84,7 +90,7 @@ class AllStations : ...@@ -84,7 +90,7 @@ class AllStations :
stnriver=i.River stnriver=i.River
else: else:
stnriver=i.River.tolist() stnriver=i.River.tolist()
if stnriver.lower()==river.lower(): if self.findWholeWord(river.strip(),stnriver):
ID.append(i.Number) ID.append(i.Number)
return ID return ID
def FindIDfromWMOregion(self, reg) : def FindIDfromWMOregion(self, reg) :
......
Markdown is supported
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