Exemple #1
0
def avg_time_series(nc_in, param_in, region, box_in, model_in, print_info):
    '''
    avg_time_series ....

    Parameters
    ----------
    nc_in : string
        path to nc file to be analyzed
    param : string
        the parameter to be read from the nc files
    region : string
        Just the name of a region
    box_in : numerical arrays
        array of 4 numbers that indicates the long1, long2, lat1, lat2 of the
        region
    model_in : string
        Just the name of the model

    Returns
    -------
    '''
    print(nc_in)
    print(param_in)
    print(region)
    print(box_in)
    print(model_in)

    nc_in_filename = os.path.basename(nc_in)  # ###
    nc_fldmean = os.path.splitext(nc_in_filename)[0]+'_'+region+"_fldmean_85.nc"
    nc_ymean = os.path.splitext(nc_in_filename)[0]+'_'+region+"_ymean_85.nc"

    png_fldmean = os.path.splitext(nc_in_filename)[0]+'_'+region+"_fldmean_85.png"
    png_ymean = os.path.splitext(nc_in_filename)[0]+'_'+region+"_ymean_85.png"

    out_dir = nc_in.replace(nc_in_filename, 'avg_85')
    check_and_create(out_dir)
    nc_fldmean = nc_in.replace(nc_in_filename, 'avg_85/'+nc_fldmean)
    nc_ymean = nc_in.replace(nc_in_filename, 'avg_85/'+nc_ymean)

    png_fldmean = nc_in.replace(nc_in_filename, 'avg_85/'+png_fldmean)
    png_ymean = nc_in.replace(nc_in_filename, 'avg_85/'+png_ymean)

    print(nc_fldmean)
    print(nc_ymean)

    print("-"*80)

    # Initialize CDO
    cdo = Cdo()
    cdo.degub = True

    data_in = Dataset(nc_in, mode='r')  # file handler
    if print_info:
        ncdump(data_in, True)
        print("-"*80)
    data_in.close()

    box = "-sellonlatbox,%d,%d,%d,%d" % (box_in[0], box_in[1], box_in[2], box_in[3])

    # Create nc files for field mean and year mean.
    if os.path.exists(nc_fldmean):
        print("%s already exists", nc_fldmean)
    else:
        print("%s Create", nc_fldmean)
        cdo.fldmean(input=box+" "+nc_in, output=nc_fldmean, options='-f nc', returnCdf=True)

    if os.path.exists(nc_ymean):
        print("%s already exists", nc_ymean)
    else:
        print("%s Create", nc_ymean)
        cdo.yearmean(input=nc_fldmean, output=nc_ymean, options='-f nc', returnCdf=True)

    # Create file handlers for field mean and year mean
    data_fldmean = Dataset(nc_fldmean, mode='r')
    data_ymean = Dataset(nc_ymean, mode='r')

    # Check field mean is ok
    if print_info:
        ncdump(data_fldmean, True)

    # Check year mean is ok
    if print_info:
        ncdump(data_ymean, True)
        param_year = data_ymean.variables[param_in][:]
        print("number of data points: %d" % len(param_year))
        print("-"*80)

    # plot_time_series (data_fldmeanmean, param_in, region, title_fldmean)
    plot_time_series(data_ymean, param_in, region)
    # plt.savefig(png_ymean, dpi=300)

    data_fldmean.close()
    data_ymean.close()
