Ejemplo n.º 1
0
def tester_bgm_w2c():
    """wms, specify projection/extent lcc"""
    from plotter import calpost_reader as reader
    import cartopy.crs as ccrs
    with open('../data/tseries_ch4_1min_conc_co_fl.dat') as f:
        dat = reader.Reader(f, slice(60 * 12, 60 * 12 + 10))

    # background
    plotter_options = {
        'contour_options': {
            'alpha': .2
        },
        'background_manager':
        BackgroundManager(wms_options={
            'wms':
            'https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer',
            'layers': '0'
        },
                          projection=LambertConformalTCEQ(),
                          extent=[-465000, -460000, -908000, -901000]),
        'customize_once':
        lambda p: p.ax.gridlines(draw_labels=True),
    }

    x = dat['x'] * 1000
    y = dat['y'] * 1000
    p = Plotter(dat['v'], dat['ts'], x=x, y=y, plotter_options=plotter_options)
    p(outdir / 'test_bgm_w2c.png')
Ejemplo n.º 2
0
def tester_bgm_b2a():
    """specify extent/projection and bg image"""
    from plotter import calpost_reader as reader
    import cartopy.crs as ccrs
    with open('../data/tseries_ch4_1min_conc_co_fl.dat') as f:
        dat = reader.Reader(f, slice(60 * 12, 60 * 12 + 10))

    # background
    plotter_options = {
        'contour_options': {
            'alpha': .2
        },
        'background_manager':
        BackgroundManager(bgfile=bgfile_lcc,
                          projection=LambertConformalTCEQ(),
                          extent=[-465000.0, -460000.0, -902000.0, -907000.0]),
        'customize_once':
        lambda p: p.ax.gridlines(draw_labels=True),
    }

    x = dat['x'] * 1000
    y = dat['y'] * 1000
    p = Plotter(dat['v'], dat['ts'], x=x, y=y, plotter_options=plotter_options)
    p(outdir / 'test_bgm_b2a.png')
Ejemplo n.º 3
0
        lambda p:
        # adjust_text() repels labels from each other
        adjust_text(
            # make list of annotation
            list(
                # this part creates annotation for each point
                p.ax.annotate(_.Site_Label, (_.geometry.x, _.geometry.y,),
                              zorder=11, fontsize='xx-small')
                # goes across all points but filter by Site_Label
                for _ in df.itertuples()  # if _.Site_Label in (f'{site}',)
            ),
        ),
        # modeled box
        lambda p: p.ax.add_geometries(
            [Polygon([(extent[x], extent[y]) for x, y in ((0, 2), (0, 3), (1, 3), (1, 2), (0, 2))])],
            crs=LambertConformalTCEQ(), facecolor='none', edgecolor='white', lw=.6,
        ),
    ]}

# clone the options and let each has own title
plotter_options = [{**plotter_options, 'title': title} for title in titles]

# colorbar goes to entire figure
figure_options = {
    'colorbar_options': {
        'label': r'$CH_4$ (ppbV)',
    }
}

# make a plot template
# mpl.rcParams.update({'font.size': 8})
Ejemplo n.º 4
0
                    (
                        _.geometry.x,
                        _.geometry.y,
                    ),
                    zorder=11,
                    fontsize='xx-small',
                )
                # goes across all points but filter by Site_Label
                for _ in df_shp.itertuples()), ),
        # modeled box
        lambda p: p.ax.add_geometries(
            [
                Polygon([(extent[x], extent[y])
                         for x, y in ((0, 2), (0, 3), (1, 3), (1, 2), (0, 2))])
            ],
            crs=LambertConformalTCEQ(),
            facecolor='none',
            edgecolor='black',
            lw=.6,
        ),
        # grid lines
        lambda p: add_gridlines(p.ax, {
            'bottom_labels': False,
            'right_labels': False
        })
    ]
}

# make a plot template
p = plotter_solo.Plotter(array=arr,
                         tstamps=tstamps,
Ejemplo n.º 5
0
                          cmap=colors.ListedColormap(['#e41a1c', '#377eb8', '#ffff33'])
                          ),
        # Shannon's "original" box
        lambda p: p.ax.add_geometries(
            [Polygon([(-101.8834373, 31.71350603),
                      (-101.8664281, 31.71727773),
                      (-101.8748762, 31.75052556),
                      (-101.8942724, 31.74599821),
                      (-101.8834373, 31.71350603),
                      ])],
            crs=ccrs.PlateCarree(), facecolor='none', edgecolor='white', lw=.6,
        ),
        # modeled box
        lambda p: p.ax.add_geometries(
            [Polygon([(extent[x], extent[y]) for x, y in ((0, 2), (0, 3), (1, 3), (1, 2), (0, 2))])],
            crs=LambertConformalTCEQ(), facecolor='none', edgecolor='white', lw=.6, ls='--',
        ),

    ]}

# clone the options and let each has own title
plotter_options = [{**plotter_options, 'title': title} for title in titles]

# colorbar goes to entire figure
figure_options = {
    'colorbar_options': {
        'label': r'$CH_4$ (ppbV)',
        }
    }

