disp('read P3navdata');
% vkonsa=[];%ones(10,36000);
prtit_hr=0;
P3nav=[];
yyyy='2020';
ddd=17;
hr='16';
if ~exist('ddd','var')
    ddds=input('Input yearday  ','s');
    ddd=str2double(ddds);
else
    ddds=sprintf('%03i',ddd);
end

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

way_raw_motion_wband='C:\Data\cwf\Dropbox\DATA\ATOMIC\p3\motion\';
cruise='ATOMIC';
% 0903332 IWG1_NAMES,TIME,LATref,LONref,ALTref,GPS_GeoidHt.1,ALTPAft.d,AltRaft.1,GS.d,TAS.d,IAS.d,MACH.d,GSZref,THDGref,TRK.d,DA.d,PITCHref,ROLLref,SA.d,AA.d,TA.d,TD.c,TTMref,PS.c,PQ.c,PCAB.1,WS.d,WD.d,UWZ.d,SolarZenith,SunElevAc,SunAzGnd,SunAzAc,FLID,MISSIONID,STORMID,MDSHOUR.1,MDSMINUTE.1,MDSSECOND.1,AccZfilterI-GPS.1,AccZfilterI-GPS.2,ACCZref,AccZI-GPS.1,AccZI-GPS.2,ALTGA.d,AltGPS.1,AltGPS.2,AltGPS.3,AltI-GPS.1,AltI-GPS.2,ALTPA.d,AltRa.1,AltRa.2,AltRa1.c,AltRa2.c,AXBT.1,AXBT.2,AXBT.3,COURSEcorr.d,DV.d,GPS_AltErr.1,GPS_Quality.1,GSXref,GSYref,HT.d,HUM_ABS.d,HUM_REL.d,IASkt.d,LicHum_Abs.1,LicCO2D.1,LicTDM.1,LicTTMi.1,LicTTMo.1,LicPT.1,LatGPS.1,LatGPS.2,LatGPS.3,LatI-GPS.1,LatI-GPS.2,LonGPS.1,LonGPS.2,LonGPS.3,LonI-GPS.1,LonI-GPS.2,LWC.1,MR.d,PDALPHA.1,PDALPHA.2,PDBETA.1,PDBETA.2,PitchI.1,PitchI.2,PitchI-GPS.1,PitchI-GPS.2,PitchI-GPS.3,PitchI-GPS.4,PitchRate.1,PQALPHA.1,PQBETA.1,PQM.2,PQM.3,PQM.4,PQM.1,PSM.2,PSM.1,PSURF.d,PTM.1,RollI.1,RollI.2,RollI-GPS.1,RollI-GPS.2,RollI-GPS.3,RollI-GPS.4,SfmrRainRate.R,SfmrWS.R,SST.1,TASkt.d,TDM.1,TDM.2,TDM.3,THdgI-GPS.1,THdgI-GPS.2,THdgI-GPS.3,THdgI-GPS.4,THETA.d,THETAE.d,TRadD.1,TRadS.1,TRadU.1,TRKdesired.d,TTM.1,TTM.2,TTM.3,TVIRT.d
% str2reject={'IWG1_NAMES','TIME','LATref','LONref','ALTref','GPS_GeoidHt.1','ALTPAft.d','AltRaft.1','GS.d','TAS.d','IAS.d','MACH.d','GSZref','THDGref','TRK.d','DA.d','PITCHref','ROLLref','SA.d','AA.d','TA.d','TD.c','TTMref','PS.c','PQ.c','PCAB.1','WS.d','WD.d','UWZ.d','SolarZenith','SunElevAc','SunAzGnd','SunAzAc','FLID','MISSIONID','STORMID','MDSHOUR.1','MDSMINUTE.1','MDSSECOND.1','AccZfilterI-GPS.1','AccZfilterI-GPS.2','ACCZref','AccZI-GPS.1','AccZI-GPS.2','ALTGA.d','AltGPS.1','AltGPS.2','AltGPS.3','AltI-GPS.1','AltI-GPS.2','ALTPA.d','AltRa.1','AltRa.2','AltRa1.c','AltRa2.c','AXBT.1','AXBT.2','AXBT.3','COURSEcorr.d','DV.d','GPS_AltErr.1','GPS_Quality.1','GSXref','GSYref','HT.d','HUM_ABS.d','HUM_REL.d','IASkt.d','LicHum_Abs.1','LicCO2D.1','LicTDM.1','LicTTMi.1','LicTTMo.1','LicPT.1','LatGPS.1','LatGPS.2','LatGPS.3','LatI-GPS.1','LatI-GPS.2','LonGPS.1','LonGPS.2','LonGPS.3','LonI-GPS.1','LonI-GPS.2','LWC.1','MR.d','PDALPHA.1','PDALPHA.2','PDBETA.1','PDBETA.2','PitchI.1','PitchI.2','PitchI-GPS.1','PitchI-GPS.2','PitchI-GPS.3','PitchI-GPS.4','PitchRate.1','PQALPHA.1','PQBETA.1','PQM.2','PQM.3','PQM.4','PQM.1','PSM.2','PSM.1','PSURF.d','PTM.1','RollI.1','RollI.2','RollI-GPS.1','RollI-GPS.2','RollI-GPS.3','RollI-GPS.4','SfmrRainRate.R','SfmrWS.R','SST.1','TASkt.d','TDM.1','TDM.2','TDM.3','THdgI-GPS.1','THdgI-GPS.2','THdgI-GPS.3','THdgI-GPS.4','THETA.d','THETAE.d','TRadD.1','TRadS.1','TRadU.1','TRKdesired.d','TTM.1','TTM.2','TTM.3','TVIRT.d'};
%                            10                                  15                             20                                25                            30                          35                                     40                                      45
%0004700 IWG1_NAMES	TIME LATref	LONref ALTref GPS_GeoidHt.1 ALTPAft.d AltRaft.1	GS.d TAS.d IAS.d MACH.d	GSZref THDGref TRK.d DA.d PITCHref ROLLref SA.d	AA.d TA.d TD.c TTMref PS.c PQ.c	PCAB.1 WS.d	WD.d UWZ.d SolarZenith SunElevAc SunAzGnd SunAzAc FLID MISSIONID STORMID MDSHOUR.1 MDSMINUTE.1 MDSSECOND.1 AccZfilterI-GPS.1 AccZfilterI-GPS.2 ACCZref AccZI-GPS.1 AccZI-GPS.2 ALTGA.d AltGPS.1 AltGPS.2 AltGPS.3 AltI-GPS.1 AltI-GPS.2	ALTPA.d	AltRa.1	AltRa.2	AltRa1.c AltRa2.c ASfmrWS.1 ASfmrRainRate.1	AXBT.1 AXBT.2 AXBT.3 COURSEcorr.d DV.d GPS_AltErr.1	GPS_Quality.1 GSXref GSYref	HT.d HUM_ABS.d HUM_REL.d IASkt.d LicHum_Abs.1 LicCO2D.1	LicTDM.1 LicTTMi.1 LicTTMo.1 LicPT.1 LatGPS.1 LatGPS.2 LatGPS.3	LatI-GPS.1 LatI-GPS.2 LonGPS.1 LonGPS.2	LonGPS.3 LonI-GPS.1	LonI-GPS.2 LWC.1 MR.d PDALPHA.1	PDALPHA.2 PDBETA.1 PDBETA.2	PitchI.1 PitchI.2 PitchI-GPS.1 PitchI-GPS.2	PitchI-GPS.3 PitchI-GPS.4 PitchRate.1 PQALPHA.1	PQBETA.1 PQM.2 PQM.3 PQM.4 PQM.1 PSM.2 PSM.1 PSURF.d PTM.1 RollI.1 RollI.2 RollI-GPS.1 RollI-GPS.2 RollI-GPS.3 RollI-GPS.4 SfmrRainRate.R SfmrWS.R SST.1 TASkt.d TDM.1 TDM.2 TDM.3 THdgI-GPS.1 THdgI-GPS.2 THdgI-GPS.3 THdgI-GPS.4 THETA.d THETAE.d TRadD.1 TRadS.1	TRadU.1	TRKdesired.d TTM.1 TTM.2 TTM.3 TVIRT.d
 str2reject={'IWG1_NAMES','TIME','LATref','LONref','ALTref','GPS_GeoidHt.3','ALTPAft.d','AltRaft.1','GS.d','TAS.d','IAS.d','MACH.d','GSZref','THDGref','TRK.d','DA.d','PITCHref','ROLLref','SA.d','AA.d','TA.d','TD.c','TTMref','PS.c','PQ.c','PCAB.1','WS.d','WD.d','UWZ.d','none','none','none','none','FLID','MISSIONID','STORMID','AccZfilterI-GPS.1','AccZfilterI-GPS.2','ACCZref','AccZI-GPS.1','AccZI-GPS.2','AltBCADDU.1','ALTGA.d','AltGPS.1','AltGPS.2','AltGPS.3','AltGPS.4','AltI-GPS.1','AltI-GPS.2','ALTPA.d','AltRa.1','AltRa.2','AltRa1.c','AltRa2.c','ASfmrWS.1','ASfmrRainRate.1','ASfmrTB.1','ASfmrTB.2','ASfmrTB.3','ASfmrTB.4','ASfmrTB.5','ASfmrTB.6','COURSEcorr.d','DV.d','GPS_AltErr.3','GPS_AltErr.4','GSXref','GSYref','GPS_Quality.3','GPS_Quality.4','HT.d','HUM_ABS.d','HUM_REL.d','IASkt.d','LatGPS.1','LatGPS.2','LatGPS.3','LatGPS.4','LatI-GPS.1','LatI-GPS.2','LonGPS.1','LonGPS.2','LonGPS.3','LonGPS.4','LonI-GPS.1','LonI-GPS.2','MDSHOUR.1','MDSMINUTE.1','MDSSECOND.1','MR.d','PDALPHA.1','PDALPHA.2','PDBETA.1','PDBETA.2','PitchI-GPS.1','PitchI-GPS.2','PitchI-GPS.3','PitchI-GPS.4','PQALPHA.1','PQBETA.1','PQM.1','PQM.2','PQM.3','PQM.4','PSM.1','PSM.2','PSURF.d','PTM.1','RollI-GPS.1','RollI-GPS.2','RollI-GPS.3','RollI-GPS.4','SfmrRainRate.1','SfmrT.6','SfmrT.7','SfmrWS.1','SST.1','Salinity.1','TASkt.d','TDM.1','TDM.2','TDM.3','THdgI-GPS.1','THdgI-GPS.2','THdgI-GPS.3','THdgI-GPS.4','THETA.d','THETAE.d','THETAV.d','TRadD.1','TRadS.1','TRKdesired.d','TTM.1','TTM.2','TVIRT.d'};