Exemple #2
0
def plot_basempap_regions(nc_in, param_in, region_in, box_in, model_in):
    '''

    '''

    nc_in_filename = os.path.basename(nc_in)
    nc_box = os.path.splitext(nc_in_filename)[0]+'_'+region_in+"_box.nc"
    png_box = os.path.splitext(nc_in_filename)[0]+'_'+region_in+"_box.png"

    out_dir = nc_in.replace(nc_in_filename, 'box/')
    check_and_create(out_dir)
    nc_box = nc_in.replace(nc_in_filename, 'box/'+nc_box)
    png_box = nc_in.replace(nc_in_filename, 'box/'+png_box)

    # Initialize CDO
    cdo = Cdo()
    cdo.degub = True

    box = "%d,%d,%d,%d" % (box_in[0], box_in[1], box_in[2], box_in[3])  # box of Cdo

    print(box)
    print(nc_in)
    print(nc_box)
    print(png_box)
    print(param_in)

    cdo.sellonlatbox(box, input=nc_in, output=nc_box, options='-f nc',
                     returnCdf=True)

    fh = Dataset(nc_box, 'r')

    lons = fh.variables['lon'][:]
    lats = fh.variables['lat'][:]
    param = fh.variables[param_in][:, :, :]

    # last month of last year
    param = param[-1, :, :]

    param_units = fh.variables[param_in].units
    param_name = fh.variables[param_in].long_name

    # close file
    fh.close()

    # Get some parameters for the Stereographic Projection
    # lon_0 = lons.mean()
    # lat_0 = lats.mean()

    # m = Basemap(projection='moll',lon_0=0,resolution='l')
    # m = Basemap(width=50000, height=10000,
    #             resolution='l', projection='moll',\
    #             lat_ts=40, lat_0=lat_0, lon_0=lon_0)  # stere=stereographic projection
    #
    # m = Basemap(projection='ortho', lat_0=5, lon_0=-60, resolution='l')
    m = Basemap(projection='cass', llcrnrlat=box_in[2]-5, urcrnrlat=box_in[3]+5,\
                llcrnrlon=box_in[0]-5, urcrnrlon=box_in[1]+5, resolution='h' ,\
                lon_0=box_in[0]+3, lat_0=box_in[2]+3)

    lons_dim = len(lons.shape)
    if 2 == lons_dim:
        lon = lons
        lat = lats
    elif 1 == lons_dim:
        lon, lat = np.meshgrid(lons, lats)
    else:
        print("Error in lon lat array dimension: %d" % lons_dim)

    xi, yi = m(lon, lat)

    # Plot Data
    cs = m.pcolor(xi, yi, np.squeeze(param), alpha=0.7)

    # Add Grid Lines
    m.drawparallels(np.arange(-80., 81., 10.), labels=[1, 0, 0, 0], fontsize=10)
    m.drawmeridians(np.arange(-180., 181., 10.), labels=[0, 0, 0, 1], fontsize=10)

    # Add Coastlines, States, and Country Boundaries
    m.drawcoastlines()
    # m.drawstates()
    m.drawcountries()
    m.shadedrelief()

    # Add Colorbar
    cbar = m.colorbar(cs, location='bottom', pad="10%")
    cbar.set_label("%s (%s)" % (param_name, param_units))

    draw_screen_poly(box_in, m)

    # Add Title
    title_region = ('Model ' + model_in + ' for region ' + region_in + ' box '+ box)
    plt.title(title_region)
    plt.savefig(png_box, dpi=200)
    # plt.show()
    plt.close()
Exemple #3
0
i_models = 0

# model_array = ['EC-EARTH']
# loop of all models inside the cmip5 project dir
for model, model_path in get_subdirs(project_dir):

    # if model not in model_array:
    #    continue

    print(model)
    i_models = i_models + 1
    if(i_models > max_models):
        break

    model_path_converted = model_path.replace(project_dir, project_dir_converted)
    check_and_create(model_path_converted)

    # loop of all parameters inside each model
    for param, param_path in get_subdirs(model_path):  # me da todo lo que hay dentro de la carpeta modelo (osea parametros)
        param_path_converted = param_path.replace(project_dir, project_dir_converted)
        check_and_create(param_path_converted)

        for region, region_path in get_subdirs(param_path):
            region_path_converted = region_path.replace(project_dir, project_dir_converted)
            check_and_create(region_path_converted)

            # loop all files inside the param path
            for file, file_path in get_subfiles(region_path):

                if file.startswith("._"):
                    pass  # does nothing
