Write out SNANA Simlibs

SNANA simlibs are an input to SNANA simulations and summarize the LSST OpSIM output. This notebook illustrates how to create these files from OpSim outputs.


In [1]:
import numpy as np
%matplotlib inline 
import matplotlib.pyplot as plt

import os

In [2]:
# Required packages sqlachemy, pandas (both are part of anaconda distribution, or can be installed with a python installer)
# One step requires the LSST stack, can be skipped for a particular OPSIM database in question
import OpSimSummary.summarize_opsim as so
from sqlalchemy import create_engine
import pandas as pd
print so.__file__


/Users/rbiswas/.local/lib/python2.7/site-packages/OpSimSummary-0.0.1.dev0-py2.7.egg/OpSimSummary/summarize_opsim.pyc

In [3]:
!ls /Users/rbiswas/.local/lib/python2.7/site-packages/OpSimSummary-0.0.1.dev0-py2.7.egg/OpSimSummary/summarize_opsim.pyc


ls: /Users/rbiswas/.local/lib/python2.7/site-packages/OpSimSummary-0.0.1.dev0-py2.7.egg/OpSimSummary/summarize_opsim.pyc: Not a directory

In [4]:
# This step requires LSST SIMS package MAF. The main goal of this step is to set DD and WFD to integer keys that 
# label an observation as Deep Drilling or for Wide Fast Deep.
# If you want to skip this step, you can use the next cell by uncommenting it, and commenting out this cell, if all you
# care about is the database used in this example. But there is no guarantee that the numbers in the cell below will work
# on other versions of opsim database outputs

from lsst.sims.maf import db
from lsst.sims.maf.utils import opsimUtils

In [5]:
# DD = 366
# WFD = 364

Read in OpSim output for modern versions: (sqlite formats)

Description of OpSim outputs are available on the page https://confluence.lsstcorp.org/display/SIM/OpSim+Datasets+for+Cadence+Workshop+LSST2015http://tusken.astro.washington.edu:8080 Here we will use the opsim output http://ops2.tuc.noao.edu/runs/enigma_1189/data/enigma_1189_sqlite.db.gz I have downloaded this database, unzipped and use the variable dbname to point to its location


In [6]:
dbname = '/Users/rbiswas/data/LSST/OpSimData/enigma_1189_sqlite.db'
opsdb = db.OpsimDatabase(dbname)
propID, propTags = opsdb.fetchPropInfo()
DD = propTags['DD'][0]
WFD = propTags['WFD'][0]

In [7]:
WFD


Out[7]:
364

In [7]:
enginefile = 'sqlite:///' + dbname

In [8]:
engine = create_engine(enginefile)

Read in the OpSim DataBase into a pandas dataFrame

The opsim database is a large file (approx 4.0 GB), but still possible to read into memory on new computers. You usually only need the Summary Table, which is about 900 MB. If you are only interested in the Deep Drilling Fields, you can use the read_sql_query to only select information pertaining to Deep Drilling Observations. This has a memory footprint of about 40 MB. Obviously, you can reduce this further by narrowing down the columns to those of interest only. For the entire Summary Table, this step takes a few minutes on my computer.


In [9]:
# Load to a dataframe
# Summary = pd.read_hdf('storage.h5', 'table')
Summary = pd.read_sql_table('Summary', engine)

In [10]:
EnigmaMain = Summary.query('propID == [364]')

In [11]:
EnigmaMainSummary = so.SummaryOpsim(EnigmaMain)

In [12]:
len(EnigmaMainSummary.fieldIds)


Out[12]:
2293

In [13]:
EnigmaMainSummary.writeSimlib('Enigma_1189.SIMLIB.MAIN.FULL')

In [14]:
clear(EnigmaMain, EnigmaMainSummary)




In [15]:
EnigmaMain = None

In [16]:
EnigmaMainSummary = None

In [17]:
EnigmaDeep = Summary.query('propID == [366]')

In [18]:
EnigmaDeepSummary = so.SummaryOpsim(EnigmaDeep)

In [19]:
EnigmaDeepSummary.writeSimlib('Enigma_1189.SIMLIB.DEEP.FULL')

In [20]:
DeepFields = EnigmaDeepSummary.fieldIds

In [21]:
EnigmaCombined = Summary.query('propID == [364,366]')

In [22]:
EnigmaCombined = EnigmaCombined[EnigmaCombined['fieldID'].isin(DeepFields)]

In [23]:
EnigmaCombined.drop_duplicates('expMJD', inplace=True)

In [24]:
EnigmaCombinedSummary = so.SummaryOpsim(EnigmaCombined)

In [25]:
EnigmaCombinedSummary.writeSimlib('Enigma_1189.SIMLIB.DEEP.COMBINED')

Read in OpSim output for 2.168 cast in sqlite format (even though there are some differences)


In [26]:
dbname_2168 = '/Users/rbiswas/data/LSST/OpSimData/opsim2_168_sqlite.db'
engine_name_2168 = 'sqlite:///' + dbname
engine_2168 = create_engine(engine_name_2168)
# dbAddressDict = {'dbAddress': dbname_2168, 'Summary':'summary'}
# print dbAddressDict
opsimDb2168 = opsimUtils.connectOpsimDb(engine_name_2168)


