function [wavelength,intau,intime] = thorspectrometerread(fn);
% [wavelength,intau,inttime] =  thorspectrometerread(fn);
% reads a Thorlabs Spectrometer .jdx file
% by Chuck DiMarzio Thu Oct 17 17:06:18 2019
% based on ooread
% rewritten by Joseph Hollmann, 11.8.2013
% Inputs:
% fn - filename of interest
% Outputs:
% wavelength - measurement wavelength (in nm)
% intau - intensity counts (A/D levels) (scaled by yfactor)
% inttime - integration time (in miliseconds)
% Not working.  Probably related to yfactor, but not sure - check
% the manual sometime.
intime=0; % See if we can get this later

fid = fopen(fn);
for dummy = 1:14; % Read metadata and ignore it
    fgets(fid);
end;

yftext=fgets(fid);  % Read the yfactor
py = strfind(yftext,'YFACTOR= ');
yfactor=str2num(yftext(py+9:end));

for dummy = 16:20; % Read metadata and ignore it
    fgets(fid);  % Ignore the rest of the metadata
end;

for dummy = 1:14; % Read metadata and ignore it
    fgets(fid);
end;

% Now read the data  1 column of wavelength and 8 of integer counts
columnstotal=9;
data = fscanf(fid,'%g');
lines=floor(length(data)/columnstotal);
               %Throw away the last line if it isn't full 
moose=reshape(data,columnstotal,lines)';  % Arrange it 
dwavelength=(moose(2,1)-moose(1,1))/(columnstotal-1);

count=1;
wavelength=zeros((columnstotal-1)*lines,1);
intcounts=zeros((columnstotal-1)*lines,1);
for n=1:lines;
  wavelength(count+[0:columnstotal-2]')=...
      moose(n,1)+dwavelength*[0:columnstotal-2]';
  intcounts(count+[0:columnstotal-2]')=moose(n,2:end);
  count=count+(columnstotal-1);
end;
yfactor
topcount=max(intcounts)
bottomcount=min(intcounts)
intau=intcounts*yfactor;
