Commit b43c409d authored by Lucia Rinchiuso's avatar Lucia Rinchiuso
Browse files

Selection by ID, country and river

parent dad41b15
...@@ -3,12 +3,24 @@ import numpy as np ...@@ -3,12 +3,24 @@ import numpy as np
import datetime import datetime
import sys import sys
import os import os
from inspect import currentframe, getframeinfo from inspect import currentframe, getframeinfo, getabsfile
from netCDF4 import Dataset from netCDF4 import Dataset
from netCDF4 import stringtochar, chartostring, stringtoarr from netCDF4 import stringtochar, chartostring, stringtoarr
# #
localdir=os.path.dirname(getframeinfo(currentframe()).filename) def localdir(follow_symlinks=True):
if getattr(sys, 'frozen', False): # py2exe, PyInstaller, cx_Freeze
path = os.path.abspath(sys.executable)
else:
path = getabsfile(localdir)
if follow_symlinks:
path = os.path.realpath(path)
return os.path.dirname(path)
localdir=localdir()
#localdir=os.path.dirname(getframeinfo(currentframe()).filename)
#localdir=os.path.dirname(os.path.realpath(__file__))
sys.path.append(localdir+'/Stations') sys.path.append(localdir+'/Stations')
sys.path.append(localdir+'/F90subroutines')
FillValue = 1.e+20 FillValue = 1.e+20
# #
# Gather user input. # Gather user input.
...@@ -53,20 +65,41 @@ A.fetchnetcdf(GRDCFile, metaonly=True, region=[L.lonrange,L.latrange]) ...@@ -53,20 +65,41 @@ A.fetchnetcdf(GRDCFile, metaonly=True, region=[L.lonrange,L.latrange])
# Find the IDs of all the stations listed in stations.def # Find the IDs of all the stations listed in stations.def
# #
IDs=[] IDs=[]
initial_calls=[]
#create a FindID function that calls other functions depending on the input
for teststn in StationList : for teststn in StationList :
IDs.append(A.FindID(teststn.strip())) #next step: when there is a @ in the string look at what is before
#if there is a name!=all it is a river, if it's a nuber it's a region or subregion
#this info must go in input to the ID finder
#next next step: add an exclusion list that uses the same function as below but creates a list of ID_excluded to subtract from the IDs list
#for the moment just add:
#elif there is a @ in the string:
#if after @ there is an integer
#if <10 -> region -> FindIDfromWMOreg and extend
#else -> subregion -> FindIDfromWMOsubreg and extend
#elif after @ is a string
#if 2 char and isupper true -> country -> FindIDfromCountry and extend
#else -> river -> FindIDfromRiver and extend
IDs.extend(A.FindID(teststn))
for i in A.FindID(teststn):
initial_calls.append(teststn)
print(A.FindID(teststn))
if IDs[-1] < 0 : if IDs[-1] < 0 :
print("Station ", teststn," could not be found in the GRDC file over your regions.") print("Station ", teststn," could not be found in the GRDC file over your regions.")
if len(IDs)!=len(initial_calls): print('WARNING')
# Get additional information from the GRDC file. # Get additional information from the GRDC file.
Rivers=[] Rivers=[]
Upstream=[] Upstream=[]
Names = []
for istn in IDs : for istn in IDs :
if istn >= 0 : if istn >= 0 :
Rivers.append(A.RiverfromID(istn)) Rivers.append(A.RiverfromID(istn))
Upstream.append(A.UpstreamfromID(istn)) Upstream.append(A.UpstreamfromID(istn))
Names.append(A.NamefromID(istn))
else : else :
Rivers.append("-") Rivers.append("-")
Upstream.append(0) Upstream.append(0)
Names.append("_")
# #
# Find the location within the routing graph of the stations to be diagnosed. # Find the location within the routing graph of the stations to be diagnosed.
# #
...@@ -76,7 +109,7 @@ for i in IDs : ...@@ -76,7 +109,7 @@ for i in IDs :
if i in L.ids : if i in L.ids :
Lind.append(L.ids.index(i)) Lind.append(L.ids.index(i))
else : else :
print("Station ", StationList[IDs.index(i)], " (ID =",i,") has not been located in the routing graph.") print("Station ", initial_calls[IDs.index(i)], " (ID =",i,") has not been located in the routing graph.")
Lind.append(-1) Lind.append(-1)
else : else :
Lind.append(-1) Lind.append(-1)
...@@ -103,9 +136,9 @@ ncvar=[] ...@@ -103,9 +136,9 @@ ncvar=[]
for i,loc in enumerate(Lind) : for i,loc in enumerate(Lind) :
if loc >= 0 : if loc >= 0 :
vname="ST"+str(IDs[i]) vname="ST"+str(IDs[i])
print("Creating Variable : ", vname, StationList[i]) print("Creating Variable : ", vname, initial_calls[i])
ncvar.append(nc.createVariable(vname, float, ('y','x'))) ncvar.append(nc.createVariable(vname, float, ('y','x')))
ncvar[-1].Name=StationList[i] ncvar[-1].Name=Names[i]
ncvar[-1].River=Rivers[i] ncvar[-1].River=Rivers[i]
ncvar[-1].ObsUpstream=Upstream[i] ncvar[-1].ObsUpstream=Upstream[i]
ncvar[-1].Fiindex=L.Fiindex[loc] ncvar[-1].Fiindex=L.Fiindex[loc]
......
...@@ -42,7 +42,14 @@ class AllStations : ...@@ -42,7 +42,14 @@ class AllStations :
# #
# Inquiry function # Inquiry function
# #
def FindID(self, name) : def is_integer(self,n):
try:
float(n)
except ValueError:
return False
else:
return float(n).is_integer()
def FindIDfromName(self, name) :
ID = -1 ID = -1
for i,stn in enumerate(self.stations) : for i,stn in enumerate(self.stations) :
if isinstance(stn.Name, str) : if isinstance(stn.Name, str) :
...@@ -53,6 +60,45 @@ class AllStations : ...@@ -53,6 +60,45 @@ class AllStations :
if stnname.lower().find(name.lower()) >= 0 : if stnname.lower().find(name.lower()) >= 0 :
ID = stn.Number ID = stn.Number
return ID return ID
def FindIDfromCountry(self, country) :
ID = []
for i in self.stations:
if isinstance(i.Country,str):
stncountry=i.Country
else:
stncountry=i.Country.tolist()
if stncountry==country:
ID.append(i.Number)
return ID
def FindIDfromRiver(self, river) :
ID = []
for i in self.stations:
if isinstance(i.River,str):
stnriver=i.River
else:
stnriver=i.River.tolist()
if stnriver.lower()==river.lower():
ID.append(i.Number)
return ID
def FindID(self, teststn) :
IDs = []
if self.is_integer(teststn):
IDs.append(int(teststn))
elif isinstance(teststn,str):
if teststn.find('@')!=-1:
selection=teststn.split('@')[0]
place=teststn.split('@')[1]
if self.is_integer(place):
print('it is a region, still to be implemented')
else:
if len(place)==2 and place.isupper():
IDs.extend(self.FindIDfromCountry(place.strip()))
else:
IDs.extend(self.FindIDfromRiver(place.strip()))
else:
IDs.append(self.FindIDfromName(teststn.strip()))
return IDs
def RiverfromID(self, stnid) : def RiverfromID(self, stnid) :
allids=[i.Number for i in self.stations] allids=[i.Number for i in self.stations]
if isinstance(self.stations[allids.index(stnid)].River, str) : if isinstance(self.stations[allids.index(stnid)].River, str) :
...@@ -63,6 +109,13 @@ class AllStations : ...@@ -63,6 +109,13 @@ class AllStations :
def UpstreamfromID(self, stnid) : def UpstreamfromID(self, stnid) :
allids=[i.Number for i in self.stations] allids=[i.Number for i in self.stations]
return self.stations[allids.index(stnid)].Area return self.stations[allids.index(stnid)].Area
def NamefromID(self, stnid) :
allids=[i.Number for i in self.stations]
if isinstance(self.stations[allids.index(stnid)].Name, str) :
name = self.stations[allids.index(stnid)].Name
else :
name = self.stations[allids.index(stnid)].Name.tolist()
return name
# #
# Function to dump all stations into a NetCDF file # Function to dump all stations into a NetCDF file
# #
......
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