Commit 0e973277 authored by POLCHER Jan's avatar POLCHER Jan 🚴🏾
Browse files

Adds location names and types to the graph file.

parent 6b0de4bc
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# #
import numpy as np import numpy as np
from netCDF4 import Dataset from netCDF4 import Dataset
from netCDF4 import stringtochar, chartostring, stringtoarr
import sys import sys
import codecs import codecs
# #
...@@ -77,8 +78,8 @@ class Locations : ...@@ -77,8 +78,8 @@ class Locations :
self.lat.append(g.variables["lat"][i]) self.lat.append(g.variables["lat"][i])
self.upstream.append(g.variables["area"][i]) self.upstream.append(g.variables["area"][i])
self.ltype.append("gauging station") self.ltype.append("gauging station")
# Proper information to be added later. self.lname.append(str(chartostring(g.variables["name"][i,:])))
self.lname.append("GRDCName") #
g.close() g.close()
return nb return nb
# #
...@@ -99,6 +100,7 @@ class Locations : ...@@ -99,6 +100,7 @@ class Locations :
self.lat = np.array(self.lat)[l] self.lat = np.array(self.lat)[l]
self.upstream = np.array(self.upstream)[l] self.upstream = np.array(self.upstream)[l]
self.ltype = np.array(self.ltype)[l] self.ltype = np.array(self.ltype)[l]
self.lname = np.array(self.lname)[l]
self.ij = np.array(self.ij)[l] self.ij = np.array(self.ij)[l]
self.ib = np.array(self.ib)[l] self.ib = np.array(self.ib)[l]
self.cost = np.array(self.cost)[l] self.cost = np.array(self.cost)[l]
...@@ -172,14 +174,15 @@ class Locations : ...@@ -172,14 +174,15 @@ class Locations :
gl = part.gather(procgrid.landscatter(locmap, order='F')) gl = part.gather(procgrid.landscatter(locmap, order='F'))
gc = part.gather(procgrid.landscatter(costmap, order='F')) gc = part.gather(procgrid.landscatter(costmap, order='F'))
# #
htumon = np.zeros((self.htumonmax,part.njg,part.nig), dtype=int)
htumon[:,:,:] = -1
monnb = np.zeros((part.njg,part.nig), dtype=int)
locationlist = np.zeros((5,self.nblocated), dtype=int)
#
# Eliminate locations with lower cost # Eliminate locations with lower cost
# #
if part.rank == 0: if part.rank == 0:
#
htumon = np.zeros((self.htumonmax,part.njg,part.nig), dtype=int)
htumon[:,:,:] = -1
monnb = np.zeros((part.njg,part.nig), dtype=int)
locationlist = np.zeros((5,self.nblocated), dtype=int)
#
u,n = np.unique(gl[~np.isnan(gl)], return_counts=True) u,n = np.unique(gl[~np.isnan(gl)], return_counts=True)
# Go through all IDs which are not unique # Go through all IDs which are not unique
for lid in u[np.where(n > 1)[0]] : for lid in u[np.where(n > 1)[0]] :
...@@ -206,27 +209,48 @@ class Locations : ...@@ -206,27 +209,48 @@ class Locations :
locationlist[2,i] = f[2][0]+1 locationlist[2,i] = f[2][0]+1
locationlist[3,i] = f[1][0]+1 locationlist[3,i] = f[1][0]+1
locationlist[4,i] = imon+1 locationlist[4,i] = imon+1
else :
htumon = None
monnb = None
locationlist = np.array([[0],[0]])
# #
# Add to NetCDF file # Add to NetCDF file
# #
gtypes = part.gathermergelist(list(self.lid), list(self.ltype), list(locationlist[0,:]))
gnames = part.gathermergelist(list(self.lid), list(self.lname), list(locationlist[0,:]))
if part.rank == 0:
for t in gnames :
print("XXXXX got type : ", t)
if part.rank == 0: if part.rank == 0:
# Create new dimension # Create new dimension
monmax = np.max(monnb) monmax = np.max(monnb)
strlen=60
if monmax > 0 : if monmax > 0 :
moncoord = ('htumon',) monaxe = ('htumon',)
outnf.createDimension(moncoord[0], monmax) outnf.createDimension(monaxe[0], monmax)
infocoord = ('locinfo',) infoaxe = ('locinfo',)
outnf.createDimension(infocoord[0], 5) outnf.createDimension(infoaxe[0], 5)
outnf.createDimension('str', strlen)
# Create variable for the list of stations # Create variable for the list of stations
locvar = outnf.createVariable(locname, vtyp, infocoord+loccoord) locvar = outnf.createVariable(locname, vtyp, infoaxe+loccoord)
locvar.title = loctitle locvar.long_name = loctitle
locvar.units = locunits locvar.units = locunits
locvar.description = "ID, i-index, j-index, HTU-index, monitoring-index" locvar.description = "ID, i-index, j-index, HTU-index, monitoring-index"
locvar[:,:] = locationlist locvar[:,:] = locationlist
# Create variable for the indices of the HTU to monitor # Create variable for the indices of the HTU to monitor
monvar = outnf.createVariable(monname, vtyp, moncoord+spacecoord) monvar = outnf.createVariable(monname, vtyp, monaxe+spacecoord)
monvar.title = montitle monvar.long_name = montitle
monvar.units= monunits monvar.units= monunits
monvar[:,:,:] = htumon[0:monmax,:,:] monvar[:,:,:] = htumon[0:monmax,:,:]
# Variable for type of station
loctype = outnf.createVariable("LocationType", 'c', loccoord+('str',))
loctype.long_name = "Type of location"
for it,t in enumerate(gtypes) :
loctype[it,:]=stringtoarr(t, strlen)
# Variable for type of station names
locnames = outnf.createVariable("LocationName", 'c', loccoord+('str',))
locnames.long_name = "Name of location"
for it,t in enumerate(gnames) :
locnames[it,:]=stringtoarr(t, strlen)
return return
# #
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