Exemplo n.º 1
0
def plot_vars(f_step, projection, load_all=False):
    # The one employed for the figure name when exported
    variable_name = 'gph_t_850'
    # Build the name of the output image
    run_string, _ = get_run()

    if load_all:
        f_steps = list(range(0, 79)) + list(range(81, 121, 3))
    else:
        f_steps = [f_step]

    filenames = ['/tmp/' + projection + '_' + variable_name +
                 '_%s_%03d.png' % (run_string, f_step) for f_step in f_steps]
    test_filenames = [os.path.exists(f) for f in filenames]

    if all(test_filenames):  # means the files already exist
        return filenames

    # otherwise do the plots
    dset = get_dset(vars_3d=['t@850', 'fi@500'], f_times=f_steps).squeeze()
    # Add a fictictious 1-D time dimension just to avoid problems
    if 'step' not in dset.dims.keys():
        dset = dset.expand_dims('step')
    #
    dset = subset_arrays(dset, projection)
    time = pd.to_datetime(dset.valid_time.values)
    cum_hour = dset.step.values.astype(int)

    temp_850 = dset['t'] - 273.15
    z_500 = dset['z']
    gph_500 = mpcalc.geopotential_to_height(z_500)
    gph_500 = xr.DataArray(gph_500.magnitude, coords=z_500.coords,
                           attrs={'standard_name': 'geopotential height',
                                  'units': gph_500.units})

    levels_temp = np.arange(-30., 30., 1.)
    levels_gph = np.arange(4700., 6000., 70.)

    lon, lat = get_coordinates(temp_850)
    lon2d, lat2d = np.meshgrid(lon, lat)

    cmap = get_colormap('temp')

    args = dict(filenames=filenames, projection=projection, levels_temp=levels_temp,
                cmap=cmap, lon2d=lon2d, lat2d=lat2d, lon=lon, lat=lat, temp_850=temp_850.values,
                gph_500=gph_500.values, levels_gph=levels_gph, time=time, run_string=run_string)

    if load_all:
        single_plot_param = partial(single_plot, **args)
        iterator = range(0, len(f_steps))
        pool = Pool(cpu_count())
        results = pool.map(single_plot_param, iterator)
        pool.close()
        pool.join()
    else:
        results = single_plot(0, **args)

    return results
Exemplo n.º 2
0
def rview(img, seg=None, overlay=None, colors=None, binary="rview"):
    """
    Launches rview.
    """
    if isinstance(img, Image):
        if overlay is not None and seg is not None:
            print "You cannot specify both seg and overlay"
            return
        if overlay is not None and colors is None:
            colors = 'jet'  # default colormap
        if colors is not None and overlay is None:
            overlay = img.rescale()  # we want to see img in colors
            img = zeros(img.get_header(), dtype='int16')
        if isinstance(colors, str):
            colors = utils.get_colormap(colors)
        if seg is not None and colors is None:
            colors = utils.random_colormap(seg.max())

        # now, seg == overlay
        if overlay is not None:
            seg = overlay

        if seg is not None and not isinstance(seg, Image):
            seg = Image(seg, img.get_header())

        handle, filename = tempfile.mkstemp(suffix=".nii")
        garbage.append(filename)
        imwrite(filename, img.astype('int16'))  # rview reads in "short" anyway
        args = [binary, filename]
        if seg is not None:
            handle, seg_filename = tempfile.mkstemp(suffix=".nii")
            garbage.append(seg_filename)
            handle, colors_filename = tempfile.mkstemp(suffix=".txt")
            garbage.append(colors_filename)
            imwrite(seg_filename, seg.astype('int16'))
            args.extend(["-seg", seg_filename])
            utils.colormap(colors, colors_filename)
            args.extend(["-lut", colors_filename])
        if overlay is not None:
            args.append("-labels")

    # launch rview as an independant process
    subprocess.Popen(args)
