Exemple #1
0
def test_write_no_overwrites(pr_access10):
    save_location = tempfile.NamedTemporaryFile().name + '.nc'
    pr_access10.write(save_location)
    with nc.Dataset(save_location, 'r') as dataset_reader:
        overwritten_file = NCdata(dataset_reader)
    os.remove(save_location)
    assert_almost_equal(overwritten_file.time, pr_access10.time)
    assert_almost_equal(overwritten_file.lats, pr_access10.lats)
    assert_almost_equal(overwritten_file.variable, pr_access10.variable)
    assert overwritten_file.globalattrs['frequency'] == 'day'
    assert overwritten_file.globalattrs['parent_experiment_id'] == 'historical'
Exemple #2
0
def test_write_overwrite_coords(pr_access10):
    save_location = tempfile.NamedTemporaryFile().name + '.nc'
    fake_lats = pr_access10.lats + 10
    fake_lons = pr_access10.lons + 10
    pr_access10.write(save_location, lats=fake_lats, lons=fake_lons)
    with nc.Dataset(save_location, 'r') as dataset_reader:
        overwritten_file = NCdata(dataset_reader)
    os.remove(save_location)
    assert overwritten_file.model_id == pr_access10.model_id
    assert_almost_equal(overwritten_file.lats, fake_lats)
    assert_almost_equal(overwritten_file.lons, fake_lons)
Exemple #3
0
def test_init_vas_cnrmcm5(path_vas_cnrmcm5, direct_vas_cnrmcm5):
    with nc.Dataset(path_vas_cnrmcm5, 'r') as dataset_reader:
        nc_obj = NCdata(dataset_reader)
    assert nc_obj.var_name == 'vas'
    assert nc_obj.model_id == 'CNRM-CM5'
    assert nc_obj.var_units == 'm s-1'
    assert_almost_equal(nc_obj.variable, direct_vas_cnrmcm5.variables['vas'], decimal=2)
    assert_almost_equal(nc_obj.lats, direct_vas_cnrmcm5.variables['lat'], decimal=2)
    assert_almost_equal(nc_obj.lons, direct_vas_cnrmcm5.variables['lon'], decimal=2)
    assert_almost_equal(nc_obj.time, direct_vas_cnrmcm5.variables['time'], decimal=2)
    assert nc_obj.calendar == 'gregorian'
    assert nc_obj.time_units == 'days since 2006-01-01 00:00:00'
Exemple #4
0
def test_init_uas_cmcccm(path_uas_cmcccm, direct_uas_cmcccm):
    with nc.Dataset(path_uas_cmcccm, 'r') as dataset_reader:
        nc_obj = NCdata(dataset_reader)
    assert nc_obj.var_name == 'uas'
    assert nc_obj.model_id == 'CMCC-CM'
    assert nc_obj.var_units == 'm s-1'
    assert_almost_equal(nc_obj.variable, direct_uas_cmcccm.variables['uas'], decimal=2)
    assert_almost_equal(nc_obj.lats, direct_uas_cmcccm.variables['lat'], decimal=2)
    assert_almost_equal(nc_obj.lons, direct_uas_cmcccm.variables['lon'], decimal=2)
    assert_almost_equal(nc_obj.time, direct_uas_cmcccm.variables['time'], decimal=2)
    assert nc_obj.calendar == 'standard'
    assert nc_obj.time_units == 'days since 2006-1-1'
Exemple #5
0
def test_wind_dir_from_components(path_uas_cnrmcm5, path_vas_cnrmcm5, direct_uas_cnrmcm5, direct_vas_cnrmcm5):
    with nc.Dataset(path_uas_cnrmcm5, 'r') as uas_reader, \
         nc.Dataset(path_vas_cnrmcm5, 'r') as vas_reader:
        wind_dir = NCdata.wind_dir_from_components(uas_reader, vas_reader)
    wind_dir_truth = tools.degfromnorth(np.asarray(direct_uas_cnrmcm5.variables['uas'][:]),
                                        np.asarray(direct_vas_cnrmcm5.variables['vas'][:]))
    assert wind_dir.var_name == 'wdir'
    assert wind_dir.var_units == 'degrees clockwise from north'
    assert_almost_equal(wind_dir.variable, wind_dir_truth)
    assert wind_dir.model_id == 'CNRM-CM5'
    assert wind_dir.globalattrs['frequency'] == 'day'
    assert wind_dir.globalattrs['parent_experiment_rip'] == 'r1i1p1'
    assert wind_dir.globalattrs['creation_date_uas'] == '2011-05-10T10:54:55Z'
