Coder Social home page Coder Social logo

Comments (5)

dopplershift avatar dopplershift commented on May 19, 2024

I can see many uses for that. Unfortunately, that ends up being a limitation of matplotlib, so there's not much we can do; feel free to open an issue there. It is possible to use imagemagick to convert PNG files from matplotlib into the GIFs you're interested in.

from metpy.

rankinstudio avatar rankinstudio commented on May 19, 2024

Here is a solution that works rather well converting them to transparent PNGs with PIL. These can be easily transported into Google Earth.

                       #SETUP PLOT TO SAVE WITHOUT ANY BORDERS OR INFO
                       fig = plt.figure(frameon=False)

                        cmap = ctables.registry.get_colortable('Carbone42')
                        plt.pcolormesh(xlocs, ylocs, data, cmap=cmap)
                        plt.gca().set_aspect('equal','datalim')

                        #SET LIMITS IF WANTED
                        plt.xlim(-160, 160)
                        plt.ylim(-160, 160)

                        #SETUP FIGURE TO HAVE JUST IMAGE DATA
                        a=fig.gca()
                        a.set_frame_on(False)
                        a.set_xticks([]); a.set_yticks([])
                        plt.axis('off')
                        plt.savefig('myimage.png', dpi=430, bbox_inches='tight', pad_inches = 0)

                        #******************* CONVERT TO TRANSPARENT *********************
                        # CONVERT ALL WHITE PIXELS TO TRANSPARENT
                        img = Image.open('myimage.png')
                        img = img.convert("RGBA")

                        pixdata = img.load()

                        for y in range(img.size[1]):
                            for x in range(img.size[0]):
                                if pixdata[x, y] == (255, 255, 255, 255):
                                    pixdata[x, y] = (255, 255, 255, 0)

                        img.save('myimage.png', "PNG")

from metpy.

dopplershift avatar dopplershift commented on May 19, 2024

Well if transparent PNGs are enough (before you specifically asked about GIF), then you can just set it to transparent using the matplotlib API:

fig.patch.set_alpha(0.0)
a.patch.set_alpha(0.0)

I just tried this out and it worked fine.

from metpy.

rankinstudio avatar rankinstudio commented on May 19, 2024

Much easier. Thanks!

I did notice something else. On the level II example I think the functions of MetPy are assuming that the level II scans will always have all of the data it requests. I have noticed a few of the files I download throw errors. I think it may be because they are missing data on some of the radial scans. I have had to catch the following errors on occasion:

                    try:
                        name = each
                        fn = each.split('\\')[-1]
                        f = Level2File(name)
                        #f.sweeps[0][0]

                        # Pull data out of the file
                        sweep = 0

                        # First item in ray is header, which has azimuth angle
                        az = np.array([ray[0].az_angle for ray in f.sweeps[sweep]])

                        # 5th item is a dict mapping a var name (byte string) to a tuple of (header, data array)

                        ref_hdr = f.sweeps[sweep][0][4][b'REF'][0]
                        ref_range = np.arange(ref_hdr.num_gates) * ref_hdr.gate_width + ref_hdr.first_gate
                        ref = np.array([ray[4][b'REF'][1] for ray in f.sweeps[sweep]])

                        var_data = ref
                        var_range = ref_range

                        # Turn into an array, then mask
                        data = ma.array(var_data)
                        data[np.isnan(data)] = ma.masked

                        # Convert az,range to x,y
                        xlocs = var_range * np.sin(np.deg2rad(az[:, np.newaxis]))
                        ylocs = var_range * np.cos(np.deg2rad(az[:, np.newaxis]))

                        #******************* SETUP AND SAVE FIGURE ********************

                        fig = plt.figure(frameon=False)

                        #norm, cmap = ctables.registry.get_with_steps('NWSReflectivity', 16, 16)
                        cmap = ctables.registry.get_colortable('Carbone42')
                        plt.pcolormesh(xlocs, ylocs, data, cmap=cmap)
                        plt.gca().set_aspect('equal','datalim')

                        #SET LIMITS IF WANTED
                        plt.xlim(-160, 160)
                        plt.ylim(-160, 160)

                        # fig.tight_layout()
                        a=fig.gca()
                        a.set_frame_on(False)
                        a.set_xticks([]); a.set_yticks([])
                        fig.patch.set_alpha(0.0)
                        a.patch.set_alpha(0.0)
                        plt.axis('off')

                        plt.savefig(each + '.png', dpi=430, bbox_inches='tight', pad_inches = 0)
                        self.time['text'] = fn + ".png Saved"
                        plt.clf() #CLEAR FIGURE FROM MEMORY?
                        del fig #CLEAE FIGURE FROM MEMORY?

                    except (OSError,IndexError,AssertionError,TypeError,MemoryError):
                        print('ERROR ON FILE ' + each)
                        self.time['text'] = fn + ".png Converted"
                        continue

