def setTimeAttribute(self, dataset_path, attribute_name, hour, **kwargs): if isinstance(hour, basestring): if ':' not in hour: errmsg = '"%s" is an invalid value for "%s"' raise ValueError, errmsg % (hour, attribute_name) self.setObjectAttribute(dataset_path, attribute_name, hour) else: include_timezone = kwargs.get('include_timezone', False) if not tzutils.isInTimezone(hour, self.tzinfo): tzhour = tzutils.asHourInTimezone(hour, self.tzinfo) hour_str = tzutils.hourAsString(tzhour, include_timezone) else: hour_str = tzutils.hourAsString(hour, include_timezone) self.setObjectAttribute(dataset_path, attribute_name, hour_str)
def generateProvenanceRecords(self, prov_path, start_time, data, **kwargs): """ Generates provenance records based on statistics from a single data array. Arguments -------------------------------------------------------------------- prov_path : string - path to a provenance dataset. start_time : datetime, scalar - hour/doy of provenance entry. If input data is 3D, it is the first hour/doy and entries will be generated for each day. data : 2D or 3D numpy array - data to be used to calculate provenance statistics. If 3D, time must be the 1st dimension. """ timezone = \ self.datasetAttribute(prov_path, 'timezone', self.default_timezone) start_hour = tzutils.asHourInTimezone(start_time, timezone) timestamp = kwargs.get('timestamp', self.timestamp) generator = self.provenanceGenerator(prov_path) generator_args = inspect.getargspec(generator).args records = [] if 'source' in generator_args: source = kwargs.get('source', self.fileAttribute('source', 'unknown')) if data.ndim == 2: records.append(generator(source, start_hour, timestamp, data)) end_hour = start_hour else: num_hours = data.shape[0] for hour in range(num_hours): time_ = start_hour + datetime.timedelta(hours=hour) record = generator(source, time_, timestamp, data[hour]) records.append(record) end_hour = time_ else: if data.ndim == 2: records.append(generator(start_hour, timestamp, data)) end_hour = start_hour else: num_hours = data.shape[0] for hour in range(num_hours): time_ = start_hour + datetime.timedelta(hours=hour) records.append(generator(time_, timestamp, data[hour])) end_hour = time_ return start_hour, end_hour, records
def fcastObsTimespan(self, reference_date_or_time, **kwargs): if isinstance(reference_date_or_time, datetime.date): target = kwargs.get('target_hour', self.project.target_hour) ref_time = datetime.datetime.combine(reference_date_or_time, datetime.time(target)) else: # assume it is already a datetime.datetime ref_time = reference_date_or_time timezone = kwargs.get('timezone', 'UTC') ref_time = tzutils.asHourInTimezone(ref_time, timezone) fcast_days = kwargs.get('fcast_days',self.project.fcast_days) fcast_hours = fcast_days * 24 end_time = ref_time + datetime.timedelta(hours=fcast_hours) obs_days = kwargs.get('obs_days',self.project.obs_days) obs_hours = obs_days * 24 start_time = ref_time - datetime.timedelta(hours=obs_hours) return start_time, ref_time, end_time
variable = args[0].upper() if summary is None: if variable == 'PCPN': summary = 'total' else: summary = 'extremes' if units is None: if variable == 'PCPN': units = 'in' elif variable == 'RHUM': units = '%' elif variable in ('DPT', 'TMP'): units = 'F' num_date_args = len(args) - 1 if num_date_args == 0: # 1 day ending today at target_hour hour = datetime.datetime.combine(today, datetime.time(hour=target_hour)) end_time = tzutils.asHourInTimezone(hour, local_timezone) + ONE_HOUR start_time = end_time - datetime.timedelta(hours=23) else: hour = datetime.datetime(target_year, int(args[1]), int(args[2]), target_hour) if num_date_args == 2: # one day ending at month, day, target hour end_time = tzutils.asHourInTimezone(hour, local_timezone) + ONE_HOUR start_time = end_time - datetime.timedelta(hours=24) elif num_date_args == 3: # month, first day, last day start_time = tzutils.asHourInTimezone(hour, local_timezone) + ONE_HOUR hour = datetime.datetime(target_year, int(args[1]), int(args[3]), target_hour) end_time = tzutils.asHourInTimezone(hour, local_timezone) + ONE_HOUR elif num_date_args == 4: # month, day, month, day
grib_source = options.grib_source is_utc_time = options.is_utc_time region_key = options.region source_key = options.source verbose = options.verbose or debug grib_var_name = args[0].upper() num_args = len(args) if len(args) == 1: local_hour = tzutils.asLocalHour(datetime.datetime.now(), 'US/Eastern') utc_hour = tzutils.asUtcHour(target_hour) elif num_args > 1: if num_args >= 5: time_tuple = tuple([int(t) for t in args[1:5]]) utc_hour = tzutils.asHourInTimezone(time_tuple, 'UTC') local_hour = tzutils.asLocalHour(utc_hour, 'US/Eastern') else: errmsg = 'At least 5 arguments are required when requesting a ' errmsg += 'specific time. (variable year month day hour)' raise RuntimeError, errmsg else: errmsg = 'No arguments passed to script. You must at least specify' raise RuntimeError, '%s the grib variable name.' % errmsg if debug: print 'requesting ...' print ' variable :', grib_var_name print ' local hour :', local_hour print ' UTC hour :', utc_hour
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # analysis = options.analysis debug = options.debug dev_mode = options.dev_mode grib_region = options.grib_region grib_source = options.grib_source grib_timezone = options.grib_timezone verbose = options.verbose or debug analysis_source = '.'.join((analysis,grib_source)) grib_var_name = args[0].upper() grib_time = (int(args[1]),int(args[2]),int(args[3]),int(args[4])) grib_time = tzutils.asHourInTimezone(grib_time, 'UTC') print 'requesting dump of %s grib for %s' % (analysis_source, str(grib_time)) if analysis == 'urma': from atmosci.reanalysis.urma.config import URMA_SOURCES CONFIG.sources.link(URMA_SOURCES) elif analysis == 'rtma': from atmosci.reanalysis.rtma.config import RTMA_SOURCES CONFIG.sources.link(RTMA_SOURCES) else: errmsg = '"%s" is an unsupported reanalysis.' raise ValueError, errmsg % analysis # create a factory for access to grib & static files grib_factory = ReanalysisGribFileFactory(analysis_source, CONFIG) if dev_mode: grib_factory.useDirpathsForMode('dev')
def asLocalTime(self, time_obj): return tzutils.asHourInTimezone(time_obj, self.local_tzinfo)
def asFileTime(self, time_obj): return tzutils.asHourInTimezone(time_obj, self.file_tzinfo)