import sys
sys.path.append('../src/')

from ALMAQueryCal import *

q = queryCal()

"""
Explanation: Selecting source and uid based on some criteria
End of explanation
"""


fileCal = "alma_sourcecat_searchresults.csv"
listCal = q.readCal(fileCal, fluxrange=[0.1, 9999999999])

print "Number of selected sources: ", len(listCal)

"""
Explanation: Select calibrator with Flux > 0.1 Jy
Calibrator list here is downloaded (2017-06-23) from ALMA calibrator source catalogue (https://almascience.eso.org/sc/)
End of explanation
"""


data    = q.queryAlma(listCal, public = True, savedb=True, dbname='calibrators_gt_0.1Jy.db')

"""
Explanation: We can found 1685 objects with F>0.1Jy* from ALMA Calibrator source catalogue
*from the first Band found in the list, usually B3
Query all information about the projects that use these objects as calibrator
using astroquery
End of explanation
"""


report = q.selectDeepfield_fromsql("calibrators_gt_0.1Jy.db", maxFreqRes=999999999, array='12m', \
        excludeCycle0=True, selectPol=False, minTimeBand={3:0., 6:1800., 7:1800.}, verbose=True, silent=True)

"""
Explanation: result of the query 'data' is in the form of Pandas DataFrame, but we save it in sql database also.
Note: Many of them only listed in calibrator list, but not observed/shown in any public data yet. 
Selection criteria
*We already choose the calibrator with F>0.1Jy + only Public data
Select sources with other criteria:

ignore freq res (for imaging)
ignore polarization product
excludeCycle0 data
only accept data from 12m array (or 12m7m)
minimum integration time per band is 1h for B3, B6, and B7 (after filtering all above)
End of explanation
"""


q.writeReport(report, "report4.txt", silent=True)

"""
Explanation: write the report in a file
End of explanation
"""


