Skip to content

Commit

Permalink
Merge f5bbda3 into df523f9
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavid committed Jan 29, 2020
2 parents df523f9 + f5bbda3 commit c59e118
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -25,7 +25,7 @@ install:
- sudo apt-get --assume-yes install gfortran

# Install dependencies in conda
- conda create -n reflex_env python=$TRAVIS_PYTHON_VERSION libgfortran
- conda create -n reflex_env python=$TRAVIS_PYTHON_VERSION
- source activate reflex_env
# Install the remaining requeriments
- conda install --file requirements.txt -c conda-forge
Expand Down
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -64,6 +64,11 @@ Clone the github repository into a folder in your PYTHONPATH:

$ git clone https://github.com/spectraphilic/reflexible.git

Optionally create a conda environment:

$ conda create --name reflexible
$ conda activate reflexible

Install the requirements:

$ conda install --file requirements.txt -c conda-forge
Expand Down
7 changes: 5 additions & 2 deletions reflexible/base_read.py
Expand Up @@ -35,7 +35,6 @@
import datetime as dt

import numpy as np
import bcolz


from .data_structures import Trajectory
Expand Down Expand Up @@ -212,7 +211,10 @@ def read_trajectories(H, trajfile='trajectories.txt', ncluster=5,


def get_quantized_ctable(dtype, cparams, quantize=None, expectedlen=None):
"""Return a ctable with the quantize filter enabled for floating point cols."""
"""Return a ctable with the quantize filter enabled for floating point cols.
"""
import bcolz

columns, names = [], []
for fname, ftype in dtype.descr:
names.append(fname)
Expand Down Expand Up @@ -265,6 +267,7 @@ def read_partpositions(filename, nspec, ctable=True, clevel=5, cname="lz4", quan
Note: Passing a `quantize` param > 0 can increase the compression ratio of the ctable
container, but it may also slow down the reading speed significantly.
"""
import bcolz

xmass_dtype = [('xmass_%d' % (i + 1), 'f4') for i in range(nspec)]
# note age is calculated from itramem by adding itimein
Expand Down
4 changes: 2 additions & 2 deletions reflexible/conv2netcdf4/flexpart_read.py
Expand Up @@ -6,12 +6,10 @@
import datetime
import re
import numpy as np
import bcolz
from math import pi, sqrt, cos
from collections import OrderedDict

import reflexible.conv2netcdf4
from .helpers import closest


def read_releases(pathname):
Expand Down Expand Up @@ -42,6 +40,8 @@ def read_releases_v10(pathname):
-------
A ctable object from bcolz package.
"""
import bcolz

# Setup the container for the data
dtype = [('IDATE1', np.int32), ('ITIME1', np.int32),
('IDATE2', np.int32), ('ITIME2', np.int32),
Expand Down
2 changes: 1 addition & 1 deletion reflexible/data_structures.py
Expand Up @@ -7,7 +7,7 @@
import os
import datetime as dt
import itertools
from collections import Iterable
from collections.abc import Iterable
import glob

from math import (pi, cos, sqrt)
Expand Down
159 changes: 70 additions & 89 deletions reflexible/mapping.py
Expand Up @@ -110,13 +110,13 @@ def map_regions(map_region='default', map_par=None, fig_par=None):
# Get the database out of the system YAML file
mapdb_file = os.path.join(os.path.dirname(__file__), 'mapping_db.yml')
with open(mapdb_file) as mapdb:
mapping_db = yaml.load(mapdb)
mapping_db = yaml.safe_load(mapdb)

# and merge it with a possible one pointed by REFLEXIBLE_MAPDB env var
if 'REFLEXIBLE_MAPDB' in os.environ:
user_mapdb_file = os.environ['REFLEXIBLE_MAPDB']
with open(user_mapdb_file) as mapdb:
mapping_db.update(yaml.load(mapdb))
mapping_db.update(yaml.safe_load(mapdb))

# Lookup the region and its aliases
try:
Expand All @@ -126,7 +126,7 @@ def map_regions(map_region='default', map_par=None, fig_par=None):
for key in mapping_db:
if 'alias' in mapping_db[key]:
alias = mapping_db[key]['alias']
if map_region in re.split(',\s*', alias):
if map_region in re.split(r',\s*', alias):
region = mapping_db[key]
break
else:
Expand Down Expand Up @@ -222,111 +222,92 @@ def draw_grid(m, xdiv=10., ydiv=5., location=[1, 0, 0, 1],
return m_p, m_m


def get_base1(map_region='default', map_par=None, fig_par=None,
figname=None, fig=None, drawlsmask=False):
def get_base_image(map_region='default', map_par=None, fig_par=None,
image=None, figname=None, fig=None, drawlsmask=False):
"""
Primarily an internally used function, creates a
basemap for plotting. Returns a fig object and
a basemap instance.
Primarily an internally used function, creates a basemap for plotting.
Returns a fig object and a basemap instance.
Usage::
Examples::
> fig, m = get_base1(map_region="region_name")
> fig, m = get_base_image(map_region="region_name")
> fig, m = get_base_image(map_region="myregion", image=image)
"""

# Use map_regions function to define input parameters for Basemap
map_par_sd, fig_par_sd = map_regions(map_region, map_par, fig_par)
map_par, fig_par = map_regions(map_region, map_par, fig_par)

# create the figure
if fig is None:
axlocs = fig_par_sd.pop('axlocs')
fig = plt.figure(**fig_par_sd)
axlocs = fig_par.pop('axlocs')
fig = plt.figure(**fig_par)
ax = fig.add_axes(axlocs)
else:
ax = fig.gca()

m = basemap.Basemap(**map_par_sd)
plt.axes(ax) # make sure axes ax are current
# draw coastlines and political boundaries.
m.drawcoastlines(linewidth=0.8)
m.drawcountries(linewidth=0.2)
m.drawstates(linewidth=0.2)
if drawlsmask:
m.drawlsmask(ocean_color='#008EBA', zorder=0)
# m.fillcontinents(zorder=0.)
# draw parallels and meridians
# use draw_grid function
m_p, m_m = draw_grid(m)
plt.axes(ax) # make the original axes current again

m = basemap.Basemap(**map_par)

if image is None:
# draw coastlines and political boundaries.
m.drawcoastlines(linewidth=0.8)
m.drawcountries(linewidth=0.2)
m.drawstates(linewidth=0.2)
if drawlsmask:
m.drawlsmask(ocean_color='#008EBA', zorder=0)
# draw parallels and meridians
# use draw_grid function
draw_grid(m)
else:
# shows how to warp an image from one map projection to another.
# image from http://visibleearth.nasa.gov/

# need to change back to [0.1,0.1,0.7,0.7]
#ax = fig.add_axes([0.1, 0.1, 0.7, 0.7])

# read in jpeg image to rgba array of normalized floats.
pilImage = Image.open(image)
rgba = mpl.image.pil_to_array(pilImage)
rgba = rgba.astype(np.float32) / 255. # convert to normalized floats.

# define lat/lon grid that image spans (projection='cyl').
nlons = rgba.shape[1]
#nlats = rgba.shape[0]
delta = 360. / float(nlons)
lons = np.arange(-180. + 0.5 * delta, 180., delta)
lats = np.arange(-90. + 0.5 * delta, 90., delta)

# transform to nx x ny regularly spaced native projection grid
# nx and ny chosen to have roughly the same horizontal res as original
# image.
dx = 2. * np.pi * m.rmajor / float(nlons)
nx = int((m.xmax - m.xmin) / dx) + 1
ny = int((m.ymax - m.ymin) / dx) + 1
rgba_warped = np.zeros((ny, nx, 4), np.float64)
# interpolate rgba values from proj='cyl' (geographic coords) to 'lcc'
try:
for k in range(4):
rgba_warped[:, :, k] = m.transform_scalar(
rgba[:, :, k], lons, lats, nx, ny)
except:
rgba_warped = rgba
print('problem with transform_scalar')
# plot warped rgba image.
m.imshow(rgba_warped)

# draw coastlines.
m.drawcoastlines(linewidth=0.5, color='0.5')
if drawlsmask:
m.drawlsmask(ocean_color='#008EBA', zorder=0)
# draw parallels and meridians.
draw_grid(m, linewidth=0.5, color='0.5')

if figname is not None:
plt.savefig(figname)
return fig, m


def get_base_image(imagefile, map_region='default', map_par=None, fig_par=None,
fig=None):
"""Warps NASA Blue Marble Image version.
Create basemap figure for plotting on top of
returns a figure and a basemap instance.
Usage::
> fig, m = get_base_image(imagefile, map_region="myregion")
"""

# define Lambert Conformal basemap for North America.
mp, fig_par = map_regions(map_region, map_par, fig_par)

# shows how to warp an image from one map projection to another.
# image from http://visibleearth.nasa.gov/

# read in jpeg image to rgba array of normalized floats.
pilImage = Image.open(imagefile)
rgba = mpl.image.pil_to_array(pilImage)
rgba = rgba.astype(np.float32) / 255. # convert to normalized floats.

# define lat/lon grid that image spans (projection='cyl').
nlons = rgba.shape[1]
#nlats = rgba.shape[0]
delta = 360. / float(nlons)
lons = np.arange(-180. + 0.5 * delta, 180., delta)
lats = np.arange(-90. + 0.5 * delta, 90., delta)

# create new figure
fig = plt.figure(1, **fig_par)

m = basemap.Basemap(**mp)
ax = fig.add_axes(
[0.1, 0.1, 0.7, 0.7]) # need to change back to [0.1,0.1,0.7,0.7]
plt.axes(ax) # make the original axes current again

# transform to nx x ny regularly spaced native projection grid
# nx and ny chosen to have roughly the same horizontal res as original
# image.
dx = 2. * np.pi * m.rmajor / float(nlons)
nx = int((m.xmax - m.xmin) / dx) + 1
ny = int((m.ymax - m.ymin) / dx) + 1
rgba_warped = np.zeros((ny, nx, 4), np.float64)
# interpolate rgba values from proj='cyl' (geographic coords) to 'lcc'
try:
for k in range(4):
rgba_warped[:, :, k] = m.transform_scalar(
rgba[:, :, k], lons, lats, nx, ny)
except:
rgba_warped = rgba
print('problem with transform_scalar')
# plot warped rgba image.
m.imshow(rgba_warped)
# draw coastlines.
m.drawcoastlines(linewidth=0.5, color='0.5')
# draw parallels and meridians.
draw_grid(m, linewidth=0.5, color='0.5')
# draw()
return fig, m


if __name__ == '__main__':
# Some tests for reading the mapping YAML database
map_regions(map_region=sys.argv[1])
9 changes: 3 additions & 6 deletions reflexible/plotting.py
Expand Up @@ -173,12 +173,9 @@ def _get_figure(map_region='default', map_par=None, fig_par=None,
figure = Structure()

if m is None:
if image:
fig, m = mp.get_base_image(image, map_region, map_par, fig_par)
else:
fig, m = mp.get_base1(map_region, map_par, fig_par, fig=fig)
fig, m = mp.get_base_image(map_region, map_par, fig_par, image=image)

if fig is None:
elif fig is None:
map_par, fig_par = mp.map_regions(map_region, map_par, fig_par)
fig = plt.figure(**fig_par)

Expand Down Expand Up @@ -347,7 +344,7 @@ def plot_sensitivity(H, data, data_range=None,

if m.projection != 'merc':
if lons[-1] - lons[0] < 360.:
topodat, lons = basemap.addcyclic(data, lons)
topodat, lons = basemap.addcyclic(data.values, lons)

# get min/max range
if data_range is not None:
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
@@ -1,10 +1,10 @@
matplotlib
pillow
netCDF4>=1.1.1
xarray
numpy!=1.12
netCDF4>=1.1.1,<1.5
xarray>=0.14.1
numpy>=1.14.6
pandas
basemap==1.0.7
basemap==1.2.1
pyyaml
bcolz
pytest

0 comments on commit c59e118

Please sign in to comment.