Ejemplo n.º 1
0
def plot_sounding_data_csv(filename, output):
        """Plot SkewT from a CSV sounding data file
    
        :param filename: The name of the file to open.
        :type filename: str
        :param output: The name of the file to output plot
        :type output: str

        The datafile format is CSV with the following columns: 

        - pressure (mb)
        - height (m)
        - temperature (C)
        - dew point (C)
        - wind direction (degrees)
        - wind speed (m/s)
        
        Missing values should be filled with the value -9999.00
        """
        p,z,T,Td,wdir,wspd = np.loadtxt(filename, delimiter=',',  unpack=True)
        # Pressure to Pa
        p = p * 100.

        # z to km
        #z = z / 1000.
        
        # interpolate missing wind

        nk = len(z)
        u = np.empty(nk, np.float32)
        v = np.empty(nk, np.float32)

        for k in range(nk):
           if wdir[k] == -9999. and wspd[k] == -9999.:
              u[k] = v[k] = -9999.
           else:
              u[k], v[k] = dyn.wind_deg_to_uv(wdir[k], wspd[k])

           #print('{0:5.2f} {1:5.2f} = {2:5.2f} {3:5.2f}'.format(wdir[k], wspd[k], u[k], v[k]))
           
        _z = np.empty(2,np.float32)
        _u = np.empty(2,np.float32)
        _v = np.empty(2,np.float32)
        print('INTERPOLATING')
        for k in range(nk):
           if wdir[k] == -9999. and wspd[k] == -9999.:
              kb = ke = k
              while kb >= 0 and wdir[kb] == -9999. and wspd[kb] == -9999.:
                 kb -= 1
              while ke <= nk-1 and wdir[ke] == -9999. and wspd[ke] == -9999.:
                 ke += 1

              # everything in bounds
              if kb >= 0 and ke <= nk-1:
                 _z[0] = z[kb]
                 _z[1] = z[ke]
                 _u[0] = u[kb]
                 _u[1] = u[ke]
                 _v[0] = v[kb]
                 _v[1] = v[ke]
                 
                 u[k] = pymeteo.interp.linear(_z, _u, z[k])
                 v[k] = pymeteo.interp.linear(_z, _v, z[k])

              elif kb < 0:
                 u[k] = u[ke]
                 v[k] = v[ke] 
              elif ke > nk-1:
                 u[k] = u[kb]
                 v[k] = v[kb] 

        for k in range(nk):
           # kt to m/s
           u[k] = u[k] * 0.5144444
           v[k] = v[k] * 0.5144444
        #   print('{0:5.2f} {1:5.2f} = {2:5.2f} {3:5.2f}'.format(wdir[k], wspd[k], u[k], v[k]))

        # calc theta
        th = np.empty(nk, np.float32)
        # calc qv
        qv = np.empty(nk, np.float32)
        for k in range(nk):
           th[k] = met.theta(T[k]+met.T00, p[k]) 
           w = met.es(Td[k]+met.T00) / met.es(T[k]+met.T00)
           pp = met.es(T[k]+met.T00) / p[k]
           qv[k] = 0.622 * pp * w
           #qv[k] = met.es(Td[k]+met.T00) / (met.Rv * (T[k]+met.T00))

        #print(z, th, p, qv, u, v)

        plot(None, z, th, p, qv, u, v, output, title='Sounding Data')
