%function mast_campbell_combine

di{1}='F:\working\STKH_mast\GPS';
di{2}='F:\working\STKH_mast\TRH';
di{3}='F:\working\STKH_mast\TRH_volt';
do='F:\working\STKH_mast\combined';

year='2014';
month(1,:)='01';month(2,:)='02';month(3,:)='03';
month(4,:)='04';month(5,:)='05';month(6,:)='06';
month(7,:)='07';month(8,:)='08';month(9,:)='09';
month(10,:)='10';month(11,:)='11';month(12,:)='12';

list_gps=dir(di{1});
list_TRH=dir(di{2});
list_TRH_volt=dir(di{3});

for n=8:8%10
    for m=29:eomday(str2num(year),n)
        display(m)
        STK_Mast.PC_TIME=ones((24*60*60),6).*NaN;
        STK_Mast.PC_TIME(:,1)=str2num(year);STK_Mast.PC_TIME(:,2)=n;STK_Mast.PC_TIME(:,3)=m;
        STK_Mast.GPS_TIME=ones((24*60*60),3).*NaN;
        STK_Mast.Latitude=ones((24*60*60),1).*NaN;
        STK_Mast.Longitude=ones((24*60*60),1).*NaN;
        STK_Mast.Altitude=ones((24*60*60),1).*NaN;
        STK_Mast.T1=ones((24*60*60),1).*NaN;
        STK_Mast.T1V=ones((24*60*60),1).*NaN;
        STK_Mast.RH1=ones((24*60*60),1).*NaN;
        STK_Mast.RH1V=ones((24*60*60),1).*NaN;
        STK_Mast.T2=ones((24*60*60),1).*NaN;
        STK_Mast.T2V=ones((24*60*60),1).*NaN;
        STK_Mast.RH2=ones((24*60*60),1).*NaN;
        STK_Mast.RH2V=ones((24*60*60),1).*NaN;
        STK_Mast.PTemp=ones((24*60*60),1).*NaN;
        STK_Mast.Batt_V=ones((24*60*60),1).*NaN;
        
        counter=0;
        for nm=0:23
            for cc=3:length(list_gps)
                load(fullfile(di{1},list_gps(cc).name));
                data_gps=[];
                if ((data.PC_TIME(1,1)==str2num(year))&(data.PC_TIME(1,2)==n)&(data.PC_TIME(1,3)==m)&(data.PC_TIME(1,4)==nm))
                    data_gps=data;
                    break;
                end    
                clear data
            end
            
            for cc=3:length(list_TRH)
                load(fullfile(di{2},list_TRH(cc).name));
                data_TRH=[];
                if ((data.PC_TIME(1,1)==str2num(year))&(data.PC_TIME(1,2)==n)&(data.PC_TIME(1,3)==m)&(data.PC_TIME(1,4)==nm))
                    data_TRH=data;
                    break;
                end    
                clear data
            end
            
            for cc=3:length(list_TRH_volt)
                load(fullfile(di{3},list_TRH_volt(cc).name));
                data_TRH_volt=[];
                if ((data.PC_TIME(1,1)==str2num(year))&(data.PC_TIME(1,2)==n)&(data.PC_TIME(1,3)==m)&(data.PC_TIME(1,4)==nm))
                    data_TRH_volt=data;
                    break;
                end    
                clear data
            end
            for c1=0:59
                for c2=0:59
                    counter=counter+1;
                    STK_Mast.PC_TIME(counter,4)=nm;STK_Mast.PC_TIME(counter,5)=c1;STK_Mast.PC_TIME(counter,6)=c2;
                    if ~isempty(data_gps)
                        ix=find( (data_gps.PC_TIME(:,1)==STK_Mast.PC_TIME(1,1)) & ...
                            (data_gps.PC_TIME(:,2)==STK_Mast.PC_TIME(1,2)) & ...   
                            (data_gps.PC_TIME(:,3)==STK_Mast.PC_TIME(1,3)) & ...
                            (data_gps.PC_TIME(:,4)==nm) & ...
                            (data_gps.PC_TIME(:,5)==c1) & ...
                            (data_gps.PC_TIME(:,6)==c2));
                        if ~isempty(ix)
                            STK_Mast.GPS_TIME(counter,:)=data_gps.GPS_TIME(ix,:);
                            STK_Mast.Latitude(counter)=data_gps.latitude(ix);
                            STK_Mast.Longitude(counter)=data_gps.longitude(ix);
                            STK_Mast.Altitude(counter)=data_gps.altitude(ix);
                        end    
                    end
                    if ~isempty(data_TRH)
                        ix=find( (data_TRH.PC_TIME(:,1)==STK_Mast.PC_TIME(1,1)) & ...
                            (data_TRH.PC_TIME(:,2)==STK_Mast.PC_TIME(1,2)) & ...   
                            (data_TRH.PC_TIME(:,3)==STK_Mast.PC_TIME(1,3)) & ...
                            (data_TRH.PC_TIME(:,4)==nm) & ...
                            (data_TRH.PC_TIME(:,5)==c1) & ...
                            (data_TRH.PC_TIME(:,6)==c2));
                        if ~isempty(ix)
                            STK_Mast.T1(counter)=data_TRH.T1(ix);
                            STK_Mast.RH1(counter)=data_TRH.RH1(ix);
                            STK_Mast.T2(counter)=data_TRH.T2(ix);
                            STK_Mast.RH2(counter)=data_TRH.RH2(ix);
                        end
                    end
                    if ~isempty(data_TRH_volt)
                        ix=find( (data_TRH_volt.PC_TIME(:,1)==STK_Mast.PC_TIME(1,1)) & ...
                            (data_TRH_volt.PC_TIME(:,2)==STK_Mast.PC_TIME(1,2)) & ...   
                            (data_TRH_volt.PC_TIME(:,3)==STK_Mast.PC_TIME(1,3)) & ...
                            (data_TRH_volt.PC_TIME(:,4)==nm) & ...
                            (data_TRH_volt.PC_TIME(:,5)==c1) & ...
                            (data_TRH_volt.PC_TIME(:,6)==c2));
                        if ~isempty(ix)
                            STK_Mast.T1V(counter)=data_TRH_volt.T1V(ix);
                            STK_Mast.RH1V(counter)=data_TRH_volt.RH1V(ix);
                            STK_Mast.T2V(counter)=data_TRH_volt.T2V(ix);
                            STK_Mast.RH2V(counter)=data_TRH_volt.RH2V(ix);
                            STK_Mast.PTemp(counter)=data_TRH_volt.PTemp(ix);
                            STK_Mast.Batt_V(counter)=data_TRH_volt.Batt_V(ix);
                        end
                    end
                end
            end
        end
        
        day=num2str(m);
        if length(day)<2
            day=['0',day];
        end
    
        display(['saving ',fullfile(do,['Mast_',year,month(n,:),day,'.mat'])])
        save(fullfile(do,['Mast_',year,month(n,:),day,'.mat']),'STK_Mast')
    
        clear STK_Mast
    end
end