function profiledata=readMK21(fname,uniqueid)
%this function reads a single profile from a MA file and creates the
%structure "profiledata" containing all the variables necessary to either
%plot or write the data into another format.
%make these global so they can be seen from the reading routine:
global calls
global cruiseID

CONFIG

%setup output files:
profiledata.nss=num2str(uniqueid);

% initialise strings
str1 = ' ';
str4 = '    ';
str6 = '      ';
str8 = '        ';
str10 = '          ';
str12 = '            ';

fid=fopen(fname);
%Get the header data and ouput to both files:
d=fgets(fid);
d=fgets(fid);

d=fgets(fid);
profiledata.year=str2num(d(24:27));
profiledata.day=str2num(d(21:22));
profiledata.month=str2num(d(18:19));

d=fgets(fid);

%change time to decimal:
profiledata.time=(str2num(d(18:19))*100 + str2num(d(21:22))) *100;

d=fgets(fid);

d=fgets(fid);
s=find(d=='S');
if(isempty(s));
    s=find(d=='N');
    ss=0;
else
    ss=1;
end
profiledata.lat=str2num(d(18:19))+str2num(d(21:s-1))/60;
if(ss);profiledata.lat=-profiledata.lat;end

d=fgets(fid);
e=find(d=='E');
if(isempty(e))
    e=find(d=='W');
    ee=0;
else
    ee=1;
end
space=find(d(18:end)==' ');

profiledata.lon=str2num(d(18:18+space-1))+str2num(d(18+space:e-1))/60; 
if(~ee);profiledata.lon=-profiledata.lon;end
    
profiledata.latitude=profiledata.lat;
%need to multiply longitude by -1 and change to 360 degree long
profiledata.longitude=profiledata.lon;
if(profiledata.longitude<0)
    profiledata.longitude=360+profiledata.longitude;
end
profiledata.lon=profiledata.longitude;
        
%if this data is to be added:
%CS: Fill these extra fields:
profiledata.mky=str8;
profiledata.onedegsq=str8;
profiledata.cruiseID=str10; %fill in below
profiledata.datat='XB';
profiledata.iumsgno=str12;
profiledata.streamsource=str1;
profiledata.uflag=str1;
profiledata.medssta=str8;
profiledata.qpos='1';
profiledata.qdatetime='1';
profiledata.qrec='1';
profiledata.bultime=str12;
profiledata.bulheader=str6;
    clo=datestr(clock,24);
    update=[clo(1:2) clo(4:5) clo(7:10)];
profiledata.update=update;
profiledata.sourceID=str4;
profiledata.streamident='CSXB';
profiledata.QCversion=str4;
profiledata.dataavail='A';

%read the extra bits in the file:
d=fgets(fid);
serno=d(18:end);

for i=1:7  %get rid of comment lines:
    d=fgets(fid);
end

probetypec=d(21:end);
if(strmatch('T-4',probetypec));
    if(str2num(coeff1)==6.691)
        probetype='002';
    else
        disp('ERROR!!! - you must use the new fall rate coefficients')
        pause
    end
elseif(strmatch('T-7',probetypec) | strmatch('Deep',probetypec));
    if(str2num(coeff1)==6.691)
        probetype='042';
    else
        disp('ERROR!!! - you must use the new fall rate coefficients')
        pause
    end
elseif(strmatch('XCTD',probetypec));
    probetype='700';
    profiledata.datat='XC';
else
    disp('ERROR! - only Deep Blue, T-7, T-4 and XCTD probes are coded')
    pause
end

coeff4=[];
while(isempty(coeff4))
d=fgets(fid);
    if(strmatch('Depth Coeff',d))
        coeff1=deblank(d(21:end));
        d=fgets(fid);
        coeff2=deblank(d(21:end-1));
        d=fgets(fid);
        coeff3=deblank(d(21:end-1));
        d=fgets(fid);
        coeff4=deblank(d(21:end-1));   
    end
end

profiledata.nparms=0;
profiledata.nsurfc=0;
profiledata.nhists=0;

profiledata.dup_flag='';
profiledata.digit_code='';
profiledata.standard='';

profiledata.profiledata.pcode='';
profiledata.profiledata.parm='';
profiledata.profiledata.qparm='';
profiledata.surfpcode='';
profiledata.surfparm='';
profiledata.surfqparm='';
profiledata.identcode='';
profiledata.PRCcode='';
profiledata.Version='';
profiledata.PRCdate='';
profiledata.Actcode='';
profiledata.Actparm='';
profiledata.AuxID=0;
profiledata.PreviousVal='';
profiledata.flagseverity=0;

