warnings.filterwarnings('ignore', "All-NaN axis encountered")
warnings.filterwarnings('ignore', "All-NaN slice encountered")
warnings.filterwarnings('ignore', "invalid value encountered in greater")
warnings.filterwarnings('ignore', "invalid value encountered in less")
warnings.filterwarnings('ignore', "Mean of empty slice")
# MUST ALSO TURN OFF WARNING FILTERS AT END OF SCRIPT !!!!!

for slice_start, slice_end in periods:
    # make sure that a file exists for the requeste time period
    filepath = grid_factory.analysisGridFilepath(slice_start, grid_variable,
                                                 grid_region)
    if not os.path.exists(filepath):
        grid_factory.buildReanalysisGridFile(slice_start, grid_variable,
                                             grid_region, 'UTC')

    manager = grid_factory.gridFileManager(slice_start, grid_variable,
                                           grid_region)
    var_times = manager.timeAttributes(grid_variable, True)
    manager.close()
    grid_start_time = var_times['start_time']
    grid_end_time = var_times['end_time']
    grid_tzinfo = manager.tzinfo

    if debug:
        print '\ngrid file time attrs :'
        print '    grid start time :', grid_start_time
        print '      grid end time :', grid_end_time
        print '      grid timezone :', repr(grid_tzinfo)
    if verbose:
        print '\ngrib request time attrs :'
        print '    grib start time :', slice_start
        print '      grib end time :', slice_end