/Users/rbiswas/src/LSST/sims_catalogs_generation/python/lsst/sims/catalogs/generation/db/dbConnection.py:119: FutureWarning: Database name 'sqlite:////Users/rbiswas/data/LSST/OpSimData/enigma_1189_sqlite.db' is invalid but looks like a dbAddress. Attempting to convert to database, driver, host, and port parameters. Any usernames and passwords are ignored and must be in the db-auth.paf policy file. 
  "be in the db-auth.paf policy file. "%(self.database), FutureWarning)

In [27]:
propID, propTags = opsimDb2168.fetchPropInfo()

In [28]:
propTags


Out[28]:
{'DD': [366], 'WFD': [364]}

This does not work for the old format. Read the SSTar document: http://opsimcvs.tuc.noao.edu/runs/opsim2.168/SSTAR-opsim2-168.pdf and find the deep drilling propIDs from Table 1.


In [29]:
DDold = 359, 360

This subsection is for sqlite transforms of old Versions of OpSim Outputs (eg. 2.168)


In [30]:
OpSim_2168_all = pd.read_sql_query('SELECT * FROM SUMMARY WHERE PROPID is 359 or PROPID is 360 or PROPID is 361', engine_2168)

As shown from a similar command as above this OpSim database has 197332 rows (close to about twice as the enigma case) and 46 columns. The memory on disc is also larger by a similar factor and is 70 MB


In [31]:
OpSim_2168_all.columns


Out[31]:
Index([u'obsHistID', u'sessionID', u'propID', u'fieldID', u'fieldRA', u'fieldDec', u'filter', u'expDate', u'expMJD', u'night', u'visitTime', u'visitExpTime', u'finRank', u'finSeeing', u'transparency', u'airmass', u'vSkyBright', u'filtSkyBrightness', u'rotSkyPos', u'lst', u'altitude', u'azimuth', u'dist2Moon', u'solarElong', u'moonRA', u'moonDec', u'moonAlt', u'moonAZ', u'moonPhase', u'sunAlt', u'sunAz', u'phaseAngle', u'rScatter', u'mieScatter', u'moonIllum', u'moonBright', u'darkBright', u'rawSeeing', u'wind', u'humidity', u'slewDist', u'slewTime', u'fiveSigmaDepth', u'ditheredRA', u'ditheredDec'], dtype='object')

In [32]:
OpSim_2168_Deep = OpSim_2168_all.query('propID == [359, 360]')

In [33]:
Opsim2168_Deep_FI = OpSim_2168_Deep.fieldID.unique()

In [34]:
Opsim_2168_Main = OpSim_2168_all.query('propID == [361]')

In [35]:
OpSim_2168_Main_summary = so.SummaryOpsim(Opsim_2168_Main)

In [36]:
OpSim_2168_Main_summary.writeSimlib('opsim_2168.SIMLIB.MAIN.FULL')

In [37]:
OpSim_2168_Main = None
Opsim_2168_Main_summary = None

In [38]:
OpSim_2168_Combined = OpSim_2168_all[OpSim_2168_all['fieldID'].isin(Opsim2168_Deep_FI)]

In [39]:
OpSim_2168_Deep_Summary = so.SummaryOpsim(OpSim_2168_Deep)

In [40]:
OpSim_2168_Deep_Summary.writeSimlib('opsim_2168.SIMLIB.DEEP.FULL_new')

In [41]:
OpSim_2168_Deep_Summary = None
OpSim_2168_Deep = None

In [42]:
OpSim_2168_Combined_Summary = so.SummaryOpsim(OpSim_2168_Combined)

In [43]:
OpSim_2168_Combined_Summary.writeSimlib('opsim_2168.SIMLIB.COMBINED.FULL')

In [49]:
opsim_test_df = pd.read_csv('test_opsimObsData.dat', delimiter='\s+')

In [50]:
opsim_test_df


Out[50]:
RA Dec MJD filter Alt Az Airmass Cloud RawSeeing finSeeing ... SunAz MoonAlt MoonAz MoonPhase MoonPhase(opsim) filtSkyBrightness fiveSigmaDepth SurveyNight Downtime fieldID
0 69.45379 -53.47048 49353.114583 i 66.16264 182.5537 1.089 0 0.729 0.802 ... 210.640 11.377 70.72 41.63 87.37 19.58 25.26 0 0 519
1 69.45379 -53.47048 49353.123877 r 65.90848 187.4313 1.091 0 0.839 0.928 ... 207.446 14.046 68.92 41.73 87.32 19.98 25.46 0 0 519

2 rows × 21 columns


In [52]:
testopsim = so.SummaryOpsim(opsim_test_df)

In [55]:
testopsim.simlib(519)


Out[55]:
RA Dec MJD filter Alt Az Airmass Cloud RawSeeing finSeeing ... MoonPhase MoonPhase(opsim) filtSkyBrightness fiveSigmaDepth SurveyNight Downtime fieldID simLibPsf simLibZPTAVG simLibSkySig
0 69.45379 -53.47048 49353.114583 i 66.16264 182.5537 1.089 0 0.729 0.802 ... 41.63 87.37 19.58 25.26 0 0 519 1.706383 34.854557 226.955848
1 69.45379 -53.47048 49353.123877 r 65.90848 187.4313 1.091 0 0.839 0.928 ... 41.73 87.32 19.98 25.46 0 0 519 1.974468 35.171022 218.390877

2 rows × 24 columns


In [58]:
EnigmaDeepSummary.df.simLibSkySig.max()


Out[58]:
269.33638549970993

In [ ]: