Skip to content

Commit f9850d1

Browse files
committed
Merge branch 'feature/latex_tables' into main
2 parents 842fe77 + e1d891a commit f9850d1

File tree

2 files changed

+507
-3
lines changed

2 files changed

+507
-3
lines changed

neoexchange/core/tests/test_views.py

Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8498,6 +8498,356 @@ def test_muchfail(self):
84988498
self.assertEqual(expected_msg, msg)
84998499

85008500

8501+
class TestCreateLatexTable(TestCase):
8502+
8503+
def setUp(self):
8504+
body_params = {
8505+
'provisional_name': None,
8506+
'provisional_packed': None,
8507+
'name': '2005 QN173',
8508+
'origin': 'O',
8509+
'source_type': 'A',
8510+
'source_subtype_1': 'M',
8511+
'source_subtype_2': None,
8512+
'elements_type': 'MPC_MINOR_PLANET',
8513+
'active': True,
8514+
'fast_moving': False,
8515+
'urgency': None,
8516+
'epochofel': datetime(2020, 12, 17, 0, 0),
8517+
'orbit_rms': 0.65,
8518+
'orbinc': 0.06654,
8519+
'longascnode': 174.67813,
8520+
'argofperih': 145.83613,
8521+
'eccentricity': 0.2260268,
8522+
'meandist': 3.0664,
8523+
'meananom': 332.73044,
8524+
'perihdist': None,
8525+
'epochofperih': None,
8526+
'abs_mag': 16.02,
8527+
'slope': 0.15,
8528+
'score': None,
8529+
'discovery_date': datetime(2000, 11, 27, 0, 0),
8530+
'num_obs': 292,
8531+
'arc_length': 7149.0,
8532+
'not_seen': 380.627780639225,
8533+
'updated': True,
8534+
'ingest': datetime(2021, 7, 9, 15, 4, 0, 238661),
8535+
'update_time': datetime(2020, 6, 24, 0, 0)}
8536+
8537+
self.test_body = Body.objects.create(**body_params)
8538+
8539+
proposal_params = { 'code' : 'KEY202B-009',
8540+
'title' : 'LOOK Project',
8541+
'active' : True
8542+
}
8543+
self.key_proposal, created = Proposal.objects.get_or_create(**proposal_params)
8544+
8545+
sblock_params = {
8546+
'body' : self.test_body,
8547+
'cadence' : False,
8548+
'block_start' : datetime(2021, 7, 4, 3, 0),
8549+
'block_end' : datetime(2021, 7, 4,19, 30),
8550+
'proposal' : self.key_proposal,
8551+
'groupid' : '2005 QN173_E10-20210704_spectra',
8552+
'tracking_number' : '1255400',
8553+
}
8554+
self.test_sblock_spectra = SuperBlock.objects.create(**sblock_params)
8555+
8556+
self.block_spectra_params = {
8557+
'body' : self.test_body,
8558+
'superblock' : self.test_sblock_spectra,
8559+
'telclass' : '2m0',
8560+
'site' : 'coj',
8561+
'block_start' : datetime(2021, 7, 4, 3, 30),
8562+
'block_end' : datetime(2021, 7, 4, 19, 30),
8563+
'obstype' : Block.OPT_SPECTRA,
8564+
'request_number' : 4242,
8565+
'num_observed' : 1,
8566+
'when_observed' : datetime(2021, 7, 4, 6, 15),
8567+
'num_exposures' : 1,
8568+
'exp_length' : 3600
8569+
}
8570+
self.test_specblock = Block.objects.create(**self.block_spectra_params)
8571+
8572+
self.frame_params = { 'block' : self.test_specblock,
8573+
'filename' : 'coj2m002-en99-20210704-0042-e90.fits',
8574+
'sitecode' : 'E10',
8575+
'frametype': Frame.SPECTRUM_FRAMETYPE,
8576+
'filter' : 'SLIT_30.0x6.0AS',
8577+
'exptime' : 3600,
8578+
'midpoint' : datetime(2021, 7, 4, 6, 30)
8579+
}
8580+
test_frame = Frame.objects.create(**self.frame_params)
8581+
8582+
sblock_params = {
8583+
'body' : self.test_body,
8584+
'cadence' : True,
8585+
'block_start' : datetime(2021, 7, 1, 19, 0),
8586+
'block_end' : datetime(2019, 7, 23,19, 30),
8587+
'proposal' : self.key_proposal,
8588+
'tracking_number' : '1255300',
8589+
'period' : 7*24,
8590+
'jitter' : 72
8591+
}
8592+
self.test_sblock = SuperBlock.objects.create(**sblock_params)
8593+
8594+
for sitecode, day in zip(['K91', 'K93', 'K92'], range(7,22,7)):
8595+
self.block_params = {
8596+
'body' : self.test_body,
8597+
'superblock' : self.test_sblock,
8598+
'site' : 'cpt',
8599+
'block_start' : datetime(2021, 7, day, 22, 30),
8600+
'block_end' : datetime(2021, 7, day, 6, 30),
8601+
'obstype' : Block.OPT_IMAGING,
8602+
'request_number' : day*1000,
8603+
'num_observed' : 1,
8604+
'when_observed' : datetime(2021, 7, day, 23, 15),
8605+
'num_exposures' : 4,
8606+
'exp_length' : 120
8607+
}
8608+
self.test_objblock = Block.objects.create(**self.block_params)
8609+
8610+
for i, obs_filter in enumerate(['gp', 'gp', 'rp', 'rp']):
8611+
self.frame_params = { 'block' : self.test_objblock,
8612+
'filename' : f'cpt1m003-fa99-202107{day}-{42+i:04d}-e91.fits',
8613+
'sitecode' : sitecode,
8614+
'frametype': Frame.BANZAI_RED_FRAMETYPE,
8615+
'exptime' : 120,
8616+
'filter' : obs_filter,
8617+
'midpoint' : datetime(2021, 7, day, 3, (i*2)+1, 0)
8618+
}
8619+
8620+
test_frame = Frame.objects.create(**self.frame_params)
8621+
8622+
self.table_hdr = [ '\\begin{table}\n',
8623+
'\\caption{Table of observations for 2005 QN173 with LCOGT}\n',
8624+
'\\begin{tabular}{cccccccc}\n',
8625+
'\\hline \\hline\n',
8626+
'Block Start & Block End & Site & Telclass & MPC Site Code & Observation Type & Filters & Num Exposures \\\\\n',
8627+
'\\hline\n'
8628+
]
8629+
8630+
self.deluxetable_hdr = [\
8631+
'\\begin{deluxetable}{cccccccc}\n',
8632+
'\\tablecaption{Table of observations for 2005 QN173 with LCOGT}\n',
8633+
r'\tablehead{\colhead{Block Start} & \colhead{Block End} & \colhead{Site} & \colhead{Telclass} & \colhead{MPC Site Code} & \colhead{Observation Type} & \colhead{Filters} & \colhead{Num Exposures}}'+'\n',
8634+
'\\startdata\n'
8635+
]
8636+
8637+
self.table_hdr_no_obstype = \
8638+
[ '\\begin{table}\n',
8639+
'\\caption{Table of observations for 2005 QN173 with LCOGT}\n',
8640+
'\\begin{tabular}{ccccccc}\n',
8641+
'\\hline \\hline\n',
8642+
'Block Start & Block End & Site & Telclass & MPC Site Code & Filters & Num Exposures \\\\\n',
8643+
'\\hline\n'
8644+
]
8645+
8646+
self.table_ftr = [
8647+
'\\hline\n',
8648+
'\\end{tabular}\n',
8649+
'\n',
8650+
'\\end{table}\n'
8651+
]
8652+
8653+
self.deluxetable_ftr = [
8654+
'\\enddata\n',
8655+
'\n',
8656+
'\\end{deluxetable}\n'
8657+
]
8658+
8659+
self.expected_colnames = ['Block Start', 'Block End', 'Site', 'Telclass', 'MPC Site Code', 'Observation Type', 'Filters', 'Num Exposures']
8660+
8661+
self.maxDiff = None
8662+
8663+
def test_table_by_body(self):
8664+
8665+
lines = [
8666+
"2021-07-04 06:00 & 2021-07-04 07:00 & coj & 2m0 & E10 & Opt. spectra & $30.0\\arcsec\\times6.0\\arcsec$ slit & 0/1 \\\\\n",
8667+
"2021-07-07 03:00 & 2021-07-07 03:08 & cpt & 1m0 & K91 & Opt. imaging & g',r' & 4/4 \\\\\n",
8668+
"2021-07-14 03:00 & 2021-07-14 03:08 & cpt & 1m0 & K93 & Opt. imaging & g',r' & 4/4 \\\\\n",
8669+
"2021-07-21 03:00 & 2021-07-21 03:08 & cpt & 1m0 & K92 & Opt. imaging & g',r' & 4/4 \\\\\n",
8670+
]
8671+
8672+
expected_lines = self.table_hdr + lines + self.table_ftr
8673+
8674+
self.assertEqual(1, Body.objects.all().count())
8675+
self.assertEqual(2, SuperBlock.objects.all().count())
8676+
self.assertEqual(4, Block.objects.all().count())
8677+
self.assertEqual(13, Frame.objects.all().count())
8678+
8679+
out_buf = create_latex_table(self.test_body, return_table=False)
8680+
with open(os.path.join('/tmp', 'Didymos_obs_no_obstype.tex'), 'w') as fd:
8681+
out_buf.seek(0)
8682+
shutil.copyfileobj(out_buf, fd)
8683+
8684+
out_buf.seek(0)
8685+
8686+
for i, line in enumerate(out_buf.readlines()):
8687+
self.assertEqual(expected_lines[i], line)
8688+
8689+
def test_deluxetable_by_body(self):
8690+
8691+
lines = [
8692+
"2021-07-04 06:00 & 2021-07-04 07:00 & coj & 2m0 & E10 & Opt. spectra & $30.0\\arcsec\\times6.0\\arcsec$ slit & 0/1 \\\\\n",
8693+
"2021-07-07 03:00 & 2021-07-07 03:08 & cpt & 1m0 & K91 & Opt. imaging & g',r' & 4/4 \\\\\n",
8694+
"2021-07-14 03:00 & 2021-07-14 03:08 & cpt & 1m0 & K93 & Opt. imaging & g',r' & 4/4 \\\\\n",
8695+
"2021-07-21 03:00 & 2021-07-21 03:08 & cpt & 1m0 & K92 & Opt. imaging & g',r' & 4/4\n",
8696+
]
8697+
8698+
expected_lines = self.deluxetable_hdr + lines + self.deluxetable_ftr
8699+
8700+
self.assertEqual(1, Body.objects.all().count())
8701+
self.assertEqual(2, SuperBlock.objects.all().count())
8702+
self.assertEqual(4, Block.objects.all().count())
8703+
self.assertEqual(13, Frame.objects.all().count())
8704+
8705+
out_buf = create_latex_table(self.test_body, return_table=False, deluxetable=True)
8706+
# with open(os.path.join('/tmp', 'Didymos_obs_no_obstype.tex'), 'w') as fd:
8707+
# out_buf.seek(0)
8708+
# shutil.copyfileobj(out_buf, fd)
8709+
8710+
out_buf.seek(0)
8711+
8712+
for i, line in enumerate(out_buf.readlines()):
8713+
self.assertEqual(expected_lines[i], line)
8714+
8715+
def test_table_by_name(self):
8716+
8717+
lines = [
8718+
"2021-07-04 06:00 & 2021-07-04 07:00 & coj & 2m0 & E10 & Opt. spectra & $30.0\\arcsec\\times6.0\\arcsec$ slit & 0/1 \\\\\n",
8719+
"2021-07-07 03:00 & 2021-07-07 03:08 & cpt & 1m0 & K91 & Opt. imaging & g',r' & 4/4 \\\\\n",
8720+
"2021-07-14 03:00 & 2021-07-14 03:08 & cpt & 1m0 & K93 & Opt. imaging & g',r' & 4/4 \\\\\n",
8721+
"2021-07-21 03:00 & 2021-07-21 03:08 & cpt & 1m0 & K92 & Opt. imaging & g',r' & 4/4 \\\\\n",
8722+
]
8723+
8724+
expected_lines = self.table_hdr + lines + self.table_ftr
8725+
8726+
self.assertEqual(1, Body.objects.all().count())
8727+
self.assertEqual(2, SuperBlock.objects.all().count())
8728+
self.assertEqual(4, Block.objects.all().count())
8729+
self.assertEqual(13, Frame.objects.all().count())
8730+
8731+
out_buf = create_latex_table(self.test_body.name, return_table=False)
8732+
out_buf.seek(0)
8733+
8734+
for i, line in enumerate(out_buf.readlines()):
8735+
self.assertEqual(expected_lines[i], line)
8736+
8737+
def test_return_table(self):
8738+
8739+
lines = [
8740+
"2021-07-04 06:00 & 2021-07-04 07:00 & coj & 2m0 & E10 & Opt. spectra & $30.0\\arcsec\\times6.0\\arcsec$ slit & 0/1 \\\\\n",
8741+
"2021-07-07 03:00 & 2021-07-07 03:08 & cpt & 1m0 & K91 & Opt. imaging & g',r' & 4/4 \\\\\n",
8742+
"2021-07-14 03:00 & 2021-07-14 03:08 & cpt & 1m0 & K93 & Opt. imaging & g',r' & 4/4 \\\\\n",
8743+
"2021-07-21 03:00 & 2021-07-21 03:08 & cpt & 1m0 & K92 & Opt. imaging & g',r' & 4/4 \\\\\n",
8744+
]
8745+
8746+
expected_lines = self.table_hdr + lines + self.table_ftr
8747+
8748+
self.assertEqual(1, Body.objects.all().count())
8749+
self.assertEqual(2, SuperBlock.objects.all().count())
8750+
self.assertEqual(4, Block.objects.all().count())
8751+
self.assertEqual(13, Frame.objects.all().count())
8752+
8753+
out_buf, data_table = create_latex_table(self.test_body.name, return_table=True)
8754+
out_buf.seek(0)
8755+
8756+
for i, line in enumerate(out_buf.readlines()):
8757+
self.assertEqual(expected_lines[i], line)
8758+
8759+
self.assertFalse(type(data_table) == dict)
8760+
self.assertEqual(Block.objects.all().count(), len(data_table))
8761+
self.assertEqual(self.expected_colnames, data_table.colnames)
8762+
8763+
def test_table_filter_trans(self):
8764+
8765+
# Create MuSCAT block
8766+
self.test_objblock.id = None
8767+
self.test_objblock.site = 'ogg'
8768+
self.test_objblock.telclass = '2m0'
8769+
self.test_objblock.block_start : datetime(2021, 7, 22, 5, 30)
8770+
self.test_objblock.block_end : datetime(2021, 7, 22, 15, 30)
8771+
self.test_objblock.when_observed : datetime(2021, 7, 22, 15, 15)
8772+
self.test_objblock.save()
8773+
8774+
for i, obs_filter in enumerate(['gp', 'rp', 'ip', 'zs']):
8775+
frame_params = { 'block' : self.test_objblock,
8776+
'filename' : f'ogg2m001-ep99-20210822-{42+i:04d}-e91.fits',
8777+
'sitecode' : 'F65',
8778+
'frametype': Frame.BANZAI_RED_FRAMETYPE,
8779+
'exptime' : 60,
8780+
'filter' : obs_filter,
8781+
'midpoint' : datetime(2021, 7, 22, 15, i*2+1, 0)
8782+
}
8783+
8784+
test_frame = Frame.objects.create(**frame_params)
8785+
8786+
lines = [
8787+
"2021-07-07 03:00 & 2021-07-07 03:08 & cpt & 1m0 & K91 & Opt. imaging & g',r' & 4/4 \\\\\n",
8788+
"2021-07-14 03:00 & 2021-07-14 03:08 & cpt & 1m0 & K93 & Opt. imaging & g',r' & 4/4 \\\\\n",
8789+
"2021-07-21 03:00 & 2021-07-21 03:08 & cpt & 1m0 & K92 & Opt. imaging & g',r' & 4/4 \\\\\n",
8790+
"2021-07-22 15:00 & 2021-07-22 15:06 & ogg & 2m0 & F65 & Opt. imaging & g',r',i',$\mathrm{z_{s}}$ & 4/4 \\\\\n",
8791+
]
8792+
8793+
expected_lines = self.table_hdr + lines + self.table_ftr
8794+
8795+
self.assertEqual(1, Body.objects.all().count())
8796+
self.assertEqual(2, SuperBlock.objects.all().count())
8797+
self.assertEqual(5, Block.objects.all().count())
8798+
self.assertEqual(17, Frame.objects.all().count())
8799+
8800+
def test_table_filter_trans_no_obstype(self):
8801+
8802+
# Remove spectra SuperBlock
8803+
self.test_sblock_spectra.delete()
8804+
8805+
# Create MuSCAT block
8806+
self.test_objblock.id = None
8807+
self.test_objblock.site = 'ogg'
8808+
self.test_objblock.telclass = '2m0'
8809+
self.test_objblock.block_start : datetime(2021, 7, 22, 5, 30)
8810+
self.test_objblock.block_end : datetime(2021, 7, 22, 15, 30)
8811+
self.test_objblock.when_observed : datetime(2021, 7, 22, 15, 15)
8812+
self.test_objblock.save()
8813+
8814+
for i, obs_filter in enumerate(['gp', 'rp', 'ip', 'zs']):
8815+
frame_params = { 'block' : self.test_objblock,
8816+
'filename' : f'ogg2m001-ep99-20210822-{42+i:04d}-e91.fits',
8817+
'sitecode' : 'F65',
8818+
'frametype': Frame.BANZAI_RED_FRAMETYPE,
8819+
'exptime' : 60,
8820+
'filter' : obs_filter,
8821+
'midpoint' : datetime(2021, 7, 22, 15, i*2+1, 0)
8822+
}
8823+
8824+
test_frame = Frame.objects.create(**frame_params)
8825+
8826+
lines = [
8827+
"2021-07-07 03:00 & 2021-07-07 03:08 & cpt & 1m0 & K91 & g',r' & 4/4 \\\\\n",
8828+
"2021-07-14 03:00 & 2021-07-14 03:08 & cpt & 1m0 & K93 & g',r' & 4/4 \\\\\n",
8829+
"2021-07-21 03:00 & 2021-07-21 03:08 & cpt & 1m0 & K92 & g',r' & 4/4 \\\\\n",
8830+
"2021-07-22 15:00 & 2021-07-22 15:07 & ogg & 2m0 & F65 & g',r',i',$\mathrm{z_{s}}$ & 4/4 \\\\\n",
8831+
]
8832+
8833+
expected_lines = self.table_hdr_no_obstype + lines + self.table_ftr
8834+
8835+
self.assertEqual(1, Body.objects.all().count())
8836+
self.assertEqual(1, SuperBlock.objects.all().count())
8837+
self.assertEqual(4, Block.objects.all().count())
8838+
self.assertEqual(16, Frame.objects.all().count())
8839+
8840+
out_buf, data_table = create_latex_table(self.test_body.name, return_table=True)
8841+
out_buf.seek(0)
8842+
8843+
for i, line in enumerate(out_buf.readlines()):
8844+
self.assertEqual(expected_lines[i], line)
8845+
8846+
self.assertFalse(type(data_table) == dict)
8847+
self.assertEqual(Block.objects.all().count(), len(data_table))
8848+
self.expected_colnames.remove('Observation Type')
8849+
self.assertEqual(self.expected_colnames, data_table.colnames)
8850+
85018851
@override_settings(MEDIA_ROOT=tempfile.mkdtemp())
85028852
class TestDisplayDataproduct(TestCase):
85038853
def setUp(self):

0 commit comments

Comments
 (0)