Exemple #4
0
def plot_basempap_regions(nc_in, param_in, region_in, box_in, model_in):
    '''

    '''

    nc_in_filename = os.path.basename(nc_in)
    nc_box = os.path.splitext(nc_in_filename)[0] + '_' + region_in + "_box.nc"
    png_box = os.path.splitext(
        nc_in_filename)[0] + '_' + region_in + "_box.png"

    out_dir = nc_in.replace(nc_in_filename, 'box/')
    check_and_create(out_dir)
    nc_box = nc_in.replace(nc_in_filename, 'box/' + nc_box)
    png_box = nc_in.replace(nc_in_filename, 'box/' + png_box)

    # Initialize CDO
    cdo = Cdo()
    cdo.degub = True

    box = "%d,%d,%d,%d" % (box_in[0] - 7, box_in[1] + 8, box_in[2] - 10,
                           box_in[3] + 7)  # box of Cdo

    print(box)
    print(nc_in)
    print(nc_box)
    print(png_box)
    print(param_in)

    cdo.sellonlatbox(box,
                     input=nc_in,
                     output=nc_box,
                     options='-f nc',
                     returnCdf=True)

    fh = Dataset(nc_box, 'r')

    lons = fh.variables['lon'][:]
    lats = fh.variables['lat'][:]
    param = fh.variables[param_in][:, :]

    # last month of last year
    #param = param[-1, :, :]

    param_units = fh.variables[param_in].units
    param_name = fh.variables[param_in].long_name

    # close file
    fh.close()

    # Get some parameters for the Stereographic Projection
    # lon_0 = lons.mean()
    # lat_0 = lats.mean()

    # m = Basemap(projection='moll',lon_0=0,resolution='l')
    # m = Basemap(width=50000, height=10000,
    #             resolution='l', projection='moll',\
    #             lat_ts=40, lat_0=lat_0, lon_0=lon_0)  # stere=stereographic projection
    #
    # m = Basemap(projection='ortho', lat_0=5, lon_0=-60, resolution='l')
    m = Basemap(projection='cass', llcrnrlat=box_in[2]-6, urcrnrlat=box_in[3]+4,\
                llcrnrlon=box_in[0]-4, urcrnrlon=box_in[1]+6, resolution='h' ,\
                lon_0=box_in[0]+3, lat_0=box_in[2]+3)

    lons_dim = len(lons.shape)
    if 2 == lons_dim:
        lon = lons
        lat = lats
    elif 1 == lons_dim:
        lon, lat = np.meshgrid(lons, lats)
    else:
        print("Error in lon lat array dimension: %d" % lons_dim)

    xi, yi = m(lon, lat)

    # Plot Data
    # cmap = plt.get_cmap('terrain')''
    cmap = mpl.colors.ListedColormap([
        'royalblue', 'darkgreen', 'green', 'forestgreen', 'yellowgreen',
        'khaki', 'wheat', 'burlywood', 'tan', 'goldenrod', 'darkgoldenrod',
        'sienna', 'saddlebrown', 'whitesmoke'
    ])
    cmap.set_over('white')
    cmap.set_under('blue')

    bounds = [0, 50, 200, 400, 600, 800, 1000, 1200, 1400, 1600,
              1800]  # 11 colors
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
    # cb3 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
    #                                 norm=norm,
    #                                 boundaries=[-10] + bounds + [10],
    #                                 extend='both',
    #                                 extendfrac='auto',
    #                                 ticks=bounds,
    #                                 spacing='uniform',
    #                                 orientation='horizontal')

    cs = m.pcolor(xi, yi, np.squeeze(param), alpha=0.7, cmap=cmap, norm=norm)

    # Add Grid Lines
    m.drawparallels(np.arange(-80., 81., 10.),
                    labels=[1, 0, 0, 0],
                    fontsize=10)
    m.drawmeridians(np.arange(-180., 181., 10.),
                    labels=[0, 0, 0, 1],
                    fontsize=10)

    # Add Coastlines, States, and Country Boundaries
    m.drawcoastlines()
    # m.drawstates()
    m.drawcountries()
    m.shadedrelief()

    # Add Colorbar
    cbar = m.colorbar(cs, location='bottom', pad="10%")
    cbar.set_label("%s (%s)" % (param_name, param_units))

    draw_screen_poly(box_in, m)

    # Add Title
    title_region = ('Model ' + model_in + ' for region ' + region_in +
                    ' box ' + box)
    plt.title(title_region)
    plt.savefig(png_box, dpi=200)
    # plt.show()
    plt.close()