profiledata.D_P_Code='';
profiledata.profile_type='';


if length(cruiseID)>10
    c=cruiseID;
    clear cruiseID;
    cruiseID=c(1:10);
end
%CS: need to pad to string10 - can't think of a better way
blk = ' ';
nblk = length(str10)-length(cruiseID);
for j = 1:nblk, cruiseID=[cruiseID blk]; end
nblk = length(str10)-length(calls);
for j = 1:nblk, calls=[calls blk]; end
uqid = char(num2str(uniqueid));
nblk = length(str10)-length(uqid);
for j = 1:nblk, uqid=[uqid blk]; end
nblk = length(str10)-length(probetype);
for j = 1:nblk, probetype=[probetype blk]; end

nblk = length(str10)-length(coeff1);
for j = 1:nblk, coeff1=[coeff1 blk]; end
nblk = length(str10)-length(coeff2);
for j = 1:nblk, coeff2=[coeff2 blk]; end
nblk = length(str10)-length(coeff3);
for j = 1:nblk, coeff3=[coeff3 blk]; end
nblk = length(str10)-length(coeff4);
for j = 1:nblk, coeff4=[coeff4 blk]; end

nblk = length(str10)-length(serno);
for j = 1:nblk, serno=[serno blk]; end



%fill surface codes group:

surfpcode = ['IOTA';...
             'GCLL';...
             'CSID';...
             'PEQ$';...
             'RCT$';...
             'DCO1';...
             'DC02';...
             'DC03';...
             'DC04';...
             'SER#'];
surfparm  = ['          ';...
             calls;...
             uqid;...
             probetype;...
             '06        ';...
             coeff1;...
             coeff2;...
             coeff3;...
             coeff4;...
             serno];      
surfqparm = ['0';'0';'0';'0';'0';'0';'0';'0';'0';'0'];
profiledata.nsurfc=length(surfqparm);

profiledata.surfpcode=surfpcode;
profiledata.surfparm=surfparm;
profiledata.surfqparm=surfqparm;

profiledata.depth(1,1,1)=0;
profiledata.depresQ(1,1,1)=0;
profiledata.profParm(1,1,1)=0;
profiledata.profQparm(1,1,1)=0;
profiledata.nodepths=0;

%now read the data types, then the data
%while (strmatch('Depth (m)',d)==0 | isempty(strmatch('Depth (m)',d)))
for l=1:9
    d=fgets(fid);
end

proftypes=d;
kk=find(proftypes=='-');
profiledata.nprof=length(kk);

kt=strfind(proftypes,'Temp');
ks=strfind(proftypes,'Sal');
ksv=strfind(proftypes,'Sound');
kd=strfind(proftypes,'Dens');
kc=strfind(proftypes,'Cond');

if(~isempty(kt))
   kindt=find(kk<kt);
   profiledata.prof_type(kindt(end),:)='TEMP            ';
end
if(~isempty(ks))
   kinds=find(kk<ks);
   profiledata.prof_type(kinds(end),:)='PSAL            ';
end
if(~isempty(ksv))
   kindsv=find(kk<ksv);
   profiledata.prof_type(kindsv(end),:)='SVEL            ';
end
if(~isempty(kd))
   kindd=find(kk<kd);
   profiledata.prof_type(kindd(end),:)='DENS            ';
end
if(~isempty(kc))
   kindc=find(kk<kc);
   profiledata.prof_type(kindc(end),:)='COND            ';
end

m=0;
while(~feof(fid))
    d=fgets(fid);
    finished=0;
    nd=0;
    clear data
    while ~finished
        [tt, d] = strtok(d);
        if isempty(d) 
            finished = 1;
        else
            nd = nd + 1;
            data(nd) = sscanf(tt, '%f');
        end
    end

    m=m+1;

    for j=2:length(data)
        profiledata.depth(j-1,m)=data(1);
        profiledata.depresQ(j-1,m)='0';
        profiledata.profparm(j-1,m)=data(j);
        profiledata.profQparm(j-1,m)='0';
    end
end
    

for i=1:profiledata.nprof
    profiledata.dup_flag(i)='N';
    profiledata.digit_code(i)='7';
    profiledata.standard(i)='2';
    profiledata.ndep(i)=m;
    profiledata.D_P_Code(i)='D';
    profiledata.deep_depth(i)=profiledata.depth(i,m);   
end
profiledata.autoqc=0;

return