from metpy.

dopplershift avatar dopplershift commented on May 19, 2024

The level 2 code should be fault tolerant. Go ahead and open a new issue for that problem and post a link to the problematic file, if you can.

Ryan

On Jun 13, 2015, at 11:22, rankinstudio [email protected] wrote:

Much easier. Thanks!

I did notice something else. On the level II example I think the functions of MetPy are assuming that the level II scans will always have all of the data it requests. I have noticed a few of the files I download throw errors. I think it may be because they are missing data on some of the radial scans. I have had to catch the following errors on occasion:

                try:
                    name = each
                    fn = each.split('\\')[-1]
                    f = Level2File(name)
                    #f.sweeps[0][0]

                    # Pull data out of the file
                    sweep = 0

                    # First item in ray is header, which has azimuth angle
                    az = np.array([ray[0].az_angle for ray in f.sweeps[sweep]])

                    # 5th item is a dict mapping a var name (byte string) to a tuple of (header, data array)

                    ref_hdr = f.sweeps[sweep][0][4][b'REF'][0]
                    ref_range = np.arange(ref_hdr.num_gates) * ref_hdr.gate_width + ref_hdr.first_gate
                    ref = np.array([ray[4][b'REF'][1] for ray in f.sweeps[sweep]])

                    var_data = ref
                    var_range = ref_range

                    # Turn into an array, then mask
                    data = ma.array(var_data)
                    data[np.isnan(data)] = ma.masked

                    # Convert az,range to x,y
                    xlocs = var_range * np.sin(np.deg2rad(az[:, np.newaxis]))
                    ylocs = var_range * np.cos(np.deg2rad(az[:, np.newaxis]))

                    #******************* SETUP AND SAVE FIGURE ********************

                    fig = plt.figure(frameon=False)

                    #norm, cmap = ctables.registry.get_with_steps('NWSReflectivity', 16, 16)
                    cmap = ctables.registry.get_colortable('Carbone42')
                    plt.pcolormesh(xlocs, ylocs, data, cmap=cmap)
                    plt.gca().set_aspect('equal','datalim')

                    #SET LIMITS IF WANTED
                    plt.xlim(-160, 160)
                    plt.ylim(-160, 160)

                    # fig.tight_layout()
                    a=fig.gca()
                    a.set_frame_on(False)
                    a.set_xticks([]); a.set_yticks([])
                    fig.patch.set_alpha(0.0)
                    a.patch.set_alpha(0.0)
                    plt.axis('off')

                    plt.savefig(each + '.png', dpi=430, bbox_inches='tight', pad_inches = 0)
                    self.time['text'] = fn + ".png Saved"
                    plt.clf() #CLEAR FIGURE FROM MEMORY?
                    del fig #CLEAE FIGURE FROM MEMORY?

                except (OSError,IndexError,AssertionError,TypeError,MemoryError):
                    print('ERROR ON FILE ' + each)
                    self.time['text'] = fn + ".png Converted"
                    continue


Reply to this email directly or view it on GitHub.

from metpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.