%% Created by Chris Cosgrove 23 Apr 2018

%{
This script opens my remote camera photos to be visually interpreted for
snow depth and stores the result in a table by camera name

MOST IMPORTANT IS TO CHANGE THE FOLDER WHERE THE IMAGES ARE STORED, THE
LOCATION STRING TO MATCH THAT IN THE IMAGE FILENAMES, AND THE CAMERA NAME.

You can also set the time that you wish to code for, e.g. once per day at
12:00.

%}

%% Set the folder where the camera images are stored
startFolder = 'Fielddata_Archive/Cameras/all_jpgs';
cd(startFolder)

%% This the folder where the .mat table files for each camera are stored
tableFolder = 'Fielddata_Archive/Cameras/Depth_Tables/jaegermesa_nabesna';

%% This section sets up the variables
% Sets the field location and camera number to be analysed
location = 'jaegermesa_nabesna';
camera = 'C14';

% Chooses the time of day to select images by
time = '120000';

% Gets a structure of files matching location, camera and time pattern
files = dir(strcat(location,'_',camera,'*',time,'*'));

%% This section opens a previous incomplete table or starts a new one 
% Gets the name of the table
tableFilename = char(strcat(location,'_',camera,'depth_table','.mat'));

% Goes to the table folder and check if the table exists
cd(tableFolder);

if exist(tableFilename, 'file') == 2
    snowTable = load(tableFilename); % loads the table if it does
    snowTable = snowTable.snowTable;
    lastdate = datenum(max(snowTable.date)); % gets the last entry date
else
    snowTable = table(); % new table if it doesn;t
    lastdate = 0; % zero last date otherwise
end
    
%% This loops starts building the table by opening up each image file in 
% sequence and running the tableRow/getSnow functions (see below)

for i = 1:length(files)
    if files(i).datenum > lastdate % Opens images until the last date
        image = char(strcat(files(i).folder,'/',files(i).name));
        [tableRow] = tRow(char(image));
        snowTable = [snowTable;tableRow]; % Adds the row with depth to the main table
        save(tableFilename,'snowTable') % Saves the table
    else
        disp('table complete!'); % Gives message if all images coded
    end
end
    
%% This function builds a table row for each image
function [tableRow] = tRow(image)
[depth] = getSnow(image); % This calls the image opener
imgInfo = imfinfo(image); % This gets the datetime of image creation
date = datetime(imgInfo.DateTime,'InputFormat','yyyy:MM:dd HH:mm:ss');% Gets the date of the image creation
date.TimeZone = 'America/Anchorage'; % Sets the timezone of the datetime object
date = datetime(date, 'Format','dd-MM-y HH:mm:ssZ'); % Adds 9 hours to the time to convert to UTC and formats it to the ISO standard with timezone
date = string(strrep(char(date), ' ', 'T')); % Converts the datetime object to a string and adds a 'T' instead of a space
filename = string(imgInfo.Filename); % Gets the image filename 
tableRow = table(date, depth, filename); % Create a single table row for adding to the main table
end

%% This function opens the image, a dialog box to record th emanually read
% snow depth into. The top of each stripe on the snow stake indicates a 5cm
% interval.

function [depth] = getSnow(image)
I = imread(image); imshow(I); % Reads in images and opens it
prompt = {'Enter snow depth (cm):'}; % Sets the prompt for pop-up dialog box
title = 'snow depth'; % Dialog box title
dims = [1 35]; % Specifies the height of each edit field when dims is a scalar value
definput = {'0'}; % Default value for prompt
answer = inputdlg(prompt,title,dims,definput); % Opens the dialog box in front of the image
depth = str2num(cell2mat(answer(1))); % Changes the entry to a number from a string
clf % Clears the opened image
end









