windng
Check-in [b9977fb29f]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Improvements to turbulence().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:b9977fb29f8a60207fb8d946254d8b2fdf9e69ea
User & Date: goyo 2015-10-28 23:48:31
Original Comment: Improvements to zcode>turbulence().
Context
2015-11-24
08:09
Make it work with pandas 0.17.1 check-in: da152f922f user: goyo tags: trunk
2015-10-28
23:48
Improvements to turbulence(). check-in: b9977fb29f user: goyo tags: trunk
2015-10-20
06:57
Drop support for pandas <= 0.15.1 in windng.io. check-in: b0ca1456a2 user: goyo tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to test/test_wind.py.

1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783

1784

1785
1786
1787

1788

1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814

1815
1816
1817
1818
1819
1820
1821
              .354, .4415, .4278, .4434, .4418, .4276, .4418, .4286,
             .4419, .4422, .3963, .4407, .4295, .4446, .4288, .4421,
             .4434, .4274, .4440, .4288, .4385, .4399, .4124, .4425,
             .4298, .4416, .4264, .4426, .821
        ]

        result = wind.turbulence(speed, sstd)
        expected = pd.Index(range(1, 11), name='speed')
        pdt.assert_index_equal(result.index, expected)

        expected = [0, 0, 0, 0, 2, 10, 7, 4, 5, 1]
        self.assertListEqual(result['count'].tolist(), expected)

        expected = np.array([
            0.121301, 0.072326, 0.062078, 0.055559, 0.047576, 0.044411
        ])
        self.assertTrue(np.allclose(result['mean_TI'][4:10], expected, 1e-4))
        self.assertTrue(np.isnan(result['mean_TI'][[0, 1, 2, 3, 4]]).all())



        expected = np.array([0.180078, 0.077094, 0.064786, 0.058111, 0.051982])
        self.assertTrue(np.allclose(result['char_TI'][4:9], expected))
        self.assertTrue(np.isnan(result['char_TI'][[0, 1, 2, 3, 4, 10]]).all())



        expected = np.array([0.196535, 0.078429, 0.065544, 0.058826, 0.053216])
        self.assertTrue(np.allclose(result['repr_TI'][4:9], expected))
        self.assertTrue(np.isnan(result['repr_TI'][[0, 1, 2, 3, 4, 10]]).all())

    def test_turbulence_edition(self):
        """Test function turbulence with argument edition."""
        speed = [
            8.778644, 7.402747, 5.365009, 7.457073, 7.495371, 6.856766,
            5.671003, 6.349403, 9.167583, 6.873596, 5.726119, 8.490143,
            9.670927, 8.645196, 7.537691, 5.826243, 6.794400, 7.661626,
            8.758176, 6.643368, 5.579056, 5.690266, 6.256741, 9.368920,
            6.350109, 7.659327, 5.961506, 6.169605, 5.041072
        ]
        sstd = [
              .354, .4415, .4278, .4434, .4418, .4276, .4418, .4286,
             .4419, .4422, .3963, .4407, .4295, .4446, .4288, .4421,
             .4434, .4274, .4440, .4288, .4385, .4399, .4124, .4425,
             .4298, .4416, .4264, .4426, .821
        ]

        with warnings.catch_warnings(record=True) as w:
            result = wind.turbulence(speed, sstd, edition=2)
            msg = 'Argument edition is deprecated.'
            self.assertTrue(len(w), 1)
            self.assertTrue(isinstance(w[0].message, DeprecationWarning))
            self.assertEqual(str(w[0].message), msg)



class Test_TI_limits(unittest.TestCase):
    """Test functions IEC_X_edY."""
    def test_IEC_A_ed2(self):
        speed = range(3, 30)
        limits = wind.IEC_A_ed2(speed)







<
<

|


|
|
<
<
<
>
|
>
|
<
<
>

>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>







1766
1767
1768
1769
1770
1771
1772


1773
1774
1775
1776
1777
1778



1779
1780
1781
1782


1783
1784
1785
1786

























1787
1788
1789
1790
1791
1792
1793
1794
              .354, .4415, .4278, .4434, .4418, .4276, .4418, .4286,
             .4419, .4422, .3963, .4407, .4295, .4446, .4288, .4421,
             .4434, .4274, .4440, .4288, .4385, .4399, .4124, .4425,
             .4298, .4416, .4264, .4426, .821
        ]

        result = wind.turbulence(speed, sstd)



        expected = [0, 0, 0, 0, 0, 2, 10, 7, 4, 5, 1]
        self.assertListEqual(result['count'].tolist(), expected)

        expected = np.array([np.nan, np.nan, np.nan, np.nan, np.nan,
                0.121301, 0.072326, 0.062078, 0.055559, 0.047576, 0.044411])



        np.testing.assert_array_almost_equal(result['mean_TI'], expected)
        
        expected = np.array([np.nan, np.nan, np.nan, np.nan, np.nan,
                0.180078, 0.077094, 0.064786, 0.058111, 0.051982, np.nan])


        np.testing.assert_array_almost_equal(result['char_TI'], expected)

        expected = np.array([np.nan, np.nan, np.nan, np.nan, np.nan,
                0.196535, 0.078429, 0.065544, 0.058826, 0.053216, np.nan])

























        np.testing.assert_array_almost_equal(result['repr_TI'], expected)


