示例#1
0
 def test_load_1file_weight(self):
     fake_data_file(version, 1987, 7, 2, 6)
     with patch.object(pandas, 'read_fwf',
                       side_effect=fake_prfwf) as mock_load:
         o = twcr.load_observations_fortime(datetime.datetime(
             1987, 7, 2, 6),
                                            version=version)
     # Right dimensions
     self.assertEqual(len(o.weight), 20)
     # All equal to 1
     expected = numpy.repeat(1, 20)
     self.assertTrue((o.weight == expected).all())
示例#2
0
 def test_load_1file_weight(self):
     fake_data_file(version, 1987, 7, 2, 6)
     fake_data_file(version, 1987, 7, 2, 12)
     with patch.object(pandas, 'read_fwf',
                       side_effect=fake_prfwf) as mock_load:
         o = twcr.load_observations_fortime(datetime.datetime(
             1987, 7, 2, 7, 30),
                                            version=version)
     # Right dimensions
     self.assertEqual(len(o.weight), 40)
     # Closer to 6 than 12
     expected = numpy.concatenate(
         (numpy.repeat(0.75, 20), numpy.repeat(0.25, 20)))
     self.assertTrue((o.weight == expected).all())
year = 1916
month = 3
day = 12
hour = 6

# Get the 20CR data
#ic=twcr.load('prmsl',datetime.datetime(2009,3,12,18),
ic = twcr.load('prmsl',
               datetime.datetime(year, month, day, hour),
               version='2c')
ic = ic.extract(iris.Constraint(member=1))
#ic=ic.collapsed('member', iris.analysis.MEAN)

# Make the fake observations
obs = twcr.load_observations_fortime(datetime.datetime(1916, 3, 12, 6),
                                     version='2c')
ensemble = []
for index, row in obs.iterrows():
    ensemble.append(
        ic.interpolate([('latitude', row['Latitude']),
                        ('longitude', row['Longitude'])],
                       iris.analysis.Linear()).data.item())
ensemble = numpy.array(ensemble, dtype=numpy.float32)
ensemble = normalise(ensemble)
obs_t = tf.convert_to_tensor(ensemble, numpy.float32)
obs_t = tf.reshape(obs_t, [1, 488])

# Get the assimilation model
model_save_file = ("%s/Machine-Learning-experiments/" +
                   "oldstyle_assimilation_1916/" +
                   "saved_models/Epoch_%04d") % (os.getenv('SCRATCH'), 50)
# South America centred projection
projection = ccrs.RotatedPole(pole_longitude=120, pole_latitude=125)
scale = 25
extent = [scale * -1 * aspect / 3.0, scale * aspect / 3.0, scale * -1, scale]

# On the left - spaghetti-contour plot of original 20CRv3
ax_left = fig.add_axes([0.005, 0.01, 0.323, 0.98], projection=projection)
ax_left.set_axis_off()
ax_left.set_extent(extent, crs=projection)
ax_left.background_patch.set_facecolor((0.88, 0.88, 0.88, 1))
mg.background.add_grid(ax_left)
land_img_left = ax_left.background_img(name='GreyT', resolution='low')

# 20CRv3 data
prmsl = twcr.load('prmsl', dte, version='4.5.1')
obs_t = twcr.load_observations_fortime(dte, version='4.5.1')

# Plot the observations
mg.observations.plot(ax_left, obs_t, radius=0.2)

# PRMSL spaghetti plot
mg.pressure.plot(ax_left,
                 prmsl,
                 scale=0.01,
                 type='spaghetti',
                 resolution=0.25,
                 levels=numpy.arange(875, 1050, 10),
                 colors='blue',
                 label=False,
                 linewidths=0.1)
# Get the DWR observations within +- 15 hours
obs = DWR.load_observations('prmsl', dte - datetime.timedelta(hours=12),
                            dte + datetime.timedelta(hours=12))
# Discard everthing except Fort William
obs = obs[obs.name == 'FORTWILLIAM']

