% AnalyzeClimateData.m
%
% Josh Thompson, 4.22.13

% Load State ID's
	S = importdata('ushcn-v2.5-stations.txt',',');
	for i = 1:size(S,1); T(i) = str2num(S{i}(1:2)); end

 % Student Project: have students input a map of US in MATLAB, and color each region/state
 % according to the slopes computered from a least squares approximation of the climate data at that region. (usamap, command)

% Settings			
	clear Slope Intercept
	C = colormap(summer(48));
	J = 2:13; %14;
	k = 1;


% For each data file, load, compute regression and plot
	for i = 1:length(S)
	
	% Load each matrix
	load(['~/Teaching/MA211/ClimateData/MaxTempsStation_',num2str(i),'.mat']);
	%load(['/Volumes/JOSHTHOM/pub/ClimateData/MaxTempsStation_',num2str(i),'.mat']);
	
	x = 1:length(F(:,1));
		
		% For each matrix, compute a Least Squares for each Month
		for j = J %:13;
	
			% Least Sq computation	
			[v,xd,yd] = LeastSq(1,F(:,j)');
			%y = v(1) *exp( v(2)*x);
			y = v(1) * v(2)*x;
			
			% Assign unique labels/colors to each city station
			StationNum(k)  = i;
			StationID(k,:) = C(T(i),:);

			% Accumulate slopes computed in the regression
			Slope(k) = v(2);
			Intercept(k) = v(1);
			k = k+1;
		
		end
		length(S)-i
	end

% Plotting
	disp(['Number of Positive Slopes ', num2str(length(find(Slope>0)))]);
	disp(['Number of Negative Slopes ', num2str(length(find(Slope<0)))]);

	close all;

	screen_size = get(0, 'ScreenSize');
	f1 = figure(1);
	set(f1, 'Position', [0 0 screen_size(3) screen_size(4) ] );

	 hold on;

	%close all; figure; hold on;
	hT = title('Max Monthly Temp: Least Square Slopes of 1214 Climate Stations for years 1895-2012.. ');
	set(hT,'FontSize',20)
	hX = xlabel('Station Number');
	hY = ylabel('Rate of Temp Increase');
	set(hX,'FontSize',20)
	set(hY,'FontSize',20)

	scatter(1:length(Slope),Slope,40,StationID,'fill')
	plot(1:length(Slope),zeros(1,length(Slope)),'k-','LineWidth',10)
	
	K = randperm(1200,1200);
	for i = 1:5
		k = K(i); %randi(length(Slope));
		h = plot(k,Slope(k),'k.','MarkerSize',10);

		%disp(S{StationNum(k)}(end-36:end));
		%t = text(k,Slope(k),S{StationNum(k)}(end-36:end));
		%set(t,'FontName','Verdana')
		%set(t,'FontSize',12);
		%set(h,'Visible','Off')
		%drawnow
  		%M(i) = getframe(1);
		disp(1200 - i)
	end
		

%movie2avi(M,'ClimateStationTemps.avi');
