/
marker.py
executable file
·517 lines (470 loc) · 19.5 KB
/
marker.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# Adapted for numpy/ma/cdms2 by convertcdms.py
# Marker (Tm) module
###############################################################################
# #
# Module: marker (Tm) module #
# #
# Copyright: 2000, Regents of the University of California #
# This software may not be distributed to others without #
# permission of the author. #
# #
# Author: PCMDI Software Team #
# Lawrence Livermore NationalLaboratory: #
# support@pcmdi.llnl.gov #
# #
# Description: Python command/1d
# wrapper for VCS's marker secondary object. #
# #
# Version: 4.0 #
# #
###############################################################################
#
#
#
import VCS_validation_functions
import vcs
import genutil
def process_src(nm, code):
"""Takes VCS script code (string) as input and generates oneD gm from it"""
try:
gm = Tm(nm)
except:
gm = vcs.elements["marker"][nm]
# process attributes with = as assignement
for att in ["projection", ]:
i = code.find(att)
if i == -1:
continue
j = code[i:].find(",") + i
if j - i == -1: # last one no comma
j = None
scode = code[i:j]
sp = scode.split("=")
nm = sp[0].strip()
nm = nm.replace("#", "")
try:
# int will be converted
setattr(gm, nm, int(sp[1]))
except:
try:
# int and floats will be converted
setattr(gm, nm, eval(sp[1]))
except:
# strings
try:
setattr(gm, nm, sp[1])
except:
pass # oh well we stick to default value
# Datawc
for att in ["mtyp", "msize", "mci", "vp", "wc"]:
i = code.find(" %s(" % att)
if i > -1:
j = code[i:].find(")") + i
cd = code[i + len(att) + 2:j]
vals = cd.split(",")
values = []
# print "ATT:",att,vals
for v in vals:
try: # int first
values.append(int(v))
except:
try:
values.append(float(v))
except:
values.append(v)
try:
if att == "mtyp":
gm.type = values
elif att == "msize":
gm.size = values
elif att == "mci":
gm.color = values
elif att == "vp":
gm.viewport = values
elif att == "wc":
gm.worldcoordinate = values
else:
raise Exception("Unkwnow marker attribute: %s" % att)
except:
pass
for att in ["x", "y"]:
i = code.find(" %s(" % att)
if i == -1:
i = code.find(",%s(" % att)
if i > -1:
v = genutil.get_parenthesis_content(code[i:])
setattr(gm, att, eval(v))
#############################################################################
# #
# Marker (Tm) Class. #
# #
#############################################################################
class Tm(object):
"""
Class: Tm # Marker
Description of Tm Class:
The Marker object allows the manipulation of marker type, size, and color index.
This class is used to define an marker table entry used in VCS, or it
can be used to change some or all of the marker attributes in an
existing marker table entry.
Other Useful Functions:
a=vcs.init() # Constructor
a.show('marker') # Show predefined marker objects
a.update() # Updates the VCS Canvas at user's request
a.mode=1, or 0 # If 1, then automatic update, else if
0, then use update function to
update the VCS Canvas.
Example of Use:
a=vcs.init()
To Create a new instance of marker use:
mk=a.createmarker('new','red') # Copies content of 'red' to 'new'
mk=a.createmarker('new') # Copies content of 'default' to 'new'
To Modify an existing marker use:
mk=a.getmarker('red')
mk.list() # Will list all the marker attribute values
mk.color=100 # Range from 1 to 256
mk.size=100 # Range from 1 to 300
Specify the marker type:
mk.type='dot' # Same as mk.type=1
mk.type='plus' # Same as mk.type=2
mk.type='star' # Same as mk.type=3
mk.type='circle' # Same as mk.type=4
mk.type='cross' # Same as mk.type=5
mk.type='diamond' # Same as mk.type=6
mk.type='triangle_up' # Same as mk.type=7
mk.type='triangle_down' # Same as mk.type=8
mk.type='triangle_left' # Same as mk.type=9
mk.type='triangle_right' # Same as mk.type=10
mk.type='square' # Same as mk.type=11
mk.type='diamond_fill' # Same as mk.type=12
mk.type='triangle_up_fill' # Same as mk.type=13
mk.type='triangle_down_fill' # Same as mk.type=14
mk.type='triangle_left_fill' # Same as mk.type=15
mk.type='triangle_right_fill' # Same as mk.type=16
mk.type='square_fill' # Same as mk.type=17
mk.priority=1 # Set the graphics priority on the canvas
mk.viewport=[0, 1.0, 0,1.0] # FloatType [0,1]x[0,1]
mk.worldcoordinate=[0,1.0,0,1.0] # FloatType [#,#]x[#,#]
mk.x=[[0,.1,.2], [.3,.4,.5]] # List of FloatTypes
mk.y=[[.5,.4,.3], [.2,.1,0]] # List of FloatTypes
"""
__slots__ = [
's_name',
'name',
'color',
'priority',
'type',
'size',
'viewport',
'worldcoordinate',
'x',
'y',
'colormap',
'_name',
'_color',
'_priority',
'_type',
'_size',
'_viewport',
'_worldcoordinate',
'_x',
'_y',
'_projection',
'_colormap',
]
colormap = VCS_validation_functions.colormap
def _getname(self):
return self._name
def _setname(self, value):
value = VCS_validation_functions.checkname(self, 'name', value)
if value is not None:
self._name = value
name = property(_getname, _setname)
def _getfillareacolors(self):
return self._color
def _setfillareacolors(self, value):
if isinstance(value, int):
value = [value, ]
if value is not None:
value = VCS_validation_functions.checkColorList(
self,
'color',
value)
self._color = value
color = property(_getfillareacolors, _setfillareacolors)
def _gettype(self):
return self._type
def _settype(self, value):
if not isinstance(value, (list, tuple)) and value is not None:
value = [value, ]
if value is not None:
value = VCS_validation_functions.checkMarkersList(
self,
'type',
value)
self._type = value
type = property(_gettype, _settype)
def _getsize(self):
return self._size
def _setsize(self, value):
if VCS_validation_functions.isNumber(value):
value = [value, ]
if value is not None:
value = VCS_validation_functions.checkListOfNumbers(
self,
'size',
value,
minvalue=0,
maxvalue=300)
self._size = value
size = property(_getsize, _setsize)
def _getpriority(self):
return self._priority
def _setpriority(self, value):
value = VCS_validation_functions.checkInt(
self,
'priority',
value,
minvalue=0)
self._priority = value
priority = property(_getpriority, _setpriority)
def _getprojection(self):
return self._projection
def _setprojection(self, value):
value = VCS_validation_functions.checkProjection(
self,
'projection',
value)
self._projection = value
projection = property(_getprojection, _setprojection)
def _getwc(self):
return self._worldcoordinate
def _setwc(self, value):
value = VCS_validation_functions.checkListOfNumbers(
self,
'worldcoordinate',
value,
maxelements=4)
self._worldcoordinate = value
worldcoordinate = property(_getwc, _setwc)
def _getvp(self):
return self._viewport
def _setvp(self, value):
value = VCS_validation_functions.checkListOfNumbers(
self,
'viewport',
value,
maxelements=4,
minvalue=0.,
maxvalue=1.)
self._viewport = value
viewport = property(_getvp, _setvp)
def _getx(self):
return self._x
def _setx(self, value):
if value is None:
self._x = None
return
if not isinstance(value, (list, tuple)):
raise ValueError(
'x must be a tuple or list of values. You sent: %s' %
value)
try:
# first we'll see if it is simply a list of values
value = VCS_validation_functions.checkListOfNumbers(
self,
'x',
value)
except:
# ok it was not, so it maybe a list of list of numbers ?
val = []
for v in value:
tmp = VCS_validation_functions.checkListOfNumbers(self, 'x', v)
val.append(tmp)
value = val
# ok it worked
self._x = value
x = property(_getx, _setx)
def _gety(self):
return self._y
def _sety(self, value):
if value is None:
self._y = None
return
if not isinstance(value, (list, tuple)):
raise ValueError(
'y must be a tuple or list of values. You sent: %s' %
value)
try:
# first we'll see if it is simply a list of values
value = VCS_validation_functions.checkListOfNumbers(
self,
'y',
value)
except:
# ok it was not, so it maybe a list of list of numbers ?
val = []
for v in value:
tmp = VCS_validation_functions.checkListOfNumbers(self, 'y', v)
val.append(tmp)
value = val
# ok it worked
self._y = value
y = property(_gety, _sety)
##########################################################################
# #
# Initialize the marker attributes. #
# #
##########################################################################
def __init__(self, Tm_name, Tm_name_src='default'):
# #
###########################################################
# Initialize the marker class and its members #
# The getTmmember function retrieves the values of the #
# marker members in the C structure and passes back the #
# appropriate Python Object. #
###########################################################
# #
if (Tm_name is None):
raise ValueError('Must provide a marker name.')
self._name = Tm_name
self.s_name = 'Tm'
if Tm_name == "default":
self._type = ["dot", ]
self._size = [1.0, ]
self._color = [1, ]
self._priority = 1
self._viewport = [0., 1., 0., 1.]
self._worldcoordinate = [0., 1., 0., 1.]
self._x = None
self._y = None
self._projection = "default"
self._colormap = None
else:
if isinstance(Tm_name_src, Tm):
Tm_name_src = Tm_name_src.name
if Tm_name_src not in vcs.elements['marker']:
raise ValueError(
"The marker object '%s' does not exists" %
Tm_name_src)
src = vcs.elements["marker"][Tm_name_src]
for att in ['colormap', 'projection', 'color', 'size',
'type', 'viewport', 'worldcoordinate', 'priority', 'x', 'y']:
setattr(self, att, getattr(src, att))
# Ok now we need to stick in the elements
vcs.elements["marker"][Tm_name] = self
##########################################################################
# #
# List out marker members (attributes). #
# #
##########################################################################
def list(self):
if (self.name == '__removed_from_VCS__'):
raise ValueError('This instance has been removed from VCS.')
print "", "----------Marker (Tm) member (attribute) listings ----------"
print "secondary method =", self.s_name
print "name =", self.name
print "type =", self.type
print "size =", self.size
print "color =", self.color
print "priority =", self.priority
print "viewport =", self.viewport
print "worldcoordinate =", self.worldcoordinate
print "x =", self.x
print "y =", self.y
print "projection =", self.projection
print "colormap =", self.colormap
##########################################################################
# #
# Script out secondary marker method in VCS to a file. #
# #
##########################################################################
def script(self, script_filename=None, mode=None):
"""
Function: script # Calls _vcs.scriptTm
Description of Function:
Saves out a marker graphics method in Python or VCS script form to a
designated file.
Example of Use:
script(scriptfile_name, mode)
where: script_name is the output name of the script file.
mode is either "w" for replace or "a" for append.
Note: If the the filename has a ".py" at the end, it will produce a
Python script. If the filename has a ".scr" at the end, it will
produce a VCS script. If neither extensions are give, then by
default a Python script will be produced.
a=vcs.init()
mrk=a.createboxfill('temp')
mrk.script('filename.py') # Append to a Python file "filename.py"
mrk.script('filename.scr') # Append to a VCS file "filename.scr"
mrk.script('filename','w') # Create or overwrite to a Python file "filename.py"
"""
if (script_filename is None):
raise ValueError(
'Error - Must provide an output script file name.')
if (mode is None):
mode = 'a'
elif (mode not in ('w', 'a')):
raise ValueError(
'Error - Mode can only be "w" for replace or "a" for append.')
# By default, save file in json
scr_type = script_filename.split(".")
if len(scr_type) == 1 or len(scr_type[-1]) > 5:
scr_type = "json"
if script_filename != "initial.attributes":
script_filename += ".json"
else:
scr_type = scr_type[-1]
if scr_type == '.scr':
raise DeprecationWarning("scr script are no longer generated")
elif scr_type == "py":
mode = mode + '+'
py_type = script_filename[
len(script_filename) -
3:len(script_filename)]
if (py_type != '.py'):
script_filename = script_filename + '.py'
# Write to file
fp = open(script_filename, mode)
if (fp.tell() == 0): # Must be a new file, so include below
fp.write("#####################################\n")
fp.write("# #\n")
fp.write("# Import and Initialize VCS #\n")
fp.write("# #\n")
fp.write("#############################\n")
fp.write("import vcs\n")
fp.write("v=vcs.init()\n\n")
unique_name = '__Tm__' + self.name
fp.write(
"#----------Marker (Tm) member (attribute) listings ----------\n")
fp.write("tm_list=v.listelements('marker')\n")
fp.write("if ('%s' in tm_list):\n" % self.name)
fp.write(" %s = v.getmarker('%s')\n" % (unique_name, self.name))
fp.write("else:\n")
fp.write(
" %s = v.createmarker('%s')\n" %
(unique_name, self.name))
fp.write("%s.type = %s\n" % (unique_name, self.type))
fp.write("%s.size = %s\n" % (unique_name, self.size))
fp.write("%s.color = %s\n\n" % (unique_name, self.color))
fp.write("%s.priority = %d\n" % (unique_name, self.priority))
fp.write("%s.viewport = %s\n" % (unique_name, self.viewport))
fp.write(
"%s.worldcoordinate = %s\n" %
(unique_name, self.worldcoordinate))
fp.write("%s.x = %s\n" % (unique_name, self.x))
fp.write("%s.y = %s\n" % (unique_name, self.y))
fp.write("%s.projection = %s\n" % (unique_name, self.projection))
fp.write(
"%s.colormap = '%s'\n\n" %
(unique_name, repr(
self.colormap)))
else:
# Json type
mode += "+"
f = open(script_filename, mode)
vcs.utils.dumpToJson(self, f)
f.close()
##########################################################################
# END OF FILE #
##########################################################################