
%------------------------------
% Initialize fields & DO modes.
%------------------------------

Bottom_Gravity_Current_SetupDO

%-------------------------------
% Set GMM Observation Parameters
%-------------------------------

if ~isfield (app,'truenoise')
   app.truenoise = 0.01;   % Actual standard deviation of observation noise
end;
if ~isfield (app,'modelnoise')
   app.modelnoise = 0.05;  % Modeled standard deviation of observation noise
end;

if ~isfield (app,'EnKF')
   app.EnKF = 0;          % 1: Ensemble Kalman filter; 0: GMM-DO filter
end;

% Times of calling observation script (round to nearest saving time)

if ~isfield (app,'GMMoffset')
   app.GMMoffset = 0;
end;
if isfield (app,'ObsTimes')
   ObsTimes = app.ObsTimes;
else
   ObsTimes = ((25:5:120)-app.GMMoffset).*(60*nu_x/h^2);
end;

%%% hardwire absolute pathnames

app.abspaths = 1;

% round observation times down to nearest saving times
ObsTimes = round(ObsTimes/(app.PlotIntrvl*app.dt));

n_obssteps = length(ObsTimes);
obsstep = 1;

% Spatial coordinates (x,y) at which to make observations
if isfield (app,'ObsLocs')
   ObsLocs = app.ObsLocs;       
else
   ObsLocs = [...       
    2000, (A1-2000)*tan_theta + B1 + 25;
    2000, (A1-2000)*tan_theta + B1 + 75;
    2000, (A1-2000)*tan_theta + B1 + 125;
    3000, (A1-3000)*tan_theta + B1 + 30;
    3000, (A1-3000)*tan_theta + B1 + 90;
    3000, (A1-3000)*tan_theta + B1 + 150;
    4000, (A1-4000)*tan_theta + B1 + 30;
    4000, (A1-4000)*tan_theta + B1 + 90;
    4000, (A1-4000)*tan_theta + B1 + 150]/h;
end;

n_uvars = length(u);           % number of u-velocity state variables
n_vvars = length(v);           % number of v-velocity state variables
n_rhovars = length(rho);
n_vars  = n_uvars + n_vvars + n_rhovars; % total number of state variables

% One observation per location: salinity
n_obs = size(ObsLocs,1);

% Create observation matrix
H = sparse(zeros(n_obs,n_vars));

% Find indexes of observed state variables
for i = 1 : n_obs
    [~,xind] = min(abs(XP(1,:) - ObsLocs(i,1)));
    [~,yind] = min(abs(YP(:,1) - ObsLocs(i,2)));
    H(i, n_uvars + n_vvars + NodeRho(yind+1,xind+1)) = 1;
end;

% Create observation noise covariance matrix
R = (app.modelnoise^2)*eye(n_obs);

% Minimum and maximum number of components of GMM
if ~isfield (app,'ObsDataDir');
   app.M_Min = 20;
end;
if ~isfield (app,'ObsDataDir');
   app.M_Max = 20;
end;

app.ObsScript = 'Bottom_Gravity_Current_GMM_Filter';

if ~isfield (app,'ObsDataDir');
   app.ObsDataDir = 'Save/mat/Bottom_Gravity_Current_23';
end;

% initialize array of observation log-likelihoods
loglikes = double.empty(0,2) ;


