disp('read_scs_STRATUS_2019.m')
fclose('all');
%Couldn't get a RS-232 feed from the SCS so get data from WHOI (they use
%TCP protocole). LB - 06/14/2012
%Yr Mo Da Hr Mi Se LatD LatM  LonD LonM Head SST SSS Wspd Wdir ATMP HRH BPR Depth
 jd=109;
                                                  %10-min index
 jax=1;                              %line counter
 %declare arrays
 nr2=19;scsdata=ones(nr2,60*24)*NaN;
 clear dir
  dr=dir(['F:\Stratus_2015\Cabo_de_Hornos\SCS\Raw\04\']);
  e=dr(1).name;
 
 disp(['Reading scs file ',e]);
%  flist=fopen(['F:\Stratus_2015\Cabo_de_Hornos\SCS\Raw\04\WHOIsShipWXT.mat.txt']);
temp=[];scsdata=[];
%  if flist>0,                                 %if the file exists,
%      while ~feof(flist)
%  %             temp=textscan(flist,'%f %f %f %*c %f %f %f %*c %f %f %*c %f %f %*c %f %f %*c %f %f %*c %f %f %f %f %f  %*[^\n]','delimiter', ' ', 'treatAsEmpty', {'SBE-21','****','FWD','Temp'}, 'headerlines', 1,  'emptyvalue', NaN);
%              temp=textscan(flist,'%2f/%2f/%4f %2f:%2f:%2f %f %f %f %f %*[^\n]','delimiter',' ', 'headerlines', 1,  'emptyvalue', NaN);
%              scsdata=[scsdata cell2mat(temp)'];        
%      end;  %end while
%  end;      %     end if list
%  %fclose(flist);
%  % Date,Time,TrueWindSpd-TrueWindDir-RelWindSpd-RelWindDir
%  
jd_scs=datenum(scsdata(3,:),scsdata(1,:),scsdata(2,:),scsdata(4,:),scsdata(5,:),scsdata(6,:))-datenum(scsdata(3,1),1,0);   %Day-Of-Year
  llpos=find(jd_scs>=ddd & jd_scs<ddd+1);
  jd_scs=jd_scs(llpos);
 
%D:\Data\STRATUS_2019\CabodeHornos\SCS\CdeH
scs_path = 'D:\Data\STRATUS_2019\CabodeHornos\SCS\S18\W13_2_hr.mat';

load(scs_path);
imu =     scsdata(7,llpos)*0.514;   % m/s
imd =     scsdata(8,llpos);  % deg 
imur =     scsdata(9,llpos)*0.514; %m/s
imdr =     scsdata(10,llpos); %deg

imum = scs.wspd.*0.51444; %scs wind speed
imdm = scs.wdir;  % scs wind dir
impresm = scs.bpr; %scs pressure
tam = scs.atmp;   %scs atmospheric temp 
rhm = scs.hrh;  % scs humidity
lrgm = scs.heading;  %scs heading
sogm = scs.speed.*0.5144444; % m/s    scs ship speed
jd_scs = scs.mday - datenum(2014,12,31);

[m,d]=yd2md(str2num(yyyy), ddd);
%Vdate=[str2num(yyyy) m d];

%Vdate=[scsdata(3,1) scsdata(1,1) scsdata(2,1)];

%data not available or no sensor
%ir=   scsdata(19,llpos)*NaN;        
%solar=   scsdata(19,llpos)*NaN;      
%imrain=   scsdata(19,llpos)*NaN;     
%odec=   scsdata(19,llpos)*NaN;        



% % %interpolate to 1min;
% rdcon=pi/180;
% % 
% % sogx=sog'.*cos(cog'*rdcon);    %convert to N
% % sogy=sog'.*sin(cog'*rdcon);    %convert to E
%imux=imu'.*cos(imd'*rdcon);   %convert to N
%imuy=imu'.*sin(imd'*rdcon);   %convert to E
% % lrgc=cos(lrg'*rdcon);        %convert to N
% % lrgs=sin(lrg'*rdcon);        %convert to E
% % 
%  jd_ref=ddd+(0:1439)/60/24;
% % latm=intavg(jd_scs,latx,jd_ref);
% % lonm=intavg(jd_scs,lonx,jd_ref);
% % tsgm=intavg(jd_scs,tsg,jd_ref);          
% % tssm=intavg(jd_scs,tss,jd_ref);        
% % rhm=intavg(jd_scs,rh,jd_ref);          
% % tam=intavg(jd_scs,ta,jd_ref);           
% % impresm=intavg(jd_scs,impres,jd_ref);
% imum=intavg(jd_scs,imu,jd_ref);
% imdm=intavg(jd_scs,imd,jd_ref);
% % % lrgm=intavg(jd_scs,lrg,jd_ref);
% % sogxm=intavg(jd_scs,sogx,jd_ref);
% % sogym=intavg(jd_scs,sogy,jd_ref);
% imuxm=intavg(jd_scs,imux,jd_ref);
% imuym=intavg(jd_scs,imuy,jd_ref);
% % lrgcm=intavg(jd_scs,lrgc,jd_ref);
% % lrgsm=intavg(jd_scs,lrgs,jd_ref);
% % 
% % % sogm=   lrgm*NaN;  	
% % % cogm=   lrgm*NaN;  	
% % irm=    rhm*NaN;  	    
% % solarm=     rhm*NaN;  	      
% % rainm=     rhm*NaN;  	   
% % odecm=    rhm*NaN;  	       
% % 
% % sogm=sqrt(sogxm.*sogxm+sogym.*sogym);                         %1-min sog
% % cogm=mod(atan2(sogym,sogxm)/rdcon+360,360);
% imum=sqrt(imuxm.*imuxm+imuym.*imuym);                         %1-min imet speed, m/s
% imdm=mod(atan2(imuym,imuxm)/rdcon+360,360);
% % lrgm=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);         %laser ring gyro

%i don't think interpolation is needed as data are every 1min - LB - 07/10/2013
%jd_ref=jd_scs;	%pcode gps time,
%latm=latx;	%decimal latitude, deg
%lonm=lonx;	%longitude, deg
%cogm=cog;	% pcode cog, deg
%sogm=sog;	% pcode sog, m/s
%tsgm=tsg;	%thermosalinograph T, C
%tssm=tss;	%salinity, psu
%rhm=rh;	%rel hum,%
%tam=ta;	%air t, C
%solarm=solar;	%solar flux, w/m^2
%lrgm=lrg;	%laser gyro, deg
%imum=imu;	%imet true wind speed, m/s
%imdm=imd;	%imet true wind dir, deg
%rainm=imrain; %rain, mm 
%odecm=odec; %odec 
%irm=ir; %imet ir
%impresm=impres; %imet press (mb)


% 
% 
%if plotit && ~isempty(imu)
% 
     figure;subplot(2,1,1)
      plot(jd_scs,imu,'r');
      xlabel('Hour (UTC)');ylabel('Wind speed (m/s)');xlim([ddd ddd+1])
      title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS Wind speed',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
      set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
      xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
      grid
      subplot(2,1,2)
      plot(jd_scs,imd,'.')
      xlabel('Hour (UTC)');ylabel('Wind direction (deg)');axis([ddd ddd+1 0 360])
      title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS Wind direction',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
      set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
      xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
      grid
      annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
      if prtit; print(graphdevice,[way_images_flux  cruise '_SCS_Wind_Speed_Dir_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
 
     figure;
     plot(jd_scs,imd,jd_scs,'.');
    xlabel('Hour (UTC)');ylabel('Temperatures (degC)');xlim([ddd ddd+1]);ylim([20 36])
     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS temperatures',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
     legend('Sea (tsg)','Air','Location','Best')
     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
     grid
     annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
     if prtit; print(graphdevice,[way_images_flux  cruise '_SCS_Temperatures_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
% 
     figure;
     plot(jd_scs,tss);
     xlabel('Hour (UTC)');ylabel('Salinity (psu)');xlim([ddd ddd+1])
     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS salinity',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
     grid
     annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
     if prtit; print(graphdevice,[way_images_flux  cruise '_SCS_Salinity_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
 
     figure;
      plot(jd_scs,rh);
     xlabel('Hour (UTC)');ylabel('RH (%)');axis([ddd ddd+1 40 100])
     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS Relative Humidity',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
     grid
     annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
     if prtit; print(graphdevice,[way_images_flux  cruise '_SCS_RH_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
 
     figure;
     subplot(2,1,1);plot(jd_scs,solar);
     ylabel('Solar flux (W/m^2)');axis([ddd ddd+1 -5 1200]);
     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS solar flux',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
     grid
     annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
     
     subplot(2,1,2);plot(jd_scs,ir);
     xlabel('Hour (UTC)');ylabel('IR flux (W/m^2)');axis([ddd ddd+1 300 500]);
     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS IR flux',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
     grid
     
     if prtit; print(graphdevice,[way_images_flux  cruise '_SCS_Solarflux_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
 
% % %     figure;
% % %     plot(jd_scs,bp);
% % %     xlabel('Hour (UTC)');ylabel('Pressure (mb)');
% % %     axis([ddd ddd+1 median(bp(~isnan(bp)))-10  median(bp(~isnan(bp)))+10]);
% % %     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  SCS barometric pressure',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
% % %     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
% % %     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
% % %     grid
% % %     annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
% % %     if prtit; print(graphdevice,[way_images_flux  cruise '_SCS_pressure_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
% 
% %     figure;
% %         eval(['map_', currentMname,'(lond,latd,cruise,ship,[Lonmin Lonmax Latmin Latmax])'])
% %         annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
% %     if prtit
% % %         print('-depsc2','-tiff',[way_raw_images_flux 'track' cruise yyyy '_' jd '.eps']);
% %         print(graphdevice,[way_images_flux cruise '_SCS_shiptrack_map_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]);
% %     end
% %     figure;
% %     plot(lonm,latm,'.'); title(['DOY',jd,' UTC  Longitude/Latitude from SCS GPS. ', cruise],'Interpreter','none');xlabel('Longitude (deg)');ylabel('Latitude (deg)');
% %     annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
% %     if prtit; print(graphdevice,[way_images_flux cruise '_SCS_lat_lon_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end
%end


np=length(sogm);
 jd_scs=jd_ref;
 if saveit
prt_jas_scs_STRATUS_2015;
 end;