mg.observations.plot(ax_map,
                     obs,
                     lat_label='latitude',
                     lon_label='longitude',
                     radius=0.15,
                     facecolor='red')

# Add the observations from 20CR
obs_t = twcr.load_observations_fortime(dte, '4.5.1')
# Filter to those assimilated near the UK
obs_s = obs_t.loc[((obs_t['Latitude'] > 0) & (obs_t['Latitude'] < 90)) & (
    (obs_t['Longitude'] > 240) | (obs_t['Longitude'] < 100))].copy()
mg.observations.plot(ax_map, obs_s, radius=0.15)

# load the pressures
prmsl = twcr.load('prmsl', dte, version='4.5.1')

# For each ensemble member, make a contour plot
CS = mg.pressure.plot(ax_map,
                      prmsl,
                      resolution=0.25,
                      type='spaghetti',
                      scale=0.01,
                      levels=numpy.arange(875, 1050, 10),
示例#6
0
projection = ccrs.RotatedPole(pole_longitude=180, pole_latitude=30)
scale = 25
extent = [scale * -1 * aspect / 2, scale * 1.5 * aspect, scale * -1, scale]

# Map plot on the left
ax_map = fig.add_axes([0.01, 0.01, 0.98, 0.98], projection=projection)
ax_map.set_axis_off()
ax_map.set_extent(extent, crs=projection)

# Background, grid and land
ax_map.background_patch.set_facecolor((0.88, 0.88, 0.88, 1))
mg.background.add_grid(ax_map, sep_minor=1, sep_major=5)
land_img = ax_map.background_img(name='GreyT', resolution='low')

# Add the observations from Reanalysis
obs = twcr.load_observations_fortime(dte, version='3')
mg.observations.plot(ax_map,
                     obs,
                     radius=0.25,
                     edgecolor=(0.2, 0.2, 0.2),
                     linewidth=0.01)
# Plot the current position
obs = IMMA.read(
    os.path.join(os.path.dirname(__file__),
                 '../../../imma/Princesse_Alice_II_1898.imma'))
for ob in obs:
    if ob['LAT'] is None: continue
    if ob['LON'] is None: continue
    if ob['YR'] is None: continue
    if ob['MO'] is None: continue
    if ob['DY'] is None: continue
ax_full = fig.add_axes([0, 0, 1, 1])
ax_full.set_axis_off()
ax_full.add_patch(
    Rectangle((0, 0), 1, 1, facecolor=(1, 1, 1, 1), fill=True, zorder=1))

t2m = twcr.load("air.2m", dte,
                version="4.6.1").extract(iris.Constraint(member=0))
u10m = twcr.load("uwnd.10m", dte,
                 version="4.6.1").extract(iris.Constraint(member=0))
v10m = twcr.load("vwnd.10m", dte,
                 version="4.6.1").extract(iris.Constraint(member=0))
precip = twcr.load("prate", dte,
                   version="4.6.1").extract(iris.Constraint(member=0))
precip = normalise_precip(precip)
obs = twcr.load_observations_fortime(dte, version="4.6.1")
# prmsl all members for spread
prmsl = twcr.load("prmsl", dte, version="4.6.1")
prmsl = prmsl.collapsed("member", iris.analysis.STD_DEV)
# Load the climatological prmsl stdev from v2c

prevt = datetime.datetime(dte.year, dte.month, dte.day,
                          int(dte.hour) - int(dte.hour) % 6)
prevcsd = iris.load_cube(
    "/data/users/hadpb/20CR/version_3.4.1/standard.deviation/prmsl.nc",
    iris.Constraint(time=iris.time.PartialDateTime(
        year=1981, month=prevt.month, day=prevt.day, hour=prevt.hour)),
)
nextt = prevt + datetime.timedelta(hours=6)
nextcsd = iris.load_cube(
    "/data/users/hadpb/20CR/version_3.4.1/standard.deviation/prmsl.nc",