Ejemplo n.º 2
0
def plot_sounding_data_csv(filename, output):
    """Plot SkewT from a CSV sounding data file
    
        :param filename: The name of the file to open.
        :type filename: str
        :param output: The name of the file to output plot
        :type output: str

        The datafile format is CSV with the following columns: 

        - pressure (mb)
        - height (m)
        - temperature (C)
        - dew point (C)
        - wind direction (degrees)
        - wind speed (m/s)
        
        Missing values should be filled with the value -9999.00
        """
    p, z, T, Td, wdir, wspd = np.loadtxt(filename, delimiter=',', unpack=True)
    # Pressure to Pa
    p = p * 100.

    # z to km
    #z = z / 1000.

    # interpolate missing wind

    nk = len(z)
    u = np.empty(nk, np.float32)
    v = np.empty(nk, np.float32)

    for k in range(nk):
        if wdir[k] == -9999. and wspd[k] == -9999.:
            u[k] = v[k] = -9999.
        else:
            u[k], v[k] = dyn.wind_deg_to_uv(wdir[k], wspd[k])

        #print('{0:5.2f} {1:5.2f} = {2:5.2f} {3:5.2f}'.format(wdir[k], wspd[k], u[k], v[k]))

    _z = np.empty(2, np.float32)
    _u = np.empty(2, np.float32)
    _v = np.empty(2, np.float32)
    print('INTERPOLATING')
    for k in range(nk):
        if wdir[k] == -9999. and wspd[k] == -9999.:
            kb = ke = k
            while kb >= 0 and wdir[kb] == -9999. and wspd[kb] == -9999.:
                kb -= 1
            while ke <= nk - 1 and wdir[ke] == -9999. and wspd[ke] == -9999.:
                ke += 1

            # everything in bounds
            if kb >= 0 and ke <= nk - 1:
                _z[0] = z[kb]
                _z[1] = z[ke]
                _u[0] = u[kb]
                _u[1] = u[ke]
                _v[0] = v[kb]
                _v[1] = v[ke]

                u[k] = pymeteo.interp.linear(_z, _u, z[k])
                v[k] = pymeteo.interp.linear(_z, _v, z[k])

            elif kb < 0:
                u[k] = u[ke]
                v[k] = v[ke]
            elif ke > nk - 1:
                u[k] = u[kb]
                v[k] = v[kb]

    for k in range(nk):
        # kt to m/s
        u[k] = u[k] * 0.5144444
        v[k] = v[k] * 0.5144444
    #   print('{0:5.2f} {1:5.2f} = {2:5.2f} {3:5.2f}'.format(wdir[k], wspd[k], u[k], v[k]))

    # calc theta
    th = np.empty(nk, np.float32)
    # calc qv
    qv = np.empty(nk, np.float32)
    for k in range(nk):
        th[k] = met.theta(T[k] + met.T00, p[k])
        w = met.es(Td[k] + met.T00) / met.es(T[k] + met.T00)
        pp = met.es(T[k] + met.T00) / p[k]
        qv[k] = 0.622 * pp * w
        #qv[k] = met.es(Td[k]+met.T00) / (met.Rv * (T[k]+met.T00))

    #print(z, th, p, qv, u, v)

    plot(None, z, th, p, qv, u, v, output, title='Sounding Data')
# Read data
ds = xr.open_dataset(file)
p = ds['pressure'].isel({'sounding': 0}).values * units.hPa
z = ds['altitude'].isel({'sounding': 0}).values * units.meters
T = ds['temperature'].isel({'sounding': 0}).values * units.degC
Td = ds['dewPoint'].isel({'sounding': 0}).values * units.degC
wind_speed = ds['windSpeed'].isel({
    'sounding': 0
}).values * (units.meter / units.second)
wind_dir = ds['windDirection'].isel({'sounding': 0}).values * units.degrees

# Calculate input for skewt
u, v = mpcalc.wind_components(wind_speed, wind_dir)
th = met.theta(np.array(T) + met.T00, np.array(p) * 100)
w = met.es(np.array(Td) + met.T00) / met.es(np.array(T) + met.T00)
pp = met.es(np.array(T) + met.T00) / (np.array(p) * 100)
qv = 0.622 * pp * w

# Create skewt

skewt.plot(None,
           np.array(z),
           th,
           np.array(p) * 100,
           qv,
           np.array(u),
           np.array(v),
           'sounding.pdf',
           title=os.path.basename(file))