From 0d88e1f2cd6cf4da98aee6571dd8eed45f7c0627 Mon Sep 17 00:00:00 2001 From: Jaap Schellekens Date: Wed, 6 Apr 2016 07:13:04 +0200 Subject: [PATCH] netcdf bug --- wflow-py/wflow/wf_netcdfio.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/wflow-py/wflow/wf_netcdfio.py b/wflow-py/wflow/wf_netcdfio.py index 960dc702..f6237fc8 100644 --- a/wflow-py/wflow/wf_netcdfio.py +++ b/wflow-py/wflow/wf_netcdfio.py @@ -441,11 +441,26 @@ def __init__(self, netcdffile, logging, vars=[]): a = pcr2numpy(cover(0.0), 0.0).flatten() # Determine steps to load in mem based on estimated memory usage floatspermb = 1048576 / 4 - maxmb = 4000 - self.maxsteps = maxmb * len(a) / floatspermb + 1 + maxmb = 40 + + maxlentime = len(self.dataset.variables['time']) + self.maxsteps = minimum(maxmb * len(a) / floatspermb + 1,maxlentime - 1) self.fstep = 0 self.lstep = self.fstep + self.maxsteps + + # Now check Y values to see if we must flip the data + try: + self.y = self.dataset.variables['lat'][:] + except: + self.y = self.dataset.variables['y'][:] + + if self.y[0] > self.y[-1]: + self.flip = False + else: + self.flip = True + + for var in vars: try: self.alldat[var] = self.dataset.variables[var][self.fstep:self.maxsteps] @@ -453,6 +468,8 @@ def __init__(self, netcdffile, logging, vars=[]): self.alldat.pop(var, None) logging.warn("Variable " + var + " not found in netcdf file: " + netcdffile) + + def gettimestep(self, timestep, logging, var='P', shifttime=False): """ Gets a map for a single timestep. reads data in blocks assuming sequential access @@ -476,8 +493,12 @@ def gettimestep(self, timestep, logging, var='P', shifttime=False): self.fstep = ncindex self.lstep = ncindex + self.maxsteps np_step = self.alldat[var][ncindex - self.fstep, :, :] + miss = float(self.dataset.variables[var]._FillValue) - return numpy2pcr(Scalar, np_step, miss), True + if self.flip: + return numpy2pcr(Scalar, flipud(np_step).copy(), miss), True + else: + return numpy2pcr(Scalar, np_step, miss), True else: #logging.debug("Var (" + var + ") not found returning 0") return cover(scalar(0.0)), False @@ -505,7 +526,7 @@ def __init__(self, netcdffile, logging, vars=[]): a = pcr2numpy(cover(0.0), 0.0).flatten() # Determine steps to load in mem based on estimated memory usage floatspermb = 1048576 / 4 - maxmb = 4000 + maxmb = 40 self.maxsteps = maxmb * len(a) / floatspermb + 1 self.fstep = 0 self.lstep = self.fstep + self.maxsteps