Exemple #6
0
def test_write_overwrite_time(pr_access10):
    save_location = tempfile.NamedTemporaryFile().name + '.nc'
    fake_time_data = np.random.rand(len(pr_access10.time),)
    fake_time_units = 'epochs'
    fake_calendar = 'corgi'
    pr_access10.write(save_location, time_var=fake_time_data, time_units=fake_time_units, calendar=fake_calendar)
    with nc.Dataset(save_location, 'r') as dataset_reader:
        overwritten_file = NCdata(dataset_reader)
    os.remove(save_location)
    assert_almost_equal(overwritten_file.time, fake_time_data)
    assert overwritten_file.model_id == pr_access10.model_id
    assert overwritten_file.time_units == fake_time_units
    assert overwritten_file.calendar == fake_calendar
Exemple #7
0
def test_write_overwrite_variable(pr_access10):
    save_location = tempfile.NamedTemporaryFile().name + '.nc'
    fake_var_data = np.random.rand(*pr_access10.variable.shape)
    fake_var_unit = 'slugs'
    fake_var_name = 'force'
    pr_access10.write(save_location, variable=fake_var_data, var_units=fake_var_unit, var_name=fake_var_name)
    with nc.Dataset(save_location, 'r') as dataset_reader:
        overwritten_file = NCdata(dataset_reader)
    os.remove(save_location)
    assert_almost_equal(overwritten_file.variable, fake_var_data)
    assert overwritten_file.var_units == fake_var_unit
    assert overwritten_file.var_name == fake_var_name
    assert overwritten_file.model_id == pr_access10.model_id
Exemple #8
0
def test_init_pr_access10(path_pr_access10, direct_pr_access10):
    with nc.Dataset(path_pr_access10, 'r') as dataset_reader:
        nc_obj = NCdata(dataset_reader)
    assert nc_obj.var_name == 'pr'
    assert nc_obj.model_id == 'ACCESS1-0'
    assert nc_obj.var_units == 'kg m-2 s-1'
    assert_almost_equal(nc_obj.variable, direct_pr_access10.variables['pr'], decimal=2)
    assert_almost_equal(nc_obj.lats, direct_pr_access10.variables['lat'], decimal=2)
    assert_almost_equal(nc_obj.lons, direct_pr_access10.variables['lon'], decimal=2)
    assert_almost_equal(nc_obj.time, direct_pr_access10.variables['time'], decimal=2)
    assert nc_obj.calendar == 'proleptic_gregorian'
    assert nc_obj.time_units == 'days since 0001-01-01'
    assert nc_obj.globalattrs['frequency'] == 'day'
    assert nc_obj.globalattrs['parent_experiment_id'] == 'historical'
Exemple #9
0
def vas_cnrmcm5(path_vas_cnrmcm5):
    """Daily vas CNRM-CM5 rcp85"""
    with nc.Dataset(path_vas_cnrmcm5, 'r') as dataset_reader:
        return NCdata(dataset_reader)
Exemple #10
0
def uas_cmcccm(path_uas_cmcccm):
    """Daily uas CMCC-CM rcp85"""
    with nc.Dataset(path_uas_cmcccm, 'r') as dataset_reader:
        return NCdata(dataset_reader)
Exemple #11
0
def pr_access10(path_pr_access10):
    """Daily pr ACCESS1-0 rcp85"""
    with nc.Dataset(path_pr_access10, 'r') as dataset_reader:
        return NCdata(dataset_reader)
Exemple #12
0
def wdir_ncdata_access13(path_uas_access13, path_vas_access13):
    with nc.Dataset(path_uas_access13, 'r') as uas_reader, \
         nc.Dataset(path_vas_access13, 'r') as vas_reader:
        return NCdata.wind_dir_from_components(uas_reader, vas_reader)
