예제 #1
0
 def make_proj():
     p = ProjectionPlot(spf, 'x', ["density"], center='c', origin='native')
     return p
def region_projections(node, fields, weight_field=("gas", "density"),
                       axes="xyz", particle_projections=True,
                       output_format="ds", output_dir="."):

    ds = node.ds
    sphere = node.sphere
    left  = sphere.center - 1.05 * sphere.radius
    right = sphere.center + 1.05 * sphere.radius
    region = ds.box(left, right)

    output_key = get_output_key(node, output_format)

    for ax in axes:
        do_fields = \
            [field for field in fields
             if not os.path.exists(
                     get_projection_filename(
                         node, ax, field=field, weight_field=weight_field,
                         output_format=output_format, output_dir=output_dir))]

        if do_fields:
            add_p2p_fields(ds)
            p = ProjectionPlot(
                ds, ax, do_fields, weight_field=weight_field,
                center=sphere.center, width=2*sphere.radius,
                data_source=region)
            for field, cmap in fields.items():
                p.set_cmap(field, cmap)
            decorate_plot(node, p)
            p.save(output_dir + "/" + output_key)
            del p

    if not particle_projections:
        return

    do_axes = \
        [ax for ax in axes
         if not os.path.exists(
                 get_projection_filename(
                     node, ax, particle_projections=True,
                     output_format=output_format, output_dir=output_dir))]

    for ax in do_axes:
        p = ParticleProjectionPlot(
            ds, ax, ("all", "particle_mass"),
            center=sphere.center, width=2*sphere.radius,
            data_source=region)
        p.set_unit(("all", "particle_mass"), "Msun")
        p.set_cmap(("all", "particle_mass"), "turbo")
        decorate_plot(node, p)
        p.save(output_dir + "/" + output_key)
        del p

    sphere.clear_data()
    region.clear_data()
    del sphere, region, ds
예제 #3
0
 def make_proj():
     p = ProjectionPlot(spf, "x", ["density"], center="c", origin="native")
     return p
