disp('read P3navdata');
% vkonsa=[];%ones(10,36000);
%prtit_hr=0;
P3nav=[];
% yyyy='2020';
% ddd=15;
% 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 '*.nc']);
filnx = dr.name;
fname = [way_raw_motion_wband filnx];

%% read file

P3nav=read_netcdfFile2(fname);
yy=str2num(year);
post=findstr(fname,year)+4;
mm=str2num(fname(post:post+1));dd=str2num(fname(post+2:post+3));
P3_doy=datenum(yy,mm,dd)-datenum(yy-1,12,31);
hors=P3nav.HH;
mins=P3nav.MM;
secs=P3nav.SS;
P3_hha=hors+mins/60+secs/3600;
ii=find(~isnan(P3_hha));P3_hh=P3_hha(ii);
P3_lat=P3nav.LATref;P3_lat=P3_lat(ii);jj=find(abs(P3_lat)>90);P3_lat(jj)=NaN;
P3_lon=P3nav.LONref;P3_lon=P3_lon(ii);jj=find(abs(P3_lon)>180);P3_lon(jj)=NaN;
P3_altref=P3nav.ALTref;P3_altref=P3_altref(ii);jj=find(P3_altref>8000 | P3_altref<-10);P3_altref(jj)=NaN;
P3_pitchref=P3nav.PITCHref-1.1;P3_pitchref=P3_pitchref(ii);%+ for nose up, 1.1 correction for radar pitch offset
P3_rollref=P3nav.ROLLref;P3_rollref=P3_rollref(ii);%for left wind up
P3_cog=P3nav.TRKd;P3_cog=P3_cog(ii);%COG
P3_Windspeed=P3nav.WSd;P3_Windspeed=P3_Windspeed(ii);%flight level U
P3_WindDir=P3nav.WDd;P3_WindDir=P3_WindDir(ii);%flight level dir
P3_sfmrU=P3nav.SfmrWS1;P3_sfmrU=P3_sfmrU(ii);%Microwave 10-m wind 118
P3_sfmrR=P3nav.SfmrRainRate1;P3_sfmrR=P3_sfmrR(ii);%117
P3_sst=P3nav.TRadD1;P3_sst=P3_sst(ii);%119
P3_GS=P3nav.GSd;P3_GS=P3_GS(ii);%SOG in m/s
P3_Hed=P3nav.THDGref;P3_Hed=P3_Hed(ii);%aircraft heading
P3_TAS=P3nav.TASd;P3_TAS=P3_TAS(ii);
P3_press=P3nav.PSc;P3_press=P3_press(ii);
P3_Td=P3nav.TDc;P3_Td=P3_Td(ii);
P3_Ta=P3nav.TAd;P3_Ta=P3_Ta(ii);
P3_RH=P3nav.HUM_RELd;P3_RH=P3_RH(ii);jj=find(P3_RH>110);P3_RH(jj)=NaN;
P3_Q=qair3([P3_press P3_Ta P3_RH]);%P3nav.HUM_ABSd;P3_Q=P3_Q(ii);P3_Q(jj)=NaN;
P3_TradD=P3nav.TRadD1;P3_TradD=P3_TradD(ii);%IR sst?
P3_TradS=P3nav.TRadS1;P3_TradS=P3_TradS(ii);
P3_cab=P3nav.PCAB1;P3_cab=P3_cab(ii);
P3_wvel=P3nav.UWZd;P3_wvel=P3_wvel(ii);jj=find(abs(P3_wvel)>20);P3_wvel(jj)=NaN;
P3_uvel=P3nav.UWXd;P3_uvel=P3_uvel(ii);
P3_vvel=P3nav.UWYd;P3_vvel=P3_vvel(ii);




%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',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]);end;


function z=qairv3(y)
p=y(:,1);%pressure in mb
t=y(:,2);%temperature in C
h=y(:,3);%relative humidity in %
es=6.112.*exp(17.502.*t./(t+241.0)).*(1.0007+3.46e-6*p).*h/100;
q=es*622./(p-.378*es*h/100);

tdk=273.16;
Rgas=287.1;
rhoa=p*100/(Rgas*(t+tdk)*(1+0.61*q/1000));
rhov=rhoa*q;% (g/m^3)
z=q;
end
