def get_daterange(self): from awrams.utils.io.netcdf_wrapper import epoch_from_nc epoch = epoch_from_nc(self.ncd_group) time = self.ncd_group.variables['time'] return [(epoch + dt.days(int(ts))) for ts in (time[0], time[len(time) - 1])]
def _get_period_idx(self): from awrams.utils.io.netcdf_wrapper import epoch_from_nc epoch = epoch_from_nc(self.ncd) dti = pd.DatetimeIndex([(epoch + dt.days(int(ts))) for ts in self.ncd.variables['time']], freq='d') return dti.searchsorted(self.period)
def get_dates(self): from awrams.utils.io.netcdf_wrapper import epoch_from_nc epoch = epoch_from_nc(self.ncd_group) dti = pd.DatetimeIndex([(epoch + dt.days(int(ts))) for ts in self.ncd_group.variables['time']], freq='d') return dti
def __init__(self,variable,search_pattern=None,period=None): self.variable = variable self.db_open_with = db_open_with() if not search_pattern: search_pattern = variable+"*.nc" self.matcher = FileMatcher(search_pattern) try: self._all_files = sorted(self.matcher.locate()) except: from awrams.utils.io.messages import MSG_CANT_MATCH_VARIABLE_WITH_PATTERN logger.critical(MSG_CANT_MATCH_VARIABLE_WITH_PATTERN,variable,search_pattern) raise if not len(self._all_files): raise BaseException("No matching files for variable %s using search pattern %s"%(variable,search_pattern)) if period is not None: years = dt.years_for_period(period) f_files = [] for in_file in self._all_files: file_year = re.match('.*([0-9]{4})',in_file).groups()[0] if int(file_year) in years: f_files.append(in_file) self.files = f_files else: self.files = self._all_files if len(self.files) == 0: # +++ quick fix for solar if whole period is less than 1990 self.files = self._all_files[:1] ref_file = self._open_data(self.files[0]) end_ref_file = self._open_data(self.files[-1]) if len(self.files)>1 else ref_file self.epoch_offset = int(ref_file.variables['time'][0]) end_offset = int(end_ref_file.variables['time'][-1]) self.epoch = epoch_from_nc(ref_file) self.start_date = dt.datetime.fromordinal(self.epoch.toordinal()+self.epoch_offset) self.end_date = dt.datetime.fromordinal(self.epoch.toordinal()+end_offset) self.geospatial_reference = geospatial_reference_from_nc(ref_file) self.latitudes = aquantize(np.float64(ref_file.variables['latitude'][:]),0.05) self.longitudes = aquantize(np.float64(ref_file.variables['longitude'][:]),0.05) if 'var_name' in ref_file.ncattrs(): self.variable = ref_file.getncattr('var_name') try: self.units = ref_file.variables[self.variable].units except AttributeError: pass ref_file.close() if len(self.files)>1: end_ref_file.close()
def __init__(self, variable, search_pattern, period=None, day_exist_chn_name=None): self.variable = variable self._reader = InputReader(variable) self._pattern = search_pattern self.db_open_with = db_open_with() if not search_pattern: search_pattern = variable + "*.nc" self.matcher = FileMatcher(search_pattern) self._all_files = sorted(self.matcher.locate()) if not len(self._all_files): raise NoMatchingFilesException( "No matching files for variable %s using search pattern %s" % (variable, search_pattern)) if period is not None: years = dt.years_for_period(period) f_files = [] for in_file in self._all_files: file_year = re.match('.*([0-9]{4})', in_file).groups()[0] if int(file_year) in years: f_files.append(in_file) self.files = f_files else: self.files = self._all_files if len(self.files) == 0: # +++ quick fix for solar if whole period is less than 1990 self.files = self._all_files[:1] self.open_files = [self._open_data(fn) for fn in self.files] ref_file = self.open_files[0] end_ref_file = self.open_files[-1] self.epoch_offset = int(ref_file.variables['time'][0]) self.epoch = epoch_from_nc(ref_file) self.start_date = start_date(ref_file) self.end_date = end_date(end_ref_file, day_exist_chn_name) self.file_map = {} self._map_files() self.length = sum( [ds.variables[self.variable].shape[0] for ds in self.open_files]) self.shape = np.ma.concatenate( [[self.length], ref_file.variables[self.variable].shape[1:]]) self.geospatial_reference = geospatial_reference_from_nc(ref_file) self.latitudes = ref_file.variables['latitude'][:] self.longitudes = ref_file.variables['longitude'][:]
def get_coord(self, coord): ''' Get a Coordinates object whose name matches 'coord' ''' from awrams.utils.io.netcdf_wrapper import epoch_from_nc from awrams.utils.helpers import aquantize def from_epoch(epoch, ts): return epoch + dt.days(int(ts)) if coord == 'time': epoch = epoch_from_nc(self.ncd_group) time_var = self.ncd_group.variables['time'] dti = pd.DatetimeIndex( [(epoch + dt.days(int(ts))) for ts in self.ncd_group.variables['time'][:]], freq='d') if 'bounds' in time_var.ncattrs(): bounds_var = self.ncd_group.variables[time_var.bounds] boundaries = [] for b in bounds_var[:]: boundaries.append( [from_epoch(epoch, b[0]), from_epoch(epoch, b[1] - 1)]) #Attempt to infer period frequency from boundaries... p = infer_period(boundaries) return BoundedTimeCoordinates(TimeDimension(epoch), dti.to_period(p), boundaries) else: return TimeCoordinates(TimeDimension(epoch), dti) elif coord == 'latitude': lat = np.float64(self.ncd_group.variables['latitude'][:]) if not hasattr(lat, '__len__'): lat = np.array([lat]) return Coordinates(latitude, aquantize(lat, 0.05)) # return Coordinates(latitude,aquantize(np.float64(self.ncd_group.variables['latitude'][:]),0.05)) elif coord == 'longitude': lon = np.float64(self.ncd_group.variables['longitude'][:]) if not hasattr(lon, '__len__'): lon = np.array([lon]) return Coordinates(longitude, aquantize(lon, 0.05)) # return Coordinates(longitude,aquantize(np.float64(self.ncd_group.variables['longitude'][:]),0.05)) else: ncvar = self.ncd_group.variables[coord] if hasattr(ncvar, 'units'): units = Units(ncvar.units) else: units = Units('unknown unit') dim = Dimension(ncvar.dimensions[0], units, ncvar.dtype) return Coordinates(dim, ncvar[:])