% Extract image metadata from FLIR T640BX
%Danielle Hare
%dhare@geo.umass.edu
%Department of Geosciences
%University of Massachusetts, Amherst

% USGS Volunteer for Science
% Office of Groundwater Branch of Geophysics
% United States Geologic Survey
% For USGS support contact: Martin Briggs  mbriggs@usgs.gov  (860)487-7402

%Original release: November 19, 2013

%This script extracts Latitude, Longitude, and Heading from FLIR image
%metadata, and converts Latitide and Longitude into Decimal Degrees. The
%datum is that which you set your FLIR to- the default is WSG84. To verify
%the datum the script outputs the geodectic datum from output.GPSInfo.GPSMapDatum.
%All these values are exported in ascii file name FLIR_METADATA.csv
%
 
%For use after downloading FLIR images.
% Add Picture files to path, if from the FLIR camera should be titled IR_XXXX
% and have a related normal picture titled DC_XXXY, where Y=X-1.
% Due to this format I have made the loop go every 2nd value just to get IR
% pictures. All pictures MUST have Compass and GPS coords or it will show
% an error. 

clear
r=0;
 
Image1 = 'What value is the first image in the sequence? IR_XXXX  IR_';
I1 = input(Image1);
Image2 = 'What value is the last image in the sequence? IR_XXXX  IR_';
I2 = input(Image2);
 
for  i=I1:2:I2 % for trial color picking use the interval of picture you want to analyze           
r=r+1;
filename = sprintf('IR_%.4d.jpg',i); % iterative loop reading each file name 
img = imread(filename); %reads image
output = imfinfo(filename);  %reads all file details/properties 
IRGPS(r,1:3)=output.GPSInfo.GPSLatitude; %within output structure reads LAT
IRGPS(r,4:6)=output.GPSInfo.GPSLongitude;%LONG
IRGPS(r,7)=output.GPSInfo.GPSImgDirection; %Compass Direction
azi=IRGPS(r,7);% compass direction in 360 degress
Ecpt=sin(pi/180*azi); % East component of vector
Ncpt=cos(pi/180*azi); % North component of vector
IRGPS(r,9)=Ecpt*100; % scaling for ability to plot
IRGPS(r,10)=Ncpt*100;
IRGPS(r,8)=i; % records image number so that each GPS coord can be related to a picture
end
 
IRGPS(:,2)=IRGPS(:,2)/60; %converts Degree.Minutes to Decimal Degrees
IRGPS(:,5)=IRGPS(:,5)/60; %converts Degree.Seconds to Decimal Degrees

Geodeticdatum= output.GPSInfo.GPSMapDatum
 
% converts to decimal degrees and organizes better export matrix
IRGPS_export(:,1)=IRGPS(:,8); %Image Number
IRGPS_export(:,2)=IRGPS(:,1)+IRGPS(:,2); % Latitude: adds together minutes and seconds
IRGPS_export(:,3)= -1*(IRGPS(:,4)+IRGPS(:,5)); % Longitude: adds together minutes and seconds (western hemisphere)
IRGPS_export(:,4)=IRGPS(:,7); %Compass 360
IRGPS_export(:,5)=IRGPS(:,9);% North component of Vector
IRGPS_export(:,6)=IRGPS(:,10); %East component of Vector
 
csvwrite('FLIR_METADATA.csv', IRGPS_export);
 
 


