%plot 12-bit or 8-bit image and check saturation and total image
%brightness.
%two versions of the image are plotted, one is the image as it is usually
%seen, the other is a log intensity plot which shows up the really dark
%pixels.
%
%example usage: theImage=plotSingleImage('/ligo/data/camera/archive/2015/05/31/','H1 ITMX Green (h1cam22)_2015-05-31-00-15-16.tiff')
%
%this script creates a structure with image and info on th number of
%saturated pixels
%Pixelcount is total image brightness
%satPix is number of saturated pixels
%maxPix is brightness of brightest pixel (4096 is saturated for a 12 bit image, 255 for 8 nit.)
%approxNosieFloor looks at the average pixel values of a small corner of
%the picture, assumed to be dark.

function theImage=plotSingleImage(fileLocation,fileName)

%addpath('/ligo/home/eleanor.king/scattering/')
%cd('/ligo/data/camera/archive/2015/05/31/'); %currently reading image from the default folder cameras save images to.

fileName='H1 ITMX Green (h1cam22)_2015-05-31-00-15-16.tiff';
NewImage=importfile2([fileLocation fileName]);

%set window of a small portion of image to look at noise floor.
xmin=610;
xmax=640; %actual max x coordinate
ymin=430;
ymax=480; %actual max y coordinate

%detect whether 8-bit or 12-bit image, and set the maximum (saturate) pixel
%value based on this.
if isa(NewImage,'uint8');
    maxPixel=255; %8-bit images
elseif isa(NewImage,'int32');
    maxPixel=4095; %12-bit images
else
    warning('unknown image type. not uint8 (8bit) or uint32 (12bit).')
    maxPixel=NaN;
end

%open each new image in a new figure.
if exist('ii','var')==0 %if ii doesn't exit,  initialise code
    ii=0;
    PixelCount=struct();
    satPix=struct();
end
if exist('PixelCount','var')==0 %PixelCount should have been created, if not code did not initialise properly.
    error('Code did not initialise correctly. clear ii to initialise code.')
end

%save image data as a new structure
ii=ii+1;
theImage.PixelCount=sum(sum(NewImage)); %total image brightness
theImage.satPix=sum(sum(double(NewImage)==maxPixel)); %number of saturated pixels
theImage.maxPix=max(max(double(NewImage))); %brightness of brightest pixel (4096 is saturated for a 12 bit image.)
theImage.approxnoiseFloor=mean(mean(double(NewImage(ymin:ymax,xmin:xmax)))); %check a small part of background
theImage.image=NewImage;

%figure(100+ii);imagesc(newestImage(ymin:ymax,xmin:xmax));colorbar;title([fileName ' noisefloor'],'interpreter','none')
%colorbar
figure(200+ii);imagesc(NewImage);title(fileName,'interpreter','none');
colorbar
cmap = contrast(NewImage);
colormap(gray(256))
%caxis([0  2000])
figure(300+ii);imagesc(log10(double(NewImage)));title([fileName ' (log intensity scale)'],'interpreter','none');
colorbar
cmap = contrast(log10(double(NewImage)));
colormap(gray(256))
%caxis([0 4000])

end

%%
function NewImage=importfile2(fileToRead1)
%IMPORTFILE(FILETOREAD1)
%  Imports data from the specified file
%  FILETOREAD1:  file to read
%imageName will be the vaiable where image data is stored.
%function importfile(fileToRead1,imageName)


% Import the file
rawData1 = importdata(fileToRead1);
% 
% % For some simple files (such as a CSV or JPEG files), IMPORTDATA might
% % return a simple array.  If so, generate a structure so that the output
% % matches that from the Import Wizard.
% [name] = 'NewImage'; %fileparts(fileToRead1);
% newData1.(genvarname(name)) = rawData1;
% 
% % Create new variables in the base workspace from those fields.
% vars = fieldnames(newData1);

% for i = 1:length(vars)
%     assignin('base', vars{i}, newData1.(vars{i}));
% end
NewImage=rawData1;


end

