%FOR MAC USERS - COMMENT OUT LINE 24 AND COMMENT IN LINE 25
%IF YOU ARE USING CERES DATA BEYOND 2020, ADD PATH BELOW LINE 124
function[cld_frac, timeDif2] = m_srbParseNC(year, month, day,month1,day1,hours1,day_utc,hours_utc,latitude,longitude)

time_idx=find(month1==month & day1==day);
try
    time_idx=time_idx(1,1);
    if day_utc(time_idx)~=day1(time_idx)
        timeDif2=(24-hours_utc(time_idx)+hours1(time_idx));
    else
        timeDif2=hours1(time_idx)-hours_utc(time_idx);
    end

catch
    timeDif2=1.e+15;
    disp(strcat('No data for ', int2str(month), '/', int2str(day), ' so no time diff calculated'));
end

try

% the following gets the index for the requesting hour.
enterDate = datenum(year,month,day); %used in the if statement below because the if statement in the lines 21 through 25 belongs to SRB data only

filepath ='..\..\PutYourCERESdataHere\CERES_SYN1deg-3H_Terra-Aqua-MODIS_Ed4.1_Subset_';
%filepath ='../../PutYourCERESdataHere/CERES_SYN1deg-3H_Terra-Aqua-MODIS_Ed4.1_Subset_';

    if enterDate >= datenum(2009,05,01)  && enterDate <= datenum(2009,09,07)
        filename = [filepath, '20090501-20090907.nc']  ;
    end
    if enterDate >= datenum(2009,09,08)  && enterDate <= datenum(2010,01,15)
        filename = [filepath, '20090908-20100115.nc']  ;
    end
    if enterDate >= datenum(2010,01,16)  && enterDate <= datenum(2010,05,25)
        filename = [filepath, '20100116-20100525.nc']  ;
    end
    if enterDate >= datenum(2010,05,26)  && enterDate <= datenum(2010,10,02)
        filename = [filepath, '20100526-20101002.nc']  ;
    end
    if enterDate >= datenum(2010,10,03)  && enterDate <= datenum(2011,02,09)
        filename = [filepath, '20101003-20110209.nc']  ;
    end
    if enterDate >= datenum(2011,02,10)  && enterDate <= datenum(2011,06,19)
        filename = [filepath, '20110210-20110619.nc']  ;
    end 
    if enterDate >= datenum(2011,06,20)  && enterDate <= datenum(2011,10,27)
        filename = [filepath, '20110620-20111027.nc']  ;
    end 
    if enterDate >= datenum(2011,10,28)  && enterDate <= datenum(2012,03,05)
        filename = [filepath, '20111028-20120305.nc']  ;
    end
    if enterDate >= datenum(2012,03,06)  && enterDate <= datenum(2012,07,13)
        filename = [filepath, '20120306-20120713.nc']  ;
    end
    if enterDate >= datenum(2012,07,14)  && enterDate <= datenum(2012,11,20)
        filename = [filepath, '20120714-20121120.nc']  ;
    end
    if enterDate >= datenum(2012,11,21)  && enterDate <= datenum(2013,03,30)
        filename = [filepath, '20121121-20130330.nc']  ;
    end
    if enterDate >= datenum(2013,03,31)  && enterDate <= datenum(2013,08,07)
        filename = [filepath, '20130331-20130807.nc']  ;
    end
    if enterDate >= datenum(2013,08,08)  && enterDate <= datenum(2013,12,15)
        filename = [filepath, '20130808-20131215.nc']  ;
    end
    if enterDate >= datenum(2013,12,16)  && enterDate <= datenum(2014,04,24)
        filename = [filepath, '20131216-20140424.nc']  ;
    end
    if enterDate >= datenum(2014,04,25)  && enterDate <= datenum(2014,09,01)
        filename = [filepath, '20140425-20140901.nc']  ;
    end
    if enterDate >= datenum(2014,09,02)  && enterDate <= datenum(2015,01,09)
        filename = [filepath, '20140902-20150109.nc']  ;
    end
    if enterDate >= datenum(2015,01,10)  && enterDate <= datenum(2015,05,19)
        filename = [filepath, '20150110-20150519.nc']  ;
    end
    if enterDate >= datenum(2015,05,20)  && enterDate <= datenum(2015,09,26)
        filename = [filepath, '20150520-20150926.nc']  ;
    end
    if enterDate >= datenum(2015,09,27)  && enterDate <= datenum(2016,02,03)
        filename = [filepath, '20150927-20160203.nc']  ;
    end
    if enterDate >= datenum(2016,02,04)  && enterDate <= datenum(2016,06,12)
        filename = [filepath, '20160204-20160612.nc']  ;
    end
    if enterDate >= datenum(2016,06,13)  && enterDate <= datenum(2016,10,20)
        filename = [filepath, '20160613-20161020.nc']  ;
    end
    if enterDate >= datenum(2016,10,21)  && enterDate <= datenum(2017,02,27)
        filename = [filepath, '20161021-20170227.nc']  ;
    end
    if enterDate >= datenum(2017,02,28)  && enterDate <= datenum(2017,07,07)
        filename = [filepath, '20170228-20170707.nc']  ;
    end
    if enterDate >= datenum(2017,07,08)  && enterDate <= datenum(2017,11,14) 
        filename = [filepath, '20170708-20171114.nc']  ;
    end
    if enterDate >= datenum(2017,11,15)  && enterDate <= datenum(2018,03,24)
        filename = [filepath, '20171115-20180324.nc']  ;
    end
    if enterDate >= datenum(2018,03,25)  && enterDate <= datenum(2018,08,01)    
        filename = [filepath, '20180325-20180801.nc']  ;
    end
    if enterDate >= datenum(2018,08,02)  && enterDate <= datenum(2018,12,09)
        filename = [filepath, '20180802-20181209.nc']  ;
    end
    if enterDate >= datenum(2018,12,10)  && enterDate <= datenum(2019,04,18)         
        filename = [filepath, '20181210-20190418.nc']  ;
    end
    if enterDate >= datenum(2019,04,19)  && enterDate <= datenum(2019,08,26)      
        filename = [filepath, '20190419-20190826.nc']  ;
    end
    if enterDate >= datenum(2019,08,27)  && enterDate <= datenum(2020,01,03)
        filename = [filepath, '20190827-20200103.nc']  ;
    end
    if enterDate >= datenum(2020,01,04)  && enterDate <= datenum(2020,05,12)
        filename = [filepath, '20200104-20200512.nc']  ;
    end
    if enterDate >= datenum(2020,05,13)  && enterDate <= datenum(2020,09,19)
        filename = [filepath, '20200513-20200919.nc']  ;
    end
    if enterDate >= datenum(2020,09,20)  && enterDate <= datenum(2020,12,31)
        filename = [filepath, '20200920-20201231.nc']  ;
    end

    
    ncid = netcdf.open(filename,'NC_NOWRITE');
    