Exemple #5
0
def pr_time_series(nc_in, param_in, model_in, print_info=False):
    '''
    avg_time_series ....

    Parameters
    ----------
    nc_in : string
        path to nc file to be analyzed
    param : string
        the parameter to be read from the nc files
    region : string
        Just the name of a region
    box_in : numerical arrays
        array of 4 numbers that indicates the long1, long2, lat1, lat2 of the
        region
    model_in : string
        Just the name of the model

    Returns
    -------
    '''
    print(nc_in)
    print(param_in)
    print(model_in)

    nc_in_filename = os.path.basename(nc_in)  # ###
    nc_pmax = os.path.splitext(nc_in_filename)[0]+"_pmax.nc"
    nc_pmax_fldmax = os.path.splitext(nc_in_filename)[0]+"_pmax_fldmax.nc"

    png_pmax = os.path.splitext(nc_in_filename)[0]+"_pmax.png"
    png_pmax_fldmax = os.path.splitext(nc_in_filename)[0]+"_pmax_fldmax.png"

    out_dir = nc_in.replace(nc_in_filename, 'pmax')
    check_and_create(out_dir)
    nc_pmax = nc_in.replace(nc_in_filename, 'pmax/'+nc_pmax)
    nc_pmax_fldmax = nc_in.replace(nc_in_filename, 'pmax/'+nc_pmax_fldmax)

    png_pmax = nc_in.replace(nc_in_filename, 'pmax/'+png_pmax)
    png_pmax_fldmax = nc_in.replace(nc_in_filename, 'pmax/'+png_pmax_fldmax)

    print(nc_pmax_fldmax)
    print("-"*80)

    # Initialize CDO
    cdo = Cdo()
    cdo.degub = True

    data_in = Dataset(nc_in, mode='r')  # file handler
    if print_info:
        ncdump(data_in, True)
        print("-"*80)
    data_in.close()

    # box = "-sellonlatbox,%d,%d,%d,%d" % (box_in[0], box_in[1], box_in[2], box_in[3])

    # Create nc files for precipitation max for year.
    if os.path.exists(nc_pmax):
        print("%s already exists", nc_pmax)
    else:
        print("%s Create", nc_pmax)
    #   cdo.yearmax(input=box+" "+nc_in, output=nc_pmax, options='-f nc', returnCdf=True)
        cdo.yearmax(input=nc_in, output=nc_pmax, options='-f nc', returnCdf=True)

    if os.path.exists(nc_pmax_fldmax):
        print("%s already exists", nc_pmax_fldmax)

    else:
        print("%s Create", nc_pmax_fldmax)
        cdo.fldmax(input=nc_pmax, output=nc_pmax_fldmax, options='-f nc', returnCdf=True)

    # Create file handlers for field mean and year mean
    data_pmax = Dataset(nc_pmax, mode='r')
    data_fld_max = Dataset(nc_pmax_fldmax, mode='r')

    # Check pmax and fldmax are ok
    if print_info:
        ncdump(data_pmax, True)

    if print_info:
        ncdump(data_fld_max, True)

    # Check pmax temporal is ok
    if print_info:
        ncdump(data_pmax, True)
        param_year = data_pmax.variables[param_in][:]
        print("number of data points: %d" % len(param_year))
        print("-"*80)

    title_pmax = ('Precipitation max for ' + nc_in_filename)

    plot_time_series(data_fld_max, param_in, title_pmax)
    # plt.savefig(png_pmax_fldmax)
    plt.savefig('../'+region+'_'+param+'.png', dpi=300)   # ../ queda guardado en el directorio anterior en donde esta
    data_pmax.close()
    data_fld_max.close()
    plt.show()