Пример #1
0
if offset is None:
    offset = ndfd_config.bbox_offset
    if isinstance(offset, (ndfd_config.__class__, dict)):
        lat_offset = offset.lat
        lon_offset = offset.lon
    elif isinstance(offset, (tuple, list)):
        if offset[0] < 0:
            lon_offset, lat_offset = offset
        else:
            lat_offset, lon_offset = offset
    else:
        lat_offset = lon_offset = offset
print 'using lat offset =', lat_offset
print 'using lon offset =', lon_offset

grib_filepath = factory.forecastGribFilepath(ndfd_config, fcast_date,
                                             time_span, grib_variable)
print '\nreading gribs from', grib_filepath

gribs = pygrib.open(grib_filepath)
ndfd_bbox = []
ndfd_bbox.append(region_bbox[0] - lon_offset)
ndfd_bbox.append(region_bbox[1] - lat_offset)
ndfd_bbox.append(region_bbox[2] + lon_offset)
ndfd_bbox.append(region_bbox[3] + lat_offset)
print 'NDFD bounding box', ndfd_bbox
#for grib in gribs.read():
#    print '\n', grib, '\n', grib.keys()
print 'exploring grib variable "%s"' % grib_variable
grib = gribs.select(name=var_name_map[grib_variable])
print '\n', grib
print '\nin grib 0 :'
# create empty arrays to contain coordinates of NDFD nodes closest to each ACIS node
ndfd_lats = N.full(acis_lats.shape, N.nan)
ndfd_lons = N.full(acis_lons.shape, N.nan)
if verbose:
    print "ndfd static lats shape :", ndfd_lats.shape
    print "ndfd static lons shape :", ndfd_lons.shape

# arrays to track x,y indexes for nodes in NDFD coordinate datasets
distance = N.zeros(acis_lats.shape, dtype=float)
x_indexes = N.zeros(acis_lats.shape, dtype="<i2")
y_indexes = N.zeros(acis_lats.shape, dtype="<i2")

# get lon/lat from an existisng CONUS NDFD grib file

ndfd_filepath = ndfd_factory.forecastGribFilepath(None, fcast_date, timespan,
                                                  gribvar)
print '\nreading latitude, longitude from NDFD grib file\n', ndfd_filepath
# open grib file using cfgrib directly ... a little flaky
# ds = cfgrib.open_file("/Volumes/Transport/data/app_data/shared/forecast/ndfd/20190901/001-003-maxt.grib")
# open grib file with xarray ... access thru cfgrib with xarray context
ds = xarray.open_dataset(ndfd_filepath, engine='cfgrib')
grib_lats = ds.variables['latitude'].data
grib_lons = ds.variables['longitude'].data
ds.close()  # this doesn't work with cfgrib alone
neg_lons = grib_lons - 360.

if extreme_debug:
    print "grib_lats :", grib_lats[0, 0], grib_lats[0, -1]
    print "grib_lats :", grib_lats[-1, 0], grib_lats[-1, -1]
    print "grib_lons :", grib_lons[0, 0], grib_lons[0, -1]
    print "grib_lons :", grib_lons[-1, 0], grib_lons[-1, -1]
Пример #3
0
ndfd_indexes = [
    reader.getData('ndfd.y_indexes').flatten(),
    reader.getData('ndfd.x_indexes').flatten()
]
reader.close()
del reader

reader = factory.getSourceFileReader(source, target_date.year, region, 'temps')
last_obs_date = \
    asDatetimeDate(reader.getDatasetAttribute('temps.mint', 'last_obs_date'))
print '    last obs date', last_obs_date
del reader

# create a template for the NDFD grib file path
filepath_template = \
    factory.forecastGribFilepath(ndfd, target_date, '%s', '%s')
# in case the daily download hasn't occurred yet - go back one day
if not os.path.exists(filepath_template % ('001-003', 'mint')):
    date = target_date - ONE_DAY
    filepath_template = factory.forecastGribFilepath(ndfd, date, '%s', '%s')

# 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 !!!!!

fcast_start = None
temps = {}
Пример #4
0
if source_key is None:
    source_key = factory.project.source
grid_source = factory.sourceConfig(source_key)
print 'source =', grid_source.description

reader = factory.staticFileReader(grid_source, grid_region)
grid_shape, grib_indexes = reader.gribSourceIndexes('ndfd')
data_mask = reader.getData('cus_mask')
reader.close()

grid = N.empty((num_hours, ) + grid_shape, dtype=float)
grid.fill(N.nan)

grib_times = []

grib_filepath = factory.forecastGribFilepath(factory.sourceConfig('ndfd'),
                                             fcast_date, timespan, variable)
print '\nreading gribs from', grib_filepath

gribs = pygrib.open(grib_filepath)
for grib in gribs.select():
    index = grib.forecastTime - 1
    missing_value = float(grib.missingValue)
    grib_time = grib.validDate

    values = grib.values[grib_indexes].reshape(grid_shape)
    if N.ma.is_masked(values): values = values.data
    values[N.where(values >= missing_value)] = N.nan
    values[N.where(data_mask == True)] = N.nan
    grid[index, :, :] = values
    grib_times.append((index, grib_time))