Beispiel #1
0
def test_all_as_array():
    uas = np.array([
        1, 1, 0, -np.sqrt(3), -1,
        np.sqrt(3), -1, -np.sqrt(3), -1, 1, -1,
        np.sqrt(3)
    ])
    vas = np.array([1, np.sqrt(3), 1, 1, -1, -1, 0, -1, np.sqrt(3), -1, 1, -1])
    assert (degfromnorth(uas, vas) == np.array(
        [225, 210, 180, 120, 45, 300, 90, 60, 150, 315, 135, 300])).all()
Beispiel #2
0
    def wind_dir_from_components(cls, uas_reader, vas_reader):
        """
        Create a NCdata object for wind direction based on uas and vas.  For global attributes, the attributes that the
        two datasets share will be input into the wind direction object exactly as they were.  Those attributes that
        differ (usually history, creation_date, etc.) will be post-pended with `_uas` or `_vas` and added to the global
        attributes as such
        :param uas_reader: netCDF4 Dataset object for uas
        :param vas_reader: netCDF Dataset object for vas
        :return: NCdata object for wind direction with appropriately updated data and units
        """
        if not all(
                isinstance(reader, netCDF4._netCDF4.Dataset)
                for reader in [uas_reader, vas_reader]):
            raise TypeError(
                'Input to NCData should be a netCDF4._netCDF4.Dataset object')

        # Ensure inputs are of proper type
        if 'uas' not in uas_reader.variables.keys():
            raise TypeError("uas_reader must have a variable 'uas'")
        if 'vas' not in vas_reader.variables.keys():
            raise TypeError("vas_reader must have a variable 'vas'")

        # Ensure uas and vas align and can be converted to wind direction
        uas = NCdata(uas_reader)
        vas = NCdata(vas_reader)
        shared_vars = [
            item for item in list(uas.__dict__.keys())
            if item not in ['var_name', 'variable', 'globalattrs']
        ]
        wind_dir = cls.__new__(cls)
        for var in shared_vars:
            if (np.asarray(getattr(uas, var) != getattr(vas, var))).all():
                raise TypeError(
                    'uas and vas are not aligned (do not have the same value) for global attribute: ',
                    var)
            setattr(wind_dir, var, getattr(uas, var))

        # Created combined global attributes
        wdir_globalattrs = {}
        for key, value in uas.globalattrs.items():
            if value == vas.globalattrs[key] == value:
                wdir_globalattrs[key] = value
            else:
                wdir_globalattrs[key + '_uas'] = value
                wdir_globalattrs[key + '_vas'] = vas.globalattrs[key]

        # Set new attributes
        wind_dir.var_name = 'wdir'
        wind_dir.var_units = 'degrees clockwise from north'
        wind_dir.globalattrs = wdir_globalattrs
        wind_dir.variable = tools.degfromnorth(uas=uas.variable,
                                               vas=vas.variable)
        return wind_dir
Beispiel #3
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'
Beispiel #4
0
def test_45_deg():
    assert degfromnorth(1, 1) == 225
Beispiel #5
0
def test_315_deg():
    assert degfromnorth(1, -1) == 315
Beispiel #6
0
def test_330_deg():
    assert degfromnorth(np.sqrt(3), -1) == 300
Beispiel #7
0
def test_225_deg():
    assert degfromnorth(-1, -1) == 45
Beispiel #8
0
def test_240_deg():
    assert degfromnorth(-1, -np.sqrt(3)) == 30
Beispiel #9
0
def test_210_deg():
    assert degfromnorth(-np.sqrt(3), -1) == 60
Beispiel #10
0
def test_30_deg():
    assert degfromnorth(np.sqrt(3), 1) == 240
Beispiel #11
0
def test_180_deg():
    assert degfromnorth(-1, 0) == 90
Beispiel #12
0
def test_150_deg():
    assert degfromnorth(-np.sqrt(3), 1) == 120
Beispiel #13
0
def test_120_deg():
    assert degfromnorth(-1, np.sqrt(3)) == 150
Beispiel #14
0
def test_90_deg():
    assert degfromnorth(0, 1) == 180
Beispiel #15
0
def test_60_deg():
    assert degfromnorth(1, np.sqrt(3)) == 210