def tell(self): if self._time > self.times[-1]: raise gradsfile.EndOfFileException() if not self._end_time_valid: return self._time - self.window else: return self._time
def read(self, n=1, squeeze=True): end_step = self.step + n if self.ar.shape[TIME_DIM] < end_step: self.step = end_step raise gradsfile.EndOfFileException() if squeeze: ret_ar = self.ar[..., self.step:end_step, 0:1].squeeze() else: ret_ar = self.ar[..., self.step:end_step, 0:1] self.step = end_step return np.ma.MaskedArray(ret_ar, False)
def read(self, n_times=1, squeeze=True): read_end_time = self._time + n_times * self.descr.timestep if read_end_time > self.last_time: raise gradsfile.EndOfFileException() if n_times == 1: values = self._read_one_step() else: shape_full = (len(self._x), len(self._y), len(self._z), n_times, 1) values = np.empty(shape_full) for ind_time in range(n_times): values[..., ind_time, 0] = self._read_one_step().squeeze() if squeeze: values = values.squeeze() return values
def read(self, n=1, squeeze=True): outbuf = np.zeros(self._shape + (n, )) mask = None #print 'ttt', self._time, self.t()[-1] if self._time > self.times[-1]: raise gradsfile.EndOfFileException() for step in range(n): count = 0 aver_start = self._time - self.window aver_end = self._time print 'Maxxing:', aver_start, 'to', aver_end, 'now', self.wrapped.tell( ) #if self.wrapped.tell() > aver_end: # self.wrapped.rewind() while self.wrapped.tell() <= aver_start: self.wrapped.seek(1) try: while aver_start < self.wrapped.tell() <= aver_end: print '...read:', self.wrapped.tell() fld_inst = self.wrapped.read(squeeze=False) fld_inst.shape = fld_inst.shape[:-1] if count == 0: outbuf[..., step] = fld_inst else: outbuf[..., step] = np.maximum(outbuf[..., step], fld_inst) if mask is not None: mask[..., step] = np.logical_or(mask[..., step], fld_inst.mask) elif hasattr(fld_inst, 'mask'): mask = np.zeros(outbuf.shape) mask[..., step] = fld_inst.mask count += 1 except gradsfile.EndOfFileException: print 'eof reached' pass # The window is possibly not fully covered, but still valid. self._time += self.window if mask is not None: outbuf = np.ma.MaskedArray(outbuf, mask) if squeeze: outbuf = outbuf.squeeze() return outbuf
def read(self, n=1, squeeze=True): if self._eof: raise gradsfile.EndOfFileException() ind_time = self.t().index(self._time) if ind_time + n > len(self._times): raise Exception() ret_arr = np.empty(self._shape + (n, )) mask = np.empty(self._shape + (n, )) for step in range(n): time = self._times[ind_time + step] for ind, time_past in enumerate(self.reader.t()): time_future = self.reader.t()[ind + 1] if time_past < time <= time_future: break step_sec = float(util.dt2sec(time_future - time_past)) weight_futr = util.dt2sec(time - time_past) / step_sec weight_past = 1 - weight_futr #print 'current, past, future', time, time_past, time_future, weight_past, weight_futr self.reader.goto(time_past) fld_past = self.reader.read(squeeze=False) self.reader.goto(time_future) fld_futr = self.reader.read(squeeze=False) fld_interp = weight_past * fld_past + weight_futr * fld_futr ret_arr[..., step] = fld_interp[..., 0] # last dim is time since no squeeze mask[..., step] = fld_interp.mask[..., 0] self._eof = (ind_time + n == len(self._times)) ret_arr = np.ma.MaskedArray(ret_arr, mask) if n == 1: ret_arr = ret_arr.reshape(self._shape) if squeeze: ret_arr = ret_arr.squeeze() #print ret_arr return ret_arr
def read(self, n=1, squeeze=True): fld_aver = np.zeros(self._shape + (n, )) fld_mask = np.zeros(self._shape + (n, ), dtype=bool) for step in range(n): count = 0 if len(self.windows) <= self._window_ind: raise gradsfile.EndOfFileException() aver_start, aver_end = self.windows[self._window_ind] print 'Averaging:', aver_start, aver_end, self.wrapped.tell() if self.wrapped.tell() > aver_end: self.wrapped.rewind() self._posit_wrapped(aver_start) try: if self.wrapped.tell() > aver_end: self.wrapped.rewind() #while self.wrapped.tell() <= aver_start: # self.wrapped.seek(1) assert self.wrapped.tell() > aver_start while aver_start < self.wrapped.tell() <= aver_end: print '...read:', self.wrapped.tell() fld_inst = self.wrapped.read(squeeze=False) fld_inst.shape = fld_inst.shape[:-1] fld_aver[..., step] += fld_inst fld_mask[..., step] = np.logical_or(fld_mask[..., step], fld_inst.mask) count += 1 except gradsfile.EndOfFileException: print 'eof reached' pass # The window is possibly not fully covered, but still valid. fld_aver[..., step] /= count self._window_ind += 1 fld_aver = np.ma.MaskedArray(fld_aver, fld_mask) if squeeze: fld_aver = fld_aver.squeeze() return fld_aver
def read(self, n=1, squeeze=True): fld_aver = np.zeros(self._shape + (n, )) fld_mask = np.zeros(self._shape + (n, ), dtype=bool) #print 'ttt', self._time, self.t()[-1] if self._time > self.times[-1]: raise gradsfile.EndOfFileException() for step in range(n): count = 0 aver_start = self._time - self.window aver_end = self._time print 'Averaging:', aver_start, 'to', aver_end, 'now', self.wrapped.tell( ) #if self.wrapped.tell() > aver_end: # self.wrapped.rewind() while self.wrapped.tell() <= aver_start: self.wrapped.seek(1) try: while aver_start < self.wrapped.tell() <= aver_end: print '...read:', self.wrapped.tell() fld_inst = self.wrapped.read(squeeze=False) fld_inst.shape = fld_inst.shape[:-1] fld_aver[..., step] += fld_inst fld_mask[..., step] = np.logical_or(fld_mask[..., step], fld_inst.mask) count += 1 except gradsfile.EndOfFileException: print 'eof reached' pass # The window is possibly not fully covered, but still valid. fld_aver[..., step] /= count self._time += self.window fld_aver = np.ma.MaskedArray(fld_aver, fld_mask) if squeeze: fld_aver = fld_aver.squeeze() return fld_aver
def seek(self, n): time_to = self._time + n * self.descr.timestep if time_to > self.last_time: raise gradsfile.EndOfFileException() self._time = time_to
def tell(self): if self._at_eof: raise gradsfile.EndOfFileException() return self._time
def read(self, n_steps=1, squeeze=True): if self._at_eof: raise gradsfile.EndOfFileException() if self.have_const_timestep: ind_time = self._get_time_ind_const() else: ind_time = self._get_time_ind_var() self._set_time_slice(ind_time, ind_time + n_steps) values = self.ncvar[self._slice].copy() #values = self.ncvar[self._slice] #1/0 #mask = np.zeros(values.shape, dtype=np.bool) mask = False # at_eof true if last step read self._at_eof = ind_time + n_steps >= len(self._times) if not self._at_eof: self.seek(n_steps) else: self._time = self._last_time #return values #if squeeze: values = values.squeeze() if self._flip: # order of dimensions values = self._flip(values) #Flip does the wrong job with projected reader (default one)! ### if self._flip_ew: ### # change to scan east to west ### values = self._flip_ew(values) ### if self._flip_ns: ### # change to scan south to north ### values = self._flip_ns(values) if not squeeze: shape = (len(self._x), len(self._y), len(self._z), n_steps, 1) values = values.reshape(shape, order='f') if self.mask_mode == 'full': #print values-self._undef mask = np.abs(values - self._undef) < self._eps elif self.mask_mode == 'first': if self._first_mask is None: mask = np.abs(values - self._undef) < self._eps self._first_mask = mask else: mask = self._first_mask elif self.mask_mode == False: mask = False else: raise ValueError('Unsupported mask mode') # must take care to promote values to right type. values *= scale doesn't work if # (when) values is integer type. # print values.dtype, self._scale_factor, self._add_offset if (issubclass(values.dtype.type, np.floating)): if self._scale_factor is not None: values *= self._scale_factor if self._add_offset is not None: values += self._add_offset else: #Integer offsets might be tricky if self._scale_factor is not None: #print 'before', values.dtype values = values[...] * self._scale_factor #values *= self._scale_factor #print 'after', values.dtype if self._add_offset is not None: if self._scale_factor is not None: # avoid copying values += self._add_offset # Force stuff that is within a descrete from zero to zero values[np.abs(values) < np.abs(self._scale_factor)] = 0 else: values = values[...] + self._add_offset return np.ma.MaskedArray(values, mask)
def tell(self): try: return self.times[self.step] except IndexError: raise gradsfile.EndOfFileException()
def seek(self, n): if self.step + n < self.ar.shape[TIME_DIM]: self.step += n else: raise gradsfile.EndOfFileException()
def seek(self, num_steps): if self._window_ind + num_steps >= len(self.windows): raise gradsfile.EndOfFileException() self._window_ind += num_steps
def tell(self): if len(self.windows) <= self._window_ind: raise gradsfile.EndOfFileException() window = self.windows[self._window_ind] return window[1] if self.end_valid else window[0]