Beispiel #1
0
def get_sounding(station):
###################################### Collect Sounding ################################################
    cache = settings.AppCache
    #Retrieves Sounding Details and returns them
    
    #If soundings are cached and valid, returns the cache, otherwise retreive new sounding
    key='sounding_'+str(station)
    df = cache.get(key)

    if df is not None:
        #Get sounding from cache and return it
        logging.info("Sounding Cache Hit")
        print("Sounding Cahce Hit")
        return df
    else:

        logging.info("Cache Miss")
        print("Cahce Miss")
        today = datetime.utcnow()
        if today.hour<1:
        #If date is not yet 02:00 make it yesterday... :)
            today = datetime.utcnow() - timedelta(days=1) 
            print("Trying yesterday's sounding data",file=sys.stdout)
        if (today.hour > 13):
            hour = 12
        else:
            hour = 0
        try:
            date = datetime(today.year, today.month, today.day, hour)
            df = WyomingUpperAir.request_data(date, station)
            cache.set(key, df, expire=1800,tag='Sounding Data ')

            #TODO GET LATEST DATE WHEN AVAILABLE
        except Exception as e:
            if (hour==12):
                hour = 0
                print("Unable to retriece 12Z data attempting 00Z data",file=sys.stderr)
                try:
                    date = datetime(today.year, today.month, today.day, hour)
                    station = '40179'
                    df = WyomingUpperAir.request_data(date, station)
                    #TODO GET LATEST DATE WHEN AVAILABLE
                    cache.set(key, df, expire=1800,tag='Sounding Data ')
                except Exception as e:
                    print(e,file=sys.stderr)
                    sys.exit(-1)
               
            else:
                print(e,file=sys.stderr)
                sys.exit(-1)

    return df
Beispiel #2
0
    def read_web(self, date):

        #Pull down the sounding
        sounding = WyomingUpperAir.request_data(date, self.fpath)

        #Convert sounding to proper data format and attach to PySonde object
        self.release_time = datetime.utcfromtimestamp(
            sounding["time"].values[0].tolist() / 1e9)
        self.release_site = sounding["station"].values[0]
        self.release_lat = sounding["latitude"].values[0] * mu(
            sounding.units["latitude"]).to(mu.deg)
        self.release_lon = sounding["longitude"].values[0] * mu(
            sounding.units["longitude"]).to(mu.deg)
        self.release_elv = sounding["elevation"].values[0] * mu(
            sounding.units["elevation"]).to(mu.meter)

        skeys = ["pres", "temp", "dewp", "uwind", "vwind", "lon", "lat", "alt"]
        wkeys = [
            "pressure", "temperature", "dewpoint", "u_wind", "v_wind",
            "longitude", "latitude", "height"
        ]
        for sk, wk in zip(skeys, wkeys):
            self.sounding[sk] = sounding[wk].values * mu(
                sounding.units[wk]).to(self.sounding_units[sk])

        #Fill in time array with Nans
        self.sounding["time"] = numpy.ones(
            self.sounding["pres"].shape) * numpy.nan

        #Ensure that heights are AMSL and not AGL
        if (self.sounding["alt"][0] < self.release_elv):
            self.sounding["alt"] += self.release_elv

        #Returning
        return
def acquire_sounding_wind_data(date, station):
    """
    Get sounding data using siphon, extract necessary variables and return a
    dictionary with heigth and wind data for pyart FourDD algorithm.

    Parameters
    ----------
    date: date of the sounding (datetime.datetime)
    station: name of the METAR sounding station

    Returns
    -------
    d: "dictionary" of sounding wind data
    """

    sounding = WyomingUpperAir.request_data(date, station)
    sounding = sounding.dropna(subset=('height', 'speed', 'direction',
                                       'u_wind', 'v_wind'),
                               how='all')

    height = sounding['height'].values
    speed = sounding['speed'].values
    direction = sounding['direction'].values
    lat = sounding['latitude'].values
    lon = sounding['longitude'].values

    profile = HorizontalWindProfile(height,
                                    speed,
                                    direction,
                                    latitude=lat,
                                    longitude=lon)

    return profile
Beispiel #4
0
def test_wyoming_no_station():
    """Test that we handle stations with no ID from the Wyoming archive."""
    df = WyomingUpperAir.request_data(datetime(1976, 3, 4, 0), '72349')

    assert (df['time'][0] == datetime(1976, 3, 4))
    assert (df['station'][0] == '')
    assert (df['station_number'][0] == 72349)
    assert (df['latitude'][0] == 36.88)
    assert (df['longitude'][0] == -93.9)
    assert (df['elevation'][0] == 438.0)

    assert_almost_equal(df['pressure'][5], 884.0, 2)
    assert_almost_equal(df['height'][5], 1140, 2)
    assert_almost_equal(df['temperature'][5], 14.6, 2)
    assert_almost_equal(df['dewpoint'][5], 12.8, 2)
    assert_almost_equal(df['u_wind'][5], -10.940, 2)
    assert_almost_equal(df['v_wind'][5], 25.774, 2)
    assert_almost_equal(df['speed'][5], 28.0, 1)
    assert_almost_equal(df['direction'][5], 157.0, 1)

    assert (df.units['pressure'] == 'hPa')
    assert (df.units['height'] == 'meter')
    assert (df.units['temperature'] == 'degC')
    assert (df.units['dewpoint'] == 'degC')
    assert (df.units['u_wind'] == 'knot')
    assert (df.units['v_wind'] == 'knot')
    assert (df.units['speed'] == 'knot')
    assert (df.units['direction'] == 'degrees')
    assert (df.units['latitude'] == 'degrees')
    assert (df.units['longitude'] == 'degrees')
    assert (df.units['elevation'] == 'meter')
    assert (df.units['station'] is None)
    assert (df.units['station_number'] is None)
    assert (df.units['time'] is None)