Exemple #2
0
elif num_time_args == 1: 
    new_obs_time = datetime.datetime(now.year, now.month, now.day, int(args[1])
elif num_time_args == 2: 
    new_obs_time = datetime.datetime(now.year, now.month, int(args[1]), int(args[1]))
elif num_time_args == 3:
    new_obs_time = datetime.datetime(now.year, int(args[1]), int(args[2]), int(args[3]))
else:
    errmsg = 'Too many time arguments (%d) passed to script. Up to 3 are supported.'
    raise RuntimeError, errmsg % num_time_args

# create a factory for access to grid files
factory = ReanalysisGridFileFactory(analysis, grib_source, grib_server)
if dev_mode: factory.useDirpathsForMode('dev')
region = factory.regionConfig(grid_region)

manager = factory.gridFileManager(new_obs_time, variable, grid_region, mode='r')
print '\nBefore repairs to %s variable:' % variable
print '    last_obs_time :', manager.dateAttribute(variable, 'last_obs_time', None)
print '    rtma_end_time :', manager.dateAttribute(variable, 'rtma_end_time')
print '  last_valid_time :', manager.dateAttribute(variable, 'last_valid_time', None)
manager.close()

manager.open(mode='a')
manager.setTimeAttribute(variable, 'last_obs_time', new_obs_time)    
manager.setTimeAttribute(variable, 'rtma_end_time', new_obs_time)
manager.setTimeAttribute(variable, 'last_valid_time', new_obs_time)
manager.close()

print '\nAfter repairs to %s variable:' % variable
manager.open(mode='r')
print '    last_obs_time :', manager.timeAttribute(variable, 'last_obs_time')
    # filter annoying numpy warnings
    warnings.filterwarnings('ignore',"All-NaN axis encountered")
    warnings.filterwarnings('ignore',"All-NaN slice encountered")
    warnings.filterwarnings('ignore',"invalid value encountered in greater")
    warnings.filterwarnings('ignore',"invalid value encountered in less")
    warnings.filterwarnings('ignore',"Mean of empty slice")
    # MUST ALSO TURN OFF WARNING FILTERS AT END OF SCRIPT !!!!!


    for slice_start, slice_end in periods:
        # make sure that a file exists for the requeste time period
        filepath = grid_factory.analysisGridFilepath(slice_start, grid_variable, grid_region)
        if not os.path.exists(filepath):
            grid_factory.buildReanalysisGridFile(slice_start, grid_variable, grid_region, 'UTC')

        manager = grid_factory.gridFileManager(slice_start, grid_variable, grid_region)
        var_times = manager.timeAttributes(grid_variable, True)
        manager.close()
        grid_start_time = var_times['start_time']
        grid_end_time = var_times['end_time']
        grid_tzinfo = manager.tzinfo

        if debug:
            print '\ngrid file time attrs :'
            print '    grid start time :', grid_start_time
            print '      grid end time :', grid_end_time
            print '      grid timezone :', repr(grid_tzinfo)
        if verbose:
            print '\ngrib request time attrs :'
            print '    grib start time :', slice_start
            print '      grib end time :', slice_end
Exemple #4
0
    #                    5 = last month 6 = last day, 7 = last hour
    year = int(args[0])
    update_start_time = hourInTimezone(year, int(args[1]), int(args[2]), int(args[3]), file_tzinfo)
    update_end_time = hourInTimezone(year, int(args[4]), int(args[5]), int(args[6]), file_tzinfo)


# create a factory for access to grid files
grid_factory = ReanalysisGridFileFactory()
if dev_mode: grid_factory.useDirpathsForMode('dev')

if update_start_time is None:
    filepath = grid_factory.analysisGridFilepath(now, 'RHUM', grid_region)
    if not os.path.exists(filepath):
        grid_factory.buildReanalysisGridFile(now, 'RHUM', grid_region, 'UTC')

    reader = grid_factory.gridFileManager(now, 'RHUM', grid_region)
    time_attr_key = '%s_end_time' % analysis.lower()
    last_anal_time = reader.timeAttribute('RHUM', time_attr_key, None)
    if last_anal_time is None: # file never updated before
        update_start_time = reader.timeAttribute('RHUM', 'start_time')
    else: # starting 1 hour after last valid time in file
        update_start_time = last_anal_time + datetime.timedelta(hours=1)

    # now find the end time in the TMP file
    reader = grid_factory.gridFileManager(update_start_time, 'TMP', grid_region) 
    time_attr_key = '%s_end_time' % analysis.lower()
    update_end_time = reader.timeAttribute('TMP', time_attr_key, None)

    # check hether we can (or should) do the update
    if update_end_time is None: # temperature never updated
        monthyear = update_start_time.strftime('%M, %Y')
        errmsg = 'Incorrect number of argumets. %s' % allowed_args
    raise RuntimeError, errmsg

reference_time = \
    tzutils.asLocalTime(datetime.datetime(*time_tuple), local_timezone)

# create a factory for access to grid files
grid_factory = \
    ReanalysisGridFileFactory('reanalysis', timezone=local_timezone)
if dev_mode: grid_factory.useDirpathsForMode('dev')
region = grid_factory.regionConfig(region_key)

# get manager for the reference time span
manager = grid_factory.gridFileManager(reference_time,
                                       variable_name,
                                       region_key,
                                       num_hours=num_hours,
                                       use_time_in_path=use_time_in_path)
time_attrs = manager.timeAttributes(variable_name)
if debug:
    print 'updating file for ...'
    print '     variable :', variable_name
    print '    file date :', reference_time.date()
    print '    ', manager.filepath
manager.close()

valid_time = time_attrs['last_valid_time']
fcast_end = time_attrs.get('fcast_end_time', None)
fcast_start = time_attrs.get('fcast_start_time', None)
rtma_time = time_attrs.get('rtma_end_time', None)
urma_time = time_attrs.get('urma_end_time', None)
if dev_mode: grid_factory.useDirpathsForMode('dev')
region = grid_factory.regionConfig(region_key)

timespan = grid_factory.fileTimespan(reference_date, obs_days, fcast_days,
                                     target_hour, local_timezone)
file_start_time, reference_time, file_end_time = timespan
if debug:
    print 'timespan in local timezone...'
    print '    start hour :', file_start_time
    print '    ref hour :', reference_time
    print '    end hour :', file_end_time

# get readers for temperature and dew point files
dewpt_reader = grid_factory.gridFileManager('DPT',
                                            region_key,
                                            start_time=file_start_time,
                                            end_time=file_end_time,
                                            use_time_in_path=use_time_in_path)
dewpt_units = dewpt_reader.datasetAttribute('DPT', 'units')
last_valid_time = dewpt_reader.timeAttribute('DPT', 'last_valid_time')

temp_reader = grid_factory.gridFileManager('TMP',
                                           region_key,
                                           start_time=file_start_time,
                                           end_time=file_end_time,
                                           use_time_in_path=use_time_in_path)
temp_units = temp_reader.datasetAttribute('TMP', 'units')
last_valid_time = min(last_valid_time,
                      temp_reader.timeAttribute('TMP', 'last_valid_time'))

# get latitude and longitude grids to use in new file