# Inspecting the netcdf object
###############################

# ESTABLISHING DATA LOCATION
# folder = r"C:\repos\wnpmonsoon\tests\data"
folder = '/home/rwegener/repos/wnpmonsoon/tests/data/'
# precip_test_file = folder + r"\ACCESS1-0\pr_day_ACCESS1-0_rcp85_r1i1p1_1year_spatial_clip.nc"
# uas_test_file = folder + r"\CMCC-CM\uas_day_CMCC-CM_rcp85_r1i1p1_1year_spatial_clip.nc"
# vas_test_file = folder + r"\CMCC-CM\vas_day_CMCC-CM_rcp85_r1i1p1_1year_spatial_clip.nc"
precip_test_file_lin = folder + r"/ACCESS1-0/pr_day_ACCESS1-0_rcp85_r1i1p1_1year_spatial_clip.nc"
uas_test_file_lin = folder + r"/CNRM-CM5/uas_day_CNRM-CM5_rcp85_r1i1p1_1year_spatial_clip.nc"
vas_test_file_lin = folder + r"/CNRM-CM5/vas_day_CNRM-CM5_rcp85_r1i1p1_1year_spatial_clip.nc"

# EXAMPLE WIND DIRECTION
save_at = '/home/rwegener/repos/wnpmonsoon/winddir-tst.nc'
wdir_obj = wind_direction(uas_test_file, vas_test_file, save_location=save_at)

print('model:', wdir_obj.model_id)
print('variable:', wdir_obj.var_name)
print('max and min of variable data:', wdir_obj.variable.max(),
      wdir_obj.variable.min())

# EXAMPLE PRECIPITATION RATE
with nc.Dataset(precip_test_file_lin, 'r') as dataset_reader:
    precip_obj = NCdata(dataset_reader)
precip_obj.pr_unit_conversion()

print('model:', precip_obj.model_id)
print('variable:', precip_obj.var_name)
print('variable units:', precip_obj.var_units)
Exemple #14
0
import netCDF4 as nc
import os
from wnpmonsoon.netcdf import NetCDFWriter
from wnpmonsoon.ncdata import NCdata

model_precip = NCdata(file_, 'pr')
model_precip.pr_unit_conversion()
model_precip.write(output_filename)

print("processing data for file:", file_)
# set up output directory and filename
output_file = directory_saved_files + '/../../../pr_rate/' + os.path.basename(
    file_).replace("pr", "pr_rate")

