function [] = lightning_and_iar()
%% Preamble/Readme:
% This is the top level function developed by Charles Nokes for analyzing,
% visualizing, and studying lightning excitation of the ionospheric Alfvén
% resonator, using data from CARISMA (20 Hz or 100 Hz) and data from WWLLN.
% The function is used to:

% 1) define the local folder inside which the data is located. A potential
% improvement is defining a folder to the data on the server, but the data
% on the CARISMA server is in a zipped format.

% 2) Define top level user inputs to the analysis and visualisation
% function lightning_and_iar_functions.m
%
%% Inputs by description: (data folder location, date,
% other string detail ('' or 'rand' for example, see **), station,
% mag data sample rate (100 samp/sec or 20 s/sec), start hour, stop hour,
% PSD window size by number of points, direction of B field,
% filtering toggle (yes/no), set spectral plot to UTC or MLT time,
% selection toggle allowing only one matched event per superposed epoch window,
% manually determined location of spike in psd cannot be greater than round(fs/(npt-overlap)*24*3600)-1,
% distance for local lightning in km,
% distance for non-local lightning in km, plotting subset variable (remove),
% quantile for B field peak threshold, lightning energy threshold,
% propagation time limits, superposed epoch window size in seconds,
% start and end hours of IAR activity and bin size in hours)

% ** Most times this input is left as a blank string: ''
% But Charles would sometimes put data for doing random comparisons in a
% folder with naming convention YYYYMMDDrand, and tagging 'rand' at the end
% was necessary.

%% Inputs by format or value:
% (string, 'YYYYMMDD', 'STTN', '100' or '20', value (0-23), value (0-23),
% power of 2, 'H' or 'D' or 'H\_band' or 'D\_band', 'yes' or 'no', 'UTC' or
% 'MLT', '' or 'unique', value, value or 'outside', value, value, value,
% value, 1x2 vector, value, 1x3 vector)

%% Input variable names inside the function they are used
%
% This is so you know where to look for the variable in the
% lightning_and_iar_functions.m function, and understand what happens if you change
% it.
%
% (data_folder, datestring, randstring, stationstring, fstring, starthour,
% stophour, npt, direction, filterchoice, utc_or_mlt, winuniquestr,
% spikeloc, local, proximal, subsize, heightfact, heightfact2,
% strikepeakdt, peakwint, IARwin)


%% Recommended default inputs

% data_folder: defined as string below

% date: user defined for day being analyzed. E.g. '20090824'
% other string detail: ''
% station: user defined. E.g. 'MSTK'

% mag data sample rate: user defined, '100' recommended

% start hour: 0

% stop hour: 23 (not 24)

% PSD window size: Sample rate dependent. For '20' recommend using 2^12,
% for '100' recommend using 2^14. This is the number of time series points
% (a waveform of that many data points ) used to generate the FFT of a
% subset of the whole time series dataset, which creates one window of the
% PSD.

% direction of B field: user defined, 'D' or 'H', or if there is a wideband
% tone that needs band passing, specify 'D\_band' or 'H\_band'

% filtering toggle: recommend 'yes'; this is used to remove the frequency
% components in the signal so that the time series bisects 0 in the
% vertical axis, i.e., removing the DC.

% UTC or MLT: 'UTC' ('MLT' functionality not 100% complete)

% selection toggle: 'unique' recommended unless specifically addressing
% challenge of better matching lightning to mag pulses

% spike location: user defined, no longer really used, must be less than round(fs/(npt-overlap)*24*3600)-1

% local lightning: 0 or something less than non-local lightning OR if
% creating an exclusion zone, set to 'outside' and then only events
% occurring outside the distance set in non-local will be considered

% non-local lightning: 2000 or something greater than local lightning
% plotting subset: 4, no longer really used

% plotting subset variable (remove)

% quantile for B field peak threshold: user defined, use a decimal between
% 0 and 1. 0 sets a low threshold, 1 sets the highest threshold. See figure
% 3.1 of Charles Nokes' MSc. Thesis, Lightning Excitation of the IAR

