2016-07-18 23:05:35 +05:30
|
|
|
|
#!/usr/bin/env python2.7
|
2015-02-11 22:55:49 +05:30
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
|
import os,sys
|
2015-02-11 22:55:49 +05:30
|
|
|
|
import numpy as np
|
|
|
|
|
from optparse import OptionParser
|
2015-09-24 14:54:42 +05:30
|
|
|
|
from PIL import Image
|
2015-02-11 22:55:49 +05:30
|
|
|
|
import damask
|
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
# MAIN
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
|
Generate PNG image from data in given column (or 2D data of overall table).
|
|
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
|
parser.add_option('-l','--label',
|
|
|
|
|
dest = 'label',
|
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
|
help = 'column containing data [all]')
|
|
|
|
|
parser.add_option('-r','--range',
|
|
|
|
|
dest = 'range',
|
|
|
|
|
type = 'float', nargs = 2, metavar = 'float float',
|
|
|
|
|
help = 'data range (min max) [auto]')
|
|
|
|
|
parser.add_option('--gap', '--transparent',
|
|
|
|
|
dest = 'gap',
|
|
|
|
|
type = 'float', metavar = 'float',
|
|
|
|
|
help = 'value to treat as transparent [%default]')
|
|
|
|
|
parser.add_option('-d','--dimension',
|
|
|
|
|
dest = 'dimension',
|
|
|
|
|
type = 'int', nargs = 2, metavar = 'int int',
|
|
|
|
|
help = 'data dimension (width height) [native]')
|
|
|
|
|
parser.add_option('--color',
|
|
|
|
|
dest = 'color',
|
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
|
help = 'color scheme [%default]')
|
|
|
|
|
parser.add_option('--invert',
|
|
|
|
|
dest = 'invert',
|
|
|
|
|
action = 'store_true',
|
|
|
|
|
help = 'invert color scheme')
|
|
|
|
|
parser.add_option('--abs',
|
|
|
|
|
dest = 'abs',
|
|
|
|
|
action = 'store_true',
|
|
|
|
|
help = 'magnitude of values')
|
|
|
|
|
parser.add_option('--log',
|
|
|
|
|
dest = 'log',
|
|
|
|
|
action = 'store_true',
|
|
|
|
|
help = 'log10 of values')
|
|
|
|
|
parser.add_option('--fliplr',
|
|
|
|
|
dest = 'flipLR',
|
|
|
|
|
action = 'store_true',
|
|
|
|
|
help = 'flip around vertical axis')
|
|
|
|
|
parser.add_option('--flipud',
|
|
|
|
|
dest = 'flipUD',
|
|
|
|
|
action = 'store_true',
|
|
|
|
|
help = 'flip around horizontal axis')
|
|
|
|
|
parser.add_option('--crop',
|
|
|
|
|
dest = 'crop',
|
|
|
|
|
type = 'int', nargs = 4, metavar = 'int int int int',
|
|
|
|
|
help = 'pixels cropped on left, right, top, bottom')
|
|
|
|
|
parser.add_option('-N','--pixelsize',
|
|
|
|
|
dest = 'pixelsize',
|
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
|
help = 'pixel per data point')
|
|
|
|
|
parser.add_option('-x','--pixelsizex',
|
|
|
|
|
dest = 'pixelsizex',
|
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
|
help = 'pixel per data point along x')
|
|
|
|
|
parser.add_option('-y','--pixelsizey',
|
|
|
|
|
dest = 'pixelsizey',
|
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
|
help = 'pixel per data point along y')
|
|
|
|
|
parser.add_option('--show',
|
|
|
|
|
dest = 'show',
|
|
|
|
|
action = 'store_true',
|
|
|
|
|
help = 'show resulting image')
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
|
|
|
|
parser.set_defaults(label = None,
|
|
|
|
|
range = [0.0,0.0],
|
2015-07-07 20:23:55 +05:30
|
|
|
|
gap = None,
|
2015-02-11 22:55:49 +05:30
|
|
|
|
dimension = [],
|
|
|
|
|
abs = False,
|
|
|
|
|
log = False,
|
|
|
|
|
flipLR = False,
|
|
|
|
|
flipUD = False,
|
|
|
|
|
color = "gray",
|
|
|
|
|
invert = False,
|
|
|
|
|
crop = [0,0,0,0],
|
2015-06-19 17:06:21 +05:30
|
|
|
|
pixelsize = 1,
|
2015-02-11 22:55:49 +05:30
|
|
|
|
pixelsizex = 1,
|
|
|
|
|
pixelsizey = 1,
|
|
|
|
|
show = False,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
if options.pixelsize > 1: (options.pixelsizex,options.pixelsizey) = [options.pixelsize]*2
|
|
|
|
|
|
|
|
|
|
# --- color palette ---------------------------------------------------------------------------------
|
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
|
theMap = damask.Colormap(predefined = options.color)
|
2015-02-11 22:55:49 +05:30
|
|
|
|
if options.invert: theMap = theMap.invert()
|
2015-08-08 00:33:26 +05:30
|
|
|
|
theColors = np.uint8(np.array(theMap.export(format = 'list',steps = 256))*255)
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
2015-08-14 02:55:08 +05:30
|
|
|
|
if filenames == []: filenames = [None]
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-14 02:55:08 +05:30
|
|
|
|
try:
|
|
|
|
|
table = damask.ASCIItable(name = name,
|
2015-08-21 01:12:05 +05:30
|
|
|
|
buffered = False,
|
2016-03-02 15:41:20 +05:30
|
|
|
|
labeled = options.label is not None,
|
2015-08-21 01:12:05 +05:30
|
|
|
|
readonly = True)
|
2015-08-14 02:55:08 +05:30
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
|
|
table.head_read()
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
2015-05-22 03:24:47 +05:30
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-05-20 02:44:19 +05:30
|
|
|
|
|
2015-05-22 03:24:47 +05:30
|
|
|
|
missing_labels = table.data_readArray(options.label)
|
|
|
|
|
if len(missing_labels) > 0:
|
2015-09-24 14:54:42 +05:30
|
|
|
|
damask.util.croak('column {} not found.'.format(options.label))
|
2015-05-20 02:44:19 +05:30
|
|
|
|
table.close(dismiss = True) # close ASCIItable and remove empty file
|
|
|
|
|
continue
|
2016-03-02 15:41:20 +05:30
|
|
|
|
# convert data to values between 0 and 1 and arrange according to given options
|
2015-02-11 22:55:49 +05:30
|
|
|
|
if options.dimension != []: table.data = table.data.reshape(options.dimension[1],options.dimension[0])
|
|
|
|
|
if options.abs: table.data = np.abs(table.data)
|
2015-05-13 19:15:26 +05:30
|
|
|
|
if options.log: table.data = np.log10(table.data);options.range = np.log10(options.range)
|
2015-02-11 22:55:49 +05:30
|
|
|
|
if options.flipLR: table.data = np.fliplr(table.data)
|
|
|
|
|
if options.flipUD: table.data = np.flipud(table.data)
|
2015-07-07 20:23:55 +05:30
|
|
|
|
|
2016-03-02 15:41:20 +05:30
|
|
|
|
mask = np.logical_or(table.data == options.gap, np.isnan(table.data))\
|
|
|
|
|
if options.gap else np.logical_not(np.isnan(table.data)) # mask gap and NaN (if gap present)
|
2015-06-19 17:06:21 +05:30
|
|
|
|
if np.all(np.array(options.range) == 0.0):
|
2015-07-07 20:23:55 +05:30
|
|
|
|
options.range = [table.data[mask].min(),
|
|
|
|
|
table.data[mask].max()]
|
2015-09-24 14:54:42 +05:30
|
|
|
|
damask.util.croak('data range: {0} – {1}'.format(*options.range))
|
2015-06-19 17:06:21 +05:30
|
|
|
|
|
|
|
|
|
delta = max(options.range) - min(options.range)
|
|
|
|
|
avg = 0.5*(max(options.range) + min(options.range))
|
2015-07-07 20:23:55 +05:30
|
|
|
|
|
2015-06-19 17:06:21 +05:30
|
|
|
|
if delta * 1e8 <= avg: # delta around numerical noise
|
|
|
|
|
options.range = [min(options.range) - 0.5*avg, max(options.range) + 0.5*avg] # extend range to have actual data centered within
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
|
|
|
|
table.data = (table.data - min(options.range)) / \
|
|
|
|
|
(max(options.range) - min(options.range))
|
2015-06-19 17:06:21 +05:30
|
|
|
|
|
2015-02-11 22:55:49 +05:30
|
|
|
|
table.data = np.clip(table.data,0.0,1.0).\
|
2015-07-07 20:23:55 +05:30
|
|
|
|
repeat(options.pixelsizex,axis = 1).\
|
|
|
|
|
repeat(options.pixelsizey,axis = 0)
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
2015-07-17 04:04:26 +05:30
|
|
|
|
mask = mask.\
|
|
|
|
|
repeat(options.pixelsizex,axis = 1).\
|
|
|
|
|
repeat(options.pixelsizey,axis = 0)
|
|
|
|
|
|
2015-02-11 22:55:49 +05:30
|
|
|
|
(height,width) = table.data.shape
|
2015-09-24 14:54:42 +05:30
|
|
|
|
damask.util.croak('image dimension: {0} x {1}'.format(width,height))
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
|
im = Image.fromarray(np.dstack((theColors[np.array(255*table.data,dtype = np.uint8)],
|
2015-07-07 20:23:55 +05:30
|
|
|
|
255*mask.astype(np.uint8))), 'RGBA').\
|
2015-02-11 22:55:49 +05:30
|
|
|
|
crop(( options.crop[0],
|
|
|
|
|
options.crop[2],
|
|
|
|
|
width -options.crop[1],
|
|
|
|
|
height-options.crop[3]))
|
|
|
|
|
|
2015-05-01 23:28:10 +05:30
|
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2015-02-11 22:55:49 +05:30
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
|
im.save(sys.stdout if not name else
|
2015-08-08 00:33:26 +05:30
|
|
|
|
os.path.splitext(name)[0]+ \
|
2016-03-02 15:41:20 +05:30
|
|
|
|
('' if options.label is None else '_'+options.label)+ \
|
2015-08-08 00:33:26 +05:30
|
|
|
|
'.png',
|
|
|
|
|
format = "PNG")
|
|
|
|
|
|
|
|
|
|
table.close() # close ASCII table
|
|
|
|
|
if options.show: im.show()
|