Skip to content

Commit

Permalink
Merge pull request clawpack#190 from clawpack/python3
Browse files Browse the repository at this point in the history
Support Python 3
ketch authored Dec 20, 2016
2 parents 9c85e74 + 520cb91 commit 6833be6
Showing 24 changed files with 540 additions and 486 deletions.
82 changes: 43 additions & 39 deletions src/python/visclaw/Iplotclaw.py
Original file line number Diff line number Diff line change
@@ -10,9 +10,13 @@
"""

from __future__ import absolute_import
from __future__ import print_function
import sys
import six
from six.moves import input

if not sys.modules.has_key('matplotlib'):
if 'matplotlib' not in sys.modules:
import matplotlib
# Override system defaults before importing pylab
# If you set the backend here, you must start ipython w/o pylab
@@ -23,7 +27,7 @@
matplotlib.interactive(True)

from clawpack.visclaw import frametools
from iplot import Iplot
from .iplot import Iplot

#------------------------
class Iplotclaw(Iplot):
@@ -109,11 +113,11 @@ def __init__(self, setplot='setplot.py', outdir=None, \
try:
plotdata = frametools.call_setplot(self.setplot,plotdata)
except:
print '*** Problem executing setplot in Iplotclaw'
print ' setplot = ', self.setplot
print '*** Either this file does not exist or '
print ' there is a problem executing the function setplot in this file.'
print '*** PLOT PARAMETERS MAY NOT BE SET! ***'
print('*** Problem executing setplot in Iplotclaw')
print(' setplot = ', self.setplot)
print('*** Either this file does not exist or ')
print(' there is a problem executing the function setplot in this file.')
print('*** PLOT PARAMETERS MAY NOT BE SET! ***')
raise

self.plotdata = plotdata
@@ -125,15 +129,15 @@ def __init__(self, setplot='setplot.py', outdir=None, \

def plot_and_cache(self,frameno):
try:
if frameno not in self.frames.keys():
if frameno not in list(self.frames.keys()):
frametools.plotframe(self.frameno, self.plotdata, simple=self.simple, refresh=True)
self.frames[str(frameno)] = ''
else:
frametools.plotframe(self.frameno, self.plotdata, simple=self.simple, refresh=False)
except IOError as e:
print str(e)
print(str(e))
def help_n(self):
print 'n: advance to next frame\n'
print('n: advance to next frame\n')

def load_frame(self,frameno):
frame = self.plotdata.getframe(frameno, self.plotdata.outdir)
@@ -148,46 +152,46 @@ def load_frame(self,frameno):
def do_resetplot(self, rest):
if rest:
self.setplot = rest
print '*** Resetting setplot to: ',rest
print('*** Resetting setplot to: ',rest)
self.plotdata.setplot = self.setplot
print 'Executing setplot from ',self.setplot
print('Executing setplot from ',self.setplot)
try:
frametools.call_setplot(self.setplot,self.plotdata)
except:
print '*** Problem re-executing setplot'
print('*** Problem re-executing setplot')
raise

def help_resetplot(self):
print 'resetplot: re-execute the function setplot'
print ' The easiest way to change plotting parameters'
print ' is to modify setplot.py and then do resetplot.'
print ' '
print 'resetplot <new>: switch to a different setplot function'
print ' as specified by <new>, which is a function or'
print ' a string specifying the module containing setplot.'
print('resetplot: re-execute the function setplot')
print(' The easiest way to change plotting parameters')
print(' is to modify setplot.py and then do resetplot.')
print(' ')
print('resetplot <new>: switch to a different setplot function')
print(' as specified by <new>, which is a function or')
print(' a string specifying the module containing setplot.')

# show plot parameters:
# ---------------------
def do_show(self, rest):
self.plotdata.showitems()

def help_show(self):
print 'show: show the current plot items'
print('show: show the current plot items')


# cleargauges
# ---------
def do_cleargauges(self, rest):
if rest=='':
self.plotdata.gaugesoln_dict.clear()
print 'Cleared all gauges'
print('Cleared all gauges')
else:
print 'Not implemented: try cleargauges'
print('Not implemented: try cleargauges')

def help_cleargauges(self):
print 'cleargauges: delete gauge data from cache to replot'
print ' use if you have rerun the code and want to plot the'
print ' latest results'
print('cleargauges: delete gauge data from cache to replot')
print(' use if you have rerun the code and want to plot the')
print(' latest results')


# plotgauge commands:
@@ -202,8 +206,8 @@ def do_plotgauge(self, rest):
try:
import clawpack.amrclaw.data as amrclaw
except ImportError as e:
print "You must have AMRClaw installed to plot gauges."
print "continuing..."
print("You must have AMRClaw installed to plot gauges.")
print("continuing...")
return

gaugedata = amrclaw.GaugeData()
@@ -220,15 +224,15 @@ def do_plotgauge(self, rest):
gaugetools.plotgauge(gaugeno,self.plotdata)

if n < len(gaugenos) - 1:
ans = raw_input(" Hit return for next gauge or q to quit ")
ans = input(" Hit return for next gauge or q to quit ")
if ans == "q":
break


def help_plotgauge(self):
print 'plotgauge n : plot figure for gauge number n, if found'
print 'plotgauge all: loop through plots of all gauges'
print 'plotgauge : loop through plots of all gauges'
print('plotgauge n : plot figure for gauge number n, if found')
print('plotgauge all: loop through plots of all gauges')
print('plotgauge : loop through plots of all gauges')


# Convenience functions for examining solution or making additional
@@ -288,9 +292,9 @@ def otherfigures(self):

plotdata = self.plotdata
if len(plotdata.otherfigure_dict)==0:
print "No other figures specified."
print("No other figures specified.")
else:
for name in plotdata.otherfigure_dict.iterkeys():
for name in six.iterkeys(plotdata.otherfigure_dict):
otherfigure = plotdata.otherfigure_dict[name]
fname = otherfigure.fname
makefig = otherfigure.makefig
@@ -299,17 +303,17 @@ def otherfigures(self):
try:
exec(makefig)
except:
print "*** Problem executing makefig "
print " for otherfigure ",name
print("*** Problem executing makefig ")
print(" for otherfigure ",name)
import pdb; pdb.set_trace()
else:
try:
makefig(plotdata)
except:
print "*** Problem executing makefig function"
print " for otherfigure ",name
print("*** Problem executing makefig function")
print(" for otherfigure ",name)
else:
print "No makefig function specified for ",name
print("No makefig function specified for ",name)


# end of Iplotclaw.
1 change: 1 addition & 0 deletions src/python/visclaw/JSAnimation/IPython_display.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import
from .html_writer import HTMLWriter
from matplotlib.animation import Animation
import matplotlib.pyplot as plt
10 changes: 6 additions & 4 deletions src/python/visclaw/JSAnimation/JSAnimation_frametools.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
"""

from __future__ import absolute_import
from __future__ import print_function
import glob
from matplotlib import image, animation
from matplotlib import pyplot as plt
@@ -25,7 +27,7 @@ def make_plotdir(plotdir='_plots', clobber=True):
raise IOError('*** Cannot clobber existing directory %s' % plotdir)
else:
os.system("mkdir %s" % plotdir)
print "Figure files for each frame will be stored in ", plotdir
print("Figure files for each frame will be stored in ", plotdir)


def save_frame(frameno, plotdir='_plots', fname_base='frame', verbose=False):
@@ -39,7 +41,7 @@ def save_frame(frameno, plotdir='_plots', fname_base='frame', verbose=False):
filename = '%s/%s%s.png' % (plotdir, fname_base, str(frameno).zfill(5))
plt.savefig(filename)
if verbose:
print "Saved ",filename
print("Saved ",filename)


def make_anim(plotdir, fname_base=None, figno=None, figsize=(10,6)):
@@ -64,7 +66,7 @@ def make_anim(plotdir, fname_base=None, figno=None, figsize=(10,6)):
fname_base = 'frame'
else:
if fname_base is not None:
print "*** Warning: figno is specified so fname_base ignored"
print("*** Warning: figno is specified so fname_base ignored")
fname_base = 'frame*fig%s' % figno

