# -----------------------------------------------------------------------------
# Script to make images of maps at specific contour levels.
#
def make_map_image(map_path, level, image_path):

    from chimera import runCommand as run, viewer
    run('open ' + map_path)                     # Open map.

    # Set threshold level
    from VolumeViewer import volume_dialog
    d = volume_dialog(create = True)
    dr = d.focus_region
    dr.set_parameters(surface_levels = [level])         # Set contour level

    # Set some other display options
    color = (1,.8,.2,1)               # Red, green, blue, opacity (0-1 scale).
    dr.set_parameters(surface_colors = [color])
    ro = dr.rendering_options
    ro.surface_smoothing = True       # Smoother surfaces.
#    dr.representation = 'mesh'        # Mesh instead of a solid surface.
#    ro.square_mesh = True             # Square mesh instead of triangles.

    d.show_data_regions([dr])

    run('turn x 90')                            # Rotate 90 deg about x axis.
    run('set bg_color white')                   # White background color
    run('scale 1.5')                            # Zoom in.
    run('windowsize 512 512')                   # Set window size.
    viewer.showSilhouette = True                # Surface edge highlights
    run('copy file %s png' % image_path)        # Save image.
#    run('copy file %s png raytrace rtwait rtclean' % image_path) # Save raytraced image.
    run('close session')

# -----------------------------------------------------------------------------
#
map_dir = '/usr/local/src/chimera-demos/volume/emd'
maps = (('emd_1007.map', 35.0),                 # File name and threshold
        ('emd_1046.map', 0.1),
        ('emd_1282.map', 0.15),
        )
image_dir = '/usr/local/src/chimera-demos/volume/emd/images'
from os.path import join
for map_file, level in maps:
    map_path = join(map_dir, map_file)
    image_file = map_file.split('.')[0] + '.png'   # Replace suffix with .png
    image_path = join(image_dir, image_file)
    make_map_image(map_path, level, image_path)