Beispiel #5
0
def test_wyoming():
    """Test that we are properly parsing data from the Wyoming archive."""
    df = WyomingUpperAir.request_data(datetime(1999, 5, 4, 0), 'OUN')

    assert (df['time'][0] == datetime(1999, 5, 4, 0))
    assert (df['station'][0] == 'OUN')
    assert (df['station_number'][0] == 72357)
    assert (df['latitude'][0] == 35.18)
    assert (df['longitude'][0] == -97.44)
    assert (df['elevation'][0] == 345.0)

    assert_almost_equal(df['pressure'][5], 867.9, 2)
    assert_almost_equal(df['height'][5], 1219., 2)
    assert_almost_equal(df['temperature'][5], 17.4, 2)
    assert_almost_equal(df['dewpoint'][5], 14.3, 2)
    assert_almost_equal(df['u_wind'][5], 6.60, 2)
    assert_almost_equal(df['v_wind'][5], 37.42, 2)
    assert_almost_equal(df['speed'][5], 38.0, 1)
    assert_almost_equal(df['direction'][5], 190.0, 1)

    assert (df.units['pressure'] == 'hPa')
    assert (df.units['height'] == 'meter')
    assert (df.units['temperature'] == 'degC')
    assert (df.units['dewpoint'] == 'degC')
    assert (df.units['u_wind'] == 'knot')
    assert (df.units['v_wind'] == 'knot')
    assert (df.units['speed'] == 'knot')
    assert (df.units['direction'] == 'degrees')
    assert (df.units['latitude'] == 'degrees')
    assert (df.units['longitude'] == 'degrees')
    assert (df.units['elevation'] == 'meter')
    assert (df.units['station'] is None)
    assert (df.units['station_number'] is None)
    assert (df.units['time'] is None)
Beispiel #6
0
def read_upper_air_uwyo(date, p_lvl):

    pressures = np.full((len(stations)), np.nan)
    heights = np.full((len(stations)), np.nan)
    temperatures = np.full((len(stations)), np.nan)
    lats = np.full((len(stations)), np.nan)
    lons = np.full((len(stations)), np.nan)
    wspd = np.full((len(stations)), np.nan)
    wdir = np.full((len(stations)), np.nan)

    for ii, stn in enumerate(stations):

        print(stn, p_lvl)
        ####################################################
        # Make the request (a pandas dataframe is returned).
        try:
            df = WyomingUpperAir.request_data(date, stn)
        except ValueError:
            print("No data for station", stn)
            continue

        ##!#####################################################
        ##!## Units are stored in a dictionary with the variable name as the key in the `units` attribute
        ##!## of the dataframe.
        ##!#print(df.units)
        ##!#
        ##!#####################################################
        ##!#print(df.units['pressure'])

        ##!#####################################################
        ##!## Units can then be attached to the values from the dataframe.
        ##!#pressure = df['pressure'].values * units(df.units['pressure'])
        ##!#temperature = df['temperature'].values * units(df.units['temperature'])
        ##!#dewpoint = df['dewpoint'].values * units(df.units['dewpoint'])
        ##!#u_wind = df['u_wind'].values * units(df.units['u_wind'])
        ##!#v_wind = df['v_wind'].values * units(df.units['v_wind'])

        if (np.min(df['pressure']) > p_lvl):
            print("ERROR: ", stn, " data not high enough")
            continue
        else:
            pressures[ii] = df['pressure'][df['pressure'] == p_lvl]
            heights[ii] = df['height'][df['pressure'] == p_lvl]
            temperatures[ii] = df['temperature'][df['pressure'] == p_lvl]
            lats[ii] = df['latitude'][df['pressure'] == p_lvl]
            lons[ii] = df['longitude'][df['pressure'] == p_lvl]
            wspd[ii] = df['speed'][df['pressure'] == p_lvl]
            wdir[ii] = df['direction'][df['pressure'] == p_lvl]

    out_dict = {}
    out_dict['stations'] = stations
    out_dict['pressures'] = pressures
    out_dict['heights'] = heights
    out_dict['temperatures'] = temperatures
    out_dict['lats'] = lats
    out_dict['lons'] = lons
    out_dict['wspd'] = wspd
    out_dict['wdir'] = wdir

    return out_dict
Beispiel #7
0
def test_high_alt_wyoming():
    """Test Wyoming data that starts at pressure less than 925 hPa."""
    df = WyomingUpperAir.request_data(datetime(2010, 12, 9, 12), 'BOI')

    assert (df['time'][0] == datetime(2010, 12, 9, 12))
    assert (df['station'][0] == 'BOI')
    assert (df['station_number'][0] == 72681)
    assert (df['latitude'][0] == 43.56)
    assert (df['longitude'][0] == -116.21)
    assert (df['elevation'][0] == 874.0)

    assert_almost_equal(df['pressure'][2], 890.0, 2)
    assert_almost_equal(df['height'][2], 1133., 2)
    assert_almost_equal(df['temperature'][2], 5.4, 2)
    assert_almost_equal(df['dewpoint'][2], 3.9, 2)
    assert_almost_equal(df['u_wind'][2], -0.42, 2)
    assert_almost_equal(df['v_wind'][2], 5.99, 2)
    assert_almost_equal(df['speed'][2], 6.0, 1)
    assert_almost_equal(df['direction'][2], 176.0, 1)

    assert (df.units['pressure'] == 'hPa')
    assert (df.units['height'] == 'meter')
    assert (df.units['temperature'] == 'degC')
    assert (df.units['dewpoint'] == 'degC')
    assert (df.units['u_wind'] == 'knot')
    assert (df.units['v_wind'] == 'knot')
    assert (df.units['speed'] == 'knot')
    assert (df.units['direction'] == 'degrees')
    assert (df.units['latitude'] == 'degrees')
    assert (df.units['longitude'] == 'degrees')
    assert (df.units['elevation'] == 'meter')
    assert (df.units['station'] is None)
    assert (df.units['station_number'] is None)
    assert (df.units['time'] is None)
