def test_numbers(self): self.assertEqual(abs(-2.), 2., 'Python float to Java double') self.assertEqual(abs(-2), 2, 'Python int to Java long') self.assertEqual(abs(-2), 2, 'Python long to Java long') try: abs(-123456789123456789123) except TypeError: pass
def histogram(data, bins=20, width=400, height=400, title="VisAD Histogram", color=None, bottom=None, top=None, panel=None, clip=1): """ Quick plot of a histogram from <data>. <bins> is the number of bins to use (def = 20), <panel> is the name of a panel to put this into (default= make a new one), <color> is the color to use, <width> and <height> are the dimensions, <title> is the phrase for the title bar. Returns a reference to the display. """ if isinstance(data,PyList) or isinstance(data,PyTuple): data = field(data) from java.lang.Math import abs x=[] y=[] h = hist(data, [0], [bins]) dom = getDomain(h) d = dom.getSamples() step2 = dom.getStep()/2 hmin = h[0].getValue() hmax = hmin for i in range(0,len(h)): hval = h[i].getValue() if hval < hmin: hmin = hval if hval > hmax: hmax = hval for i in range(0,len(h)): xm = d[0][i]-step2 xp = d[0][i]+step2 x.append(xm) y.append(hmin) x.append(xm) hval = h[i].getValue() y.append(hval) x.append(xp) y.append(hval) x.append(xp) y.append(hmin) domt = domainType(h) rngt = rangeType(h) xaxis = ScalarMap(domt[0], Display.XAxis) yaxis = ScalarMap(rngt, Display.YAxis) yaxis.setRange(hmin, hmax + abs(hmax * .05)) disp = subs.makeDisplay( (xaxis, yaxis) ) subs.drawLine(disp, (x,y), mathtype=(domt[0],rngt), color=color) showAxesScales(disp,1) subs.setBoxSize(disp,.65,clip) subs.setAspectRatio(disp, float(width)/float(height)) subs.showDisplay(disp,width,height,title,bottom,top,panel) return disp
from test_support import * print_test('Basic Java Integration (test_jbasic.py)', 1) print_test('type conversions', 2) print_test('numbers', 3) from java.lang.Math import abs assert abs(-2.) == 2., 'Python float to Java double' assert abs(-2) == 2l, 'Python int to Java long' assert abs(-2l) == 2l, 'Python long to Java long' try: abs(-123456789123456789123l) except TypeError: pass print_test('strings', 3) from java.lang import Integer, String assert Integer.valueOf('42') == 42, 'Python string to Java string' print_test('arrays', 3) chars = ['a', 'b', 'c'] assert String.valueOf(chars) == 'abc', 'char array' print_test('Enumerations', 3) from java.util import Vector vec = Vector() items = range(10)
from test_support import * print 'Basic Java Integration (test_jbasic.py)' print 'type conversions' print 'numbers' from java.lang.Math import abs assert abs(-2.) == 2., 'Python float to Java double' assert abs(-2) == 2l, 'Python int to Java long' assert abs(-2l) == 2l, 'Python long to Java long' try: abs(-123456789123456789123l) except TypeError: pass print 'strings' from java.lang import Integer, String assert Integer.valueOf('42') == 42, 'Python string to Java string' print 'arrays' chars = ['a', 'b', 'c'] assert String.valueOf(chars) == 'abc', 'char array' print 'Enumerations' from java.util import Vector vec = Vector() items = range(10) for i in items: vec.addElement(i)
def histogram(data, bins=20, width=400, height=400, title="VisAD Histogram", color=None, bottom=None, top=None, panel=None, clip=1): """ Quick plot of a histogram from <data>. <bins> is the number of bins to use (def = 20), <panel> is the name of a panel to put this into (default= make a new one), <color> is the color to use, <width> and <height> are the dimensions, <title> is the phrase for the title bar. Returns a reference to the display. """ if isinstance(data, PyList) or isinstance(data, PyTuple): data = field(data) from java.lang.Math import abs x = [] y = [] h = hist(data, [0], [bins]) dom = getDomain(h) d = dom.getSamples() step2 = dom.getStep() / 2 hmin = h[0].getValue() hmax = hmin for i in range(0, len(h)): hval = h[i].getValue() if hval < hmin: hmin = hval if hval > hmax: hmax = hval for i in range(0, len(h)): xm = d[0][i] - step2 xp = d[0][i] + step2 x.append(xm) y.append(hmin) x.append(xm) hval = h[i].getValue() y.append(hval) x.append(xp) y.append(hval) x.append(xp) y.append(hmin) domt = domainType(h) rngt = rangeType(h) xaxis = ScalarMap(domt[0], Display.XAxis) yaxis = ScalarMap(rngt, Display.YAxis) yaxis.setRange(hmin, hmax + abs(hmax * .05)) disp = subs.makeDisplay((xaxis, yaxis)) subs.drawLine(disp, (x, y), mathtype=(domt[0], rngt), color=color) showAxesScales(disp, 1) subs.setBoxSize(disp, .65, clip) subs.setAspectRatio(disp, float(width) / float(height)) subs.showDisplay(disp, width, height, title, bottom, top, panel) return disp
def mapimage(imagedata, mapfile="outlsupw", panel=None, colortable=None, width=400, height=400, lat=None, lon=None, title="VisAD Image and Map"): """ Display an image with a basemap. <imagedata> is the image object, <mapfile> is the name of the map file to use (def = outlsupw). <panel> is the name of a panel to put this into (default= make a new one), <colortable> is a color table to use (def = gray scale), <width> and <height> are the dimensions. <lat> and <lon> are lists/tuples of the range (min->max) of the domain (def = compute them). <title> is the phrase for the title bar. Returns a reference to the display. """ rng = RealType.getRealType(rangeType(imagedata, 0)) rngMap = ScalarMap(rng, Display.RGB) xMap = ScalarMap(RealType.Longitude, Display.XAxis) yMap = ScalarMap(RealType.Latitude, Display.YAxis) maps = (xMap, yMap, rngMap) dom = getDomain(imagedata) xc = dom.getX() yc = dom.getY() xl = len(xc) yl = len(yc) if xl > 1024 or yl > 1024: print "Resampling image from", yl, "x", xl, "to", min(yl, 1024), "x", min( xl, 1024) imagedata = resample( imagedata, makeDomain(dom.getType(), xc.getFirst(), xc.getLast(), min(xl, 1024), yc.getFirst(), yc.getLast(), min(yl, 1024))) if lat is None or lon is None: c = dom.getCoordinateSystem() ll = c.toReference(((0, 0, xl, xl), (0, yl, 0, yl))) import java.lang.Double.NaN as missing if (min(ll[0]) == missing) or (min(ll[1]) == missing) or (min( ll[1]) == max(ll[1])) or (min(ll[0]) == max(ll[0])): # compute delta from mid-point...as an estimate xl2 = xl / 2.0 yl2 = yl / 2.0 ll2 = c.toReference(((xl2, xl2, xl2, xl2 - 10, xl2 + 10), (yl2, yl2 - 10, yl2 + 10, yl2, yl2))) dlon = abs((ll2[1][4] - ll2[1][3]) * xl / 40.) + abs( (ll2[0][4] - ll2[0][3]) * yl / 40.) dlat = abs((ll2[0][2] - ll2[0][1]) * yl / 40.) + abs( (ll2[1][2] - ll2[1][1]) * xl / 40.) lonmin = max(-180., min(ll2[1][0] - dlon, min(ll[1]))) lonmax = min(360., max(ll2[1][0] + dlon, max(ll[1]))) latmin = max(-90., min(ll2[0][0] - dlat, min(ll[0]))) latmax = min(90., max(ll2[0][0] + dlat, min(ll[0]))) xMap.setRange(lonmin, lonmax) yMap.setRange(latmin, latmax) print "computed lat/lon bounds=", latmin, latmax, lonmin, lonmax else: xMap.setRange(min(ll[1]), max(ll[1])) yMap.setRange(min(ll[0]), max(ll[0])) else: yMap.setRange(lat[0], lat[1]) xMap.setRange(lon[0], lon[1]) disp = subs.makeDisplay(maps) if colortable is None: # make a gray-scale table gray = [] for i in range(0, 255): gray.append(float(i) / 255.) colortable = (gray, gray, gray) rngMap.getControl().setTable(colortable) mapdata = load(mapfile) drm = subs.addData("basemap", mapdata, disp) dr = subs.addData("addeimage", imagedata, disp) subs.setBoxSize(disp, .80, clip=1) subs.setAspectRatio(disp, float(width) / float(height)) subs.showDisplay(disp, width, height, title, None, None, panel) return disp
def mapimage(imagedata, mapfile="outlsupw", panel=None, colortable=None, width=400, height=400, lat=None, lon=None, title="VisAD Image and Map"): """ Display an image with a basemap. <imagedata> is the image object, <mapfile> is the name of the map file to use (def = outlsupw). <panel> is the name of a panel to put this into (default= make a new one), <colortable> is a color table to use (def = gray scale), <width> and <height> are the dimensions. <lat> and <lon> are lists/tuples of the range (min->max) of the domain (def = compute them). <title> is the phrase for the title bar. Returns a reference to the display. """ rng = RealType.getRealType(rangeType(imagedata,0)) rngMap = ScalarMap(rng, Display.RGB) xMap = ScalarMap(RealType.Longitude, Display.XAxis) yMap = ScalarMap(RealType.Latitude, Display.YAxis) maps = (xMap, yMap, rngMap) dom = getDomain(imagedata) xc = dom.getX() yc = dom.getY() xl = len(xc) yl = len(yc) if xl > 1024 or yl > 1024: print "Resampling image from",yl,"x",xl,"to",min(yl,1024),"x",min(xl,1024) imagedata = resample(imagedata, makeDomain(dom.getType(), xc.getFirst(), xc.getLast(), min(xl, 1024), yc.getFirst(), yc.getLast(), min(yl, 1024) ) ) if lat is None or lon is None: c=dom.getCoordinateSystem() ll = c.toReference( ( (0,0,xl,xl),(0,yl,0,yl) ) ) import java.lang.Double.NaN as missing if (min(ll[0]) == missing) or (min(ll[1]) == missing) or (min(ll[1]) == max(ll[1])) or (min(ll[0]) == max(ll[0])): # compute delta from mid-point...as an estimate xl2 = xl/2.0 yl2 = yl/2.0 ll2 = c.toReference( ( (xl2,xl2,xl2,xl2-10, xl2+10),(yl2,yl2-10,yl2+10,yl2,yl2))) dlon = abs((ll2[1][4] - ll2[1][3])*xl/40.) + abs((ll2[0][4] - ll2[0][3])*yl/40.) dlat = abs((ll2[0][2] - ll2[0][1])*yl/40.) + abs((ll2[1][2] - ll2[1][1])*xl/40.) lonmin = max( -180., min(ll2[1][0] - dlon, min(ll[1]))) lonmax = min( 360., max(ll2[1][0] + dlon, max(ll[1]))) latmin = max(-90., min(ll2[0][0] - dlat, min(ll[0]))) latmax = min(90., max(ll2[0][0] + dlat, min(ll[0]))) xMap.setRange(lonmin, lonmax) yMap.setRange(latmin, latmax) print "computed lat/lon bounds=",latmin,latmax,lonmin,lonmax else: xMap.setRange(min(ll[1]), max(ll[1])) yMap.setRange(min(ll[0]), max(ll[0])) else: yMap.setRange(lat[0], lat[1]) xMap.setRange(lon[0], lon[1]) disp = subs.makeDisplay(maps) if colortable is None: # make a gray-scale table gray = [] for i in range(0,255): gray.append( float(i)/255.) colortable = (gray, gray, gray) rngMap.getControl().setTable(colortable) mapdata = load(mapfile) drm = subs.addData("basemap", mapdata, disp) dr=subs.addData("addeimage", imagedata, disp) subs.setBoxSize(disp, .80, clip=1) subs.setAspectRatio(disp, float(width)/float(height)) subs.showDisplay(disp,width,height,title,None,None,panel) return disp