Ejemplo n.º 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()
Ejemplo n.º 2
0
lat2 = 8.25
long1 = 284.0
long2 = 288.75

box = "-sellonlatbox,%d,%d,%d,%d" % (long1, long2, lat1, lat2)

data_spat_avg = cdo.fldmean(input=box + " " + nc_in,
                            options='-f nc',
                            returnCdf=True)
cdo.fldmean(input=box + " " + nc_in,
            output=nc_spat_avg,
            options='-f nc',
            returnCdf=True)
#cdo -f nc -r -b 64 yearmean t2m_mon_2011-2015.nc t2m_mon_2011-2015_annual.nc
data_ymean = cdo.yearmean(input=nc_spat_avg, options='-f nc', returnCdf=True)
ncdump(data_ymean, True)
t2my = data_ymean.variables['t2m'][:]  # shape is time, lat, lon as shown above
print(t2my)
print("-" * 70)
# write to a file
# cdo.fldmean(input="-sellonlatbox,-65,-80,-15,15 "+nc_in, output=nc_spat_avg \
#                   , options='-f nc', returnCdf=True)

# verify file is written correctly
# type: <class 'netCDF4._netCDF4.Dataset'>
# nc_fh = Dataset(nc_spat_avg, 'r') #filehandler

ncdump(data_spat_avg, True)
lats = data_spat_avg.variables['lat'][:]  # extract/copy the data
lons = data_spat_avg.variables['lon'][:]
time = data_spat_avg.variables['time'][:]
Ejemplo n.º 3
0
        http://dx.doi.org/10.1175/1520-0477(1996)077<0437:TNYRP>2.0.CO;2
'''

import datetime as dt  # Python standard library datetime  module
import numpy as np
from netCDF4 import Dataset  # http://code.google.com/p/netcdf4-python/
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, addcyclic, shiftgrid
from useful_functions import ncdump

#here starts the program
TK_SILENCE_DEPRECATION = 1
nc_f = '../nc_files/examples/air.sig995.2012.nc'  # Your filename
nc_fid = Dataset(nc_f, 'r')  # Dataset is the class behavior to open the file
# and create an instance of the ncCDF4 class
nc_attrs, nc_dims, nc_vars = ncdump(nc_fid, True)
# Extract data from NetCDF file
lats = nc_fid.variables['lat'][:]  # extract/copy the data
lons = nc_fid.variables['lon'][:]
time = nc_fid.variables['time'][:]
air = nc_fid.variables['air'][:]  # shape is time, lat, lon as shown above

time_idx = 237  # some random day in 2012
# Python and the renalaysis are slightly off in time so this fixes that problem
offset = dt.timedelta(hours=48)
# List of all times in the file as datetime objects
dt_time = [dt.date(1, 1, 1) + dt.timedelta(hours=t) - offset for t in time]
cur_time = dt_time[time_idx]

# Plot of global temperature on our random day
fig = plt.figure()
Ejemplo n.º 4
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()
Ejemplo n.º 5
0
# type: <class 'netCDF4._netCDF4.Dataset'>
# nc_data = cdo.fldmean(input="-sellonlatbox,-65,-80,-15,15 "+nc_in \
#                   , options='-f nc', returnCdf=True)

# write to a file

#box values for avg (from insumos5 example):
lat1 = 3.75
lat2 = 8.25
long1 = 284.0
long2 = 288.75

box = "-sellonlatbox,%d,%d,%d,%d" % (long1, long2, lat1, lat2)
cdo.fldmean(input=box+" "+nc_in, output=nc_out \
                  , options='-f nc', returnCdf=True)

# verify file is written correctly
# type: <class 'netCDF4._netCDF4.Dataset'>
nc_fh = Dataset(nc_out, 'r')  #filehandler
ncdump(nc_fh, True)
nc_fh.close()

# add_offset: 293.69328912771044
# scale_factor: 0.00038463520406703495

ds = xr.open_dataset(nc_out)  # NetCDF or OPeNDAP URL
#ds is of type <class 'xarray.core.dataset.Dataset'>
# select a variable to plot
ds['t2m'].plot()
plt.show()
Ejemplo n.º 6
0
TK_SILENCE_DEPRECATION = 1

nc_file = "pr_Amon_MPI-ESM-P_historical_r1i1p1_185001-200512"
nc_dir = "../nc_files/cmip5_converted/MPI-ESM-P/pr"
cmip5_std_dir = "cmip5/HadGEM2AO/pr/"

nc_in = nc_dir+cmip5_std_dir+nc_file  # Your filename
nc_spat_avg = os.path.splitext(nc_in)[0]+"_avg_spat.nc"
nc_spat_avg2 = os.path.splitext(nc_in)[0]+"_avg_spat2.nc"

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

fh_orig = Dataset(nc_in, mode='r')  # file handler
ncdump(fh_orig, True)
print("-"*80)
# put vars into numpy arrays
lons = fh_orig.variables['lon'][:]
lats = fh_orig.variables['lat'][:]

# print(type(lons))<class 'numpy.ma.core.MaskedArray'>
# print(type(lats))
# From http://www.bamboodream.sakura.ne.jp/hiroblog/?page_id=552

# instead of writing to file, return the data directly
# type: <class 'netCDF4._netCDF4.Dataset'>

# box values for avg (from insumos5 example):
# Region 1 (Colombia)
lat1 = -3