<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Reference Light Curves of SN
The basic plan
The set of quantities related to supernovae used in Twinkles are in a table called TwinkSN
on a computer at UW. This code in the Monitor package accesses this data to have a complete
characterization of the SN truth parameters.
It then creates an instance of lsst.sims.catUtils.supernovae.SNObject using these values.
Since SNObject has methods that estimate flux, and estimated flux uncertainty given 
parameters that characterize the observation, we can use this to find the light curves of
the supernovae. The light curves have two main quantities, the measured flux and uncertainty
on the measured flux. These quantities depend on the properties of the astrophysical objects,
and the properties of the observation. By properties of observation, we mean site/Hardware
properties and the properties of the sky at the time of the observation. The most important of
these are the bandpasses (transmissions) and fivesigma depth (obtained from OpSim). 
Thus,  monitor.RefLightCuves must be passed these quantities in order to obtain the light curve.
However, for analyzing the Twinkles run, where we have a clearer idea of both the bandpasses (LSST)
and the properties of the observation (from a selection of OpSim visits), we have a simple method to
set this up
Step1:  Observations
The quantities needed for the light curves are time (MJD), and fiveSigmaDepth. While this can come
from various formats, right now, there is only one that has been implemented
Step2:  Obtaining the parameters from the database
Setting up a connection to the CatSim DataBase which has parameters of the SN model.
Step3:  The RefLightCurves Class
This is instantiated in the following way
Step4:  Find the number of objects in the table
Step5:  Get all ids on the table
What are the IDs in the table (unique IDs)?
Step6:  The astrophysical object properties can be obtained by using the following function in the form of a pd.DataFrame
Or if the table is too large, and you only want to get a few of the IDs
Step7:  Get the parameters for an object
Step8:  The instance of the class representing the astrophysical object itself can be obtained by the following method for SN
Step9:  Following the usual methods in sims.catUtils.supernovae.SNObject the properties of this SN can be seen using
Calculating Light Curves
Step10:  Simpler methods for Twinkles
For the Twinkles Data Analysis, we can use a simpler method. The full API is shown below
Step11:  The tableName, idCol, objectTypeID, and columns will change from one astrophysical object to another.
The defaults are currently correct for SN, so one could simply do
reflcTwink = RefLightCurves.fromTwinklesData(tableName='TwinkSN")
The light Curves can be accessed in the following way
Step12:  We can get the light curves for each band by
<ASSISTANT_TASK:>
Python Code:
from __future__ import absolute_import, division, print_function
import os
import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
# Import from monitor
from desc.monitor import RefLightCurves
import desc.monitor as monitor
Explanation: Reference Light Curves of SN
The basic plan
The set of quantities related to supernovae used in Twinkles are in a table called TwinkSN
on a computer at UW. This code in the Monitor package accesses this data to have a complete
characterization of the SN truth parameters.
It then creates an instance of lsst.sims.catUtils.supernovae.SNObject using these values.
Since SNObject has methods that estimate flux, and estimated flux uncertainty given 
parameters that characterize the observation, we can use this to find the light curves of
the supernovae. The light curves have two main quantities, the measured flux and uncertainty
on the measured flux. These quantities depend on the properties of the astrophysical objects,
and the properties of the observation. By properties of observation, we mean site/Hardware
properties and the properties of the sky at the time of the observation. The most important of
these are the bandpasses (transmissions) and fivesigma depth (obtained from OpSim). 
Thus,  monitor.RefLightCuves must be passed these quantities in order to obtain the light curve.
However, for analyzing the Twinkles run, where we have a clearer idea of both the bandpasses (LSST)
and the properties of the observation (from a selection of OpSim visits), we have a simple method to
set this up:
monitor.RefLightCurve.fromTwinklesData.
Imports and Setup
End of explanation
data_dir = os.path.join(os.environ['MONITOR_DIR'], 'data')
opsimCsv = os.path.join(data_dir, 'SelectedKrakenVisits.csv')
opsimdf = pd.read_csv(opsimCsv, index_col='obsHistID')
df = opsimdf[['expMJD', 'filter', 'fiveSigmaDepth']]
Explanation: Observations
The quantities needed for the light curves are time (MJD), and fiveSigmaDepth. While this can come
from various formats, right now, there is only one that has been implemented: a  dataframe. The data directory
of the package desc.Monitor contains a csv file which has the necessary pointings
End of explanation
import pymssql
from lsst.utils import getPackageDir
import lsst.sims.catUtils.baseCatalogModels as bcm
from lsst.daf.persistence import DbAuth
config = bcm.BaseCatalogConfig()
config.load(os.path.join(getPackageDir("sims_catUtils"), "config", "db.py"))
username = DbAuth.username(config.host, config.port)    
password = DbAuth.password(config.host, config.port)
hostname = config.host
DBConnection = pymssql.connect(user=username,
                               password=password,
                               host=hostname,
                               database=config.database,
                               port=config.port)
db = DBConnection.cursor()
Explanation: Obtaining the parameters from the database
Setting up a connection to the CatSim DataBase which has parameters of the SN model.
End of explanation
# The ids are obtained from Instance Catalogs
reflc = RefLightCurves(idSequence=(6144007055260714, 6144158471480362),
                       tableName='TwinkSN',
                       dbConnection=DBConnection,
                       dbCursor=db)
Explanation: The RefLightCurves Class
This is instantiated in the following way:
End of explanation
reflc.dbConnection
print(reflc.get_numObjects())
reflc.get_numObjects()
Explanation: Find the number of objects in the table
End of explanation
ids = reflc.allIdinTable(chunksize=None)
print(ids.astype(int).values.flatten())
Explanation: Get all ids on the table
What are the IDs in the table (unique IDs)?
End of explanation
ids = reflc.allIdinTable(chunksize=10)
print(ids.next().astype(int).values.flatten())
Explanation: The astrophysical object properties can be obtained by using the following function in the form of a pd.DataFrame
Or if the table is too large, and you only want to get a few of the IDs
End of explanation
reflc.get_params(6144007055260714)
allParamsInIdSequence = reflc.get_params()
allParamsInIdSequence
reflcAll = RefLightCurves(tableName='TwinkSN',
                       dbConnection=DBConnection,
                       dbCursor=db)
# Slow because all rows
allParams = reflcAll.get_params()
allParams.head()
Explanation: Get the parameters for an object
End of explanation
sn = reflc.astro_object(idValue=6144007055260714)
Explanation: The instance of the class representing the astrophysical object itself can be obtained by the following method for SN
End of explanation
from lsst.sims.photUtils import BandpassDict
# Get the `bandpassDict` instance from files using catsim methods
lsstBP = BandpassDict.loadBandpassesFromFiles()
# This is a tuple, the first component gives the total bandpass, while the second gives the hardware bandpass
# Pass the bandpass and get the lightcurve for the observations 
reflc.lightCurve(idValue=6144007055260714, observations=df, bandPassDict=lsstBP[0])
Explanation: Following the usual methods in sims.catUtils.supernovae.SNObject the properties of this SN can be seen using
Calculating Light Curves
End of explanation
reflcTwink = RefLightCurves.fromTwinklesData(tableName='TwinkSN',
                                             idCol='id',
                                             objectTypeID=42,
                                             dbHostName=None,
                                             columns=('id', 'redshift', 'snra', 'sndec', 't0',
                                                      'x0', 'x1', 'c'),
                                             idSequence=None)
Explanation: Simpler methods for Twinkles
For the Twinkles Data Analysis, we can use a simpler method. The full API is shown below:
End of explanation
reflcTwink.idCol
reflcTwink.lightCurve(idValue=6144007055260714)
Explanation: The tableName, idCol, objectTypeID, and columns will change from one astrophysical object to another.
The defaults are currently correct for SN, so one could simply do
reflcTwink = RefLightCurves.fromTwinklesData(tableName='TwinkSN")
The light Curves can be accessed in the following way
End of explanation
reflcTwink.lightCurve(idValue=6144007055260714, bandName='r')
Explanation: We can get the light curves for each band by
End of explanation
<END_TASK>