%readnetcdf - this script reads the profile data from the file - 
%       currently it reads only the variables required for Mquest.
%   The entire file is read in the script "readnetcdfforexport".
%
%  The profiledata structure for Mquest is:



clear temp 
clear depth 
clear numhists 
clear qc 
clear QC_code 
clear QC_depth 
clear profiledata;

%retrieveguidata

clear filenam;
keysdata = handles.keys;

filenam=keysdata.prefix;
nss=num2str(ss);
for j=1:2:length(num2str(ss));

	if(j+1>length(nss))
        if(ispc)
filenam=[filenam '\' nss(j)];
        else
filenam=[filenam '/' nss(j)]; 
        end
    else	
        if(ispc)
filenam=[filenam '\' nss(j:j+1)];
        else
filenam=[filenam '/' nss(j:j+1)];
        end
    end
end
if(~exist('rawfile'))
    rawfile=0;
end
if(rawfile)
    filenam=[filenam 'raw.nc'];
    handles.changed='Y';
%    saveguidata
else
    filenam=[filenam 'ed.nc'];
end

% setup the profile structure by filling it from the edited data file:

format short g;
profiledata.latitude=getnc(filenam,'latitude',-1,-1,-1,-1,1);
profiledata.longitude=getnc(filenam,'longitude',-1,-1,-1,-1,1);
wd=num2str(getnc(filenam,'woce_date'));

profiledata.year=wd(1:4);
profiledata.month=wd(5:6);
profiledata.date=wd(7:8);
profiledata.pos_qc=getnc(filenam,'Q_Pos');
profiledata.juld_qc=getnc(filenam,'Q_Date_Time');

wt=(getnc(filenam,'woce_time'));
wt=floor(wt/100);
wt2=sprintf('%4i',wt);
jk=strfind(wt2,' ');
if(~isempty(jk))
    wt2(jk)='0';
end
%wt=wt(1:min(4,length(wt)));
%wt2='000000';
%wt2='0000';
%wt2(4-(length(wt)-1):4)=wt;
profiledata.time=[wt2(1:2) ':' wt2(3:4)];
profiledata.ndep=getnc(filenam,'No_Depths');
profiledata.nprof=getnc(filenam,'No_Prof');

profiledata.ptype=getnc(filenam,'Prof_Type');
if(profiledata.nprof==1);profiledata.ptype=profiledata.ptype';end
pt=strmatch('TEMP',profiledata.ptype);
ps=strmatch('PSAL',profiledata.ptype);

h=getnc(filenam,'Profparm');

de=getnc(filenam,'Depthpress');
dnan=find(~isnan(de));
qc=getnc(filenam,'ProfQP');

    
[m,n]=size(h);

if(n==profiledata.nprof)
     temp=h(:,pt);
     profiledata.depth=de(:,pt);
     profiledata.qc=qc(:,pt);
    if(~isempty(ps))
        sal=h(:,ps);
        profiledata.depthsal=de(:,ps);
        profiledata.salqc=qc(:,ps);
if(profiledata.nprof==1);profiledata.salqc=profiledata.qc';end
    end
else
     temp=h(pt,:);
     profiledata.depth=de(pt,:);
     profiledata.qc=qc(pt,:);
    if(~isempty(ps))
         sal=h(ps,:);
         profiledata.depthsal=de(ps,:);
         profiledata.salqc=qc(ps,:);
if(profiledata.nprof==1);profiledata.salqc=profiledata.qc';end
    end
end

if(profiledata.nprof==1);profiledata.qc=profiledata.qc';end

profiledata.temp=temp ;  

if(~isempty(ps))
    profiledata.sal=sal ;   
end

profiledata.numhists=getnc(filenam,'Num_Hists');

     profiledata.QC_code=getnc(filenam,'Act_Code');
     profiledata.QC_depth=getnc(filenam,'Aux_ID');
     profiledata.PRC_Date=getnc(filenam,'PRC_Date');
     profiledata.PRC_Code=getnc(filenam,'PRC_Code');
     profiledata.Version=getnc(filenam,'Version');
     profiledata.Act_Parm=getnc(filenam,'Act_Parm');
     profiledata.Previous_Val=getnc(filenam,'Previous_Val');
     profiledata.Ident_Code=getnc(filenam,'Ident_Code');
     try
         profiledata.Flag_severity=getnc(filenam,'Flag_severity');
     catch
         for kk=1:profiledata.numhists
             profiledata.Flag_severity(kk)=0;   %indicates this is unknown at present
         end
     end

handles.profile_data=profiledata;

            
%saveguidata