Exemplo n.º 3
0
def rview( img, seg=None, overlay=None, colors=None, binary="rview" ):
    """
    Launches rview.
    """
    if isinstance( img, Image ):
        if overlay is not None and seg is not None:
            print "You cannot specify both seg and overlay"
            return
        if overlay is not None and colors is None:
            colors = 'jet' # default colormap
        if colors is not None and overlay is None:
            overlay = img.rescale() # we want to see img in colors
            img = zeros(img.get_header(), dtype='int16')
        if isinstance( colors, str ):
            colors = utils.get_colormap( colors )
        if seg is not None and colors is None:
            colors = utils.random_colormap(seg.max())

        # now, seg == overlay
        if overlay is not None:
            seg = overlay

        if seg is not None and not isinstance(seg, Image):
            seg = Image( seg, img.get_header() )
    
        handle,filename = tempfile.mkstemp(suffix=".nii")
        garbage.append(filename)
        imwrite( filename, img.astype('int16') ) # rview reads in "short" anyway
        args = [ binary, filename ]
        if seg is not None:
            handle,seg_filename = tempfile.mkstemp(suffix=".nii")
            garbage.append(seg_filename)
            handle,colors_filename = tempfile.mkstemp(suffix=".txt")
            garbage.append(colors_filename)
            imwrite( seg_filename, seg.astype('int16') )
            args.extend( ["-seg", seg_filename])
            utils.colormap( colors, colors_filename )
            args.extend( ["-lut", colors_filename])
        if overlay is not None:
            args.append("-labels")

    # launch rview as an independant process
    subprocess.Popen( args )
Exemplo n.º 4
0
    def thumbnail(self,
                  seg=None,
                  overlay=None,
                  colors=None,
                  opacity=0.5,
                  step=2,
                  unroll=False,
                  index=None):
        """
        Create a thumbnail.
        """
        img = self.copy('int')
        if overlay is not None and seg is not None:
            print "You cannot specify both seg and overlay"
            return
        if seg is not None:
            seg = seg.astype('int')
        if overlay is not None and colors is None:
            colors = 'jet'  # default colormap
        if colors is not None and seg is None and overlay is None:
            overlay = img.copy()  # we want to see img in colors
            img = zeros(img.get_header(), dtype='uint8')
            opacity = 1.0
        if isinstance(colors, str):
            colors = utils.get_colormap(colors)
        if seg is not None and colors is None:
            colors = utils.random_colormap(seg.max())

        if index is None:
            index = np.array(img.shape).astype('int32') / 2

        # now, seg == overlay
        if overlay is not None:
            seg = overlay

        if len(img.shape) == 2:
            if seg is not None:
                data = img.get_data('uint8')
                data = data.reshape(data.shape[0], data.shape[1], 1)
                data = np.concatenate([data, data, data], axis=2)
                rgb_overlay = utils.remap(seg, colors)
                # op = (1-opacity) * (seg != 0).astype('float') + (seg == 0).astype('float')
                # op = op.reshape(op.shape[0],
                #                 op.shape[1],
                #                 1)
                # op = np.concatenate( [ op,
                #                        op,
                #                        op ], axis=2 )
                # img = op * data  + opacity*rgb_overlay
                img = (1 - opacity) * data + opacity * rgb_overlay

            return img.astype('uint8')

        elif len(img.shape) == 3:
            if not unroll:
                shape = np.array(img.shape).max()
                if seg is None:
                    output = np.ones((shape, shape * 3 + step * (3 - 1))) * 255
                else:
                    output = np.ones(
                        (shape, shape * 3 + step * (3 - 1), 3)) * 255

                offset1 = int((shape - img.shape[1]) / 2)
                offset2 = int((shape - img.shape[2]) / 2)
                if seg is None:
                    tmp_img = img[index[0], :, :]
                else:
                    tmp_img = Image(img[index[0], :, :]).thumbnail(
                        seg[index[0], :, :], colors=colors, opacity=opacity)
                output[offset1:offset1 + img.shape[1],
                       offset2:offset2 + img.shape[2]] = tmp_img

                offset1 = int((shape - img.shape[0]) / 2)
                offset2 = int(shape + step + (shape - img.shape[2]) / 2)
                if seg is None:
                    tmp_img = img[:, index[1], :]
                else:
                    tmp_img = Image(img[:, index[1], :]).thumbnail(
                        seg[:, index[1], :], colors=colors, opacity=opacity)
                output[offset1:offset1 + img.shape[0],
                       offset2:offset2 + img.shape[2]] = tmp_img

                offset1 = int((shape - img.shape[0]) / 2)
                offset2 = int(2 * shape + 2 * step +
                              (shape - img.shape[1]) / 2)
                if seg is None:
                    tmp_img = img[:, :, index[2]]
                else:
                    tmp_img = Image(img[:, :,
                                        index[2]]).thumbnail(seg[:, :,
                                                                 index[2]],
                                                             colors=colors,
                                                             opacity=opacity)
                output[offset1:offset1 + img.shape[0],
                       offset2:offset2 + img.shape[1]] = tmp_img

                return output.astype('uint8')
            else:  # unroll is True
                if seg is None:
                    output = np.ones(
                        (self.shape[1], self.shape[2] * self.shape[0] + 2 *
                         (self.shape[0] - 1))) * 255
                else:
                    output = np.ones(
                        (self.shape[1], self.shape[2] * self.shape[0] + 2 *
                         (self.shape[0] - 1), 3)) * 255
                for k in xrange(self.shape[0]):
                    if seg is None:
                        tmp_img = img[k, :, :]
                    else:
                        tmp_img = Image(img[k, :, :]).thumbnail(
                            seg[k, :, :], colors=colors, opacity=opacity)
                    output[:, k * self.shape[2] +
                           2 * k:(k + 1) * self.shape[2] + 2 * k] = tmp_img
                return output.astype('uint8')
        else:
            raise "Wrong number of dimensions for thumbnail: " + str(
                len(self.shape))
