building new color tables from ascii text?
Posted: Fri Jul 01, 2016 4:41 pm
Hi,
I was recently doing some work in McIDAS-V and wanted to use a formula to grab some color levels from a set color table and apply it to different levels in a new color table. Since the new table was not linear, I would not be able to do this easi
1.) A way to import a color table that is built as ascii text format? I would not find it easy to create an xml or binary file that matches the current expected formats.
2.) A way to set breakpoints and construct a color table in the jython shell?
Probably it would be easier to achieve option 1. I used a temporary solution which was to import an old McX color table into McIDAS-V. Use that old color table to pick BRIT values that matched a certain temperature based on an equation, and created a McIDAS-X batch file which would replicate this old BRIT table at color levels which would match my new temperature endpoints. I brought the modified color table into McX using the batch file. Fixed the endpoint colors. Saved that new color table. Imported that back into McIDAS-V. Perhaps there should be a more fluid way of doing this. I am sure there is, but I could not figure out how to build a color table directly in McV using the java docs without messing up my current set of color tables.
Perhaps there are other ways to approach this that I have not thought about.
Thanks for your help,
Joleen
I was recently doing some work in McIDAS-V and wanted to use a formula to grab some color levels from a set color table and apply it to different levels in a new color table. Since the new table was not linear, I would not be able to do this easi
1.) A way to import a color table that is built as ascii text format? I would not find it easy to create an xml or binary file that matches the current expected formats.
2.) A way to set breakpoints and construct a color table in the jython shell?
Probably it would be easier to achieve option 1. I used a temporary solution which was to import an old McX color table into McIDAS-V. Use that old color table to pick BRIT values that matched a certain temperature based on an equation, and created a McIDAS-X batch file which would replicate this old BRIT table at color levels which would match my new temperature endpoints. I brought the modified color table into McX using the batch file. Fixed the endpoint colors. Saved that new color table. Imported that back into McIDAS-V. Perhaps there should be a more fluid way of doing this. I am sure there is, but I could not figure out how to build a color table directly in McV using the java docs without messing up my current set of color tables.
Code: Select all
def convertColorTable(inputCT=None, output_file=None, category='User'):
"""
Usage: convertColorTable(inputCT='WVJL2', outputCT='WVJL2_163_330', category='User')
inputCT: this is a color table which is scaling well for pixel "Brightness
Values" (UNIT=BRIT or brightness in field selector) values but not for
"Brightness Temperature" Values (UNIT=TEMP or Temperature in field selector)
output_file: desired name and path of output color table as ascii file name.
(outputfile will be of form filename.ascii)
If no name is provided, the code skips writing any information to
a file and does not import a new color table.
category: Category name for fileing in color table
"""
import os
import ucar.unidata.util.ColorTable as ColorTable
rgb_list=getColorTableColorList(inputCT)
if (output_file is not None):
# create ascii extension for corresponding filename of outputCT (needed for import)
filename, file_extension = os.path.splitext(output_file)
if (file_extension != '.ascii'):
output_file='{}.{}'.format(filename,'ascii')
outputCT = os.path.splitext(os.path.basename(output_file))[0]
# Importing twice creates different names, so at least warn that
# this will happend
# check color table manager to see if ct in already in manager
ctm=_mcv.getColorTableManager()
ct_checkifnew = ctm.getColorTable(outputCT)
if (ct_checkifnew is not None):
raise RuntimeError('Color table name already in use: {}'.format(outputCT))
breakpoints = bilinear(rgb_list)
if (output_file is not None):
with open(output_file, 'w+') as fh:
for breakpoint in breakpoints:
red=breakpoint[2].getRed()
grn=breakpoint[2].getGreen()
blu=breakpoint[2].getBlue()
rgbLine='{} {} {}\n'.format(red,grn,blu)
fh.write(rgbLine)
# import new color table
test=importEnhancement(filename=output_file, category=category)
return breakpoints
def getColorTableColorList(color_table):
"""
Returns a list of RGB colors from an input color table
Raises Error if color table is not already loaded in McIDAS-V
color_table - color table already in McIDAS-V.
"""
import ucar.unidata.util.ColorTable as ColorTable
# load color table
ctm=_mcv.getColorTableManager()
ct = ctm.getColorTable(color_table)
# don't continue if color table is not in list
if (ct is None):
raise NameError ('Color Table Not Found In Color Table Manager: {}'.format(color_table))
else:
table = ct.getColorTable()
rgb_list=ct.getColorList()
return rgb_list
def bilinear(rgb_list):
"""
Return a zipped array of brightness values corresponding to color level
after a bi-linear stretch has been applied.
color_list of current McIDAS-V color table (Retrieved by getColorTableList)
"""
# get scale of newCT from temp to BRIT (assuming bi-linear scaling applied to 163-330 K)
minTempScale = 163 # this should remain fixed!!!!!
maxTempScale = 330 # this should remain fixed!!!!!
scale = float(maxTempScale - minTempScale)/float(255)
brightness_values=[]
temperatures=[]
new_rgb_list=[]
ct_len=len(rgb_list)
if (ct_len < 256):
raise RuntimeError('bilinear stretch expects a color table of 256 colors, this is {}'.format(ct_len))
for temp in range(minTempScale,maxTempScale+1):
if (temp < 242) or (temp == 330):
temperatures.append(temp)
else:
temperatures.extend([temp,(temp+0.5)])
for temp in temperatures:
if (temp < 242):
# increment by one degree until 242K
BV = int(418 - temp)
else:
BV = int(660 - (2.*float(temp)))
# these tables came from fortran and I believe that the first index of these tables
# was actually 1, but here it is 0
brightness_values.append(BV)
new_rgb_list.append(rgb_list[BV])
breakpoints=list(zip(brightness_values,temperatures,new_rgb_list))
return breakpoints
Perhaps there are other ways to approach this that I have not thought about.
Thanks for your help,
Joleen