Esempio n. 1
0
def supermag(csv_file=None, ds=None, win_len=128, lag_range=10, **kwargs):
    '''
    Takes data from the SuperMAG website, windows it, and creates a network of
    stations that are connected based on canonical correlation coefficients.

    Various kwargs may pertain to the following functions:\n
    :func:`spaceweather.analysis.data_funcs.csv_to_Dataset`\n
    :func:`spaceweather.analysis.data_funcs.detrend`\n
    :func:`spaceweather.analysis.data_funcs.window`\n
    :func:`spaceweather.analysis.threshold.threshold`\n
    :func:`spaceweather.analysis.threshold.max_corr_lag`\n
    :func:`spaceweather.analysis.threshold.adj_mat`\n
    :func:`spaceweather.rcca`\n

    Parameters
    ----------
    csv_file : csv file
        CSV file downloaded from the SuperMAG website.
    ds : xarray.Dataset
        Data as converted by :func:`spaceweather.analysis.data_funcs.csv_to_Dataset`.
    win_len : int, optional
        Length of window in minutes. Default is 128.
    lag_range: int, optional
        The range, in minutes, of positive and negative shifts for the second
        station in each pair. Default is 10.

    Returns
    -------
    xarray.Dataset
        Dataset containing the adjacency coefficients.
            The data_vars are: adj_coeffs.\n
            The coordinates are: first_st, second_st, win_start.
    '''

    # if data given as csv, read into Dataset
    if ds is None:
        if csv_file is None:
            print('Error: you must input data as either\n 1) csv file\n 2) xarray.Dataset')
            return 'Error: you must input data as either\n 1) csv file\n 2) xarray.Dataset'
        else:
            ds = sad.csv_to_Dataset(csv_file = csv_file, **kwargs)

    # get adjacency matrix using lags
    adj_mat = sat.adj_mat(ds = ds, win_len = win_len,
                          lag_range = lag_range, **kwargs)

    # plot the network, or animate a gif or something later on

    return adj_mat
Esempio n. 2
0
import spaceweather.visualisation.heatmaps as svh
import spaceweather.visualisation.lines as svl
import spaceweather.visualisation.spectral_analysis as svs
import spaceweather.supermag as sm
import spaceweather.old as old
import numpy as np
# may need to install OpenSSL for cartopy to function properly
# I needed it on Windows, even though OpenSSL was already installed
# https://slproweb.com/products/Win32OpenSSL.html

################################################################################
####################### Analysis ###############################################
################################################################################

####################### threshold ##############################################
ds1 = sad.csv_to_Dataset(csv_file="Data/20190403-00-22-supermag.csv")

##### thresh_kf ----------------------------------------------------------------
thr_kf = old.thresh_kf(ds=ds1)

##### thresh_dods --------------------------------------------------------------
thr_dods = old.thresh_dods(ds=ds1)
thr_dods_25 = old.thresh_dods(ds=ds1, n0=0.25)

##### threshold ----------------------------------------------------------------
thresh_kf = old.threshold(ds=ds1, thr_meth='kf')
thresh_dods = old.threshold(ds=ds1, thr_meth='Dods')
thresh_dods_25 = old.threshold(ds=ds1, thr_meth='Dods', n0=0.25)

##### adj_mat ------------------------------------------------------------------
thr_ds = sad.csv_to_Dataset(csv_file="Data/20010305-16-38-supermag.csv")
def supermag(csv_file=None, ds=None, thr_meth='Dods', win_len=128, **kwargs):
    '''
    Takes data from the SuperMAG website, windows it, and creates a network of
    stations that are connected based on canonical correlation coefficients.
    Various kwargs may pertain to the following functions:\n
    :func:`spaceweather.analysis.threshold.threshold`\n
    :func:`spaceweather.analysis.threshold.thresh_kf`\n
    :func:`spaceweather.analysis.threshold.thresh_dods`\n
    :func:`spaceweather.analysis.threshold.adj_mat`\n
    :func:`spaceweather.visualisation.heatmaps.plot_adj_mat`\n
    :func:`spaceweather.analysis.cca.cca_coeffs`\n
    :func:`spaceweather.analysis.data_funcs.csv_to_Dataset`\n
    :func:`spaceweather.analysis.data_funcs.detrend`\n
    :func:`spaceweather.analysis.data_funcs.window`\n
    Parameters
    ----------
    csv_file : csv file
        CSV file downloaded from the SuperMAG website.
    ds : xarray.Dataset
        Data as converted by :func:`spaceweather.analysis.data_funcs.csv_to_Dataset`.
    thr_meth : str, optional
        The method used to calculate the threshold. Options are 'Dods' and 'kf'.
        Default is 'Dods'. Note you may have to add kwargs for the method.
    win_len : int, optional
        Length of window in minutes. Default is 128.
    Returns
    -------
    xarray.Dataset
        Dataset containing the adjacency coefficients.
            The data_vars are: adj_coeffs.\n
            The coordinates are: first_st, second_st, win_start.
    '''

    # if data given as csv, read into Dataset
    if ds is None:
        if csv_file is None:
            print('Error: you must input data as either\n 1) csv file\n 2) xarray.Dataset')
            return 'Error: you must input data as either\n 1) csv file\n 2) xarray.Dataset'
        else:
            ds = sad.csv_to_Dataset(csv_file = csv_file, **kwargs)

    # calculate thresholds
    thresh = sat.threshold(ds, thr_meth = thr_meth, **kwargs)

    # window the data
    ds_win = sad.window(ds = ds, win_len = win_len)

    # get constants
    stations = ds.station.values
    num_st = len(stations)
    start_windows = ds_win.win_start.values
    num_win = len(start_windows)

    # initialize output Dataset and loop through each window
    adj_ds = np.zeros(shape = (num_st, num_st, num_win))
    for i in range(num_win):
        ds_temp = ds_win[dict(win_start = i)]
        ds_temp = ds_temp.rename(dict(win_len = 'time'))
        ds_temp = ds_temp.transpose('time', 'component', 'station')
        adj_ds[:,:,i] = sat.adj_mat(ds = ds_temp,
                                    thr_xrds = thresh,
                                    thr_meth = thr_meth,
                                    **kwargs).adj_coeffs.values

    # create Dataset
    res = xr.Dataset(data_vars = {'adj_coeffs': (['first_st', 'second_st', 'win_start'], adj_ds)},
                     coords = {'first_st': stations,
                               'second_st': stations,
                               'win_start': start_windows})

    # plot the network, or animate a gif or something later on

    return res
        corr_coeff[i] = np.mean(x)

    ## Find where the correlations are highest
    s = np.where(corr_coeff == np.amax(corr_coeff))
    shift = -10 + s[0][0]
    return shift


# def angle(ts1,ts2):
#     # input 2 time series, of the same length
#     # each data point in the time series should be a 3-dimensional vector with N, E and Z components
#     # return np.rad2deg(subspace_angles(bbb_a, bbb_b))
#      return np.rad2deg(subspace_angles(ts1, ts2))

ds1 = sad.csv_to_Dataset(csv_file="Data/20190403-00-22-supermag.csv",
                         MLT=True,
                         MLAT=True)
ds2 = sad.csv_to_Dataset(csv_file="Data/20010305-16-38-supermag.csv",
                         MLT=True,
                         MLAT=True)
ds = sad.csv_to_Dataset(csv_file="Data/20190521-14-08-supermag.csv",
                        MLT=True,
                        MLAT=True)

components = ['N', 'E', 'Z']

scratch = sac.lag_mat(ds,
                      lag_range=10,
                      station1='NAL',
                      station2='LYR',
                      win_len=128,
Esempio n. 5
0
import spaceweather.analysis.data_funcs as sad
import spaceweather.analysis.gen_data as sag
import spaceweather.analysis.threshold as sat
import spaceweather.analysis.network as san
import spaceweather.visualisation.animations as sva
import spaceweather.visualisation.static as svg
import spaceweather.visualisation.heatmaps as svh
import spaceweather.visualisation.lines as svl
import spaceweather.visualisation.spectral_analysis as svs
import spaceweather.supermag as sm
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt

##### Read Data ----------------------------------------------------------------
quiet_day_ds = sad.csv_to_Dataset('Report/CSV Files/quiet-day-1998-02-02.csv',
                                  MLAT=True)
quiet_day_plot = quiet_day_ds.loc[dict(station='RAN')]
avg_meas = np.nanmean(np.nanmean(quiet_day_plot.measurements.values, axis=0))

event_ds = sad.csv_to_Dataset('Report/CSV Files/event-1997-11-05.csv',
                              MLAT=True)
event_plot = event_ds.loc[dict(station='RAN')]

##### Plot Time Series ---------------------------------------------------------
fig, axs = plt.subplots(2, figsize=(16, 8))
# fig.suptitle('Big Title')
axs[0].set_title('Station RAN: Quiet Day 1998-02-02')
axs[1].set_title('Station RAN: Event Day 1997-11-05')
axs[0].set_ylabel('Measurement, nT')
axs[1].set_ylabel('Measurement, nT')
axs[1].set_xlabel('Time')
Esempio n. 6
0
import spaceweather.visualisation.animations as sva
import spaceweather.visualisation.static as svg
import spaceweather.visualisation.heatmaps as svh
import spaceweather.visualisation.lines as svl
import spaceweather.visualisation.spectral_analysis as svs
import spaceweather.supermag as sm
import xarray as xr
import numpy as np
# may need to install OpenSSL for cartopy to function properly
# I needed it on Windows, even though OpenSSL was already installed
# https://slproweb.com/products/Win32OpenSSL.html

################################################################################
####################### Supermag ###############################################
################################################################################
ds1 = sad.csv_to_Dataset(csv_file="Data/20190403-00-22-supermag.csv",
                         MLAT=True)
ds2 = ds1[dict(time=slice(177), station=range(4))]
test = sm.supermag(ds=ds2)
################################################################################

################################################################################
####################### Analysis ###############################################
################################################################################

####################### data_funcs #############################################

##### csv_to_Dataset -----------------------------------------------------------
ds1 = sad.csv_to_Dataset(csv_file="Data/20190403-00-22-supermag.csv",
                         MLT=True,
                         MLAT=True)