Exemplo n.º 5
0
    def thumbnail( self,
                   seg=None,
                   overlay=None,
                   colors=None,
                   opacity=0.5,
                   step=2,
                   unroll=False ):
        """
        Create a thumbnail.
        """
        img = self.copy('int')
        if overlay is not None and seg is not None:
            print "You cannot specify both seg and overlay"
            return
        if seg is not None:
            seg = seg.astype('int')
        if overlay is not None and colors is None:
            colors = 'jet' # default colormap
        if colors is not None and seg is None and overlay is None:
            overlay = img.copy() # we want to see img in colors
            img = zeros(img.get_header(), dtype='uint8')
            opacity = 1.0
        if isinstance( colors, str ):
            colors = utils.get_colormap( colors )
        if seg is not None and colors is None:
            colors = utils.random_colormap(seg.max())

        # now, seg == overlay
        if overlay is not None:
            seg = overlay

        if len(img.shape) == 2:
            if seg is not None:
                data = img.get_data('uint8')
                data = data.reshape(data.shape[0],
                                    data.shape[1],
                                    1)
                data = np.concatenate( [ data,
                                         data,
                                         data ], axis=2 )
                rgb_overlay = utils.remap( seg, colors )
                op = (1-opacity) * (seg != 0).astype('float') + (seg == 0).astype('float')
                op = op.reshape(op.shape[0],
                                op.shape[1],
                                1)
                op = np.concatenate( [ op,
                                       op,
                                       op ], axis=2 )
                img = op * data  + opacity*rgb_overlay

            return img.astype('uint8')
        
        elif len(img.shape) == 3:
            if not unroll:
                shape = np.array(img.shape).max()
                if seg is None:
                    output = np.ones((shape,shape*3+step*(3-1)))*255
                else:
                    output = np.ones((shape,shape*3+step*(3-1),3))*255

                offset1 = (shape - img.shape[1])/2
                offset2 = (shape - img.shape[2])/2
                if seg is None:
                    tmp_img = img[img.shape[0]/2,:,:]
                else:
                    tmp_img = Image(img[img.shape[0]/2,:,:]).thumbnail( seg[img.shape[0]/2,:,:], colors=colors, opacity=opacity )
                output[offset1:offset1+img.shape[1],
                       offset2:offset2+img.shape[2]] = tmp_img

                offset1 = (shape - img.shape[0])/2
                offset2 = shape + step + (shape - img.shape[2])/2
                if seg is None:
                    tmp_img = img[:,img.shape[1]/2,:]
                else:
                    tmp_img = Image(img[:,img.shape[1]/2,:]).thumbnail( seg[:,img.shape[1]/2,:], colors=colors, opacity=opacity)
                output[offset1:offset1+img.shape[0],
                       offset2:offset2+img.shape[2]] = tmp_img

                offset1 = (shape - img.shape[0])/2
                offset2 = 2*shape + 2*step + (shape - img.shape[1])/2
                if seg is None:
                    tmp_img = img[:,:,img.shape[2]/2]
                else:
                    tmp_img = Image(img[:,:,img.shape[2]/2]).thumbnail( seg[:,:,img.shape[2]/2], colors=colors, opacity=opacity )
                output[offset1:offset1+img.shape[0],
                       offset2:offset2+img.shape[1]] = tmp_img

                return output.astype('uint8')
            else: # unroll is True
                if seg is None:
                    output = np.ones( ( self.shape[1],
                                        self.shape[2]*self.shape[0]+2*(self.shape[0]-1) )
                                      ) * 255
                else:
                    output = np.ones( ( self.shape[1],
                                        self.shape[2]*self.shape[0]+2*(self.shape[0]-1),
                                        3 )
                                      ) * 255
                for k in xrange(self.shape[0]):
                    if seg is None:
                        tmp_img = img[k,:,:]
                    else:
                        tmp_img = Image(img[k,:,:]).thumbnail( seg[k,:,:], colors=colors, opacity=opacity )
                    output[:,
                        k*self.shape[2]+2*k:(k+1)*self.shape[2]+2*k] = tmp_img
                return output.astype('uint8')
        else:
            raise "Wrong number of dimensions for thumbnail: " + str(len(self.shape)) 