class Test_TI_limits(unittest.TestCase):
    """Test functions IEC_X_edY."""
    def test_IEC_A_ed2(self):
        speed = range(3, 30)
        limits = wind.IEC_A_ed2(speed)

Changes to windng/wind.py.

1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
....
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105

1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116


1117
1118
1119
1120


1121
1122
1123
1124
1125
1126
1127
    sectors = sector(dir, nsectors=nsectors)
    sectors[np.isnan(sectors)] = nsectors
    sectors = sectors.astype(int)
    alphas = np.take(alpha, sectors)
    return speed*(to_height/from_height)**alphas


def turbulence(speed, sstd, edition=None):
    """Calculate turbulence intensity.

    Parameters
    ----------
    speed : array-like
        Average speed.
    sstd: array-like
................................................................................
        "count": number of values on each bin
        "mean_TI" : mean turbulence intensity on each bin.
        "char_TI": characteristic turbulence intensity (as defined by
            edition 2 of IEC 61400-1)
        "repr_TI": representative turbulence intensity (as defined by
            edition 3 of IEC 61400-1`)
    """
    if edition is not None:
        import warnings
        msg = 'Argument edition is deprecated.'
        warnings.warn(msg, DeprecationWarning, stacklevel=2)

    sstd = pd.Series(sstd)
    speed = pd.Series(speed)
    ti = sstd / speed
    last = int(np.ceil(max(speed)))
    centers = np.arange(last + 1)[1:]
    bins = centers - 0.5


    # XXX use grouby instead?
    counts = np.array([ti[speed.between(i, i+1)].count() for i in bins])
    means = np.array([ti[speed.between(i, i+1)].mean() for i in bins])
    stds = np.array([ti[speed.between(i, i+1)].std() for i in bins])

    result = pd.DataFrame.from_items([
        ('count', counts),
        ('mean_TI', means),
        ('char_TI', means + stds),
        ('repr_TI', means + 1.28*stds)


    ])

    result.set_index(centers, inplace=True)
    result.index.name = 'speed'


    return result


# TI limits as defined in IEC-61400-1

def IEC_A_ed2(speed):
    speed = np.asanyarray(speed)







|







 







<
<
<
<
<
|
<
<
|
|
<
>

<
<
<
<
<

|
|
<
<
>
>

<
<
|
>
>







1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
....
1088
1089
1090
1091
1092
1093
1094





1095


1096
1097

1098
1099





1100
1101
1102


1103
1104
1105


1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
    sectors = sector(dir, nsectors=nsectors)
    sectors[np.isnan(sectors)] = nsectors
    sectors = sectors.astype(int)
    alphas = np.take(alpha, sectors)
    return speed*(to_height/from_height)**alphas


def turbulence(speed, sstd):
    """Calculate turbulence intensity.

    Parameters
    ----------
    speed : array-like
        Average speed.
    sstd: array-like
................................................................................
        "count": number of values on each bin
        "mean_TI" : mean turbulence intensity on each bin.
        "char_TI": characteristic turbulence intensity (as defined by
            edition 2 of IEC 61400-1)
        "repr_TI": representative turbulence intensity (as defined by
            edition 3 of IEC 61400-1`)
    """





    ti = pd.Series(pd.Series(sstd) / pd.Series(speed))


    last = np.ceil(max(speed))
    bins = np.arange(last + 1) - 0.5

    stats = ti.groupby(np.digitize(speed, bins)).agg(['count', 'mean', 'std'])






    result = pd.DataFrame.from_items([
        ('count', stats['count']),
        ('mean_TI', stats['mean']),


        ('char_TI', stats['mean'] + stats['std']),
        ('repr_TI', stats['mean'] + 1.28 * stats['std'])
    ])


    result.index = result.index - 1
    result = result.reindex(pd.Index(np.arange(last + 1), name='speed'))
    result.fillna({'count': 0}, inplace=True)
    return result


# TI limits as defined in IEC-61400-1

def IEC_A_ed2(speed):
    speed = np.asanyarray(speed)