# Find all frame files:
@@ -110,5 +112,5 @@ def make_html(anim, file_name='anim.html', title=None, \
html_file.write("<html>\n <h1>%s</h1>\n" % title)
html_file.write(html_body)
html_file.close()
print "Created %s" % file_name
print("Created %s" % file_name)

3 changes: 2 additions & 1 deletion src/python/visclaw/JSAnimation/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from html_writer import HTMLWriter
from __future__ import absolute_import
from .html_writer import HTMLWriter
5 changes: 4 additions & 1 deletion src/python/visclaw/JSAnimation/examples.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import absolute_import
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from JSAnimation import IPython_display
from six.moves import zip

def basic_animation(frames=100, interval=30):
"""Plot a basic sine wave with oscillating amplitude"""
@@ -30,8 +32,9 @@ def lorenz_animation(N_trajectories=20, rseed=1, frames=200, interval=30):
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames

def lorentz_deriv((x, y, z), t0, sigma=10., beta=8./3, rho=28.0):
def lorentz_deriv(coords, t0, sigma=10., beta=8./3, rho=28.0):
"""Compute the time-derivative of a Lorentz system."""
(x, y, z) = coords
return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]

# Choose random starting points, uniformly distributed from -15 to 15
4 changes: 3 additions & 1 deletion src/python/visclaw/JSAnimation/fix_jsmovies.py
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
It would be better to figure out the bug in JSAnimation.html_writer.
"""

from __future__ import absolute_import
from __future__ import print_function
import os, glob

def fix_file(fname, verbose=True):
@@ -27,7 +29,7 @@ def fix_file(fname, verbose=True):
f.write(html)
f.close()
if verbose:
print "Fixed file ",fname
print("Fixed file ",fname)

def fix_movie(plotdir, figno):
fname = os.path.join(plotdir, 'movieframe_allframesfig%s.html' % figno)
2 changes: 2 additions & 0 deletions src/python/visclaw/JSAnimation/html_writer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import absolute_import
import os
import warnings
import random
import cStringIO
from matplotlib.animation import writers, FileMovieWriter
import random
from six.moves import range


ICON_DIR = os.path.join(os.path.dirname(__file__), 'icons')
1 change: 1 addition & 0 deletions src/python/visclaw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Output package for Pyclaw"""

from __future__ import absolute_import
import logging

11 changes: 7 additions & 4 deletions src/python/visclaw/colormaps.py
Original file line number Diff line number Diff line change
@@ -12,9 +12,12 @@
See matplotlib._cm for the data defining various maps.
"""

from __future__ import absolute_import
from __future__ import print_function
import numpy
import matplotlib.colors as colors
import matplotlib.pyplot as plt
from six.moves import range

#-------------------------
def make_colormap(color_list):
@@ -38,7 +41,7 @@ def make_colormap(color_list):



z = numpy.sort(color_list.keys())
z = numpy.sort(list(color_list.keys()))
n = len(z)
z1 = min(z)
zn = max(z)
@@ -122,7 +125,7 @@ def add_colormaps(colormaps, data_limits=[0.0,1.0], data_break=0.5,
for val in val_list:
new_dict[key].append((val[0] * 0.5, val[1], val[2]))

if 'alpha' not in rhs_dict.keys():
if 'alpha' not in list(rhs_dict.keys()):
new_dict['alpha'].append((0.0,1.0,1.0))

# Add second colorbar
@@ -131,7 +134,7 @@ def add_colormaps(colormaps, data_limits=[0.0,1.0], data_break=0.5,
for val in val_list:
new_dict[key].append(((val[0] + 1.0) * 0.5, val[1], val[2]))

if 'alpha' not in lhs_dict.keys():
if 'alpha' not in list(lhs_dict.keys()):
new_dict['alpha'].append((1.0,1.0,1.0))

N = 256
@@ -231,6 +234,6 @@ def make_amrcolors(nlevels=4):
linecolors = linecolors[:nlevels]
bgcolors = bgcolors[:nlevels]
else:
print "*** Warning, suggest nlevels <= 16"
print("*** Warning, suggest nlevels <= 16")

return (linecolors, bgcolors)
Loading

0 comments on commit 6833be6

Please sign in to comment.