Index: wflow-py/wflow/wf_netcdfio.py =================================================================== diff -u -r55ff1ea7e88351a1e76a3ad60ce5e9a4f9e169a9 -rb0c9e960073cc20f9940d562bb83cea15206b118 --- wflow-py/wflow/wf_netcdfio.py (.../wf_netcdfio.py) (revision 55ff1ea7e88351a1e76a3ad60ce5e9a4f9e169a9) +++ wflow-py/wflow/wf_netcdfio.py (.../wf_netcdfio.py) (revision b0c9e960073cc20f9940d562bb83cea15206b118) @@ -80,6 +80,7 @@ endDayNr = netCDF4.date2num(timeList[-1].replace(tzinfo=None), units=units, calendar=calendar) timeAR = linspace(startDayNr, endDayNr, num=len(timeList)) + nc_trg = netCDF4.Dataset(trgFile, 'w', format=Format, zlib=zlib, complevel=complevel) logger.info( @@ -449,6 +450,11 @@ self.lstep = self.fstep + self.maxsteps + try: + self.x = self.dataset.variables['x'][:] + except: + self.x = self.dataset.variables['lon'][:] + # Now check Y values to see if we must flip the data try: self.y = self.dataset.variables['y'][:] @@ -469,6 +475,13 @@ self.flip = True + x = _pcrut.pcr2numpy(_pcrut.xcoordinate(_pcrut.boolean(_pcrut.cover(1.0))), NaN)[0, :] + y = _pcrut.pcr2numpy(_pcrut.ycoordinate(_pcrut.boolean(_pcrut.cover(1.0))), NaN)[:, 0] + + (self.latidx,) = logical_and(self.y >= y.min(), self.y <= y.max()).nonzero() + (self.lonidx,) = logical_and(self.x >= x.min(), self.x <= x.max()).nonzero() + + for var in vars: try: self.alldat[var] = self.dataset.variables[var][self.fstep:self.maxsteps] @@ -486,6 +499,9 @@ :var logging: python logging object :var var: variable to get from the file :var shifttime: is True start at 1 in the NC file (instead of 0) + + + window = data[dpos,latidx.min():latidx.max()+1,lonidx.min():lonidx.max()+1] """ if shifttime: ncindex = timestep @@ -500,8 +516,11 @@ self.fstep = ncindex self.lstep = ncindex + self.maxsteps - np_step = self.alldat[var][ncindex - self.fstep, :, :] + np_step = self.alldat[var][ncindex - self.fstep, self.latidx.min():self.latidx.max()+1, + self.lonidx.min():self.lonidx.max()+1] + + miss = float(self.dataset.variables[var]._FillValue) if self.flip: return numpy2pcr(Scalar, flipud(np_step).copy(), miss), True @@ -539,6 +558,23 @@ self.fstep = 0 self.lstep = self.fstep + self.maxsteps + try: + self.x = self.dataset.variables['x'][:] + except: + self.x = self.dataset.variables['lon'][:] + # Now check Y values to see if we must flip the data + try: + self.y = self.dataset.variables['y'][:] + except: + self.y = self.dataset.variables['lat'][:] + + + x = _pcrut.pcr2numpy(_pcrut.xcoordinate(_pcrut.boolean(_pcrut.cover(1.0))), NaN)[0, :] + y = _pcrut.pcr2numpy(_pcrut.ycoordinate(_pcrut.boolean(_pcrut.cover(1.0))), NaN)[:, 0] + + (self.latidx,) = logical_and(self.x >= x.min(), self.x < x.max()).nonzero() + (self.lonidx,) = logical_and(self.y >= x.min(), self.y < y.max()).nonzero() + for var in vars: try: self.alldat[var] = self.dataset.variables[var][self.fstep:self.maxsteps] @@ -562,7 +598,8 @@ self.alldat[vars] = self.dataset.variables[vars][ncindex:ncindex + self.maxsteps] self.fstep = ncindex self.lstep = ncindex + self.maxsteps - np_step = self.alldat[var][ncindex - self.fstep, :, :] + np_step = self.alldat[var][ncindex - self.fstep, self.latidx.min():self.latidx.max() + 1, + self.lonidx.min():self.lonidx.max() + 1] miss = float(self.dataset.variables[var]._FillValue) return numpy2pcr(Scalar, np_step, miss), True else: @@ -587,6 +624,24 @@ logging.error(os.path.abspath(netcdffile) + " not found!") exit(ValueError) + + try: + self.x = self.dataset.variables['x'][:] + except: + self.x = self.dataset.variables['lon'][:] + # Now check Y values to see if we must flip the data + try: + self.y = self.dataset.variables['y'][:] + except: + self.y = self.dataset.variables['lat'][:] + + x = _pcrut.pcr2numpy(_pcrut.xcoordinate(_pcrut.boolean(_pcrut.cover(1.0))), NaN)[0, :] + y = _pcrut.pcr2numpy(_pcrut.ycoordinate(_pcrut.boolean(_pcrut.cover(1.0))), NaN)[:, 0] + + (self.latidx,) = logical_and(self.x >= x.min(), self.x < x.max()).nonzero() + (self.lonidx,) = logical_and(self.y >= x.min(), self.y < y.max()).nonzero() + + logging.info("Reading static input from netCDF file: " + netcdffile + ": " + str(self.dataset).replace('\n', ' ')) @@ -601,7 +656,8 @@ """ if self.dataset.variables.has_key(var): - np_step = self.dataset.variables[var][timestep-1,:,:] + np_step = self.alldat[var][timestep-1, self.latidx.min():self.latidx.max() + 1, + self.lonidx.min():self.lonidx.max() + 1] miss = float(self.dataset.variables[var]._FillValue) return numpy2pcr(Scalar, np_step, miss), True else: