%writeMA - this program takes input from the identified netcdf file 
% and outputs it in the data exchange format meds-ascii.

%retrieveguidata
profiledata=handles.profile_data;

if(strmatch('TP',profiledata.QC_code(:,:)))
    return
end
if(strmatch('DU',profiledata.QC_code(:,:)))
    return
end
prefix=[handles.outputfile '.MA'];

if(handles.first==0)
    fid=fopen(prefix,'at');
else
    fid=fopen(prefix,'wt')
end
if (fid==-1)
    return
end


clear a

a=profiledata.Mky';
if(~isempty(strfind(profiledata.time,':')))
    a=[a profiledata.One_Deg_Sq' profiledata.Cruise_ID' profiledata.year profiledata.month ...
    profiledata.date profiledata.time(1:2) profiledata.time(4:5)]
else
    a=[a profiledata.One_Deg_Sq' profiledata.Cruise_ID' profiledata.year profiledata.month ...
    profiledata.date profiledata.time]
end

a=[a profiledata.Data_Type' profiledata.Iumsgno' profiledata.Stream_Source' profiledata.Uflag' profiledata.MEDS_Sta'];

%need to convert longitude backto +/-180 degrees:

plong=profiledata.longitude;
if(plong<180);plong=-plong;end
if(plong>=180);plong=-plong+360;end

alat=sprintf('%8.2f',profiledata.latitude);
alon=sprintf('%9.2f',plong);

a=[a alat alon profiledata.Q_Pos profiledata.Q_Date_Time profiledata.Q_Record];

a=[a profiledata.Up_date' profiledata.Bul_Time' profiledata.Bul_Header' profiledata.Source_ID' profiledata.Stream_Ident'];

a=[a profiledata.QC_Version' profiledata.Data_Avail];

nprofs=sprintf('%2i',profiledata.nprof);
if(profiledata.Nparms==0)
    nparms=' 0';
else
    nparms=sprintf('%2i',profiledata.Nparms);
end
if(profiledata.Nsurfc==0)
    nsurfc=' 0';
else
    nsurfc=sprintf('%2i',profiledata.Nsurfc);
end
if(profiledata.numhists==0)
    numhists='  0';
else
    numhists=sprintf('%3i',profiledata.numhists);
end
a=[a nprofs nparms nsurfc numhists];

clear noseg

for i=1:profiledata.nprof
    noseg(i)=floor(profiledata.ndep(i)./1500)+1;
    anoseg=sprintf('%2i',noseg(i));
    a=[a anoseg ];
    a=[a profiledata.ptype(i,1:4) profiledata.Dup_Flag(i) profiledata.Digit_Code(i) profiledata.Standard(i)];

    clear adeepdepth
    adeepdepth=sprintf('%5.1f',profiledata.Deep_Depth(i));
    a=[a adeepdepth(1:5)];
end

if(profiledata.Nparms>0)
    for i=1:profiledata.Nparms
        a=[a profiledata.Pcode(i,:) profiledata.Parm(i,:) profiledata.Q_Parm(i,:)];
    end
end

if(profiledata.Nsurfc>0)
    for i=1:profiledata.Nsurfc
        a=[a profiledata.SRFC_Code(i,:) profiledata.SRFC_Parm(i,:) profiledata.SRFC_Q_Parm(i,:)];
    end
end

if(profiledata.numhists>0)
    for i=1:profiledata.numhists
        aauxid=sprintf('%8.2f',profiledata.QC_depth(i));
        try
            prevv=str2num(profiledata.Previous_Val(i,:));
            aprevv=sprintf('%10.3f',prevv);
        catch
            aprevv=profiledata.Previous_Val(i,:);
        end
        a=[a profiledata.Ident_Code(i,:) profiledata.PRC_Code(i,:) profiledata.Version(i,:) ...
            profiledata.PRC_Date(i,:) profiledata.QC_code(i,:) profiledata.Act_Parm(i,:) ...
             aauxid(1:8) aprevv(1:10)];
    end
end

% now write this bit to the file, then construct the profile data bits...
%first, find any nulls in the "a" string and replace them with ''
I = find(a == char(0));
if(~isempty(I))
   a(I) = ' ';
end

%write to the data file:

count=fprintf(fid,'%s',a);
count2=fprintf(fid,'\n');

% construct the depth/temp/quality section:
% setup header info:

for j=1:profiledata.nprof

    ij=0;
    for k=1:noseg(j)
        
    clear a

    a=profiledata.Mky';
    if(~isempty(strfind(profiledata.time,':')))
        a=[a profiledata.One_Deg_Sq' profiledata.Cruise_ID' profiledata.year profiledata.month ...
        profiledata.date profiledata.time(1:2) profiledata.time(4:5)];
    else
        a=[a profiledata.One_Deg_Sq' profiledata.Cruise_ID' profiledata.year profiledata.month ...
        profiledata.date profiledata.time];
    end

    if(k==noseg)
        ndepths=rem(profiledata.ndep(j),1500);
    else
        ndepths=1500;
    end
    ndp=sprintf('%4i',ndepths);
    nseg=sprintf('%2i',k);
    a=[a profiledata.Data_Type' profiledata.Iumsgno' profiledata.ptype(j,1:4) nseg ndp profiledata.D_P_Code(j)];

    for i=1:ndepths
        ij=ij+1;
        clear d t qd qc
        if(strfind(a,'93199304052013'))
                j=j;
                ij=ij;
                profiledata;
                ndepths
        end
        d=sprintf('%6.2f',profiledata.depth(j,ij));
        t=sprintf('%9.3f',profiledata.data(j,ij));
           
        try
            a=[a d(1:6) profiledata.depthqc(j,ij) t(1:9) profiledata.qc(j,ij)];
        catch
            ij=ij
            j=j
            profiledata.depthqc;
            profiledata.qc;
        end
    end
    
%prepare to write it to the file:
    I = find(a == char(0));
    if(~isempty(I))
        a(I) = ' '; 
    end

%write to the data file:

count=fprintf(fid,'%s',a);
count2=fprintf(fid,'\n');

    end
end

fclose(fid);



return
