예제 #1
0
def plot_diagnostic(fileout, plot_dir, project_info, model):

    """
    ;; Arguments
    ;;    fileout: dir
    ;;          directory to save the plot
    ;;
    ;; Description
    ;;    Plot diagnostic and save .png plot
    ;;
    """
    import projects
    E = ESMValProject(project_info)
    verbosity = E.get_verbosity()

    #-------------------------
    # Read model info
    #-------------------------
    
    currProject = getattr(vars()['projects'], model.split_entries()[0])()

    start_year = currProject.get_model_start_year(model)
    end_year = currProject.get_model_end_year(model)
    model_info = model.split_entries()


    # Read code output in netCDF format
    diag_name = 'sm-pr-diag-plot'
    netcdf_file = E.get_plot_output_filename(diag_name=diag_name,
                                             variable='pr-mrsos',
                                             model=model_info[1],
                                             specifier='Taylor2012-diagnostic')
    suffix = E.get_graphic_format() + "$"
    netcdf_file = re.sub(suffix, "nc", netcdf_file)
    ncf = nc4.Dataset(os.path.join(fileout, netcdf_file), 'r')
    p_val =  ncf.variables['T12_diag'][:,:]
    ncf.close()

    mp_val = np.ma.masked_equal(p_val, -999)

    # Gridding info: Global diagnostic 5x5 deg
    LON = np.arange(-180, 185, 5)
    LAT = np.arange(-60, 65, 5)

    # Define figure

    F, ax = plt.subplots(nrows=1, ncols=1, **dict(figsize=[15, 8]))

    # Cmap
    cmap = cm.get_cmap(name='bwr_r', lut=7)
    cmaplist = [cmap(i) for i in range(cmap.N)]
    cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)
    bounds = [0., 1, 5, 10, 90, 95, 99, 100]
    norm = col.BoundaryNorm(bounds, cmap.N)
    cmap.set_bad('GainsBoro')

    # Basemap
    map_ax = Basemap(ax=ax, projection='cyl', resolution='l',
                     llcrnrlat=-60, urcrnrlat=60,
                     llcrnrlon=-180, urcrnrlon=180,
                     fix_aspect=False)
    map_ax.drawcoastlines(color='k', linewidth=.7)

    # Plot p_val on basemap
    I = map_ax.pcolormesh(LON, LAT, mp_val, cmap=cmap, norm=norm)

    # Colorbar
    cax = F.add_axes([0.94, 0.15, 0.02, 0.7])
    cbar = plt.colorbar(I, cax=cax, cmap=cmap)

    plt.suptitle('Preference for afternoon precipitation over soil moisture anomalies, ' + model_info[1] + " (" + start_year + "-" + end_year + ")",
                  fontsize = 14)

    diag_name = 'sm-pr-diag-plot'
    figure_filename = E.get_plot_output_filename(diag_name=diag_name,
                                                 variable='pr-mrsos',
                                                 model=model_info[1])

    # Save figure to fileout
    plt.savefig(os.path.join(plot_dir, figure_filename))
예제 #2
0
def write_nc(fileout, xs, ys, p_vals, project_info, model):

    """ 
    ;; Arguments
    ;;    fileout: dir
    ;;          directory to save output
    ;;    xs: array [lon]
    ;;          regridding coordinates
    ;;    ys: array [lat]
    ;;          regridding coordinates
    ;;    p_vals: list
    ;;          p_values of 5x5 deg grid-boxes
    ;;
    ;; Description
    ;;    Save netCDF file with diagnostic in a regular 5x5 deg grid 
    ;;
    """
    import projects
    E = ESMValProject(project_info)
    verbosity = E.get_verbosity()

    #-------------------------
    # Read model info
    #-------------------------
    
    currProject = getattr(vars()['projects'], model.split_entries()[0])()

    model_info = model.split_entries()

       
    #------------------------------
    # Transform p_vals in 2D array
    #------------------------------
    p_val_2d = np.zeros((360/5, 120/5), dtype = 'f4')
    p_val_2d[:,:] = -999.
    i = 0    
    for x in xs:
        for y in ys:
            if p_vals[i]>-999.:
                p_val_2d[x-1, y-1] = p_vals[i]*100
            i = i+1

    #------------------------------
    # Write nc file
    #------------------------------
    diag_name = 'sm-pr-diag-plot'
    netcdf_file = E.get_plot_output_filename(diag_name=diag_name,
                                             variable='pr-mrsos',
                                             model=model_info[1],
                                             specifier='Taylor2012-diagnostic')

    suffix = E.get_graphic_format() + "$"
    netcdf_file = re.sub(suffix, "nc", netcdf_file)
    root_grp = nc4.Dataset(os.path.join(fileout, netcdf_file), 'w', format='NETCDF4')

    root_grp.description = 'Diagnostic Taylor2012: Precipitation dependance on soil moisture'

    root_grp.fillvalue = -999.0

    root_grp.createDimension('lon',360/5)
    root_grp.createDimension('lat',120/5)

    lat = root_grp.createVariable('latitude', 'f4', ('lat',))
    lon = root_grp.createVariable('longitude', 'f4', ('lon',))
    pval = root_grp.createVariable('T12_diag', 'f4', ('lat','lon'))

    lat[:] = np.arange(-60+2.5, 60, 5)
    lon[:] = np.arange(-180+2.5, 180, 5)
    pval[:,:] = np.transpose(p_val_2d)

    root_grp.close()