Example #1
0
def test_1dtimeseries(directory, sourcefiles, var, cwd):
    """ Tests plot_realizations_1d for a timeseries plot
    
    Parameters
    ----------
    directory : string
                the directory to link the files to
    sourcefiles : list
                  a list of the locations of the files to link
    var :  string
           the variable plotted
    cwd : string 
          directory of the test modules
    """
    ctt.loadtestfiles(directory, sourcefiles)
    filepattern = '*.nc'
    ens = cd.mkensemble(filepattern)
    data = cd.loadfiles(ens, var, cdostr='-fldmean -yearmean', toDatetime=True)
    fig, axt = plt.subplots(1, 1, figsize=(8, 8))
    cd.plot_realizations_1d(data,
                            var,
                            'time',
                            ax=axt,
                            pdf='tsplot',
                            kwargs={
                                'color': 'r',
                                'alpha': 0.3
                            })
    plotcompare(directory, cwd, 'tsplot')
    os.system('rm  tsplot.pdf')
Example #2
0
def save_cmip5_trends(ens, var, start_date, end_date, datapath='./'):
    """Compute trends and save as np array in HDF5.
    """
    # Compute the trends using cmipdata/cdo
    ens1 = cd.trends(ens, start_date=start_date, 
                    end_date=end_date, delete=False)
    
    # keep only the slope files, delete the intercept files
    for model, experiment, realization, variable, files in ens1.iterate():
        for file in files:
            if 'intercept' in file:
                variable.del_filename(file)
    
    # load the slope files into a np array
    slopes = cd.loadfiles(ens1, var)
    
    # Store the DataFrame in HDF5
    out_file = os.path.join(datapath, 'cmip5_trends.h5')
    h5f = h5py.File(out_file, 'a')
    sy = start_date.split('-')[0]
    ey = end_date.split('-')[0]
    ds_path = os.path.join(var, sy + '_' + ey)
    ds_name = os.path.join(ds_path, 'c5_' + var + '_trend_' + sy + '_' + ey)    
    h5f.create_dataset(ds_name, data=slopes)
    model_names = [ model.name for model in ens1.models]
    h5f.create_dataset(ds_path + '/model_names', data=model_names)
    #h5f[ds_name].dims.create_scale(h5f[ds_path + 'model_names'], 'model_names')
    #f[ds_name].dims[2].attach_scale(h5f[ds_path + 'model_names'])
    h5f.close()
        
    # clean up
    trash_files = glob.glob('slope*.nc')
    trash_files.extend(glob.glob('intercept*.nc'))
    for f in trash_files:
        os.remove(f)
Example #3
0
File: loa.py Project: wydh/cmipdata
def test_loadingtools(directory, sourcefiles, var):
    """ Loads the data from a set of files 
    """
    ctt.loadtestfiles(directory, sourcefiles)
    filepattern = '*.nc'
    ens = cd.mkensemble(filepattern)
    data = hashlib.sha1(cd.loadfiles(ens,var)['data']).hexdigest()
    return {var: data}
Example #4
0
def test_1dtimeseries(directory, sourcefiles, var, cwd):
    """ Tests plot_realizations_1d for a timeseries plot
    
    Parameters
    ----------
    directory : string
                the directory to link the files to
    sourcefiles : list
                  a list of the locations of the files to link
    var :  string
           the variable plotted
    cwd : string 
          directory of the test modules
    """  
    ctt.loadtestfiles(directory, sourcefiles)
    filepattern = '*.nc'
    ens = cd.mkensemble(filepattern)
    data = cd.loadfiles(ens, var, cdostr='-fldmean -yearmean', toDatetime=True)
    fig, axt = plt.subplots(1, 1, figsize=(8, 8))
    cd.plot_realizations_1d(data, var, 'time', ax=axt, pdf='tsplot',
                        kwargs={'color': 'r', 'alpha': 0.3})
    plotcompare(directory, cwd, 'tsplot')
    os.system('rm  tsplot.pdf')