%             1       2   3      4      5       6              7        8        9    10     11   12     13     14      15    16    17        18    19   20   21   22    23    24   25   26     27   28    29    30           31       32        33     34     35       36     37         38          39          40                 41               42      43           44        45       46       47       48       49          50        51       52     53      54      55       56         57            58     59       60    61         62           63      64          65    66      67        68      69      70         71      72         73       74      75         76        77       78         79         80      81         82       83        84         85   86    87      88          89      90       91        92       93          94        95           96           97          98          99         00      01       02   03      04    05    06     07    08    09     10        11           12       13          14          15       16            17     18     19    20      21    22     23           24         25           26         27       28       29      30      31      32        33     34    35    36                                                   
dr = dir([way_raw_motion_wband 'p3' yyyy(2:end) ddds hr '_raw.txt']);
filnx = dr.name;
flname = [way_raw_motion_wband filnx];

%% read file
temp = [];
FID = fopen(flname,'r');
if FID>0
    while ~feof(FID)
        try
%             temp=textscan(FID,'%2f%2f%3f %*4c %4f%2f%2f%*c%2f%2f%2f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*8f%*c%*f %*11c %*4c %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]','delimiter', ',', 'headerlines', 1,'treatAsEmpty',str2reject, 'emptyvalue', NaN);
%             temp=textscan(FID,'%*12c %4f%2f%2f%*c%2f%2f%2f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*8f%*c%*f %*17c %*4c %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]','delimiter', ',', 'headerlines', 1,'treatAsEmpty',str2reject, 'emptyvalue', NaN);
            temp = textscan(FID,['%*4c %4f%2f%2f%*c%2f%2f%2f ',repmat('%f ',1,31),'%*8f%*c%*f %*11c %*4c ',repmat('%f ',1,99),'%*[^\n]'],'delimiter', ',','headerlines', 1,'treatAsEmpty',str2reject, 'emptyvalue', NaN);
            P3nav = [P3nav cell2mat(temp)'];
        catch
%             [l,c]=size(temp{1,end});
%             for ll=1:length(temp)
%                  if length(temp{1,ll})~=l
%                      temp{1,ll}(l+1)=[];
%                  end
%             end
%             if isempty(temp{1,1}); continue; end
%             P3nav=[P3nav cell2mat(temp)'];
        end  %end try    
    end     %end while 
else
    P3nav=ones(1,1)*NaN;
end %end if IFD>0  

fclose(FID);


%% fix some weird points
ii=find(P3nav(1,:)~=str2num(yyyy));  P3nav(:,ii)=[];



P3_doy=datenum(P3nav(1,:),P3nav(2,:),P3nav(3,:),P3nav(4,:),P3nav(5,:),P3nav(6,:))-datenum(P3nav(1,:)-1,12,31);
P3_hh=P3nav(4,:)+P3nav(5,:)/60+P3nav(6,:)/3600;
P3_lat=P3nav(7,:);
P3_lon=P3nav(8,:);
P3_altref=P3nav(9,:);
P3_pitchref=P3nav(21,:)-1.1;%+ for nose up, 1.1 correction for radar pitch offset
P3_rollref=P3nav(22,:);%for left wind up
P3_cog=P3nav(19,:);
P3_Windspeed=P3nav(31,:);%flight level U
P3_WindDir=P3nav(32,:);%flight level dir
P3_sfmrU=P3nav(118,:);%Microwave 10-m wind 118
P3_sfmrR=P3nav(117,:);%117
P3_sst=P3nav(119,:);%119
P3_GS=P3nav(13,:);%SOG in m/s
P3_Hed=P3nav(18,:);%aircraft heading




%clear P3nav
%################## Track map limits #########################################################
Lonmin=-60;
Lonmax=-50; 
Latmin=10;
Latmax=20;
% if cruise[1:8]=='PATRICIA'
% Lonmin=-115;%-135;
% Lonmax=-90;%-120; 
% Latmin=10;%34;
% Latmax=22;%42
% end;


jk=1:10:length(P3_lon);
figure(3);clf;
subplot(2,2,1:2)
%     plot(P3_lon,P3_lat);xlabel('Lat');ylabel('Lon');
%     grid
    eval(['map_P3_cal2','(P3_lon(jk),P3_lat(jk),P3_sst(jk),cruise,[Lonmin Lonmax Latmin Latmax])'])
    title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  P3 navigation data',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')

subplot(2,2,3:4)
     plot(P3_hh,P3_altref,'.');
     xlabel('Hour (UTC)');ylabel('Altitude (m)'); %xlim([P3_doy(1) P3_doy(end)]); 
     %datetick('x','HH:MM','keepticks');
     grid
     orient tall
     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_hr; print(graphdevice,[way_raw_images_wband cruise '_P3nav_' sprintf('%04i_%02i_%02i_%03i_%02i',Vdate(1),Vdate(2),Vdate(3),ddd,jam) graphformat]);end

