%% TIDI
clear
clc
SourcePath='.\\radar\\data\\';
Files=dir(fullfile(SourcePath,'*.'));
LengthFiles=length(Files);
for i=4:4
    Path=strcat(SourcePath,Files(i).name,'\');
    Sub_Files=dir(fullfile(Path,'*.VEC'));
    Sub_LengthFiles=length(Sub_Files);
    for j=1:Sub_LengthFiles
        Date(j)=str2num(Sub_Files(j).name(13:15));
        FilestypeD(j)=str2num(Sub_Files(j).name(30:32));
        FilestypeR(j)=str2num(Sub_Files(j).name(35:36));
    end
    m=0;
    for t=1:366
        [m]=find(Date==t);
        if isempty(m)==0
            [maxValPosD]=find(FilestypeD(m)==max(FilestypeD(m)));
            n=m(maxValPosD);
            [maxValPosR]=find(FilestypeR(n)==max(FilestypeR(n)));
            filename=strcat(Path,Sub_Files(n(maxValPosR)).name);
            Vzonal=ncread(filename,'u');
            VMerdional=ncread(filename,'v');
            UTime=ncread(filename,'ut_time');
            Lat=ncread(filename,'lat');
            Lon=ncread(filename,'lon');
            Height=ncread(filename,'alt_retrieved');
            UTime=double(UTime)/(1000*3600);
            [R,C]=size(Vzonal);
            for ii=1:R
                for jj=1:C
                    if Vzonal(ii,jj)==-9999||Vzonal(R,C)==0
                        Vzonal(ii,jj)=nan;
                    end
                    if VMerdional(ii,jj)==-9999||VMerdional(R,C)==0
                        VMerdional(ii,jj)=nan;
                    end
                end
            end
            
            save(strcat(Path,filename(38:40),'_Lat.mat'),'Lat');
            save(strcat(Path,filename(38:40),'_Lon.mat'),'Lon');
            save(strcat(Path,filename(38:40),'_Height.mat'),'Height');
            save(strcat(Path,filename(38:40),'_UTime.mat'),'UTime');
            save(strcat(Path,filename(38:40),'_Vzonal.mat'),'Vzonal');
            save(strcat(Path,filename(38:40),'_VMerdional.mat'),'VMerdional');
            %         [row col] =size(Vzonal);
            %         jj =1;
            %         VzonalWuhan(1:row,1,ii) =nan;
            %         Var_VzonalWuhan(1:row,1,ii) =nan;
            %         UTWuhan(1,1,ii)         =0;
            %         latWuhan(1,1,ii)        =nan;
            %         lonWuhan(1,1,ii)        =nan;
            %         for rr =1:col
            %             if lat(rr,1)>9.52 && lat(rr,1)<29.52           %漠河（122.37°E， 53.50°N，磁倾角71°）、北京（116.25°E， 40.25°N， 磁倾角 59°）
            %                if lon(rr,1)>99.13 && lon(rr,1)<119.13   %武汉（114.61°E, 30.53°N, 磁倾角46°）、三亚（109.13°E, 19.52°N, 磁倾角27°）
            %                   VzonalWuhan(:,jj,ii) =Vzonal(:,rr);
            %                   Var_VzonalWuhan(:,jj,ii) =Var_Vzonal(:,rr);
            %                   UTdateWuhan(:,jj,ii) =UTdate(:,rr);
            %                   UTWuhan(:,jj,ii)     =UT(rr,1);
            %                   latWuhan(:,jj,ii)    =lat(rr,1);
            %                   lonWuhan(:,jj,ii)    =lon(rr,1);
            %                   jj =jj+1;
            %                end
            %             end
            %         end
            %     else
            %         NoData(tt) =ii;
            %         tt =tt+1;
        end
        % VzonalWorld(1:row,1:col,ii) =Vzonal;
        % UTWorld(1:col,1,ii)     =UT;
        % latWorld(1:col,1,ii)    =lat;
        % lonWorld(1:col,1,ii)    =lon;
    end
    clearvars -except SourcePath Files LengthFiles
end
% altWuhan =alt;
% altWorld =alt;
% clear alt
% clear row
% clear col
% clear Vzonal
% clear UTdate
% clear UT
% clear lat
% clear lon
% [row col day] =size(VzonalWuhan);
% Nullpoint =zeros(col,day);
% NullpointUT =zeros(col,day);
% Nullpointheight =NaN(row,col,day);
% for tt =1:day
%     for jj =1:col         
%         for ii =1:row
%             if VzonalWuhan(ii,jj,tt) ==-9999 || VzonalWuhan(ii,jj,tt) ==0
%                 VzonalWuhan(ii,jj,tt) =nan;
%             end
%             if Var_VzonalWuhan(ii,jj,tt) ==-9000000 || Var_VzonalWuhan(ii,jj,tt) ==0
%                 Var_VzonalWuhan(ii,jj,tt) =nan;
%             end
%         end
%         for ii=1:row-1
%             if (VzonalWuhan(ii,jj,tt)*VzonalWuhan(ii+1,jj,tt))<0
%                 Nullpoint (jj,tt) =Nullpoint(jj,tt)+1;
%                 Y =altWuhan(ii+1,1)-altWuhan(ii,1);
%                 X =VzonalWuhan(ii+1,jj,tt)-VzonalWuhan(ii,jj,tt);
%                 Nullpointheight (ii,jj,tt) =altWuhan(ii,1)-(Y/X)*VzonalWuhan(ii,jj,tt);
%                 NullpointUT (jj,tt) =UTWuhan(1,jj,tt);
%             end
%         end
%     end
% end
% NullpointTotal =zeros(1,24);
% for ii =1:col
%     for jj=1:day
%         if NullpointUT(ii,jj) ~=0
%             m =floor(NullpointUT(ii,jj));
%             NullpointTotal(1,m+1) =NullpointTotal(1,m+1)+1;
%         end
%     end
% end
% m =1;
% n =1;
% t =1;
% for tt =1:day
%     for jj =1:col
%         for ii =1:row
%             if isnan(Nullpointheight(ii,jj,tt)) ==0
%                 NullHeight(m) =Nullpointheight(ii,jj,tt);
%                 NullLT(m) =NullpointUT(jj,tt)+8;
%                 if NullLT(m) >=24
%                     NullLT(m) =NullLT(m)-24;
%                 end
%                 m=m+1;
%             end
%             if isnan(VzonalWuhan(ii,jj,tt)) ==0
%                 WuhanHeight(n) =altWuhan(ii,1);
%                 WuhanLT(n)     =UTWuhan(1,jj,tt)+8;
%                  if WuhanLT(n) >=24
%                      WuhanLT(n) =WuhanLT(n)-24;
%                  end
%                  n =n+1;
%             end
%         end
%     end
% end
% [row col day] =size(VzonalWorld);
% m =1;
% for tt =1:day
%     for jj =1:col         
%         for ii =1:row
%             if VzonalWorld(ii,jj,tt) ==-9999 || VzonalWorld(ii,jj,tt) ==0
%                 VzonalWorld(ii,jj,tt) =nan;
%             end
%             if isnan(VzonalWorld(ii,jj,tt)) ==0
%                 WorldHeight(m) =altWorld(ii,1);
%                 WorldLT(m)     =UTWorld(jj,1,tt)+lonWorld(jj,1,tt)/15;
%                 if WorldLT(m) >=24
%                    WorldLT(m) =WorldLT(m)-24; 
%                 end
%                 m =m+1;
%             end
%         end
%     end
% end


%% 
% ax(1)=gca;
% set(ax(1),'Position',[0.12 0.12 0.75 0.7])
% nx=0:1:16;
% set(ax(1),'Xtick',nx)
% set(ax(1),'Xlim',[0,32])
% ax(2)=axes('Position',get(ax(1),'Position'),...
% 'XAxisLocation','top',...
% 'YAxisLocation','right',...
% 'Color','none',...
% 'XColor','k','YColor','k');
% set(ax,'box','off')
% nx=0:100:3200;
% set(ax(2),'Xtick',nx)
% set(ax(2),'Xlim',[0,3200])
% set(ax(2),'Ylim',[70,120])
% set(ax(1),'Ylim',[70,120])
% X =zeros(21,1);
% E =sqrt(Var_VzonalWuhan(:,1,10));
% hold on
% fill([100,300,300,100],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,1,10)+200,altWuhan,E);
% hold on
% X(1:21,1) =200;
% plot(X,altWuhan,'--k');
% hold on
% plot(Zonal0(:,1)+200,height0(:,1),'r');
% hold on
% E =sqrt(Var_VzonalWuhan(:,2,10));
% hold on
% fill([500,700,700,500],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,2,10)+600,altWuhan,E);
% hold on
% X(1:21,1) =600;
% plot(X,altWuhan,'--k');
% E =sqrt(Var_VzonalWuhan(:,4,10));
% hold on
% fill([900,1100,1100,900],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,4,10)+1000,altWuhan,E);
% hold on
% X(1:21,1) =1000;
% plot(X,altWuhan,'--k');
% E =sqrt(Var_VzonalWuhan(:,5,10));
% hold on
% fill([1300,1500,1500,1300],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,5,10)+1400,altWuhan,E);
% hold on
% X(1:21,1) =1400;
% plot(X,altWuhan,'--k');
% E =sqrt(Var_VzonalWuhan(:,6,10));
% hold on
% fill([1700,1900,1900,1700],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,6,10)+1800,altWuhan,E);
% hold on
% X(1:21,1) =1800;
% plot(X,altWuhan,'--k');
% E =sqrt(Var_VzonalWuhan(:,7,10));
% hold on
% fill([2100,2300,2300,2100],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,7,10)+2200,altWuhan,E);
% hold on
% X(1:21,1) =2200;
% plot(X,altWuhan,'--k');
% E =sqrt(Var_VzonalWuhan(:,8,10));
% hold on
% fill([2500,2700,2700,2500],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,8,10)+2600,altWuhan,E);
% hold on
% X(1:21,1) =2600;
% plot(X,altWuhan,'--k');
% E =sqrt(Var_VzonalWuhan(:,3,10));
% hold on
% fill([2900,3100,3100,2900],[70,70,120,120],[0.8,0.8,0.8]);
% hold on
% errorbare('h',VzonalWuhan(:,3,10)+3000,altWuhan,E);
% hold on
% X(1:21,1) =3000;
% plot(X,altWuhan,'--k');
% set(ax,'YMinorTick','on','XMinorTick','off','Xticklabel',{''},...
% 'TickDir','in',...
% 'TickLength',[0.025 0.025],...
% 'Layer','top');
% set(ax(1),'xticklabel',{'','','8.82','','','','8.85','','','','10.47','','','','10.50'});
% set(ax(2),'xticklabel',{'','-100','0','100','','-100','0','100','','-100','0','100','','-100','0','100'});
% set(get(ax(1),'xlabel'),'string','UT (hour)');
% set(get(ax(2),'xlabel'),'string','Zonal Wind (m/s)');
% set(get(ax(1),'ylabel'),'string','Height (km)');
% set(get(ax(2),'ylabel'),'string','Height (km)');


