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=99999999, array='12m', \
        excludeCycle0=True, selectPol=True, minTimeBand={3:0., 6:0., 7:0.}, 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


choose only with %XY% polarization


excludeCycle0 data (exclude 2011.*)


only accept data from 12m array (or some cases "12m7m")


minimum integration time per band is 0 for B3, B6, and B7 -> means, show anything after filtering above criteria.
End of explanation
"""


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

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