% here I find the index of the reqeusted latitude in our latitude array.
% the same goes for the longitude array. Using this index I variablize my
% latitude and longitude. (lat, lon)
lat = latitude;
lon = longitude;
if lon < 0
    lon = lon + 360;
end


    ncDates = ncread(filename, 'time');
    ncLatRange = ncread(filename, 'lat');
    ncLonRange = ncread(filename, 'lon');
    XDIM=ncLonRange;
    YDIM = ncLatRange;
    TDIM = nan(length(ncDates),1);
    i = 1:length(ncDates);
    TDIM(i)=double(ncDates(i)+datenum(2000,03,01));  
    
    
    % Next I look into the XDIM/YDIM and find the nearest XDIM/YDIM value
    [minDistLon, lonIndex] = min(abs(XDIM-lon));
    [minDistLat, latIndex] = min(abs(YDIM-lat));
    
    
    if enterDate >= datenum(2004,01,01) 
        %CERES DATA are used. CERES DATA Start measurements at 1:30 and end at 22:30 so we add extra 30 minutes in timeIndex below and adjust R    
        R = 1:3:30;
    
    
      
        if size(find(R==timeDif2+10))>0
            rfactor=timeDif2+10;
        else
            rdif=R-(timeDif2+10);
            rdifInd=find(rdif>0);
            rfactor=R(min(rdifInd));
        end
    
    %
                       
        
        
        
        timeIndex = find(TDIM == datenum(year, month, day,rfactor,30,0));
        timeIndex = [timeIndex timeIndex+1 timeIndex+2];
        timeIndex=timeIndex';
    
    
            
        startLocation=[lonIndex latIndex timeIndex(1)];
        countLocation=[1 1 3];       
        dummy_Low = squeeze(ncread(filename, 'cldarea_low_3h', startLocation,countLocation));
        dummy_Mlow = squeeze(ncread(filename, 'cldarea_mid_low_3h', startLocation,countLocation));
        dummy_Mhigh = squeeze(ncread(filename, 'cldarea_mid_high_3h', startLocation,countLocation));
        dummy_High = squeeze(ncread(filename, 'cldarea_high_3h', startLocation,countLocation));
        cld_frac_Low=dummy_Low;
        cld_frac_Mlow=dummy_Mlow;
        cld_frac_Mhigh=dummy_Mhigh;
        cld_frac_High=dummy_High;
    
        j=[1;1;1];
        cld_frac_Low=cld_frac_Low/100;
        cld_frac_Mlow=cld_frac_Mlow/100;
        cld_frac_Mhigh=cld_frac_Mhigh/100;
        cld_frac_High=cld_frac_High/100;
        
        % 4 cloud levels: low, mid-low, mid-high, high
        cld_frac = j-((j-cld_frac_Low).*(j-cld_frac_Mlow).*(j-cld_frac_Mhigh).*(j-cld_frac_High));
    
       
        % 2 cloud levels: low, mid-low
        %cld_frac = j-((j-cld_frac_Low).*(j-cld_frac_Mlow));
        
        %just low clouds
        %cld_frac=cld_frac_Low;
        %dividing by 100 to get from percent to fraction
        %cld_frac = cld_frac./100;
    
    end
catch
    cld_frac=[99 99 99];
    disp(strcat('No CERES data for ',int2str(month),'/',int2str(day)));
    
end
end