Beispiel #8
0
def test_high_alt_wyoming():
    """Test Wyoming data that starts at pressure less than 925 hPa."""
    df = WyomingUpperAir.request_data(datetime(2010, 12, 9, 12), 'BOI')

    assert(df['time'][0] == datetime(2010, 12, 9, 12))
    assert(df['station'][0] == 'BOI')
    assert(df['station_number'][0] == 72681)
    assert(df['latitude'][0] == 43.56)
    assert(df['longitude'][0] == -116.21)
    assert(df['elevation'][0] == 874.0)

    assert_almost_equal(df['pressure'][2], 890.0, 2)
    assert_almost_equal(df['height'][2], 1133., 2)
    assert_almost_equal(df['temperature'][2], 5.4, 2)
    assert_almost_equal(df['dewpoint'][2], 3.9, 2)
    assert_almost_equal(df['u_wind'][2], -0.42, 2)
    assert_almost_equal(df['v_wind'][2], 5.99, 2)
    assert_almost_equal(df['speed'][2], 6.0, 1)
    assert_almost_equal(df['direction'][2], 176.0, 1)

    assert(df.units['pressure'] == 'hPa')
    assert(df.units['height'] == 'meter')
    assert(df.units['temperature'] == 'degC')
    assert(df.units['dewpoint'] == 'degC')
    assert(df.units['u_wind'] == 'knot')
    assert(df.units['v_wind'] == 'knot')
    assert(df.units['speed'] == 'knot')
    assert(df.units['direction'] == 'degrees')
    assert(df.units['latitude'] == 'degrees')
    assert(df.units['longitude'] == 'degrees')
    assert(df.units['elevation'] == 'meter')
    assert(df.units['station'] is None)
    assert(df.units['station_number'] is None)
    assert(df.units['time'] is None)
Beispiel #9
0
def test_wyoming_no_station():
    """Test that we handle stations with no ID from the Wyoming archive."""
    df = WyomingUpperAir.request_data(datetime(1976, 3, 4, 0), '72349')

    assert(df['time'][0] == datetime(1976, 3, 4))
    assert(df['station'][0] == '')
    assert(df['station_number'][0] == 72349)
    assert(df['latitude'][0] == 36.88)
    assert(df['longitude'][0] == -93.9)
    assert(df['elevation'][0] == 438.0)

    assert_almost_equal(df['pressure'][5], 884.0, 2)
    assert_almost_equal(df['height'][5], 1140, 2)
    assert_almost_equal(df['temperature'][5], 14.6, 2)
    assert_almost_equal(df['dewpoint'][5], 12.8, 2)
    assert_almost_equal(df['u_wind'][5], -10.940, 2)
    assert_almost_equal(df['v_wind'][5], 25.774, 2)
    assert_almost_equal(df['speed'][5], 28.0, 1)
    assert_almost_equal(df['direction'][5], 157.0, 1)

    assert(df.units['pressure'] == 'hPa')
    assert(df.units['height'] == 'meter')
    assert(df.units['temperature'] == 'degC')
    assert(df.units['dewpoint'] == 'degC')
    assert(df.units['u_wind'] == 'knot')
    assert(df.units['v_wind'] == 'knot')
    assert(df.units['speed'] == 'knot')
    assert(df.units['direction'] == 'degrees')
    assert(df.units['latitude'] == 'degrees')
    assert(df.units['longitude'] == 'degrees')
    assert(df.units['elevation'] == 'meter')
    assert(df.units['station'] is None)
    assert(df.units['station_number'] is None)
    assert(df.units['time'] is None)
Beispiel #10
0
def test_wyoming():
    """Test that we are properly parsing data from the Wyoming archive."""
    df = WyomingUpperAir.request_data(datetime(1999, 5, 4, 0), 'OUN')

    assert(df['time'][0] == datetime(1999, 5, 4, 0))
    assert(df['station'][0] == 'OUN')
    assert(df['station_number'][0] == 72357)
    assert(df['latitude'][0] == 35.18)
    assert(df['longitude'][0] == -97.44)
    assert(df['elevation'][0] == 345.0)

    assert_almost_equal(df['pressure'][5], 867.9, 2)
    assert_almost_equal(df['height'][5], 1219., 2)
    assert_almost_equal(df['temperature'][5], 17.4, 2)
    assert_almost_equal(df['dewpoint'][5], 14.3, 2)
    assert_almost_equal(df['u_wind'][5], 6.60, 2)
    assert_almost_equal(df['v_wind'][5], 37.42, 2)
    assert_almost_equal(df['speed'][5], 38.0, 1)
    assert_almost_equal(df['direction'][5], 190.0, 1)

    assert(df.units['pressure'] == 'hPa')
    assert(df.units['height'] == 'meter')
    assert(df.units['temperature'] == 'degC')
    assert(df.units['dewpoint'] == 'degC')
    assert(df.units['u_wind'] == 'knot')
    assert(df.units['v_wind'] == 'knot')
    assert(df.units['speed'] == 'knot')
    assert(df.units['direction'] == 'degrees')
    assert(df.units['latitude'] == 'degrees')
    assert(df.units['longitude'] == 'degrees')
    assert(df.units['elevation'] == 'meter')
    assert(df.units['station'] is None)
    assert(df.units['station_number'] is None)
    assert(df.units['time'] is None)
