function [Data,params] = load_bruker(path)
%LOAD_MATLAB imports TR-EPR data recorded with Bruker's xEPR
%   Usage: ['data_as_double','struct_with_parameters'] = load_bruker('C:/full/path/to/file.mat')
%
%   Imports data recorded by xEPR, where x axis is time and y axis is
%   magnetic field.
%   PATH can be either pointing to .DSC or .DTA, but both need to be there
%   and in the same folder. PATH must also contain forwardslashes only.
%   The params struct contains metadata of the measurement and is displayed
%   in the command window when calling the function.
%
%   Note that this funtion is dependent on easyspin.
%
%   Example for params output:
%   params = 
% 
%   struct with fields:                         Units
% 
%             Field_Center: 3500                G
%              Field_Sweep: 2700                G
%               Field_Step: 5                   G
%            Accumulations: 100                 -
%        laser_shotreprate: 20                  -
%              Field_Start: 2150                G
%                Field_End: 4850                G
%             Field_Vector: [1×540 double]      G
%                   mwFreq: 9.6845              GHz
%                  mwPower: 0.2000              W
%                   QValue: 22800               -
%                  mwAtten: 30                  dB
%                  mwFreqs: [681×1 double]      GHz
%                 TimeBase: [1×10001 double]    s
%                     Path: '/some/path/'       -
%                     Name: 'example_data'      -

%correct \ for /
corrpath = replace(path,"\","/");

%load Bruker data with help of easyspin
[bruker_axes,Data,bruker_params] = eprload(corrpath);

%assign metadata to custom params struct
params.Field_Center = bruker_params.YMIN + 0.5*bruker_params.YWID;
params.Field_Sweep = bruker_params.YWID;
params.Field_Step = bruker_params.YWID / (bruker_params.YPTS-1);
params.Accumulations = bruker_params.AVGS;
params.laser_shotreprate = "NOT RECORDED";
params.Field_Start = bruker_params.YMIN;
params.Field_End = bruker_params.YMIN + bruker_params.YWID;
params.Field_Vector = cell2mat(bruker_axes(1,2)).';
params.mwFreq = bruker_params.MWFQ / 1e9;
params.mwPower = bruker_params.MWPW * 1e3;
params.QValue = "NOT RECORDED";
params.mwAtten = str2double(extractBefore(string(bruker_params.PowerAtten),' '));
params.mwFreqs = "NOT RECORDED";
params.TimeBase = (bruker_params.XMIN:(bruker_params.XWID/bruker_params.XPTS):(bruker_params.XMIN+bruker_params.XWID)) .* 1e-9;

%get filename for further documentation and exporting figures
[datapath,dataname,~] = fileparts(corrpath);
params.Path = datapath;
params.Name = dataname;

%echo what information is contained in the structure called 'params'
params
end

