clear all 
close all

%%
% Use this loop to download all the ISFS station data and save it to .csv

%for i = 1:7
%   urlwrite(strcat('http://api.mesowest.net/v2/stations/timeseries?&stid=isfs',int2str(i),'&start=201012010000&end=201102010000&token=1234567890&output=csv'),strcat('data/isfs',int2str(i),'.csv'))
%end

%%
% Load time height data from the .mat file and station data from the .csv
% files. There are 7 isfs stations and each must be opened seperately. 

isfs1 = csvread('data/isfs1.csv',8,1); % Skip header and first column.
isfs2 = csvread('data/isfs2.csv',8,1);
isfs3 = csvread('data/isfs3.csv',8,1);
isfs4 = csvread('data/isfs4.csv',8,1);
isfs5 = csvread('data/isfs5.csv',8,1);
isfs6 = csvread('data/isfs6.csv',8,1);
isfs7 = csvread('data/isfs7.csv',8,1);

%%
% Deal with time in the station files.

t_stn = int2str([isfs1(:,1), isfs2(:,1), isfs3(:,1), isfs4(:,1),...
    isfs5(:,1), isfs6(:,1), isfs7(:,1)]);

dat_fmt = 'yyyymmddHHMM';

dat_stn = datenum(t_stn,dat_fmt);

dvec_stn = datevec(dat_stn);

%%

T = [isfs1(:,3), isfs2(:,3), isfs3(:,3), isfs4(:,3),...
    isfs5(:,3), isfs6(:,3), isfs7(:,3)]; 

% Calculate albedo from station data.
% albedo = (shw - net) / shw

net = [isfs1(:,13), isfs2(:,13), isfs3(:,13), isfs4(:,13),...
    isfs5(:,13), isfs6(:,13), isfs7(:,13)]; 

shw = [isfs1(:,14), isfs2(:,14), isfs3(:,14), isfs4(:,14),...
    isfs5(:,14), isfs6(:,14), isfs7(:,14)];

alb_stn = (shw - net) ./ shw;

data = [dat_stn, T];

save('albedo_isfs.mat','data');