import glob
import os
from subprocess import Popen
from __future__ import print_function

# Create output directory
os.makedirs("bgs_example_data/seek_cache")

# Get a list of all files that match the regex
files = glob.glob('bgs_example_data/2016/03/21/HIMap_RSG7M_A1_24_MP_PXX_Z0_C0*')
list.sort(files) # Sort files by name

# For some reason SEEK only processes the first file in the folder (no idea why)
# So we'll just process one file, delete it, run SEEK again, and process the next and so on
for i in range(len(files)):
    # Process first file in the folder
    os.system("seek --file-prefix='./bgs_example_data' --post-processing-prefix='bgs_example_data/seek_cache' --chi-1=20 --overwrite=True seek.config.process_survey_fft")
    # Remove this file so that in next iteration, the second file is processed
    os.remove(files[i])

print("Done. Check bgs_example_data/seek_cache/ for processed .h5 files")

"""
Explanation: <h1>SEEK: Signal Extraction and Emission Kartographer</h1>

SEEK is used for analyzing single dish radio survey data. It can process both simulated and observed data.
SEEK was developed based on the data taken at the Bleien Observatory.
For more information about SEEK refer to https://seek.readthedocs.io
We have used SEEK here to process the data from Bleien Observatory.Run the following command in your terminal to get the example data set.
$ wget -q -r -nH -np --cut-dirs=2 http://people.phys.ethz.ch/~ast/cosmo/bgs_example_data/
Installation instructions for seek can be found at https://seek.readthedocs.io
<h3> Processing the example data set using SEEK </h3>

Reference: https://github.com/jakeret/tf_unet/blob/master/demo/demo_radio_data.ipynb
After downloading the Bleien Survey data make sure that you save an extra copy of the downloaded data.
Then run the following python file in the same directory where you have downloaded the files.
<h4> Processing data generated from HIDE</h4>

For Processing Data generated by HIDE using SEEK we need to change  a setting in a SEEK config file.
The SEEK config file is process_survey_fft.py . This file is at the location "seek/seek/conifig", where the folder seek is the github repository we have cloned earlier.
In the process_survey_fft.py file on line 48 change the value of the variable "data_file_prefix" to the regular expression of the file name generated from HIDE.
End of explanation
"""




from __future__ import division, print_function
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import glob
import os
from scripts.radio_util import DataProvider
from tf_unet import unet


plt.rcParams['image.cmap'] = 'gist_earth'


# Read all required files
files = glob.glob('bgs_example_data/seek_cache/*')

# This is a tf_unet DataProvider
# It's a generator that provides training data and grund truth as required
# Still unsure what the first parameter does
data_provider = DataProvider(10000, files)
x_test, y_test = data_provider(96) # Get all 96 files

fig, ax = plt.subplots(1,2, figsize=(12,4))

for i in range(96):
    ax[0].imshow(x_test[i,...,0], aspect="auto")
    ax[1].imshow(y_test[i,...,1], aspect="auto")
	fig.savefig("pic_10000_"+str(i)+".png")



# We'll change the first parameter and see how different the data looks
# The source code did not mention anything about what each axis represents
data_provider = DataProvider(600, files)
x_test, y_test = data_provider(96)

fig, ax = plt.subplots(1,2, figsize=(12,4))

for i in range(96):
    ax[0].imshow(x_test[i,...,0], aspect="auto")
    ax[1].imshow(y_test[i,...,1], aspect="auto")
	fig.savefig("pic_600_"+str(i)+".png")


print ("Done processing. Images saved in current directory.")
print("It should look something like the ones here : https://imgur.com/a/D7vns")

"""
Explanation: <h3>Visualizing the processed data</h3>

To visualize the processed data from seek you need to have tf_unet installed.
Installation instructions of tf_unet can be found at https://tf_unet.readthedocs.io
Before doing visualization make sure that you have the processed data at ./bgs_example_data/seek_cahce or change the path in the code below.
End of explanation
"""