Exemplo n.º 6
0
def plot_var(f_step, projection):
    # NOTE!
    # If we are inside this function it means that the picture does not exist
    # The one employed for the figure name when exported
    variable_name = 'gph_t_850'
    # Build the name of the output image
    run_string, _ = get_run()
    filename = '/tmp/' + projection + '_' + \
        variable_name + '_%s_%03d.png' % (run_string, f_step)

    """In the main function we basically read the files and prepare the variables to be plotted.
  This is not included in utils.py as it can change from case to case."""
    dset = get_dset(vars_3d=['t@850', 'fi@500'], f_times=f_step).squeeze()
    dset = subset_arrays(dset, projection)
    time = pd.to_datetime(dset.valid_time.values)
    cum_hour = dset.step.values.astype(int)

    temp_850 = dset['t'] - 273.15
    z_500 = dset['z']
    gph_500 = mpcalc.geopotential_to_height(z_500)
    gph_500 = xr.DataArray(gph_500.magnitude, coords=z_500.coords,
                           attrs={'standard_name': 'geopotential height',
                                  'units': gph_500.units})

    levels_temp = np.arange(-30., 30., 1.)
    levels_gph = np.arange(4700., 6000., 70.)

    cmap = get_colormap('temp')

    fig = plt.figure(figsize=(figsize_x, figsize_y))

    ax = plt.gca()

    lon, lat = get_coordinates(temp_850)
    lon2d, lat2d = np.meshgrid(lon, lat)

    ax = get_projection_cartopy(plt, projection, compute_projection=True)

    if projection == 'euratl':
        norm = BoundaryNorm(levels_temp, ncolors=cmap.N)
        cs = ax.pcolormesh(lon2d, lat2d, temp_850, cmap=cmap, norm=norm)
    else:
        cs = ax.contourf(lon2d, lat2d, temp_850, extend='both',
                         cmap=cmap, levels=levels_temp)

    c = ax.contour(lon2d, lat2d, gph_500, levels=levels_gph,
                   colors='white', linewidths=1.)

    labels = ax.clabel(c, c.levels, inline=True, fmt='%4.0f', fontsize=6)

    maxlabels = plot_maxmin_points(ax, lon, lat, gph_500,
                                   'max', 80, symbol='H', color='royalblue', random=True)
    minlabels = plot_maxmin_points(ax, lon, lat, gph_500,
                                   'min', 80, symbol='L', color='coral', random=True)

    an_fc = annotation_forecast(ax, time)
    an_var = annotation(
        ax, 'Geopotential height @500hPa [m] and temperature @850hPa [C]', loc='lower left', fontsize=6)
    an_run = annotation_run(ax, time)

    plt.colorbar(cs, orientation='horizontal',
                 label='Temperature', pad=0.03, fraction=0.04)

    plt.savefig(filename, **options_savefig)
    plt.clf()

    return filename