# make a plot template
Ejemplo n.º 6
0
def tester_s5(quiet=True):
    """show contour from calpost with all the bells and whistles"""
    from plotter import calpost_reader as reader
    from plotter.plotter_util import LambertConformalTCEQ
    import rasterio
    import cartopy.crs as ccrs
    import geopandas as gpd
    import matplotlib.colors as colors
    from shapely.geometry import Polygon
    from adjustText import adjust_text

    import tempfile
    from pathlib import Path
    import shlex
    import subprocess


    # background (extent is used as plot's extent)
    b = rasterio.open(bgfile)
    bext = [b.transform[2], b.transform[2] + b.transform[0] * b.width,
            b.transform[5] + b.transform[4] * b.height, b.transform[5]]

    # source locations
    df = gpd.read_file(shpfile)
    df = df.to_crs('EPSG:3857')

    # read the data
    title = 'Flare'
    with open('../data/tseries_ch4_1min_conc_co_fl.dat') as f:
        dat = reader.Reader(f, slice(60 * 12, 60 * 12 + 10))

    # grab necessary info
    arr = dat['v']
    tstamps = dat['ts']
    grid = dat['grid']
    extent = [
        grid['x0'], grid['x0'] + grid['nx'] * grid['dx'],
        grid['y0'], grid['y0'] + grid['ny'] * grid['dy'],
    ]
    
    # distance in calpost is in km
    extent = [_ * 1000 for _ in extent]
    x = dat['x'] * 1000
    y = dat['y'] * 1000

    # convert unit of array from g/m3 tp ppb
    # mwt g/mol
    # molar volume m3/mol
    arr = arr / 16.043 * 0.024465403697038 * 1e9

    # Mrinali/Gary's surfer color scale
    cmap = colors.ListedColormap([
        '#D6FAFE', '#02FEFF', '#C4FFC4', '#01FE02',
        '#FFEE02', '#FAB979', '#EF6601', '#FC0100', ])
    cmap.set_under('#FFFFFF')
    # Define a normalization from values -> colors
    bndry = [1, 10, 50, 100, 200, 500, 1000, 2000]
    norm = colors.BoundaryNorm(bndry, len(bndry))

    plotter_options = {
        'extent': bext, 'projection': ccrs.epsg(3857),
        'title': title,
        'contour_options': {
            'levels': bndry,
            'cmap': cmap,
            'norm': norm,
            'alpha': .5,
        },
        'colorbar_options': {
            'label': r'$CH_4$ (ppbV)',
        },
        'customize_once': [
            # background
            lambda p: p.ax.imshow(b.read()[:3, :, :].transpose((1, 2, 0)),
                                  extent=bext, origin='upper'),
            # emission points
            lambda p: df.plot(ax=p.ax, column='kls', categorical=True, legend=False, zorder=10,
                              markersize=2,
                              # got red/blue/yellow from colorbrewer's Set1
                              cmap=colors.ListedColormap(['#e41a1c', '#377eb8', '#ffff33'])),
            # emission point annotations
            lambda p: adjust_text(
                # make list of annotation
                list(
                    # this part creates annotation for each point
                    p.ax.text(_.geometry.x, _.geometry.y, _.Site_Label, zorder=11, size=6)
                    # goes across all points but filter by Site_Label
                    for _ in df.itertuples() if _.Site_Label in ('F1', 'op3_w1', 'S4')
                ),
                # draw arrow from point to annotation
                arrowprops={'arrowstyle': '-'}
            ),
            # modeled box
            lambda p: p.ax.add_geometries(
                [Polygon([(extent[x],extent[y]) for x,y in ((0,2), (0,3), (1,3), (1,2), (0,2))])],
                crs=LambertConformalTCEQ(), facecolor='none', edgecolor='white', lw=.6,
            ),
        ]}

    # make a plot template
    p = Plotter(arr, dat['ts'], x=x, y=y, plotter_options=plotter_options)

    # make a plot
    p(outdir / 'test_s5.png')

    # make an animation
    with tempfile.TemporaryDirectory() as wdir:
        workdir = Path(wdir)

        # function to save one time frame
        def saveone(i):
            ts = tstamps[i]
            pname = workdir / f'{i:04}.png'
            footnote = str(ts)
            p(pname, tidx=i, footnote=footnote)

        n = len(tstamps)
        for i, ts in enumerate(tstamps):
            saveone(i)
        # make mpeg file
        if quiet:
            loglevel = '-loglevel error'
        else:
            loglevel = ''
        cmd = f'ffmpeg {loglevel} -i "{workdir / "%04d.png"}" -vframes 2880 -crf 3 -vcodec libx264 -pix_fmt yuv420p -f mp4 -y "{outdir / "test_pr2b.mp4"}"'
        subprocess.run(shlex.split(cmd))
Ejemplo n.º 7
0
    fname_toy = 'toy_model_allstations.txt'

    grid_toy = {
        'x0': -464.4,
        'y0': -906.7,
        'nx': 34,
        'ny': 47,
        'dx': 0.1,
        'dy': 0.1
    }
    grid_pilot = {
        'x0': -440.8,
        'y0': -730.0,
        'nx': 48,
        'ny': 44,
        'dx': 0.2,
        'dy': 0.2
    }

    hrc_toy = HysplitReceptorCoords(fname_toy, grid_toy,
                                    LambertConformalTCEQ())
    hrc_pilot = HysplitReceptorCoords(fname_pilot, grid_pilot,
                                      LambertConformalHRRR())

    print(
        hrc_pilot.get_index(
            pd.DataFrame(
                {'rdx': [1, 2, 3, 11]},
                index=[1, 2, 3, 11],
            )))