예제 #4
0
    def __init__(self,
                 ds,
                 image_name="ytProjectionImage",
                 axis='z',
                 variable=('gas', 'temperature'),
                 weight_variable=('gas', 'density'),
                 center=[0.0, 0.0, 0.0],
                 width=10.0,
                 units='kpc',
                 color_map='algae',
                 show_annotations=False):
        from yt import ProjectionPlot, OffAxisProjectionPlot
        if (axis != 'z') and (axis != 'y') and (axis != 'x'):
            p = OffAxisProjectionPlot(ds,
                                      axis,
                                      variable,
                                      width=(width, units),
                                      weight_field=weight_variable)
            #plot = p.plots[variable]
            #fig = plot.figure
            #f = BytesIO()
            #vv = yt.write_image(np.log10(p.frb[variable][:,:-1].d),f, cmap_name = color_map)
            p.hide_axes()
            p.hide_colorbar()
            p.set_cmap(variable, color_map)
            p._setup_plots()
            plot = p.plots[variable]
            plot.canvas.draw()
            buff = plot.canvas.tostring_argb()
            ncols, nrows = plot.canvas.get_width_height()
            vv = np.fromstring(buff, dtype=np.uint8).reshape((nrows, ncols, 4),
                                                             order="C")
            if show_annotations:
                p = OffAxisProjectionPlot(ds,
                                          axis,
                                          variable,
                                          width=(width, units),
                                          weight_field=weight_variable)
                p.set_cmap(variable, color_map)
                p._setup_plots()
                plot = p.plots[variable]
                plot.canvas.draw()
                buff2 = plot.canvas.tostring_argb()
                ncols2, nrows2 = plot.canvas.get_width_height()
                vv2 = np.fromstring(buff2, dtype=np.uint8).reshape(
                    (nrows2, ncols2, 4), order="C")
                #plot.canvas.draw()
                #buff = plot.canvas.tostring_argb()
                #ncols2, nrows2 = plot.canvas.get_width_height()
                #vv2 = np.fromstring(buff, dtype=np.uint8).reshape((nrows2, ncols2, 4), order="C")
        else:
            p = ProjectionPlot(ds,
                               axis,
                               variable,
                               center=center,
                               width=(width, units),
                               weight_field=weight_variable)
            #plot = p.plots[variable]
            #fig = plot.figure
            #f = BytesIO()
            #vv = yt.write_image(np.log10(p.frb[variable][:,:-1].d),f, cmap_name = color_map)
            p.hide_axes()
            p.hide_colorbar()
            p.set_cmap(variable, color_map)
            p._setup_plots()
            plot = p.plots[variable]
            plot.canvas.draw()
            buff = plot.canvas.tostring_argb()
            ncols, nrows = plot.canvas.get_width_height()
            vv = np.fromstring(buff, dtype=np.uint8).reshape((nrows, ncols, 4),
                                                             order="C")

            if show_annotations:
                p = ProjectionPlot(ds,
                                   axis,
                                   variable,
                                   center=center,
                                   width=(width, units),
                                   weight_field=weight_variable)
                p.set_cmap(variable, color_map)
                p._setup_plots()
                plot = p.plots[variable]
                plot.canvas.draw()
                buff2 = plot.canvas.tostring_argb()
                ncols2, nrows2 = plot.canvas.get_width_height()
                vv2 = np.fromstring(buff2, dtype=np.uint8).reshape(
                    (nrows2, ncols2, 4), order="C")
                #plot.canvas.draw()
                #buff = plot.canvas.tostring_argb()
                #ncols2, nrows2 = plot.canvas.get_width_height()
                #vv2 = np.fromstring(buff, dtype=np.uint8).reshape((nrows2, ncols2, 4), order="C")

        ncols = vv.shape[1]
        nrows = vv.shape[0]

        # delete if already there
        for im in bpy.data.images:
            if im.name == image_name:
                delete_unused_images(image_name)
                delete_unused_textures(image_name)
                delete_unused_materials(image_name)

        # for annotations
        for im in bpy.data.images:
            if im.name == image_name + '_ann':
                delete_unused_images(image_name + '_ann')
                delete_unused_textures(image_name + '_ann')
                delete_unused_materials(image_name + '_ann')

        # switching a from back to front, and flipping image
        # if also with annotation do a nice one in the domain
        pixels_tmp = np.array(vv)
        for i in range(0, nrows):
            #pixels_tmp[i,:,0] = vv[i,:,0]
            #pixels_tmp[i,:,1] = vv[i,:,1]
            #pixels_tmp[i,:,2] = vv[i,:,2]
            #pixels_tmp[i,:,3] = vv[i,:,3]
            pixels_tmp[i, :, 3] = vv[nrows - 1 - i, :, 0]
            pixels_tmp[i, :, 0] = vv[nrows - 1 - i, :, 1]
            pixels_tmp[i, :, 1] = vv[nrows - 1 - i, :, 2]
            pixels_tmp[i, :, 2] = vv[nrows - 1 - i, :, 3]

        if show_annotations:
            pixels_tmp2 = np.array(vv2)
        if show_annotations:
            for i in range(0, nrows2):
                pixels_tmp2[i, :, 3] = vv2[nrows2 - 1 - i, :, 0]
                pixels_tmp2[i, :, 0] = vv2[nrows2 - 1 - i, :, 1]
                pixels_tmp2[i, :, 1] = vv2[nrows2 - 1 - i, :, 2]
                pixels_tmp2[i, :, 2] = vv2[nrows2 - 1 - i, :, 3]

        # blank image
        image = bpy.data.images.new(image_name, width=ncols, height=nrows)
        if show_annotations:
            image2 = bpy.data.images.new(image_name + '_ann',
                                         width=ncols2,
                                         height=nrows2)

        pixels = pixels_tmp.ravel() / 255.
        #pixels = vv.ravel()/255.
        if show_annotations:
            pixels2 = pixels_tmp2.ravel() / 255.

        image.pixels = pixels
        if show_annotations:
            image2.pixels = pixels2

        # activate the image in the uv editor
        for area in bpy.context.screen.areas:
            if area.type == 'IMAGE_EDITOR':
                if show_annotations:
                    area.spaces.active.image = image2
                else:
                    area.spaces.active.image = image
            elif area.type == 'VIEW_3D':  # make sure material/render view is on
                for space in area.spaces:
                    if space.type == 'VIEW_3D':
                        if (space.viewport_shade != 'RENDERED') and (
                                space.viewport_shade != 'MATERIAL'):
                            space.viewport_shade = 'RENDERED'  # material is too slow

        # also, attach to the projection in the blender 3d window
        # now, set color
        figmat = makeMaterial(image_name, (0, 0, 0), (1, 1, 1), 1.0,
                              0.0)  # no emissivity or transperency for now
        setMaterial(bpy.data.objects[image_name], figmat)
        # also, make shadeless
        bpy.data.materials[image_name].use_shadeless = True

        # Create image texture from image
        cTex = bpy.data.textures.new(image_name, type='IMAGE')
        cTex.image = image
        # Add texture slot for color texture
        mat = bpy.data.materials[image_name]
        mtex = mat.texture_slots.add()
        mtex.texture = cTex
        #mtex.texture_coords = 'UV'
        mtex.texture_coords = 'OBJECT'  # this seems to work better for figures, but certainly needs to be tested
        mtex.use_map_color_diffuse = True
        mtex.use_map_color_emission = True
        mtex.emission_color_factor = 0.5
        mtex.use_map_density = True
        mtex.mapping = 'FLAT'

        # map to object
        mtex.object = bpy.data.objects[image_name]