/
jythonrc.py
302 lines (278 loc) · 11.5 KB
/
jythonrc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import sys
import os
idvjar=os.path.join(os.environ['IDV_HOME'],"idv.jar")
visadjar=os.path.join(os.environ['IDV_HOME'],"visad.jar")
sys.packageManager.addJar(idvjar,True)
sys.packageManager.addJar(visadjar,True)
sys.add_package('visad')
sys.add_package('visad.python')
sys.add_package('visad.data.units')
sys.add_package('ucar')
sys.add_package('ucar.unidata.idv')
sys.add_package('ucar.unidata.idv')
sys.add_package('ucar.unidata.ui')
import java
import visad
import visad.python
from visad import *
from visad.python.JPythonMethods import *
import ucar
from java.util import ArrayList
#idv=ucar.unidata.idv.IntegratedDataViewer(0)
idv=ucar.unidata.idv.DefaultIdv([])
IdvUIM=idv.getIdvUIManager()
import ucar.unidata.data.grid.GridUtil as GridUtil
import ucar.unidata.data.DataSelection as DataSelection
import ucar.unidata.data.GeoLocationInfo as GeoLocationInfo
import ucar.unidata.data.GeoSelection as GeoSelection
from ucar.unidata.idv.ui import ImageGenerator
from ucar.unidata.data import *
from ucar.unidata.idv import *
from ucar.unidata.data.grid import *
from java.lang import Integer
import ucar.unidata.data.grid.GridMath as GridMath
import ucar.unidata.data.DataUtil as DataUtil
import ucar.visad.Util as Util
import ucar.unidata.util.StringUtil as StringUtil
import ucar.unidata.data.grid.DerivedGridFactory as DerivedGridFactory
import ucar.unidata.data.grid.GridTrajectory as GridTrajectory
islInterpreter=ImageGenerator(idv)
JM=idv.getJythonManager()
LibHolders=JM.getLibHolders()
for i in range(len(LibHolders)):
try:
exec(LibHolders.get(i).getText())
except:
pass
def saveFormula(formulaid,desc,formula="",group=None):
""" This function makes a IDV formula from jython script and puts it in relavent group
in IDV list of formulas """
# to do to check if function used in formula is legal/available
from ucar.unidata.data import DerivedDataDescriptor,DataCategory
from java.util import ArrayList
JM=idv.getJythonManager()
if not formulaid == None or not desc== None:
if isinstance(group,list):
for gr in group:
categories=ArrayList()
categories.add(DataCategory.parseCategory(gr,True))
JM.addFormula(DerivedDataDescriptor(idv.getIdv(),formulaid,desc,formula,categories))
else:
group=str(group)
categories=ArrayList()
categories.add(DataCategory.parseCategory(group,True))
JM.addFormula(DerivedDataDescriptor(idv.getIdv(),formulaid,desc,formula,categories))
def showImg(width=None,height=None):
""" This function shows the image from current IDV frame, optional arguments are width and height in pixels, they
currently default to 400 and 300"""
from java.util import Base64 ##only in java8
from javax.imageio import ImageIO
from java.io import ByteArrayOutputStream
from ucar.unidata.ui.ImageUtils import resize,toBufferedImage
pause()
#imgx=getImage() currently not working with new changes to idv
img=idv.getViewManager().getMaster().getImage(True)
if width != None and height != None:
img=toBufferedImage(resize(img,width,height));
bos=ByteArrayOutputStream();
ImageIO.write(img, "png", Base64.getEncoder().wrap(bos));
data = bos.toString("UTF-8");
return {"display":"image","data":data}
def showImgWithFullWindow(width=None,height=None):
""" This function shows the image from current IDV window while in GUI mode. optional arguments are width and height in pixels, they
currently default to 600 and 400"""
from java.util import Base64 ##only in java8
from javax.imageio import ImageIO
from java.io import ByteArrayOutputStream
from ucar.unidata.ui.ImageUtils import resize,toBufferedImage
import java
import java.awt.Robot as Robot
import java.awt.Rectangle as Rectangle
import java.awt.Toolkit as Toolkit
from ucar.unidata.util import Misc
VM=idv.getViewManager()
myframe=VM.getDisplayWindow().getComponent()
robotx = Robot(myframe.getGraphicsConfiguration().getDevice())
VM.toFront();
#robotx.delay(250)
Misc.sleep(350)
pause()
img=robotx.createScreenCapture(Rectangle( myframe.getX(),myframe.getY(),myframe.getWidth(),myframe.getHeight()))
if width != None and height != None:
img=toBufferedImage(resize(img,width,height));
bos=ByteArrayOutputStream();
ImageIO.write(img, "png", Base64.getEncoder().wrap(bos));
data = bos.toString("UTF-8");
return {"display":"image","data":data}
def showImgWithLegend(width=None,height=None):
""" This function shows the image and legend from current IDV window while in GUI mode. Optional arguments are width and height in pixels, they currently default to 600 and 400"""
from java.util import Base64 ##only in java8
from javax.imageio import ImageIO
from java.io import ByteArrayOutputStream
from ucar.unidata.ui.ImageUtils import resize,toBufferedImage
import java
import java.awt.Robot as Robot
import java.awt.Rectangle as Rectangle
import java.awt.Toolkit as Toolkit
from ucar.unidata.util import Misc
VM=idv.getViewManager()
VMC=VM.getContents()
VMCC=VMC.getComponent(1) # the view and legend ; 0 is left most part of view window with controls for perspective views
siz=VMCC.getSize()
loc= VMCC.getLocationOnScreen()
gc= VMCC.getGraphicsConfiguration()
loc.x -= gc.getBounds().x
loc.y -= gc.getBounds().y
robotx=Robot(gc.getDevice())
VM.toFront()
Misc.sleep(250)
img = robotx.createScreenCapture(Rectangle(loc.x, loc.y,siz.width, siz.height))
if width != None and height != None:
img=toBufferedImage(resize(img,width,height));
bos=ByteArrayOutputStream();
ImageIO.write(img, "png", Base64.getEncoder().wrap(bos));
data = bos.toString("UTF-8");
return {"display":"image","data":data}
def BufferedImgToNotebook(img):
from java.io import ByteArrayOutputStream
from java.util import Base64 ##only in java8
from javax.imageio import ImageIO
bos=ByteArrayOutputStream();
ImageIO.write(img, "png", Base64.getEncoder().wrap(bos));
data = bos.toString("UTF-8");
return {"display":"image","data":data}
def showIdv(default=False):
""" This creates a new IDV GUI window for showing after setOffScreen was True, after this GUI window of
IDV is visible to the user, reset setOffScreen to False to go back into offscreen mode."""
setOffScreen(False)
#idv=ucar.unidata.idv.IntegratedDataViewer(True)
idv=ucar.unidata.idv.DefaultIdv([])
#IdvUIM=idv.getIdvUIManager()
#IdvUIM.initDone()
#IdvUIM.closeHelpTips()
if default==True:
if os.path.isfile(os.path.join(str(idv.getResourceManager().getUserPath()),"default.xidv")):
loadBundle(os.path.join(str(idv.getResourceManager().getUserPath()),"default.xidv"))
elif not len(idv.history) == 0:
for hf in idv.history:
if str(hf.getName()).endswith(".xidv"):
loadBundle(str(hf.getName()))
break
def saveJython(func=None,libname=None):
""" This function saves history of interactive session to IDV Jython Library.
When supplied by a defined class/function argument saves the code relavent to that class/function only
When supplied by second string argument it creates library from that name or write to exisiting library by that
name, only if it is editable by current user."""
try:
from org.python.core import PyFunction
from org.python.core import PyClass
from ucar.unidata.util import IOUtil
import os
import readline
from random import randint
pythonDir = IOUtil.joinDir(idv.getStore().getUserDirectory().toString(),"python")
if (libname == None):
libname="temp_"+str(randint(1000,9999))
fullname = IOUtil.cleanFileName(libname)
if not fullname.endswith(".py"):
fullname = fullname + ".py"
fullname = IOUtil.joinDir(pythonDir,fullname) # this takes care of type of slash based on os
if os.path.exists(str(fullname)):
raise Exception("A file with library name "+str(libname)+" already exists")
if func==None:
readline.write_history_file(str(fullname))
elif isinstance(func,PyFunction) or isinstance(func,PyClass): #can get rid of this else if pass is used above
readline.write_history_file("temp.txt")
funcname=func.__name__
## ad-hoc matching use regex for robust matching
if isinstance(func,PyFunction):
funcname="def "+funcname+"("
elif isinstance(func,PyClass):
funcname="class "+funcname+"("
fread=open("temp.txt").read()
fnameindx=fread.rindex(funcname)
#fnameindx=fread[:fnameindx].rindex('def')
prevIndt=None
functxt=[]
for ln,line in enumerate(fread[fnameindx:].splitlines()):
currIndt=len(line)-len(line.lstrip())
if prevIndt<=currIndt or prevIndt==None:
functxt.append(line)
else:
break
prevIndt=currIndt
f=open(str(fullname),"w")
f.write('\n'.join(functxt))
f.close()
else:
raise Exception("Unknown Error saving to IDV Jython library")
except Exception as exc:
return "Could not create a IDV Jython library: ",exc
idvBaseUrl=None
region=None
port=idv.getProperty('idv.monitorport','')
def pingIdv():
import urllib
global idvBaseUrl
global port
global region
try:
idvBaseUrl="http://localhost:"+port
return urllib.urlopen(idvBaseurl+"/ping").read();
except IOError:
idvBaseUrl="http://127.0.0.1:"+port
return urllib.urlopen(idvbaseurl+"/ping").read();
finally:
return None
def runIsl(islcode):
import urllib
global idvBaseUrl
pingIdv()
if idvBaseUrl==None:
return "Error connot connect to the IDV check your IDV version, only works for IDV > 5.3u1"
else:
urllib.urlopen(idvBaseUrl+"/loadisl?"+urllib.urlencode({'isl':islcode})).read()
#return None
def loadBundleBB(fileName,north,south,east,west):
isl=str('<isl><bundle file="'+str(fileName)+'" /><pause /><center north="'+str(north)+'" west="'+str(west)+'" south="'+str(south)+'" east="'+str(east)+'" /></isl>')
runIsl(isl)
idv.getDataManager().reloadAllDataSources()
def do_complete(text):
"""Does code completion"""
import __main__
import __builtin__
import readline
import rlcompleter
matchDict=__main__.__dict__.copy()
matchDict.update(__builtin__.globals())
rlc=rlcompleter.Completer(matchDict)
rlc.complete(text,0)
return list(set(rlc.matches)) #just to get rid of duplicates
def do_inspect(text):
""" returns help like object """
import pydoc
try:
token=text.split()[-1]
if '(' in token: #better do regex
token=token[:-1]
obj=eval(token)
ins=pydoc.plain(pydoc.render_doc(obj))
return ins
except NameError:
pass
def docHTML(text):
""" just testing """
import pydoc
try:
token=text.split()[-1]
if '(' in token: #better do regex
token=token[:-1]
obj=eval(token)
#pyobj,name=pydoc.resolve(obj,0)
ins=pydoc.plain(pydoc.render_doc(obj))
html=pydoc.HTMLDoc()
return html.page(pydoc.describe(obj), html.document(obj, name))
except NameError:
pass
def help(text):
return "Use of help from notebook is currently unsupported, use SHIFT+TAB at end of the object you need help."