function  [data,P] = PLE_photometry(whichScreen)

%PLE_PHOTOMETRY -- Arrange for photometric measurements of the monitor.
%
%  [data,P] = PLE_photometry(whichScreen)
%
%  WHICHSCREEN defaults to 1, that is, the auxiliary monitor.
%  DATA is a matrix with six columns:
%   1. trial number
%   2. LUT index (in the lookup table; between 0 and 255)
%   3. Red value
%   4. Blue value    % Green is always 0, see P.LUT
%   5. position      % -1=above, 0=center, +1=below center
%   6. photometer reading        % in cd/m^2
%
%  See also PLEXP1.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2002/02/04 20:13 $

%-- Decipher input arguments
if (nargin < 1)
   whichScreen = 1 ;   % measure the auxiliary monitor by default
end

%-- Print herald
outs = 1 ;   % output stream = standard output
fprintf(outs,'\nPLExp photometry, ver. 1.\n') ;

%-- Set random generator seed
seed = round(100*sum(clock)) ;    % see RAND's help page
rand('state',seed) ;
fprintf(outs,'\nRandom generator seed set to %d.\n',seed) ;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%   EQUIPMENT CHARACTERISTICS   %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NANAO Technology FlexScan 6600 paperwhite monitor with a P4 phosphor.
% Driven by the internal videocard of a Power Macintosh 7300/200.
% A special circuit combines two 8-bit output channels of the video-
% card to produce 24*256=6144 distinct gray levels (see MAKE_LINEAR_LUT).

GAMMA = 1.95 ;        % =mean([1.91 1.99])  % on 10-30 and 12-12-0
BRstep = 24 ;         % hardwired in the special circuit

pixel_depth = 32 ;      % must be 32 bits
if (~ismember(pixel_depth,SCREEN(whichScreen,'PixelSizes')))
   error('Sorry, I need a screen that supports 32-bit pixel depth.') ;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%   LOOKUP TABLE PARAMETERS %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% See IMAGE/LUT_PARAMS and IMAGE/MAKE_SPLIT_LUT.
P.LUT_params.gamma = GAMMA ;
P.LUT_params.BRstep = BRstep ;
P.LUT_params.contrast = 1.0 ;   % use the full dynamic range of the monitor
P.LUT_params.domain = [0 255] ;

%-- Lookup table (constructed here, installed later)
[P.LUT,gray] = make_linear_LUT(LUT_params) ;
P.colors.black = P.LUT_params.domain(1) ;
P.colors.gray = gray ;
P.colors.white = P.LUT_params.domain(2) ;
P.LUT_params.span = min(gray-P.colors.black,P.colors.white-gray) ;

%-- Open a SCREEN window and set the lookup table
fprintf(outs,'\nOpening screen %d...',whichScreen) ;
[winPtr,winRect] = SCREEN(whichScreen,'OpenWindow',gray,[],pixel_depth) ;
fprintf(outs,'   Done.\nSetting the lookup table...') ;
SCREEN(winPtr,'SetClut',P.LUT) ;
SCREEN(winPtr,'FillRect',gray) ;    % clear the screen
fprintf(outs,'   Done.\n') ;

%-- Sampling levels
levels = zeros(1,9) ;      % nine points, eight intervals
levels(1) = P.colors.black ;
levels(5) = P.colors.gray ;
levels(9) = P.colors.white ;
levels([3 7]) = round((levels([1 5])+levels([5 9]))./2) ;
levels([2 4 6 8]) = round((levels([1 3 5 7])+levels([3 5 7 9]))./2) ;
P.levels = levels ;        % [0 32 64 96 128 160 192 224 255]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%        PREPARATION        %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%-- Open the data file
data_filename = 'photometric_data.dat' ;
fprintf(outs,'Opening data file %s...',data_filename) ;
[data_fid,errmsg] = fopen(data_filename,'at') ;   % mode = append text
if (data_fid == -1)
   warning(errmsg) ;
   fprintf(outs,'Fix the problem and type ''return'' to continue.\n\n') ;
   keyboard ;
else
   fprintf(outs,'   Done.\n\n') ;
end

%-- Design for the measurements
P.locations = [-1 0 +1] ;       % [top center bottom]
P.replications = 3 ;            % 3 replications per cell

design = factor_design({P.levels,P.locations},P.replications,1) ;
N_trials = length(design) ;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%        MAIN   LOOP        %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

data = zeros(N_trials,6) ;
for k = 1:N_trials
   d(1) = k ;
   LUT_idx = design(k,1) ;           % one of P.LEVELS
   LUT_entry = P.LUT(LUT_idx+1,:) ;
   pos = design(k,2) ;
   d([1 2 3 4 5]) = [k LUT_idx LUT_entry(1) LUT_entry(3) pos] ;
   %- Fill the screen with the appropriate color
   SCREEN(winPtr,'FillRect',LUT_idx) ;
   fprintf(outs,'Trial %2d, position %+2d, LUT index %3d ..... ',...
           k,pos,LUT_idx) ;
   %- At this point the operator should measure the luminance with a 
   %- photometer and enter the reading.
   d(6) = input('photometer reading: ') ;    % <-- sic
   % Write data file
   fprintf(data_fid,'%3d %3d %+2d %3d %3d  %8.3f\n',d) ;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%         CLEAN  UP         %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf(outs,'Closing data file %s...',data_filename) ;
fclose(data_fid) ;
fprintf(outs,'   Done.\nClosing all screens and screen buffers...') ;
SCREEN('CloseAll') ;
fprintf(outs,'   Done.\n') ;

%--- Return DATA and P
%%%%% End of file PLE_PHOTOMETRY.M
