# First column is time, second frequency and third phase
start = Time(date + start_time)
end = start + dt * fh.shape[0]
total_t = (end - start).to(u.s)
print("Length of time chunk:", (end - start).to(u.s))
total_tbins = np.int((total_t / subint).value) + 1
folded = np.zeros(shape=(total_tbins, fh.shape[1], n_phase))
counts = np.zeros(shape=(total_tbins, fh.shape[1], n_phase))
print("Shape of output is:", folded.shape)

# Set values for current time, current time bin and time array
start = Time(date + start_time)
t = start.copy()
time = []
current_time_bin = Time(date + start_time, precision=-1)
current_time_bin = Time(current_time_bin.__str__())
time.append(current_time_bin)
t_bin = 0
p0 = psr_polyco(t).value

# Loop through each data point
for i in range(fh.shape[0]):
    p = psr_polyco(t).value
    # Calculate which index
    if t >= current_time_bin + subint:
        t_bin += 1
        current_time_bin += subint
        time.append(current_time_bin)
    phase = np.int(np.floor((p % 1) * n_phase))
    # Populate arrays
    folded[t_bin, :, phase] += fh[i]
period_start = Time('2021-01-02T00:00:00.000', scale='utc')
period_end = Time('2021-01-03T00:00:00.000', scale='utc')
# define image search interval cadence and width
interval_cadence = 2 * u.hour
del_interval = 30 * u.minute
# define target times over download period using interval_cadence (image times in astropy Time() format)
target_times = Time(np.arange(period_start, period_end, interval_cadence))
# generate DataFrame that defines synchronic target times as well as min/max limits
synch_times = pd.DataFrame({
    'target_time': target_times,
    'min_time': target_times - del_interval,
    'max_time': target_times + del_interval
})

# specify path and filename for download_results file
download_results_filename = "download_results_" + period_start.__str__()
pickle_file = os.path.join(App.APP_HOME, "test_data",
                           download_results_filename)

# data-file dirs
raw_data_dir = App.RAW_DATA_HOME
hdf_data_dir = App.PROCESSED_DATA_HOME
# database location
database_dir = App.DATABASE_HOME
# give the sqlite file a unique name
sqlite_filename = App.DATABASE_FNAME

# designate which database to connect to
use_db = "mysql-Q"  # 'sqlite'  Use local sqlite file-based db
# 'mysql-Q' Use the remote MySQL database on Q
user = "******"  # only needed for remote databases.