# open up the file and extract data
dataset = nc.Dataset(file_, 'r+')
model_pr = dataset.variables['pr'][:]
lats = dataset.variables['lat']
lons = dataset.variables['lon']
time = dataset.variables['time']
# establish "before" test points
# I: make sure appropriate translation is applied to test points
z_len = model_pr.shape[0] - 1
x_len = model_pr.shape[1] - 1
y_len = model_pr.shape[2] - 1
test_point_1_before = model_pr[z_len, x_len, y_len]
test_point_2_before = model_pr[z_len // 2, x_len // 3, 0]
print('test point 1 value (before):', test_point_1_before, '\n',
      'test point 2 value(before)', test_point_2_before)
# multiply by the number of seconds in a month to get the rain rate in average mm/day;
# *** if you would like to change from the default cmip5 produced pr unit of mm/s to some other unit [ex. mm/h] \
Exemple #15
0
 def __init__(self, dataset_reader):
     NCdata.__init__(self, dataset_reader)
Exemple #16
0
def wdir_ncdata_cnrmcm5(path_uas_cnrmcm5, path_vas_cnrmcm5):
    with nc.Dataset(path_uas_cnrmcm5, 'r') as uas_reader, \
         nc.Dataset(path_vas_cnrmcm5, 'r') as vas_reader:
        return NCdata.wind_dir_from_components(uas_reader, vas_reader)
Exemple #17
0
def test_wind_dir_from_comp_vas_type_error(path_uas_cmcccm, path_pr_access10):
    with nc.Dataset(path_uas_cmcccm, 'r') as uas_reader, \
         nc.Dataset(path_pr_access10, 'r') as vas_reader:
        with pytest.raises(TypeError):
            NCdata.wind_dir_from_components(uas_reader, vas_reader)
Exemple #18
0
def test_pr_units_input_error(path_uas_cmcccm):
    with pytest.raises(TypeError):
        NCdata.pr_rate_from_flux(path_uas_cmcccm)
Exemple #19
0
def test_pr_unit_conversion(path_pr_access10, direct_pr_access10):
    with nc.Dataset(path_pr_access10) as dataset_reader:
        pr = NCdata.pr_rate_from_flux(dataset_reader)
    assert pr.var_units == 'mm hr-1'
    assert_almost_equal(pr.variable, np.asarray(direct_pr_access10.variables['pr'])*86400, decimal=2)
Exemple #20
0
def test_init_type_error(path_pr_access10):
    with pytest.raises(TypeError):
        NCdata(path_pr_access10)
Exemple #21
0
def precip_rate_ncdata_cnrmcm5(path_pr_cnrmcm5):
    with nc.Dataset(path_pr_cnrmcm5, 'r') as pflux_src:
        return NCdata.pr_rate_from_flux(pflux_src)
Exemple #22
0
def test_wind_dir_from_comp_coord_misaligned_error(path_uas_cnrmcm5, path_vas_cnrmcm5_modified_lats):
    with nc.Dataset(path_uas_cnrmcm5, 'r') as uas_reader, \
         nc.Dataset(path_vas_cnrmcm5_modified_lats, 'r') as vas_reader:
        with pytest.raises(TypeError):
            NCdata.wind_dir_from_components(uas_reader, vas_reader)
Exemple #23
0
def precip_rate_ncdata_access13(path_pr_access13):
    with nc.Dataset(path_pr_access13, 'r') as pflux_src:
        return NCdata.pr_rate_from_flux(pflux_src)
Exemple #24
0
from wnpmonsoon.ncdata import NCdata
import numpy as np

uas = NCdata(file_, 'uas')
vas = NCdata(file_, 'uas')

wind_sp = np.sqrt(np.square(uas.variable) + np.square(vas.variable))

# TODO decide if this is a good idea / if there is a better way to do this
# Inheriting from another object?
variable = 'ws'
ws = NCdata('notrealfile', variable, create_new=True)
ws.var_name = variable
ws.model = uas.model_id
ws.var_units = uas.var_units
ws.variable = wind_sp
ws.lats = uas.lats
ws.lons = uas.lons
ws.time = uas.time
ws.calendar = uas.calendar
ws.t_units = uas.t_units

ws.write(outfile)
Exemple #25
0
def test_wind_dir_from_comp_model_misaligned_error(path_uas_cmcccm, path_vas_cnrmcm5):
    with nc.Dataset(path_uas_cmcccm, 'r') as uas_reader, \
         nc.Dataset(path_vas_cnrmcm5, 'r') as vas_reader:
        with pytest.raises(TypeError):
            NCdata.wind_dir_from_components(uas_reader, vas_reader)
Exemple #26
0
    def compute(cls, precip_rate, wind_direction, criteria):
        # Checking input types
        if isinstance(precip_rate, netCDF4._netCDF4.Dataset):
            pr = NCdata(precip_rate)
        elif isinstance(precip_rate, NCdata):
            pr = precip_rate
        else:
            raise TypeError(
                'precip_rate of class ', precip_rate.__class__,
                ' must be either a netCDF4 reader or a '
                'NCData object')
        if isinstance(wind_direction, netCDF4._netCDF4.Dataset):
            wind_dir = NCdata(wind_direction)
        elif isinstance(wind_direction, NCdata):
            wind_dir = wind_direction
        else:
            raise TypeError(
                'wind_direction of class ', wind_direction.__class__,
                ' must be either a netCDF4 reader or '
                'a NCData object')
        # Create new monsoon object
        NCmonsoon = MonsoonData.__new__(cls)

        # Check that pr and wind_dir have properly aligning grids
        # TODO shouldn't need this if statement if align_grids always works properly
        if pr.lats != wind_dir.lats or pr.lons != wind_dir.lons:
            pr.variable, wind_dir.variable, new_lats, new_lons = cls.align_grids(
                pr, wind_dir)
            pr.lats = wind_dir.lats = new_lats
            pr.lons = wind_dir.lons = new_lons

        # Check that pr and wind_dir are compatible; if so assign shared variables to monsoon object
        for attr in [
                'lats', 'lons', 'time', 'calendar', 'time_units', 'model_id'
        ]:
            if (np.asarray(
                    getattr(pr, attr) != getattr(wind_dir, attr))).all():
                raise TypeError(attr, ' not aligned in pr and wind_dir')
            setattr(NCmonsoon, attr, getattr(pr, attr))

        # Create boolean monsoon from pr and wind_dir
        passing_pr = criteria['precip'](pr.variable)
        passing_wd = criteria['wind_dir'](wind_dir.variable)
        blank_monsoon = passing_pr & passing_wd

        # Add newly calculated data to the monsoon object
        NCmonsoon.var_name = 'monsoon'
        NCmonsoon.var_units = 'boolean'
        NCmonsoon.variable = blank_monsoon.astype(float)

        # Created combined global attributes
        monsoon_globalattrs = {}
        all_keys = list(
            set(
                list(pr.globalattrs.keys()) +
                list(wind_dir.globalattrs.keys())))
        for key in all_keys:
            if key not in pr.globalattrs.keys():
                monsoon_globalattrs[key + '_wd'] = wind_dir.globalattrs[key]
            elif key not in wind_dir.globalattrs.keys():
                monsoon_globalattrs[key + '_pr'] = pr.globalattrs[key]
            elif pr.globalattrs[key] == wind_dir.globalattrs[key]:
                monsoon_globalattrs[key] = pr.globalattrs[key]
            else:
                monsoon_globalattrs[key + '_pr'] = pr.globalattrs[key]
                monsoon_globalattrs[key + '_wd'] = wind_dir.globalattrs[key]

        # monsoon_globalattrs['monsoon_criteria'] = criteria['summary_dict']
        NCmonsoon.globalattrs = monsoon_globalattrs
        return NCmonsoon
Exemple #27
0
# ESTABLISHING DATA LOCATION
# folder = r"C:\repos\wnpmonsoon\tests\data"
folder = r"/home/rwegener/repos/wnpmonsoon/tests/data/"
# precip_test_file = folder + r"\ACCESS1-0\pr_day_ACCESS1-0_rcp85_r1i1p1_1year_spatial_clip.nc"
precip_test_file_lin = os.path.join(
    folder, r"ACCESS1-3/pr_day_ACCESS1-3_rcp85_r1i1p1_1year_spatial_clip.nc")
uas_test_file_lin = os.path.join(
    folder, r"ACCESS1-3/uas_day_ACCESS1-3_rcp85_r1i1p1_1year_spatial_clip.nc")
vas_test_file_lin = os.path.join(
    folder, r"ACCESS1-3/vas_day_ACCESS1-3_rcp85_r1i1p1_1year_spatial_clip.nc")

# Generate precipitation rate
with nc.Dataset(precip_test_file_lin, 'r') as p_flux_reader:
    print(p_flux_reader.__class__)
    p_rate = NCdata.pr_rate_from_flux(p_flux_reader)
p_rate_path = os.path.join(folder, r"created/pr_from_flux_ACCESS1-3.nc")
# p_rate.write(p_rate_path)

# Generate wind direction
with nc.Dataset(uas_test_file_lin, 'r') as uas_reader, \
     nc.Dataset(vas_test_file_lin, 'r') as vas_reader:
    wdir = NCdata.wind_dir_from_components(uas_reader, vas_reader)
wdir_path = os.path.join(folder, r"created/wdir_ACCESS1-3.nc")
# wdir.write(wdir_path)

# Compute monsoon
criteria = criteria_generator(pr_min=5, wd_min=180, wd_max=270)

with nc.Dataset(p_rate_path, 'r') as pr_reader, \
     nc.Dataset(wdir_path, 'r') as wd_reader: