def detect_time(time, archiveLocation, fileNamePrefix, year, has_zdb):
    times = []
    print "\nAssessing time", time
    time = time.upper()
    detect = "DETECT"
    period = "P1D"
    period_value = 1 # numeric value of period
    archiveLocation = add_trailing_slash(archiveLocation)
    subdaily = False
    
    if not os.path.isdir(archiveLocation):
        message = archiveLocation + " is not a valid location"
        log_sig_err(message, sigevent_url)
        return times
    
    if (time == detect or time == '' or time.startswith(detect+'/P')) and has_zdb==False:
    #detect everything including breaks in date
        dates = []
        for dirname, dirnames, filenames in os.walk(archiveLocation, followlinks=True):
            # Print subdirectories
            for subdirname in dirnames:
                print "Searching:", os.path.join(dirname, subdirname)

            for filename in filenames:
                if str(filename).startswith(fileNamePrefix) and len(filename) == (len(fileNamePrefix) + len("YYYYJJJ") + 5):
                    try:
                        filetime = filename[-12:-5]
                        filedate = datetime.strptime(filetime,"%Y%j")
                        dates.append(filedate)
                    except ValueError:
                        print "Skipping", filename
                elif str(filename).startswith(fileNamePrefix) and len(filename) == (len(fileNamePrefix) + len("YYYYJJJHHMMSS") + 5):
                    try:
                        filetime = filename[-18:-5]
                        filedate = datetime.strptime(filetime,"%Y%j%H%M%S")
                        dates.append(filedate)
                        subdaily = True
                        period = "PT24H"
                    except ValueError:
                        print "Skipping", filename
                else:
                    print "Ignoring", filename
        dates = sorted(list(set(dates)))
        
        # DEBUG: Print the entire list of dates found for the product
        #for testdate in dates:
        #    print datetime.strftime(testdate,"%Y-%m-%dT%H:%M:%SZ")
            
        # Get period, attempt to figure out period (in days) if none
        if time.startswith(detect+'/P'):
            period = time.split('/')[1]
        else:
            if len(dates) > 3: #check if the difference between first three dates are the same
                if subdaily == False:
                    diff1 = abs((dates[0] - dates[1]).days)
                    diff2 = abs((dates[1] - dates[2]).days)
                    diff3 = abs((dates[2] - dates[3]).days)
                    if diff1==diff2==diff3:
                        period = "P"+str(diff1)+"D"
                    elif 31 in [diff1, diff2, diff3]:
                        period = "P1M"
                    if 365 in [diff1, diff2, diff3]:
                        period = "P1Y"
                else:
                    diff1 = abs((dates[0] - dates[1]))
                    diff2 = abs((dates[1] - dates[2]))
                    diff3 = abs((dates[2] - dates[3]))
                    if diff1==diff2==diff3:
                        if diff1.seconds % 3600 == 0:
                            period = "PT"+str(diff1.seconds/3600)+"H"
                        elif diff1.seconds % 60 == 0:
                            period = "PT"+str(diff1.seconds/60)+"M"
                        else:
                            period = "PT"+str(diff1.seconds)+"S"
            message = "No period in time configuration for " + fileNamePrefix + " - detected " + period
            log_sig_warn(message, sigevent_url)
        print "Using period " + str(period)
        try:
            if subdaily == False:
                period_value = int(period[1:-1])
            else:
                period_value = int(period[2:-1])
        except ValueError:
            log_sig_err("Mixed period values are not supported on server: " + period, sigevent_url)
        # Search for date ranges
        if len(dates) == 0:
            message = "No files with dates found for '" + fileNamePrefix + "' in '" + archiveLocation + "' - please check if data exists."
            log_sig_err(message, sigevent_url)
            startdate = datetime.now() # default to now
        else:
            startdate = min(dates)
            print "Start of data " + datetime.strftime(startdate,"%Y-%m-%dT%H:%M:%SZ")
        enddate = startdate # set end date to start date for lone dates
        for i, d in enumerate(dates):
            # print d
            if period[-1] == "W":
                next_day = d + timedelta(weeks=period_value)
            elif period[-1] == "M" and subdaily == False:
                next_day = d + relativedelta(months=period_value)
            elif period[-1] == "Y":
                next_day = d + relativedelta(years=period_value)
            elif period[-1] == "H":
                next_day = d + relativedelta(hours=period_value)
            elif period[-1] == "M" and subdaily == True:
                next_day = d + relativedelta(minutes=period_value)
            elif period[-1] == "S":
                next_day = d + relativedelta(seconds=period_value)
            else:
                next_day = d + timedelta(days=period_value)
            
            try:
                if dates[i+1] == next_day:
                    enddate = next_day # set end date to next existing day
                else: # end of range
                    if subdaily == False:
                        print "Break in data beginning on " + datetime.strftime(next_day,"%Y-%m-%d")
                        start = datetime.strftime(startdate,"%Y-%m-%d")
                        end = datetime.strftime(enddate,"%Y-%m-%d")
                    else:
                        print "Break in data beginning on " + datetime.strftime(next_day,"%Y-%m-%dT%H:%M:%SZ")
                        start = datetime.strftime(startdate,"%Y-%m-%dT%H:%M:%SZ")
                        end = datetime.strftime(enddate,"%Y-%m-%dT%H:%M:%SZ")    
                    times.append(start+'/'+end+'/'+period)
                    startdate = dates[i+1] # start new range loop
                    enddate = startdate
            except IndexError:
                # breaks when loop completes
                if subdaily == False:
                    start = datetime.strftime(startdate,"%Y-%m-%d")
                    end = datetime.strftime(enddate,"%Y-%m-%d")
                else:
                    start = datetime.strftime(startdate,"%Y-%m-%dT%H:%M:%SZ")
                    end = datetime.strftime(enddate,"%Y-%m-%dT%H:%M:%SZ")                    
                times.append(start+'/'+end+'/'+period)
                print "End of data " + end
                print "Time ranges: " + ", ".join(times)
                return times
    
    else:
        intervals = time.split('/')
        if intervals[0][0] == 'P': #starts with period, so no start date
            start = detect
        else:
            start = ''
        has_period = False
        for interval in list(intervals):
            if len(interval) > 0:
                if interval[0] == 'P':
                    has_period = True
                    period = interval
                    intervals.remove(interval)
            else:
                intervals.remove(interval)
        if has_period == False:
            message = "No period in time configuration for " + fileNamePrefix
            if has_zdb == False:
                message = message + " - using P1D"
            log_sig_warn(message, sigevent_url)
        print "Using period " + period
        if len(intervals) == 2:
            start = intervals[0]
            end = intervals[1]
        else:
            if start == detect:
                end = intervals[0]
            else:
                start = intervals[0]
                end = detect
              
        if start==detect or end==detect:
            newest_year = ''
            oldest_year = ''
            if year == True: # get newest and oldest years
                years = []
                for subdirname in os.walk(archiveLocation, followlinks=True).next()[1]:
                    if subdirname != 'YYYY':
                        years.append(subdirname)
                years = sorted(years)
                print "Year directories available: " + ",".join(years)
                for idx in range(0, len(years)):
                    if len(os.listdir(archiveLocation+'/'+years[idx])) > 0:
                        if years[idx].isdigit() == True:
                            oldest_year = years[idx]
                            break; 
                for idx in reversed(range(0, len(years))):
                    if len(os.listdir(archiveLocation+'/'+years[idx])) > 0:
                        if years[idx].isdigit() == True:
                            newest_year = years[idx]
                            break;
        
            if (newest_year == '' or oldest_year == '') and year==True:
                mssg = "No data files found in year directories in " + archiveLocation 
                log_sig_warn(mssg, sigevent_url)
                return times
            elif year==True:
                print "Available range with data is %s to %s" % (oldest_year, newest_year)
                            
        if start==detect:
            dates = []
            for dirname, dirnames, filenames in os.walk(archiveLocation+'/'+oldest_year, followlinks=True):
                for filename in filenames:
                    if str(filename).startswith(fileNamePrefix) and len(filename) == (len(fileNamePrefix) + len("YYYYJJJ") + 5):
                        try:
                            filetime = filename[-12:-5]
                            filedate = datetime.strptime(filetime,"%Y%j")
                            dates.append(filedate)
                        except ValueError:
                            print "Skipping", filename
                    elif str(filename).startswith(fileNamePrefix) and len(filename) == (len(fileNamePrefix) + len("YYYYJJJHHMMSS") + 5):
                        try:
                            filetime = filename[-18:-5]
                            filedate = datetime.strptime(filetime,"%Y%j%H%M%S")
                            dates.append(filedate)
                            subdaily = True
                        except ValueError:
                            print "Skipping", filename
                    else:
                        print "Ignoring", filename
            if len(dates) == 0:
                message = "No valid files with dates found for '" + fileNamePrefix + "' in '" + archiveLocation +"/"+oldest_year + "' - please check if data exists."
                log_sig_err(message, sigevent_url)
                return times
            startdate = min(dates)
            if has_zdb==True:
                try:
                    zdb = archiveLocation+'/'+oldest_year+'/'+fileNamePrefix+datetime.strftime(startdate,"%Y%j")+'_.zdb'
                    startdate = datetime.strptime(str(read_zkey(zdb, 'ASC')),"%Y%m%d%H%M%S")
                    subdaily = True
                except ValueError:
                    log_sig_err("No valid time found in " + zdb, sigevent_url)
            if subdaily == False:
                start = datetime.strftime(startdate,"%Y-%m-%d")
            else:
                start = datetime.strftime(startdate,"%Y-%m-%dT%H:%M:%SZ")
        
        if end==detect:
            dates = []
            for dirname, dirnames, filenames in os.walk(archiveLocation+'/'+newest_year, followlinks=True):
                for filename in filenames:
                    if str(filename).startswith(fileNamePrefix) and len(filename) == (len(fileNamePrefix) + len("YYYYJJJ") + 5):
                        try:
                            filetime = filename[-12:-5]
                            filedate = datetime.strptime(filetime,"%Y%j")
                            dates.append(filedate)
                        except ValueError:
                            print "Skipping", filename
                    elif str(filename).startswith(fileNamePrefix) and len(filename) == (len(fileNamePrefix) + len("YYYYJJJHHMMSS") + 5):
                        try:
                            filetime = filename[-18:-5]
                            filedate = datetime.strptime(filetime,"%Y%j%H%M%S")
                            dates.append(filedate)
                            subdaily = True
                        except ValueError:
                            print "Skipping", filename
                    else:
                        print "Ignoring", filename
            enddate = max(dates)
            if has_zdb==True:
                try:
                    zdb = archiveLocation+'/'+oldest_year+'/'+fileNamePrefix+datetime.strftime(enddate,"%Y%j")+'_.zdb'
                    enddate = datetime.strptime(str(read_zkey(zdb, 'DESC')),"%Y%m%d%H%M%S")
                    subdaily = True
                except ValueError:
                    log_sig_err("No valid time found in " + zdb, sigevent_url)
            if subdaily == False:
                end = datetime.strftime(enddate,"%Y-%m-%d")
            else:
                end = datetime.strftime(enddate,"%Y-%m-%dT%H:%M:%SZ")
        
        if has_zdb == True and has_period == False:
            time = start+'/'+end
        else:
            time = start+'/'+end+'/'+period
        print str(time)
        times.append(time)
        
    return times
Checks time element to see if start or end time must be detected on the file system.
dataset/ETHPy150Open nasa-gibs/onearth/src/layer_config/bin/oe_configure_layer.py/detect_time