% lightning energy threshold: quantile based, recommend 0.5 if not using

% propagation time limits: recommend [0 0.1] for '100' samp/sec, [0 0.2]
% for '20' samp/sec

% superposed window size: recommend 4 seconds

% start and end hours of IAR and bin size: data dependent, recommend bin
% size of 0.5 hours or 1 hour.

% Example:
% lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0, 0.5, [0 0.1], 4, [4 11 1])


%% Define data folder

data_folder = '/Users/charlesnokes2015/U of A/Research/Dr. Ian Mann/2016 Paper/'; % define folder where all data located

%% Run lightning_and_iar_functions.m for various input scenarios. Run one at a time, or if you want to do a massive batch of plots, more than one at a time.

lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0, 0.5, [0 0.1], 4, [7 11 1])
%  lightning_and_iar_functions(data_folder, '20090824','', 'PINA', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 400, 1400, 4, 0.1, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.2, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.3, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.4, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.5, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.6, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.7, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.8, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.9, 0.5, [0.02 0.07], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'PINA', '100', 0, 23, 2^14, 'D', 'yes', 'UTC', 'unique', 263, 0, 15000, 4, 0, 0.5, [0 0.4], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20100512','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 15000, 4, 0, 0.5, [0 0.4], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20100916','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 1500, 3000, 4, 0, 0.5, [0 0.2], 6, [2 7.5 0.5])
%  lightning_and_iar_functions(data_folder, '20111121','', 'PINA', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 0, 10000, 4, 0.9, 0.5, [0 0.1], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20111121','', 'PINA', '100', 0, 23, 2^14, 'H\_band', 'yes', 'UTC', 'unique', 263, 1000, 2000, 4, 0.9, 0.5, [0 0.1], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20111121','', 'DAWS', '100', 0, 23, 2^14, 'D\_band', 'yes', 'UTC', 'unique', 263, 4000, 5000, 4, 0.9, 0.5, [0 0.1], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'PINA', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 'outside', 0, 4, 0.9, 0.5, [0 0.2], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20100714','', 'DAWS', '100', 0, 23, 2^14, 'H', 'yes', 'MLT', 'unique', 263, 0, 4000, 4, 0.9, 0.5, [0 0.1], 6, [7 7.5 0.5])
%  lightning_and_iar_functions(data_folder, '20100714','', 'MSTK', '100', 0, 23, 2^14, 'D', 'yes', 'UTC', 'unique', 263, 0, 10000, 4, 0.9, 0.5, [0 0.1], 6, [4 11 1])
%  lightning_and_iar_functions(data_folder, '20100714','', 'FSMI', '100', 0, 23, 2^14, 'D', 'yes', 'MLT', 'unique', 263, 0, 3000, 4, 0.9, 0.5, [0 0.1], 6, [7 7.5 0.5])
%  lightning_and_iar_functions(data_folder, '20100714','', 'PINA', '100', 0, 23, 2^14, 'D', 'yes', 'MLT', 'unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.1], 6, [7 7.5 0.5])
%  lightning_and_iar_functions(data_folder, '20100714','', 'THRF', '100', 0, 23, 2^14, 'D', 'yes', 'MLT', 'unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.1], 6, [7 7.5 0.5])
%  lightning_and_iar_functions(data_folder, '20100512','', 'DAWS', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 10000, 4, 0.9, 0.5, [0 0.1], 6, [3.5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20100512','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 5000, 4, 0.1, 0.5, [0 0.1], 6, [3.5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20100512','', 'FSMI', '100', 0, 23, 2^14, 'D', 'yes', 'UTC', 'unique', 263, 0, 10000, 4, 0.9, 0.5, [0 0.1], 6, [3.5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20100512','', 'PINA', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 1000, 2000, 4, 0.1, 0.5, [0 0.1], 6, [3.5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'DAWS', '100', 0, 23, 2^14, 'H', 'yes', 'UTC','unique', 263, 0, 10000, 4, 0.9, 0.5, [0 0.1], 6, [4 11 1])
%  lightning_and_iar_functions(data_folder, '20090824','', 'MSTK', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0, 0.5, [0 0.2], 6, [4 10 1])
%  lightning_and_iar_functions(data_folder, '20090824','', 'FSMI', '100', 0, 23, 2^14, 'H\_band', 'yes', 'MLT', 'unique', 263, 0, 3000, 4, 0.9, 0.5, [0 0.1], 6, [6 6.5 0.5])
%  lightning_and_iar_functions(data_folder, '20090824','', 'PINA', '100', 0, 23, 2^14, 'D', 'yes', 'UTC','unique', 263, 0, 10000, 4, 0.9, 0.5, [0 0.1], 6, [4 11 1])
%  lightning_and_iar_functions(data_folder, '20090824','', 'PINA', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0.5, 0.5, [0 0.2], 6, [3 9 1])
%  lightning_and_iar_functions(data_folder, '20090824','', 'THRF', '100', 0, 23, 2^14, 'D', 'yes', 'UTC','unique', 263, 0, 10000, 4, 0.9, 0.5, [0 0.1], 6, [4 11 1])
%  lightning_and_iar_functions(data_folder, '20101009','', 'DAWS', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 4000, 4, 0.9, 0.5, [0 0.1], 6, [6 11.5 0.5])
%  lightning_and_iar_functions(data_folder, '20101009','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.1], 6, [6 11.5 0.5])
%  lightning_and_iar_functions(data_folder, '20101009','', 'FSMI', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 3000, 4, 0.9, 0.5, [0 0.1], 6, [6 11.5 0.5])
%  lightning_and_iar_functions(data_folder, '20101009','', 'PINA', '100', 0, 23, 2^14, 'D', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.1], 6, [6 11.5 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'DAWS', '100', 0, 23, 2^14, 'D\_band', 'yes', 'UTC','unique', 263, 0, 4000, 4, 0, 0.5, [0 0.1], 6, [5 9 1])
%  lightning_and_iar_functions(data_folder, '20140825','', 'DAWS', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 0, 4000, 4, 0.1, 0.5, [0 0.2], 6, [6 6.5 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'MSTK', '100', 0, 23, 2^14, 'D', 'yes', 'UTC','unique', 263, 3000, 6000, 4, 0.9, 0.5, [0 0.1], 6, [5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'MSTK', '20',  0, 23, 2^12, 'D', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0.1, 0.5, [0 0.2], 6, [6 6.5 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'RABB', '100', 0, 23, 2^14, 'H', 'yes', 'UTC','unique', 263, 0, 3000, 4, 0.9, 0.5, [0 0.1], 6, [5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'RABB', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.2], 6, [4.5 5 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'FCHU', '100', 0, 23, 2^14, 'D\_band', 'yes', 'UTC','unique', 263, 0, 3000, 4, 0.9, 0.5, [0 0.1], 6, [5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'FCHU', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 0, 3000, 4, 0.1, 0.5, [0 0.2], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'PINA', '100', 0, 23, 2^14, 'D', 'yes', 'UTC','unique', 263, 0, 2000, 4, 0, 0.5, [0 0.1], 6, [5 9 1])
%  lightning_and_iar_functions(data_folder, '20140825','', 'PINA', '20',  0, 23, 2^12, 'H', 'yes', 'UTC', 'unique', 263, 0, 1000, 4, 0.1, 0.5, [0 0.2], 6, [4.5 10 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'THRF', '100', 0, 23, 2^14, 'D', 'yes', 'UTC','unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.1], 6, [5 9 0.5])
%  lightning_and_iar_functions(data_folder, '20140825','', 'THRF', '20',  0, 23, 2^12, 'D', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.2], 6, [4.5 10 0.5])

% Random for null hypothesis
%  lightning_and_iar_functions(data_folder, '20090825','', 'MSTK', '100', 0, 23, 2^14, 'H', 'yes', 'UTC', 'unique', 263, 0, 2000, 4, 0.9, 0.5, [0 0.1], 4, [7 11 1])

end