Beispiel #11
0
def getData(station_file, hh):
    """
    This function will make use of Siphons Wyoming Upperair utility. Will pass
    the function a list of stations and data will be downloaded as pandas a 
    pandas dataframe and the corresponding lats and lons will be placed into
    a dictionary along with the data. 
    """

    print('Getting station data...')
    station_data = pd.read_excel(station_file)
    stations, lats, lons = station_data['station_id'].values, station_data[
        'lat'].values, station_data['lon'].values
    stations = list(stations)
    date = datetime.utcnow()
    date = datetime(date.year, date.month, date.day, hh)
    data = {}  #a dictionary for our data
    station_list = []

    for station, lat, lon in zip(stations, lats, lons):
        try:
            df = WyomingUpperAir.request_data(date, station)
            data[station] = [df, lat, lon]
            station_list += [station]
        except:
            pass
    print('Data retrieved...')
    return data, station_list
Beispiel #12
0
def generate_plot(site, date=None):

    if date:
        request_time = datetime.strptime(date, '%Y%m%d%H')
    else:
        now = datetime.now(timezone.utc) - timedelta(hours=2)
        request_time = now.replace(hour=(now.hour // 12) * 12,
                                   minute=0,
                                   second=0)

    # Request the data and plot
    df = WyomingUpperAir.request_data(request_time, site)
    skewt = plot_skewt(df)

    # Add the timestamp for the data to the plot
    add_timestamp(skewt.ax,
                  request_time,
                  y=1.02,
                  x=0,
                  ha='left',
                  fontsize='large')
    skewt.ax.set_title(site)
    # skewt.ax.figure.savefig(make_name(site, date, request_time))

    bio = io.BytesIO()
    skewt.ax.figure.savefig(bio, format='svg')
    bio.seek(0)
    b64 = base64.b64encode(bio.read())
    message = {}
    message['station_id'] = site
    message['sounding'] = b64
    db.soundings.replace_one({'station_id': site}, message, upsert=True)
Beispiel #13
0
def return_ensemble(*, data_file_path, ens_params, coords, flags):
    sat_time = coords.sat_times[0]
    wind_time = return_wind_time(sat_time=sat_time, coords=coords)
    q = return_single_time(data_file_path, coords.sat_times_all, sat_time,
                           [coords.sn_slice], [coords.we_slice], ['ci'])[0]
    if flags['radiosonde']:
        station = 'TUS'
        df = WyomingUpperAir.request_data(sat_time.date(), station)
        T = df['temperature'].values * units(df.units['temperature'])
        Td = df['dewpoint'].values * units(df.units['dewpoint'])
        u_wind = df['u_wind'].values * units(df.units['u_wind'])
        u_wind = u_wind.to(units.meter / units.second)
        v_wind = df['v_wind'].values * units(df.units['v_wind'])
        v_wind = v_wind.to(units.meter / units.second)
        rh = thermo.relative_humidity_from_dewpoint(T, Td)
        max_arg = np.argmax(rh)
        u_size = coords.we_stag_crop.size * coords.sn_crop.size
        v_size = coords.we_crop.size * coords.sn_stag_crop.size
        U = np.ones(u_size) * u_wind[max_arg]
        V = np.ones(v_size) * v_wind[max_arg]
    elif flags['opt_flow']:
        opt_flow_folder = os.path.split(data_file_path)[0]
        opt_flow_file = os.path.join(opt_flow_folder, 'data_opt_flow.nc')
        of_sat_time = coords.sat_times[1]
        U, V = return_single_time(opt_flow_file, coords.sat_times_all,
                                  of_sat_time,
                                  [coords.sn_slice, coords.sn_stag_slice],
                                  [coords.we_stag_slice, coords.we_slice],
                                  ['U_opt_flow', 'V_opt_flow'])

        time_step = (of_sat_time - sat_time).seconds
        U = U * (250 / time_step)
        V = V * (250 / time_step)
        U = U.clip(min=-50, max=50)
        V = V.clip(min=-50, max=50)
    else:
        U, V = return_single_time(data_file_path, coords.wind_times, wind_time,
                                  [coords.sn_slice, coords.sn_stag_slice],
                                  [coords.we_stag_slice, coords.we_slice],
                                  ['U', 'V'])
        U, V = smooth_winds(U, V)
        if flags['wrf_mean']:
            U = np.ones_like(U) * U.mean()
            V = np.ones_like(V) * V.mean()
    if flags['assim']:
        ensemble = ensemble_creator(q,
                                    U,
                                    V,
                                    CI_sigma=ens_params['ci_sigma'],
                                    wind_sigma=ens_params['winds_sigma'],
                                    ens_size=ens_params['ens_num'])
    else:
        ensemble = np.concatenate([U.ravel(), V.ravel(), q.ravel()])[:, None]
    shape = ensemble.shape
    ensemble = np.ma.compressed(ensemble).reshape(shape)
    return ensemble
Beispiel #14
0
def test_high_alt_wyoming():
    """Test Wyoming data that starts at pressure less than 925 hPa."""
    df = WyomingUpperAir.request_data(datetime(2010, 12, 9, 12), 'BOI')

    assert_almost_equal(df['pressure'][2], 890.0, 2)
    assert_almost_equal(df['height'][2], 1133., 2)
    assert_almost_equal(df['temperature'][2], 5.4, 2)
    assert_almost_equal(df['dewpoint'][2], 3.9, 2)
    assert_almost_equal(df['u_wind'][2], -0.42, 2)
    assert_almost_equal(df['v_wind'][2], 5.99, 2)
Beispiel #15
0
def test_high_alt_wyoming():
    """Test Wyoming data that starts at pressure less than 925 hPa."""
    df = WyomingUpperAir.request_data(datetime(2010, 12, 9, 12), 'BOI')

    assert_almost_equal(df['pressure'][2], 890.0, 2)
    assert_almost_equal(df['height'][2], 1133., 2)
    assert_almost_equal(df['temperature'][2], 5.4, 2)
    assert_almost_equal(df['dewpoint'][2], 3.9, 2)
    assert_almost_equal(df['u_wind'][2], -0.42, 2)
    assert_almost_equal(df['v_wind'][2], 5.99, 2)
Beispiel #16
0
def get_sounding_data(date, station):

    df = WyomingUpperAir.request_data(date, station)

    p = df['pressure'].values * units(df.units['pressure'])
    T = df['temperature'].values * units(df.units['temperature'])
    Td = df['dewpoint'].values * units(df.units['dewpoint'])
    u = df['u_wind'].values * units(df.units['u_wind'])
    v = df['v_wind'].values * units(df.units['v_wind'])
    windspeed = df['speed'].values * units(df.units['speed'])

    return p, T, Td, u, v, windspeed
def get_sounding_data(date, station):

    df = WyomingUpperAir.request_data(date, station)

    p = df['pressure'].values * units(df.units['pressure'])
    T = df['temperature'].values * units(df.units['temperature'])
    Td = df['dewpoint'].values * units(df.units['dewpoint'])
    u = df['u_wind'].values * units(df.units['u_wind'])
    v = df['v_wind'].values * units(df.units['v_wind'])
    windspeed = df['speed'].values * units(df.units['speed'])

    return p, T, Td, u, v, windspeed
Beispiel #18
0
def WyomingSondes(date_time, station_id):

    df = WyomingUpperAir.request_data(date, station)

    #Extracting the variables and attaching units
    p = df['pressure'].values * units(df.units['pressure'])
    T = df['temperature'].values * units(df.units['temperature'])
    Td = df['dewpoint'].values * units(df.units['dewpoint'])
    u = df['u_wind'].values * units(df.units['u_wind'])
    v = df['v_wind'].values * units(df.units['v_wind'])
    heights = df['height'].values * units(df.units['height'])

    return p, T, Td, u, v, heights, df
Beispiel #19
0
def add_curves_Wyoming(ax, datetime, station, linewidth=1.0, LH_Tdepend=False):
    """
    overlaying new curves of multiple soundings from Wyoming datasets
    date: using datetime module. ex. datetime(2018,06,06) 
    station: station name. ex. 'MFL' Miami, Florida
    """
    from siphon.simplewebservice.wyoming import WyomingUpperAir

    date = datetime
    station = station
    df = WyomingUpperAir.request_data(date, station)
    pressure = df['pressure'].values
    Temp = df['temperature'].values
    Temp_dew = df['dewpoint'].values
    altitude = df['height'].values
    q = mpcalc.mixing_ratio(
        mpcalc.saturation_vapor_pressure(Temp_dew * units('degC')),
        pressure * units('mbar'))
    q = mpcalc.specific_humidity_from_mixing_ratio(q)
    qs = mpcalc.mixing_ratio(
        mpcalc.saturation_vapor_pressure(Temp * units('degC')),
        pressure * units('mbar'))

    # specific energies
    if LH_Tdepend == False:
        mse = mpcalc.moist_static_energy(altitude * units('meter'),
                                         Temp * units('degC'), q)
        mse_s = mpcalc.moist_static_energy(altitude * units('meter'),
                                           Temp * units('degC'), qs)
        dse = mpcalc.dry_static_energy(altitude * units('meter'),
                                       Temp * units('degC'))
    else:
        # A short course in cloud physics, Roger and Yau (1989)
        Lvt = (2500.8 - 2.36 * T.magnitude +
               0.0016 * T.magnitude**2 - 0.00006 * T.magnitude**3) * units(
                   'joule/gram')  # latent heat of evaporation
        #Lf = 2834.1 - 0.29*T - 0.004*T**2                  # latent heat of fusion

        mse = Cp_d * T + g * altitude + Lvt * q
        mse_s = Cp_d * T + g * altitude + Lvt * qs
        dse = mpcalc.dry_static_energy(altitude, T)

    # adding curves on the main axes
    ax.plot(dse.magnitude, pressure, 'k', linewidth=linewidth)
    ax.plot(mse.magnitude, pressure, 'b', linewidth=linewidth)
    ax.plot(mse_s.magnitude, pressure, 'r', linewidth=linewidth)
Beispiel #20
0
def test_wyoming():
    """Test that we are properly parsing data from the wyoming archive."""
    df = WyomingUpperAir.request_data(datetime(1999, 5, 4, 0), 'OUN')

    assert_almost_equal(df['pressure'][5], 867.9, 2)
    assert_almost_equal(df['height'][5], 1219., 2)
    assert_almost_equal(df['temperature'][5], 17.4, 2)
    assert_almost_equal(df['dewpoint'][5], 14.3, 2)
    assert_almost_equal(df['u_wind'][5], 6.60, 2)
    assert_almost_equal(df['v_wind'][5], 37.42, 2)
    assert_almost_equal(df['speed'][5], 38.0, 1)
    assert_almost_equal(df['direction'][5], 190.0, 1)

    assert (df.units['pressure'] == 'hPa')
    assert (df.units['height'] == 'meter')
    assert (df.units['temperature'] == 'degC')
    assert (df.units['dewpoint'] == 'degC')
    assert (df.units['u_wind'] == 'knot')
    assert (df.units['v_wind'] == 'knot')
    assert (df.units['speed'] == 'knot')
    assert (df.units['direction'] == 'degrees')
Beispiel #21
0
def test_wyoming():
    """Test that we are properly parsing data from the wyoming archive."""
    df = WyomingUpperAir.request_data(datetime(1999, 5, 4, 0), 'OUN')

    assert_almost_equal(df['pressure'][5], 867.9, 2)
    assert_almost_equal(df['height'][5], 1219., 2)
    assert_almost_equal(df['temperature'][5], 17.4, 2)
    assert_almost_equal(df['dewpoint'][5], 14.3, 2)
    assert_almost_equal(df['u_wind'][5], 6.60, 2)
    assert_almost_equal(df['v_wind'][5], 37.42, 2)
    assert_almost_equal(df['speed'][5], 38.0, 1)
    assert_almost_equal(df['direction'][5], 190.0, 1)

    assert(df.units['pressure'] == 'hPa')
    assert(df.units['height'] == 'meter')
    assert(df.units['temperature'] == 'degC')
    assert(df.units['dewpoint'] == 'degC')
    assert(df.units['u_wind'] == 'knot')
    assert(df.units['v_wind'] == 'knot')
    assert(df.units['speed'] == 'knot')
    assert(df.units['direction'] == 'degrees')
                        type=str)
    parser.add_argument('-f', '--filename', help='Image filename', type=str)
    args = parser.parse_args()

    if args.date:
        request_time = datetime.strptime(args.date, '%Y%m%d%H')
    else:
        # Figure out the most recent sounding, 00 or 12. Subtracting two hours
        # helps ensure that we choose a time with data available.
        now = datetime.utcnow() - timedelta(hours=2)
        request_time = now.replace(hour=(now.hour // 12) * 12,
                                   minute=0,
                                   second=0)

    # Request the data and plot
    df = WyomingUpperAir.request_data(request_time, args.site)
    skewt = plot_skewt(df)

    # Add the timestamp for the data to the plot
    add_timestamp(skewt.ax,
                  request_time,
                  y=1.02,
                  x=0,
                  ha='left',
                  fontsize='large')
    skewt.ax.set_title(args.site)

    if args.show:
        plt.show()
    else:
        fname = args.filename if args.filename else make_name(
Beispiel #23
0
    minute = '00'

    mdate = str(year) + str(month) + str(day) + '_' + hour + minute
    return mdate


verification_time = get_verif_time(current_utc)
model_init_time = model_init_time(current_utc)
mdate = model_init_string(model_init_time)
print(verification_time)
print(model_init_time)
print(mdate)

for i in range(len(ideal_ua_station_ids)):
    try:
        df = WyomingUpperAir.request_data(verification_time,
                                          ideal_ua_station_ids[i])
        lat = df['latitude'][0]
        lon = df['longitude'][0]
        p = df['pressure']
        h5 = df[(df['pressure'] == 500)]
        z5 = h5['height'].values[0]
        obs_z5.append(z5)
        coord = (lat, lon)
        coords.append(coord)
        print(coord)
        ua_station_ids.append(ideal_ua_station_ids[i])
    except:
        'Data Unavailable For ' + ideal_ua_station_ids[i]


def mkdir_p(mypath):
Beispiel #24
0
def plot_upper_air(station='11035', date=False):
    '''
    -----------------------------
    Default use of plot_upper_air:

    This will plot a SkewT sounding for station '11035' (Wien Hohe Warte)
    plot_upper_air(station='11035', date=False)
    '''
    # sns.set(rc={'axes.facecolor':'#343837', 'figure.facecolor':'#343837',
    #  'grid.linestyle':'','axes.labelcolor':'#04d8b2','text.color':'#04d8b2',
    #  'xtick.color':'#04d8b2','ytick.color':'#04d8b2'})
    # Get time in UTC
    station = str(station)
    if date is False:
        now = datetime.utcnow()
        # If morning then 0z sounding, otherwise 12z
        if now.hour < 12:
            hour = 0
        else:
            hour = 12
        date = datetime(now.year, now.month, now.day, hour)
        datestr = date.strftime('%Hz %Y-%m-%d')
        print('{}'.format(date))
    else:
        year = int(input('Please specify the year: '))
        month = int(input('Please specify the month: '))
        day = int(input('Please specify the day: '))
        hour = int(input('Please specify the hour: '))
        if hour < 12:
            hour = 0
        else:
            hour = 12
        date = datetime(year, month, day, hour)
        datestr = date.strftime('%Hz %Y-%m-%d')
        print('You entered {}'.format(date))

    # This requests the data 11035 is
    df = WyomingUpperAir.request_data(date, station)

    # Create single variables wih the right units
    p = df['pressure'].values * units.hPa
    T = df['temperature'].values * units.degC
    Td = df['dewpoint'].values * units.degC
    wind_speed = df['speed'].values * units.knots
    wind_dir = df['direction'].values * units.degrees

    wind_speed_6k = df['speed'][df.height <= 6000].values * units.knots
    wind_dir_6k = df['direction'][df.height <= 6000].values * units.degrees

    u, v = mpcalc.get_wind_components(wind_speed, wind_dir)
    u6, v6 = mpcalc.get_wind_components(wind_speed_6k, wind_dir_6k)

    # Calculate the LCL
    lcl_pressure, lcl_temperature = mpcalc.lcl(p[0], T[0], Td[0])
    print(lcl_pressure, lcl_temperature)
    # Calculate the parcel profile.
    parcel_prof = mpcalc.parcel_profile(p, T[0], Td[0]).to('degC')
    cape, cin = mpcalc.cape_cin(p, T, Td, parcel_prof)

    #############################
    # Create a new figure. The dimensions here give a good aspect ratio
    fig = plt.figure(figsize=(9, 9))
    gs = gridspec.GridSpec(3, 3)
    skew = SkewT(fig, rotation=45, subplot=gs[:, :2])

    # Plot the data using normal plotting functions, in this case using
    # log scaling in Y, as dictated by the typical meteorological plot
    skew.plot(p, T, 'r')
    skew.plot(p, Td, 'g')
    skew.plot_barbs(p, u, v)
    skew.ax.set_ylim(1000, 100)
    skew.ax.set_xlim(-45, 40)

    # Plot LCL as black dot
    skew.plot(lcl_pressure, lcl_temperature, 'ko', markerfacecolor='black')

    # Plot the parcel profile as a black line
    skew.plot(p, parcel_prof, 'k', linewidth=2)

    # Shade areas of CAPE and CIN
    skew.shade_cin(p, T, parcel_prof)
    skew.shade_cape(p, T, parcel_prof)

    # Plot a zero degree isotherm
    skew.ax.axvline(0, color='c', linestyle='--', linewidth=2)
    skew.ax.set_title('Station: ' + str(station) + '\n' + datestr)  # set title
    skew.ax.set_xlabel('Temperature (C)')
    skew.ax.set_ylabel('Pressure (hPa)')

    # Add the relevant special lines
    skew.plot_dry_adiabats(linewidth=0.7)
    skew.plot_moist_adiabats(linewidth=0.7)
    skew.plot_mixing_lines(linewidth=0.7)

    # Create a hodograph
    # Create an inset axes object that is 40% width and height of the
    # figure and put it in the upper right hand corner.
    # ax_hod = inset_axes(skew.ax, '40%', '40%', loc=1)
    ax = fig.add_subplot(gs[0, -1])
    h = Hodograph(ax, component_range=60.)
    h.add_grid(increment=20)
    # Plot a line colored by windspeed
    h.plot_colormapped(u6, v6, wind_speed_6k)

    # add another subplot for the text of the indices
    # ax_t = fig.add_subplot(gs[1:,2])
    skew2 = SkewT(fig, rotation=0, subplot=gs[1:, 2])
    skew2.plot(p, T, 'r')
    skew2.plot(p, Td, 'g')
    # skew2.plot_barbs(p, u, v)
    skew2.ax.set_ylim(1000, 700)
    skew2.ax.set_xlim(-30, 10)

    # Show the plot
    plt.show()

    return cape
Beispiel #25
0
# Set time using a datetime object and station as variables
#

dt = datetime(2016, 10, 26, 12)
station = 'MPX'

######################################################################
# Grab Remote Data
# ----------------
#
# This requires an internet connection to access the sounding data from a
# remote server at the University of Wyoming.
#

# Read remote sounding data based on time (dt) and station
df = WyomingUpperAir.request_data(dt, station)

# Create dictionary of united arrays
data = pandas_dataframe_to_unit_arrays(df)

######################################################################
# Isolate variables and attach units
#

# Isolate united arrays from dictionary to individual variables
p = data['pressure']
T = data['temperature']
Td = data['dewpoint']
u = data['u_wind']
v = data['v_wind']
Beispiel #26
0
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import math
from datetime import *
import metpy.calc as mpcalc  #pip install metpy
from metpy.plots import add_metpy_logo, skewt
from metpy.units import units
from siphon.simplewebservice.wyoming import WyomingUpperAir  #pip install siphon
date = datetime(2020, 9, 1, 06)
num_days = 4

data = pd.DataFrame()

for i in range(0, num_days):
    station = '78988'
    try:
        data = WyomingUpperAir.request_data(date, station)
        print(data)
    except:
        print("No existen datos")

    data.to_csv(
        f'/home/dirac/Dropbox/2020/WRFDA_FAC_EAFIT/Sondas/Descarga_Sondas/new_download/{station}{date}.csv',
        index=True)
    date = date + timedelta(days=1)
Beispiel #27
0
import numpy as np
import matplotlib.pyplot as plt
from siphon.simplewebservice.wyoming import WyomingUpperAir
from metpy.plots import SkewT
from datetime import datetime, timedelta
from metpy.units import units
import metpy.calc as metcalc

dataset = WyomingUpperAir.request_data(datetime(2018, 6, 27, 0), 'ILX')

#get_ipython().run_line_magic('matplotlib', 'inline')

p = dataset['pressure'].values * units(dataset.units['pressure'])
#ip100 = np.where(p.magnitude==100.)[0][0]+1
#p = p[:ip100]
T = dataset['temperature'].values * units(dataset.units['temperature'])
#T = T[:ip100]
Td = dataset['dewpoint'].values * units(dataset.units['dewpoint'])
#Td = Td[:ip100]
u = dataset['u_wind'].values * units(dataset.units['u_wind'])
#u = u[:ip100]
v = dataset['v_wind'].values * units(dataset.units['v_wind'])
#v = v[:ip100]

fig = plt.figure(figsize=(9, 9))
skew = SkewT(fig)
skew.plot(p, T, 'r')
skew.plot(p, Td, 'g')
skew.plot_barbs(p[:-1:2], u[:-1:2], v[:-1:2])
skew.plot_dry_adiabats()
skew.plot_moist_adiabats()
Beispiel #28
0
def upper_air(config,
              station_id,
              sounding_station_id,
              date,
              use_nan_sounding=False,
              use_existing=True,
              save=True):
    """
    Retrieves upper-air data and interpolates to pressure levels. If use_nan_sounding is True, then if a retrieval
    error occurs, a blank sounding will be returned instead of an error.
    :param config:
    :param station_id: station ID of surface station used
    :param sounding_station_id: station ID of sounding station to use
    :param date: datetime
    :param use_nan_sounding: bool: if True, use sounding of NaNs instead of raising an error
    :param use_existing: bool: preferentially use existing soundings in sounding_data_dir
    :param save: bool: if True, save processed soundings to sounding_data_dir
    :return:
    """
    variables = ['height', 'temperature', 'dewpoint', 'u_wind', 'v_wind']

    # Define levels for interpolation: same as model data, except omitting lowest_p_level
    plevs = [600, 750, 850, 925]
    pres_interp = np.array([p for p in plevs if p <= config['lowest_p_level']])

    # Try retrieving the sounding, first checking for existing
    if config['verbose']:
        print('upper_air: retrieving sounding for %s' %
              datetime.strftime(date, '%Y%m%d%H'))
    nan_sounding = False
    retrieve_sounding = False
    sndg_data_dir = config['Obs']['sounding_data_dir']
    if not (os.path.isdir(sndg_data_dir)):
        os.makedirs(sndg_data_dir)
    sndg_file = '%s/%s_SNDG_%s.pkl' % (sndg_data_dir, station_id,
                                       datetime.strftime(date, '%Y%m%d%H'))
    if use_existing:
        try:
            data = read_pkl(sndg_file)
            if config['verbose']:
                print('    Read from file.')
        except:
            retrieve_sounding = True
    else:
        retrieve_sounding = True
    if retrieve_sounding:
        try:
            dset = WyomingUpperAir.request_data(
                date, config['Obs']['sounding_station_id'])
        except:
            # Try again
            try:
                dset = WyomingUpperAir.request_data(
                    date, config['Obs']['sounding_station_id'])
            except:
                if use_nan_sounding:
                    if config['verbose']:
                        print(
                            'upper_air: warning: unable to retrieve sounding; using nan.'
                        )
                    nan_sounding = True
                else:
                    raise ValueError('error retrieving sounding for %s' % date)

        # Retrieve pressure for interpolation to fixed levels
        if not nan_sounding:
            pressure = dset.variables['pressure']
            pres = np.array([p.magnitude
                             for p in list(pressure)])  # units are hPa

        # Get variables and interpolate; add to dictionary
        data = OrderedDict()
        for var in variables:
            if not nan_sounding:
                var_data = dset.variables[var]
                var_array = np.array([v.magnitude for v in list(var_data)])
                var_interp = interp(pres_interp, pres, var_array)
                data[var] = var_interp.tolist()
            else:
                data[var] = [np.nan] * len(pres_interp)

        # Save
        if save and not nan_sounding:
            with open(sndg_file, 'wb') as handle:
                pickle.dump(data, handle, protocol=2)

    return data
Beispiel #29
0
def test_no_data_wyoming():
    """Test Wyoming data when no data are available."""
    with pytest.raises(ValueError):
        WyomingUpperAir.request_data(datetime(2010, 12, 9, 1), 'BOI')
Beispiel #30
0
def get_sounding_data(date, region, station):
    return WyomingUpperAir.request_data(date, station, region=region)
Beispiel #31
0
def get_sounding(ts, site, fill_value=None):
    requester = WyomingUpperAir()
    df = requester.request_data(time=pd.Timestamp(ts), site_id=site)

    return df.replace({np.nan: fill_value, np.inf: fill_value})
Beispiel #32
0
def test_no_data_wyoming():
    """Test Wyoming data when no data are available."""
    with pytest.raises(ValueError):
        WyomingUpperAir.request_data(datetime(2010, 12, 9, 1), 'BOI')
Beispiel #33
0
"""

from datetime import datetime

from metpy.units import units

from siphon.simplewebservice.wyoming import WyomingUpperAir

####################################################
# Create a datetime object for the sounding and string of the station identifier.
date = datetime(2017, 9, 10, 6)
station = 'MFL'

####################################################
# Make the request (a pandas dataframe is returned).
df = WyomingUpperAir.request_data(date, station)

####################################################
# Inspect data columns in the dataframe.
print(df.columns)

####################################################
# Pull out a specific column of data.
print(df['pressure'])

####################################################
# Units are stored in a dictionary with the variable name as the key in the `units` attribute
# of the dataframe.
print(df.units)

####################################################
# Import the Wyoming simple web service upper air object
from siphon.simplewebservice.wyoming import WyomingUpperAir

# Create the datetime and station variables you'll need
request_time = datetime(2011, 4, 14, 18)
station = 'OUN'

# Make the request for the data
df = WyomingUpperAir.request_data(request_time, station)

# Attach units to the data
sounding = pandas_dataframe_to_unit_arrays(df)
def grabSounding(date, station):
    df = WyomingUpperAir.request_data(date, station)
    pFull = df["pressure"].values * units(df.units["pressure"])
    TFull = df["temperature"].values * units(df.units["temperature"])
    return pFull, TFull