Commits

Ville Suoranta authored 5503fd0f8b1 Merge
Merge pull request #11 in CASA/casaviewer from CAS-13018 to master

* commit 'aa2d3515a0782631ec2fc9e0e25aff41d913f310': consistent error (except) handling, task+test plotms ->CASA6, CAS-13018

src/module/private/task_imview.py

Modified
125 125 self.__set_axes( panel, axes )
126 126 self.__zoom( panel, zoom )
127 127 self.__process_colorwedges( panel )
128 128
129 129 if not gui:
130 130 viewertool.output(out_file,scale=out_scale,dpi=out_dpi,format=out_format,orientation=out_orientation,panel=panel)
131 131 viewertool.close(panel)
132 132
133 133 self.__popd( )
134 134
135 - return None
136 -
137 135 def __panel( self, gui ):
138 136 panel = viewertool.panel("viewer",gui)
139 137 return panel
140 138
141 139 def __load_raster( self, gui, panel, raster ):
142 140 ## here we can assume we have a dictionary
143 141 ## that specifies what needs to be done...
144 142 data = None
145 143 if not 'file' in raster:
146 144 return panel
147 145
148 146 if type(raster['file']) != str or not os.path.exists(raster['file']) or \
149 147 viewertool.fileinfo(raster['file'],gui) != 'image':
150 148 casalog.post( str(raster['file']) + " does not exist or is not an image", 'SEVERE')
151 - raise Exception(raster['file'] + " does not exist or is not an image")
149 + raise RuntimeError(raster['file'] + " does not exist or is not an image")
152 150
153 151 if panel is None:
154 152 panel = self.__panel(gui)
155 153
156 154 scaling = 0.0
157 155 if 'scaling' in raster:
158 156 scaling = self.__checknumeric(raster['scaling'], float, "raster scaling")
159 157
160 158
161 159 data = viewertool.load( raster['file'], 'raster', panel=panel, scaling=scaling )
162 160
163 161 if 'range' in raster:
164 162 viewertool.datarange( self.__checknumeric(raster['range'], float, "data range", array_size=2), data=data )
165 163
166 164 if 'colormap' in raster:
167 165 if type(raster['colormap']) == str:
168 166 viewertool.colormap( raster['colormap'], data )
169 167 else:
170 168 casalog.post( "raster colormap must be a string", 'SEVERE')
171 - raise Exception("raster colormap must be a string")
169 + raise RuntimeError("raster colormap must be a string")
172 170
173 171 if 'colorwedge' in raster:
174 172 if type(raster['colorwedge']) == bool:
175 173 self.__colorwedge_queue.append( (data,raster['colorwedge']) )
176 174 else:
177 175 casalog.post( "colorwedge must be a boolean", 'SEVERE')
178 - raise Exception("colorwedge must be a boolean")
176 + raise RuntimeError("colorwedge must be a boolean")
179 177
180 178 return panel
181 179
182 180 def __process_colorwedges( self, panel ):
183 181 self.__colorwedge_queue.reverse( )
184 182 while len(self.__colorwedge_queue) > 0:
185 183 element = self.__colorwedge_queue.pop( )
186 184 viewertool.colorwedge( element[1], element[0] )
187 185
188 186 def __load_contour( self, gui, panel, contour ):
189 187 ## here we can assume we have a dictionary
190 188 ## that specifies what needs to be done...
191 189 data = None
192 190 if not 'file' in contour:
193 191 return panel
194 192
195 193 if type(contour['file']) != str or not os.path.exists(contour['file']) or \
196 194 viewertool.fileinfo(contour['file'],gui) != 'image':
197 195 casalog.post( str(contour['file']) + " does not exist or is not an image", 'SEVERE')
198 - raise Exception(contour['file'] + " does not exist or is not an image")
196 + raise RuntimeError(contour['file'] + " does not exist or is not an image")
199 197
200 198 if panel is None:
201 199 panel = self.__panel(gui)
202 200
203 201 data = viewertool.load( contour['file'], 'contour', panel=panel )
204 202
205 203 if 'levels' in contour:
206 204 viewertool.contourlevels( self.__checknumeric(contour['levels'], float, "contour levels", array_size=0), data=data )
207 205 if 'unit' in contour:
208 206 viewertool.contourlevels( unitlevel=self.__checknumeric(contour['unit'], float, "contour unitlevel"), data=data )
227 225 if type(axes) == list and len(axes) == 3 and \
228 226 all( map( lambda x: type(x) == str, axes ) ) :
229 227 x = axes[0]
230 228 y = axes[1]
231 229 z = axes[2]
232 230 invoke = True
233 231 elif type(axes) == dict :
234 232 if 'x' in axes:
235 233 if type(axes['x']) != str:
236 234 casalog.post( "dimensions of axes must be strings (x is not)", 'SEVERE')
237 - raise Exception("dimensions of axes must be strings (x is not)")
235 + raise RuntimeError("dimensions of axes must be strings (x is not)")
238 236 x = axes['x']
239 237 invoke = True
240 238 if 'y' in axes:
241 239 if type(axes['y']) != str:
242 240 casalog.post( "dimensions of axes must be strings (y is not)", 'SEVERE')
243 - raise Exception("dimensions of axes must be strings (y is not)")
241 + raise RuntimeError("dimensions of axes must be strings (y is not)")
244 242 y = axes['y']
245 243 invoke = True
246 244 if 'z' in axes:
247 245 if type(axes['z']) != str:
248 246 casalog.post( "dimensions of axes must be strings (z is not)", 'SEVERE')
249 - raise Exception("dimensions of axes must be strings (z is not)")
247 + raise RuntimeError("dimensions of axes must be strings (z is not)")
250 248 z = axes['z']
251 249 invoke = True
252 250 else :
253 251 casalog.post( "'axes' must either be a string list of 3 dimensions or a dictionary", 'SEVERE')
254 - raise Exception("'axes' must either be a string list of 3 dimensions or a dictionary")
252 + raise RuntimeError("'axes' must either be a string list of 3 dimensions or a dictionary")
255 253
256 254 result = False
257 255 if invoke:
258 256 viewertool.axes( x, y, z, panel=panel )
259 257 result = True
260 258
261 259 return result
262 260
263 261
264 262 def __zoom( self, panel, zoom ) :
275 273 blc = zoom['blc']
276 274 trc = zoom['trc']
277 275 if type(blc) == list and type(trc) == list:
278 276 blc = self.__checknumeric( blc, float, "zoom blc", array_size=2 )
279 277 trc = self.__checknumeric( trc, float, "zoom trc", array_size=2 )
280 278
281 279 coord = "pixel"
282 280 if 'coordinates' in zoom:
283 281 if 'coord' in zoom:
284 282 casalog.post( "cannot specify both 'coord' and 'coordinates' for zoom", 'SEVERE')
285 - raise Exception("cannot specify both 'coord' and 'coordinates' for zoom")
283 + raise RuntimeError("cannot specify both 'coord' and 'coordinates' for zoom")
286 284 if type(zoom['coordinates']) != str:
287 285 casalog.post( "zoom coordinates must be a string", 'SEVERE')
288 - raise Exception("zoom coordinates must be a string")
286 + raise RuntimeError("zoom coordinates must be a string")
289 287 coord = zoom['coordinates']
290 288 if coord != 'world' and coord != 'pixel' :
291 289 casalog.post( "zoom coordinates must be either 'world' or 'pixel'", 'SEVERE')
292 - raise Exception("zoom coordinates must be either 'world' or 'pixel'")
290 + raise RuntimeError("zoom coordinates must be either 'world' or 'pixel'")
293 291 elif 'coord' in zoom:
294 292 if type(zoom['coord']) != str:
295 293 casalog.post( "zoom coord must be a string", 'SEVERE')
296 - raise Exception("zoom coord must be a string")
294 + raise RuntimeError("zoom coord must be a string")
297 295 coord = zoom['coord']
298 296 if coord != 'world' and coord != 'pixel' :
299 297 casalog.post( "zoom coord must be either 'world' or 'pixel'", 'SEVERE')
300 - raise Exception("zoom coord must be either 'world' or 'pixel'")
298 + raise RuntimeError("zoom coord must be either 'world' or 'pixel'")
301 299 if channel >= 0:
302 300 viewertool.channel( channel, panel=panel )
303 301 viewertool.zoom(blc=blc,trc=trc, coordinates=coord, panel=panel)
304 302 elif type(blc) == dict and type(trc) == dict and \
305 303 '*1' in blc.has_key( ) and '*1' in trc:
306 304 if channel >= 0:
307 305 viewertool.channel( channel, panel=panel )
308 306 viewertool.zoom(region=zoom,panel=panel)
309 307 else:
310 308 casalog.post( "zoom blc & trc must be either lists or dictionaries", 'SEVERE')
311 - raise Exception("zoom blc & trc must be either lists or dictionaries")
309 + raise RuntimeError("zoom blc & trc must be either lists or dictionaries")
312 310
313 311 elif type(zoom) == dict and 'regions' in zoom:
314 312 if channel >= 0:
315 313 viewertool.channel( channel, panel=panel )
316 314 viewertool.zoom(region=zoom,panel=panel)
317 315 elif type(zoom) == dict and 'file' in zoom and type(zoom['file']) == str and os.path.isfile( zoom['file'] ):
318 316 if channel >= 0:
319 317 viewertool.channel( channel, panel=panel )
320 318 viewertool.zoom(region=zoom['file'],panel=panel)
321 319 else:
322 320 if channel < 0:
323 321 casalog.post( "invalid zoom parameters", 'SEVERE')
324 - raise Exception("invalid zoom parameters")
322 + raise RuntimeError("invalid zoom parameters")
325 323 else:
326 324 viewertool.channel( channel, panel=panel )
327 325 viewertool.show(panel=panel)
328 326
329 327 def __load_files( self, filetype, gui, panel, files ):
330 328
331 329 if filetype != "raster" and filetype != "contour":
332 330 casalog.post( "internal error __load_files( )...", 'SEVERE')
333 - raise Exception("internal error __load_files( )...")
331 + raise RuntimeError("internal error __load_files( )...")
334 332
335 333 if type(files) == str:
336 334 panel = self.__load_raster( gui, panel, { 'file': files } ) if filetype == 'raster' else \
337 335 self.__load_contour( gui, panel, { 'file': files } )
338 336 elif type(files) == dict:
339 337 panel = self.__load_raster( gui, panel, files ) if filetype == 'raster' else \
340 338 self.__load_contour( gui, panel, files )
341 339 elif type(files) == list:
342 340 if all(map( lambda x: type(x) == dict, files )):
343 341 for f in files:
344 342 panel = self.__load_raster( gui, panel, f ) if filetype == 'raster' else \
345 343 self.__load_contour( gui, panel, f )
346 344 elif all(map( lambda x: type(x) == str, files )):
347 345 for f in files:
348 346 panel = self.__load_raster( gui, panel, { 'file': f } ) if filetype == 'raster' else \
349 347 self.__load_contour( gui, panel, { 'file': f } )
350 348 else:
351 349 casalog.post( "multiple " + str(filetype) + " specifications must be either all dictionaries or all strings", 'SEVERE')
352 - raise Exception("multiple " + filetype + " specifications must be either all dictionaries or all strings")
350 + raise RuntimeError("multiple " + filetype + " specifications must be either all dictionaries or all strings")
353 351 else:
354 352 casalog.post( filetype + "s can be a single file path (string), a single specification (dictionary), or a list containing all strings or all dictionaries", 'SEVERE')
355 - raise Exception(filetype + "s can be a single file path (string), a single specification (dictionary), or a list containing all strings or all dictionaries")
353 + raise RuntimeError(filetype + "s can be a single file path (string), a single specification (dictionary), or a list containing all strings or all dictionaries")
356 354 return panel
357 355
358 356
359 357 def __extract_outputinfo( self, out ):
360 358 output_file=None
361 359 output_format=None
362 360 output_scale=1.0
363 361 output_dpi=300
364 362 output_orientation="portrait"
365 363
366 364 if type(out) == str:
367 365 output_format = self.__check_filename(out)
368 366 output_file = out
369 367
370 368 elif type(out) == dict:
371 369 if 'file' in out:
372 370 if type(out['file']) != str:
373 371 casalog.post( "output filename must be a string", 'SEVERE')
374 - raise Exception("output filename must be a string")
372 + raise RuntimeError("output filename must be a string")
375 373 if 'format' in out:
376 374 if type(out['format']) != str:
377 375 casalog.post( "output format must be a string", 'SEVERE')
378 - raise Exception("output format must be a string")
376 + raise RuntimeError("output format must be a string")
379 377 output_format = self.__check_fileformat( out['format'] )
380 378 self.__check_filename( out['file'], False )
381 379 else:
382 380 output_format = self.__check_filename( out['file'] )
383 381
384 382 output_file = out['file']
385 383
386 384 else:
387 385 casalog.post( "an output dictionary must include a 'file' field", 'SEVERE')
388 - raise Exception("an output dictionary must include a 'file' field")
386 + raise RuntimeError("an output dictionary must include a 'file' field")
389 387
390 388 if 'scale' in out:
391 389 output_scale = self.__checknumeric(out['scale'], float, "output scale")
392 390
393 391 if 'dpi' in out:
394 392 output_dpi = self.__checknumeric(out['dpi'], int, "output dpi")
395 393 output_dpi = int(out['dpi'])
396 394
397 395 if 'orientation' in out:
398 396 if 'orient' in out:
399 397 casalog.post( "output dictionary cannot have both 'orient' and 'orientation' fields", 'SEVERE')
400 - raise Exception("output dictionary cannot have both 'orient' and 'orientation' fields")
398 + raise RuntimeError("output dictionary cannot have both 'orient' and 'orientation' fields")
401 399 if type(out['orientation']) != str:
402 400 casalog.post( "output orientation must be a string", 'SEVERE')
403 - raise Exception("output orientation must be a string")
401 + raise RuntimeError("output orientation must be a string")
404 402 if out['orientation'] != 'portrait' and out['orientation'] != 'landscape':
405 403 casalog.post( "output orientation must be either 'portrait' or 'landscape'", 'SEVERE')
406 - raise Exception("output orientation must be either 'portrait' or 'landscape'")
404 + raise RuntimeError("output orientation must be either 'portrait' or 'landscape'")
407 405 output_orientation = out['orientation']
408 406
409 407 if 'orient' in out:
410 408 if type(out['orient']) != str:
411 409 casalog.post( "output orient field must be a string", 'SEVERE')
412 - raise Exception("output orient field must be a string")
410 + raise RuntimeError("output orient field must be a string")
413 411 if out['orient'] != 'portrait' and out['orient'] != 'landscape':
414 412 casalog.post( "output orient field must be either 'portrait' or 'landscape'", 'SEVERE')
415 - raise Exception("output orient field must be either 'portrait' or 'landscape'")
413 + raise RuntimeError("output orient field must be either 'portrait' or 'landscape'")
416 414 output_orientation = out['orient']
417 415
418 416 return (output_file, output_format, output_scale, output_dpi, output_orientation)
419 417
420 418 def __checknumeric( self, value, otype, error_string, array_size=None ):
421 419 if array_size is not None:
422 420 if type(array_size) != int:
423 421 casalog.post( "internal error: array_size is expected to be of type int", 'SEVERE')
424 - raise Exception("internal error: array_size is expected to be of type int")
422 + raise RuntimeError("internal error: array_size is expected to be of type int")
425 423 if type(value) != list and not isinstance(value,ndarray):
426 424 casalog.post( error_string + " must be a list", 'SEVERE')
427 - raise Exception(error_string + " must be a list")
425 + raise RuntimeError(error_string + " must be a list")
428 426 if array_size > 0 and len(value) != array_size:
429 427 numbers = { '1': 'one', '2': 'two', '3': 'three' }
430 428 casalog.post( error_string + " can only be a " + numbers[str(array_size)] + " element numeric list", 'SEVERE')
431 - raise Exception(error_string + " can only be a " + numbers[str(array_size)] + " element numeric list")
429 + raise RuntimeError(error_string + " can only be a " + numbers[str(array_size)] + " element numeric list")
432 430 if not all(map( lambda x: type(x) == int or type(x) == float or isinstance(x,float64), value )):
433 431 casalog.post( error_string + " must be a numeric list", 'SEVERE')
434 - raise Exception(error_string + " must be a numeric list")
432 + raise RuntimeError(error_string + " must be a numeric list")
435 433 return list(map( lambda x: otype(x), value ))
436 434
437 435 if type(value) != int and type(value) != float:
438 436 casalog.post( error_string + " must be numeric", 'SEVERE')
439 - raise Exception(error_string + " must be numeric")
437 + raise RuntimeError(error_string + " must be numeric")
440 438
441 439 return otype(value)
442 440
443 441 def __check_fileformat( self, ext ):
444 442 supported_files = [ 'jpg', 'pdf', 'eps', 'ps', 'png', 'xbm', 'xpm', 'ppm' ]
445 443 if supported_files.count(ext.lower( )) == 0:
446 444 casalog.post( "output format '" + str(ext) + "' not supported; supported types are: " + str(supported_files), 'SEVERE')
447 - raise Exception("output format '" + str(ext) + "' not supported; supported types are: " + str(supported_files))
445 + raise RuntimeError("output format '" + str(ext) + "' not supported; supported types are: " + str(supported_files))
448 446 return ext.lower( )
449 447
450 448
451 449 def __check_filename( self, out, check_extension = True ):
452 450 dir = os.path.dirname(out)
453 451 if len(dir) > 0 and not os.path.isdir(dir):
454 452 casalog.post( "output directory (" + str(dir) + ") does not exist", 'SEVERE')
455 - raise Exception("output directory (" + str(dir) + ") does not exist")
453 + raise RuntimeError("output directory (" + str(dir) + ") does not exist")
456 454 file = os.path.basename(out)
457 455 if len(file) == 0:
458 456 casalog.post( "could not find a valid file name in '" + str(out) + "'", 'SEVERE')
459 - raise Exception("could not find a valid file name in '" + str(out) + "'")
457 + raise RuntimeError("could not find a valid file name in '" + str(out) + "'")
460 458 (base,ext) = os.path.splitext(file)
461 459 if len(ext) == 0:
462 460 casalog.post( "could not infer the ouput type from file name '" + str(file) + "'", 'SEVERE')
463 - raise Exception("could not infer the ouput type from file name '" + str(file) + "'")
461 + raise RuntimeError("could not infer the ouput type from file name '" + str(file) + "'")
464 462 return self.__check_fileformat(ext[1:]) if check_extension else ''
465 463
466 464 def __pushd( self, gui, newdir ):
467 465 try:
468 466 old_path = viewertool.cwd( gui=gui )
469 467 except:
470 468 casalog.post( "imview() failed to get the current working directory [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]", 'SEVERE')
471 - raise Exception("imview() failed to get the current working directory [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]")
469 + raise RuntimeError("imview() failed to get the current working directory [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]")
472 470
473 471 self.__dirstack.append((gui,old_path))
474 472 try:
475 473 viewertool.cwd(newdir,gui)
476 474 except:
477 475 casalog.post( "imview() failed to change to the new working directory (" + os.path.abspath(os.curdir) + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]", 'SEVERE')
478 - raise Exception("imview() failed to change to the new working directory (" + os.path.abspath(os.curdir) + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]")
476 + raise RuntimeError("imview() failed to change to the new working directory (" + os.path.abspath(os.curdir) + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]")
479 477
480 478
481 479 def __popd( self ):
482 480 try:
483 481 gui, path = self.__dirstack.pop( )
484 482 viewertool.cwd(path,gui)
485 483 except:
486 484 casalog.post( "imview() failed to restore the old working directory (" + old_path + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]", 'SEVERE')
487 - raise Exception("imview() failed to restore the old working directory (" + old_path + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]")
485 + raise RuntimeError("imview() failed to restore the old working directory (" + old_path + ") [" + str(sys.exc_info()[0]) + ": " + str(sys.exc_info()[1]) + "]")
488 486
489 487
490 488 imview = __imview_class( )

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut