% ======================== %
% Plot Realistic 3D Fields %
% ======================== %

depth = app.depth;
lat = app.lat;
lon = app.lon;

app.mask = double(app.mask);
app.mask(app.mask == 0) = NaN;
permuted_mask = permute(app.mask, [2, 3, 1]);

%plot_depth_idx = app.plot_depth_idx;
%
%if isempty(plot_depth_idx)
%    plot_depth_idx = linspace(1, length(depth), length(depth));
%end


plot_depth_idx = 2:2:42;

sec2day = 86400;
milsec2day = sec2day*1000;
spacing = 0.002*3;
ref_datenum = datenum('January 1, 1970, 00:00:00');

lon_grid = min(lon(:)):spacing:max(lon(:));
lat_grid = min(lat(:)):spacing:max(lat(:));
[LON, LAT] = meshgrid(lon_grid, lat_grid);


for idx = plot_depth_idx
        
   
    namestring = [sprintf('%06d', idx*app.dz), '_', app.whichFTLE, '_', sprintf('%02d', timeWindow*24), 'hr_start_', num2str(round((datenum(app.start_date) - ref_datenum)*milsec2day + 1000*app.t0)), '.asc'];
    temp_file_name_ftle = [app.SaveDir, '/xfm_', namestring];
    
    ftle_temp = permute(final_flowmap.x(:, :, idx), [2, 1, 3]).*permuted_mask(:, :, idx);
    si_obj_ftle = scatteredInterpolant(lon(:), lat(:), ftle_temp(:), 'linear', 'none');
    ftle_read = si_obj_ftle(LON(:), LAT(:));
    ftle_read = reshape(ftle_read, size(LON));
    
    arcgridwrite(temp_file_name_ftle, lon_grid, lat_grid', ftle_read);
    
    namestring = [sprintf('%06d', idx*app.dz), '_', app.whichFTLE, '_', sprintf('%02d', timeWindow*24), 'hr_start_', num2str(round((datenum(app.start_date) - ref_datenum)*milsec2day + 1000*app.t0)), '.asc'];
    temp_file_name_ftle = [app.SaveDir, '/yfm_', namestring];
    
    ftle_temp = permute(final_flowmap.y(:, :, idx), [2, 1, 3]).*permuted_mask(:, :, idx);
    si_obj_ftle = scatteredInterpolant(lon(:), lat(:), ftle_temp(:), 'linear', 'none');
    ftle_read = si_obj_ftle(LON(:), LAT(:));
    ftle_read = reshape(ftle_read, size(LON));
    
    arcgridwrite(temp_file_name_ftle, lon_grid, lat_grid', ftle_read);

    namestring = [sprintf('%06d', idx*app.dz), '_', app.whichFTLE, '_', sprintf('%02d', timeWindow*24), 'hr_start_', num2str(round((datenum(app.start_date) - ref_datenum)*milsec2day + 1000*app.t0)), '.asc'];
    temp_file_name_ftle = [app.SaveDir, '/zfm_', namestring];
    
    ftle_temp = permute(final_flowmap.z(:, :, idx), [2, 1, 3]).*permuted_mask(:, :, idx);
    si_obj_ftle = scatteredInterpolant(lon(:), lat(:), ftle_temp(:), 'linear', 'none');
    ftle_read = si_obj_ftle(LON(:), LAT(:));
    ftle_read = reshape(ftle_read, size(LON));
    
    arcgridwrite(temp_file_name_ftle, lon_grid, lat_grid', ftle_read);
    
end

if exist('savedir.txt', 'file')
    fid = fopen('savedir.txt', 'a');
    fprintf(fid, '%s\n', app.SaveDir);
    fclose(fid);
else
    fid = fopen('savedir.txt', 'w+');
    fprintf(fid,'%s\n', app.SaveDir);
    fclose(fid);
end

fid = fopen('date_start.txt', 'w+');
fprintf(fid, '%s', datestr(startDate_start, 'mmmm dd, yyyy HH:MM:SS'));
fclose(fid);

fid = fopen('date_end.txt', 'w+');
fprintf(fid, '%s', datestr(startDate_end, 'mmmm dd, yyyy HH:MM:SS'));
fclose(fid);

fid = fopen('start_date_shift.txt', 'w+');
fprintf(fid, '%s', num2str(startDay_shift*24));
fclose(fid);
