forked from jfrostburke/snex2
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhooks.py
More file actions
1024 lines (835 loc) · 38.8 KB
/
hooks.py
File metadata and controls
1024 lines (835 loc) · 38.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import requests
import logging
from astropy.time import Time, TimezoneInfo
from tom_dataproducts.models import ReducedDatum
import json
from tom_targets.models import Target
from custom_code.management.commands.ingest_ztf_data import get_ztf_data
from requests_oauthlib import OAuth1
from astropy.coordinates import SkyCoord
from astropy import units as u
from datetime import datetime, date, timedelta
import numpy as np
from django.contrib.auth.models import User
from django.conf import settings
import urllib
from sqlalchemy import create_engine, pool, and_, or_, not_, text
from sqlalchemy.orm import sessionmaker, aliased
from sqlalchemy.ext.automap import automap_base
from contextlib import contextmanager
from collections import OrderedDict
logger = logging.getLogger(__name__)
instrument_dict = {'2M0-FLOYDS-SCICAM': 'floyds',
'1M0-SCICAM-SINISTRO': 'sinistro',
'2M0-SCICAM-MUSCAT': 'muscat',
'0M4-SCICAM-SBIG': 'sbig0m4',
'0M4-SCICAM-QHY600': 'qhy',
}
priority_dict = {'NORMAL': 'normal',
'TIME_CRITICAL': 'time_critical',
'RAPID_RESPONSE': 'immediate_too'}
@contextmanager
def _get_session(db_address):
Base = automap_base()
engine = create_engine(db_address, poolclass=pool.NullPool)
Base.metadata.bind = engine
db_session = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
session = db_session()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
def _return_session(db_address=settings.SNEX1_DB_URL):
### This one is not run within a with loop, must be closed manually
Base = automap_base()
engine = create_engine(db_address, poolclass=pool.NullPool)
Base.metadata.bind = engine
db_session = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
session = db_session()
return session
def _load_table(tablename, db_address):
Base = automap_base()
engine = create_engine(db_address, poolclass=pool.NullPool)
Base.prepare(engine, reflect=True)
table = getattr(Base.classes, tablename)
return(table)
def _str_to_timestamp(datestring):
"""
Converts string to a timestamp compatible with MYSQL timestamp field
"""
timestamp = datetime.strptime(datestring, '%Y-%m-%dT%H:%M:%S')
return timestamp.strftime('%Y-%m-%d %H:%M:%S')
def _str_to_jd(datestring):
"""
Converts string to JD compatible with MYSQL double field
"""
newdatestring = _str_to_timestamp(datestring)
return np.round(Time(newdatestring, format='iso', scale='utc').jd, 8)
def _get_tns_params(target):
logger.info(f'Target sent for TNS parameters, {target}')
names = [target.name] + [t.name for t in target.aliases.all()]
tns_name = False
for name in names:
if 'SN' in name[:3]:
tns_name = name.replace(' ','').replace('SN', '')
break
elif 'AT' in name[:3]:
tns_name = name.replace(' ','').replace('AT', '')
break
if not tns_name:
return {'status': 'No TNS name'}
api_key = os.environ['TNS_APIKEY']
tns_id = os.environ['TNS_APIID']
tns_url = 'https://www.wis-tns.org/api/get/object'
json_list = [('objname',tns_name), ('objid',''), ('photometry','1'), ('spectra','0')]
json_file = OrderedDict(json_list)
try:
logger.info(f'Querying TNS for target {target} to url {tns_url} and json file {json_file}')
response = requests.post(tns_url, headers={'User-Agent': 'tns_marker{"tns_id":'+str(tns_id)+', "type":"bot", "name":"SNEx_Bot1"}'}, data={'api_key': api_key, 'data': json.dumps(json_file)})
parsed = json.loads(response.text, object_pairs_hook=OrderedDict)
result = json.dumps(parsed, indent=4)
result = json.loads(result)
discoverydate = result['data']['discoverydate']
discoverymag = result['data']['discoverymag']
discoveryfilt = result['data']['discmagfilter']['name']
nondets = {}
dets = {}
photometry = result['data']['photometry']
for phot in photometry:
remarks = phot['remarks']
if 'Last non detection' in remarks:
nondet_jd = phot['jd']
nondet_filt = phot['filters']['name']
nondet_limmag = phot['limflux']
nondets[nondet_jd] = [nondet_filt, nondet_limmag]
else:
det_jd = phot['jd']
det_filt = phot['filters']['name']
det_mag = phot['flux']
dets[det_jd] = [det_filt, det_mag]
first_det = min(dets.keys())
last_nondet = 0
for nondet, phot in nondets.items():
if nondet > last_nondet and nondet < first_det:
last_nondet = nondet
response_data = {'success': 'Completed',
'nondetection': '{} ({})'.format(date.strftime(Time(last_nondet, scale='utc', format='jd').datetime, "%m/%d/%Y"), round(last_nondet, 2)) if last_nondet > 0 else None,
'nondet_mag': nondets[last_nondet][1] if last_nondet > 0 else None,
'nondet_filt': nondets[last_nondet][0] if last_nondet > 0 else None,
'detection': '{} ({})'.format(date.strftime(Time(first_det, scale='utc', format='jd').datetime, "%m/%d/%Y"), round(first_det, 2)),
'det_mag': dets[first_det][1],
'det_filt': dets[first_det][0]}
except:
logger.warning('TNS parameter ingestion failed for target {}'.format(target))
response_data = {'failure': 'Parameters not ingested'}
return response_data
def target_post_save(target, created, group_names=None, wrapped_session=None):
logger.info('Target post save hook: %s created: %s', target, created)
if not created:
### Add the last nondetection and first detection from TNS, if it exists
tns_results = _get_tns_params(target)
if tns_results.get('success', ''):
if tns_results['nondetection'] == None:
print('No TNS last nondetection found for target',target)
else:
nondet_date = tns_results['nondetection'].split()[0]
nondet_jd = tns_results['nondetection'].split()[1].replace('(', '').replace(')', '')
nondet_value = json.dumps({
'date': nondet_date,
'jd': nondet_jd,
'mag': tns_results['nondet_mag'],
'filt': tns_results['nondet_filt'],
'source': 'TNS'
})
logger.info(f'Saving target {target} after TNS nondetection ingestion')
Target.objects.filter(pk=target.pk).update(last_nondetection=nondet_value)
if tns_results['detection'] == None:
print('No TNS detection found for target',target)
else:
det_date = tns_results['detection'].split()[0]
det_jd = tns_results['detection'].split()[1].replace('(', '').replace(')', '')
det_value = json.dumps({
'date': det_date,
'jd': det_jd,
'mag': tns_results['det_mag'],
'filt': tns_results['det_filt'],
'source': 'TNS'
})
logger.info(f'Target {target} first detection saved from TNS.')
Target.objects.filter(pk=target.pk).update(first_detection=det_value)
### Ingest ZTF data, if a ZTF target
get_ztf_data(target)
### Not currently functional
#gaia_name = next((name for name in target.names if 'Gaia' in name), None)
#if gaia_name:
# base_url = 'http://gsaweb.ast.cam.ac.uk/alerts/alert'
# lightcurve_url = f'{base_url}/{gaia_name}/lightcurve.csv'
# response = requests.get(lightcurve_url)
# data = response._content.decode('utf-8').split('\n')[2:-2]
# jd = [x.split(',')[1] for x in data]
# mag = [x.split(',')[2] for x in data]
# for i in reversed(range(len(mag))):
# try:
# datum_mag = float(mag[i])
# datum_jd = Time(float(jd[i]), format='jd', scale='utc')
# value = {
# 'magnitude': datum_mag,
# 'filter': 'G_Gaia',
# 'error': 0 # for now
# }
# rd, created = ReducedDatum.objects.get_or_create(
# timestamp=datum_jd.to_datetime(timezone=TimezoneInfo()),
# value=value,
# source_name=target.name,
# source_location=lightcurve_url,
# data_type='photometry',
# target=target)
# rd.save()
# except:
# pass
else:
if wrapped_session:
db_session = wrapped_session
else:
db_session = _return_session(settings.SNEX1_DB_URL)
Targets = _load_table('targets', db_address=settings.SNEX1_DB_URL)
Targetnames = _load_table('targetnames', db_address=settings.SNEX1_DB_URL)
Groups = _load_table('groups', db_address=settings.SNEX1_DB_URL)
# Insert into SNEx 1 db
if group_names:
groupidcode = 0
for group_name in group_names:
groupidcode += int(db_session.query(Groups).filter(Groups.name==group_name).first().idcode)
else:
groupidcode = 32769 #Default in SNEx1
snex1_target = Targets(id=target.id, ra0=target.ra, dec0=target.dec, groupidcode=groupidcode, lastmodified=target.modified, datecreated=target.created)
db_session.add(snex1_target)
db_session.add(Targetnames(targetid=target.id, name=target.name, datecreated=target.created, lastmodified=target.modified))
if not wrapped_session:
try:
db_session.commit()
except:
db_session.rollback()
finally:
db_session.close()
else:
db_session.flush()
def targetextra_post_save(target):
'''
Hook to sync target classifications and redshifts
with SNEx1
'''
if not settings.DEBUG:
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
Targets = _load_table('targets', db_address=settings.SNEX1_DB_URL)
Classifications = _load_table('classifications', db_address=settings.SNEX1_DB_URL)
targetid = target.id
if target.classification != '': # Update the classification in the targets table in the SNex 1 db
classification = target.classification # Get the new classification
classification_query = db_session.query(Classifications).filter(Classifications.name==classification).first()
if classification_query:
# Get the corresponding id from the classifications table
classificationid = classification_query.id
db_session.query(Targets).filter(Targets.id==targetid).update({'classificationid': classificationid}) # Update the classificationid in the targets table
if target.redshift != '': # Now update the targets table with the redshift info
db_session.query(Targets).filter(Targets.id==targetid).update({'redshift': target.redshift})
logger.info(f'redshift should be now changed: {db_session.query(Targets).filter(Targets.id==targetid).first().redshift}')
db_session.commit()
logger.info(f'Classification and Redshift target post save hook: {target}')
def targetname_post_save(targetname, created):
'''
Hook to sync target name with SNEx1
'''
if not settings.DEBUG:
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
Names = _load_table('targetnames', db_address=settings.SNEX1_DB_URL)
targetid = int(targetname.target_id) # Get the targetid of our saved entry
name = targetname.name
if created:
db_session.add(Names(targetid=targetid, name=name, datecreated=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')))
db_session.commit()
logger.info('targetname post save hook: %s created: %s', targetname, created)
def sync_observation_with_snex1(snex_id, params, requestgroup_id, wrapped_session=None):
'''
Hook to sync an obervation record submitted through SNEx2
to the obslog table in the SNEx1 database
'''
if wrapped_session:
db_session = wrapped_session
else:
db_session = _return_session(settings.SNEX1_DB_URL)
#with _get_session(db_address=_snex1_address) as db_session:
Obslog = _load_table('obslog', db_address=settings.SNEX1_DB_URL)
filtlist = ['U', 'B', 'V', 'R', 'I', 'u', 'gp', 'rp', 'ip', 'zs', 'w']
if params['observation_type'] == 'IMAGING':
filters = ''
exptimes = ''
numexp = ''
for filt in filtlist:
filt_params = params.get(filt)
if filt_params and filt_params[0]:
if filters:
filters += ',' + filt[0]
else:
filters += filt[0]
if exptimes:
exptimes += ',' + str(int(filt_params[0]))
else:
exptimes += str(int(filt_params[0]))
if numexp:
numexp += ',' + str(filt_params[1])
else:
numexp += str(filt_params[1])
slit = 9999
else:
filters = 'floyds'
exptimes = params['exposure_time']
numexp = params['exposure_count']
slit = 2.0
if params.get('cadence_strategy'):
window = min(float(params.get('cadence_frequency')), 1)
else:
window = float(params.get('cadence_frequency'))
db_session.add(
Obslog(
user=67,
targetid=params['target_id'],
triggerjd=_str_to_jd(params['start']),
windowstart=_str_to_jd(params['start']),
windowend=_str_to_jd(params['start']) + window,
filters=filters,
exptime=exptimes,
numexp=numexp,
proposal=params['proposal'],
site=params.get('site', 'any'),
instrument=instrument_dict[params['instrument_type']],
sky=9999,
seeing=9999,
airmass=params['max_airmass'],
slit=slit,
priority=priority_dict.get(params['observation_mode'].replace(' ', '_'), 'normal'),
#priority=params['observation_mode'].lower().replace(' ', '_'),
ipp=params['ipp_value'],
requestsid=snex_id,
tracknumber=requestgroup_id
)
)
if not wrapped_session:
try:
db_session.commit()
except:
db_session.rollback()
finally:
db_session.close()
else:
db_session.flush()
logger.info('Sync observation request with SNEx1 hook: Observation for SNEx1 ID {} synced'.format(snex_id))
def sync_sequence_with_snex1(params, group_names, userid=67, comment=False, targetid=None, wrapped_session=None):
'''
Hook to sync an observation sequence submitted through SNEx2
to the obsrequests table in the SNEx1 database
'''
if wrapped_session:
db_session = wrapped_session
else:
db_session = _return_session(settings.SNEX1_DB_URL)
#with _get_session(db_address=_snex1_address) as db_session:
Obsrequests = _load_table('obsrequests', db_address=settings.SNEX1_DB_URL)
Groups = _load_table('groups', db_address=settings.SNEX1_DB_URL)
Notes = _load_table('notes', db_address=settings.SNEX1_DB_URL)
Users = _load_table('users', db_address=settings.SNEX1_DB_URL)
# Get the idcodes from the groups in the group_list
groupidcode = 0
for group_name in group_names:
groupidcode += int(db_session.query(Groups).filter(Groups.name==group_name).first().idcode)
filtlist = ['U', 'B', 'V', 'R', 'I', 'u', 'gp', 'rp', 'ip', 'zs', 'w']
if params['observation_type'] == 'IMAGING':
filters = ''
exptimes = ''
expnums = ''
blocknums = ''
for filt in filtlist:
filt_params = params.get(filt)
if filt_params and filt_params[0]:
if filters:
filters += ',' + filt[0]
else:
filters += filt[0]
if exptimes:
exptimes += ',' + str(int(filt_params[0]))
else:
exptimes += str(int(filt_params[0]))
if expnums:
expnums += ',' + str(filt_params[1])
else:
expnums += str(filt_params[1])
if blocknums:
blocknums += ',' + str(filt_params[2])
else:
blocknums += str(filt_params[2])
slit = 9999
else:
filters = 'none'
exptimes = params['exposure_time']
expnums = params['exposure_count']
blocknums = '1'
slit = 2
if params.get('cadence_strategy') == 'SnexResumeCadenceAfterFailureStrategy':
cadence = float(params.get('cadence_frequency'))
autostop = 0
window = min(cadence, 1)
else:
cadence = float(params.get('cadence_frequency', 1.0))
autostop = 1
window = float(params.get('cadence_frequency', 1.0))
if params.get('reminder'):
nextreminder = _str_to_timestamp(params.get('reminder'))
else:
nextreminder = '0000-00-00 00:00:00'
if userid != 67:
try:
# Get SNEx1 id corresponding to this user
snex2_user = User.objects.get(id=userid)
snex1_user = db_session.query(Users).filter(Users.name==snex2_user.username).first()
snex1_userid = snex1_user.id
except:
snex1_userid = 67
else:
snex1_userid = 67
newobsrequest = Obsrequests(
targetid=params['target_id'],
sequencestart=_str_to_timestamp(params['start']),
sequenceend='0000-00-00 00:00:00',
userstart=snex1_userid,
cadence=cadence,
window=window,
filters=filters,
exptimes=exptimes,
expnums=expnums,
blocknums=blocknums,
proposalid=params['proposal'],
ipp=params['ipp_value'],
site=params.get('site', 'any'),
instrument=instrument_dict[params['instrument_type']],
airmass=float(params['max_airmass']),
moondistlimit=float(params['min_lunar_distance']),
slit=slit,
acqradius=int(params.get('acquisition_radius', 0)),
guidermode=params.get('guider_mode', '').upper(),
guiderexptime=int(params.get('guider_exposure_time', 10)),
priority=priority_dict.get(params['observation_mode'].replace(' ', '_'), 'normal'),
#priority=params['observation_mode'].lower().replace(' ', '_'),
approved=1,
nextreminder=nextreminder,
groupidcode=groupidcode,
dismissed=0,
autostop=autostop,
datecreated=_str_to_timestamp(params['start']),
lastmodified=_str_to_timestamp(params['start'])
)
db_session.add(newobsrequest)
db_session.flush()
snex_id = newobsrequest.id
if comment and targetid:
newcomment = Notes(
targetid=targetid,
note=comment,
tablename='obsrequests',
tableid=snex_id,
posttime=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),
userid=snex1_userid,
datecreated=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
)
db_session.add(newcomment)
if not wrapped_session:
try:
db_session.commit()
except:
db_session.rollback()
finally:
db_session.close()
else:
db_session.flush()
logger.info('Sync observation sequence with SNEx1 hook: Observation for SNEx1 ID {} synced'.format(snex_id))
return snex_id
def cancel_sequence_in_snex1(snex_id, comment=False, tableid=None, userid=67, targetid=None, wrapped_session=None):
'''
Hook to cancel an observation sequence in SNEx1
that was canceled in SNEx2
'''
if wrapped_session:
db_session = wrapped_session
else:
db_session = _return_session(settings.SNEX1_DB_URL)
#with _get_session(db_address=_snex1_address) as db_session:
Obsrequests = _load_table('obsrequests', db_address=settings.SNEX1_DB_URL)
Notes = _load_table('notes', db_address=settings.SNEX1_DB_URL)
Users = _load_table('users', db_address=settings.SNEX1_DB_URL)
if userid != 67:
try:
# Get SNEx1 id corresponding to this user
snex2_user = User.objects.get(id=userid)
snex1_user = db_session.query(Users).filter(Users.name==snex2_user.username).first()
snex1_userid = snex1_user.id
except:
snex1_userid = 67
else:
snex1_userid = 67
snex1_row = db_session.query(Obsrequests).filter(Obsrequests.id==snex_id).first()
snex1_row.sequenceend = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
snex1_row.userend = snex1_userid
if comment and tableid and targetid:
newcomment = Notes(
targetid=targetid,
note=comment,
tablename='obsrequests',
tableid=tableid,
posttime=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),
userid=snex1_userid,
datecreated=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
)
db_session.add(newcomment)
if not wrapped_session:
try:
db_session.commit()
except:
db_session.rollback()
finally:
db_session.close()
else:
db_session.flush()
logger.info('Cancel sequence in SNEx1 hook: Sequence with SNEx1 ID {} synced'.format(snex_id))
def approve_sequence_in_snex1(snex_id):
'''
Hook to approve a pending observation request in SNEx1
that was approved in SNEx2
'''
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
Obsrequests = _load_table('obsrequests', db_address=settings.SNEX1_DB_URL)
snex1_row = db_session.query(Obsrequests).filter(Obsrequests.id==snex_id).first()
snex1_row.approved = 1
snex1_row.lastmodified = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
db_session.commit()
logger.info('Approve sequence in SNEx1 hook: Sequence with SNEx1 ID {} synced'.format(snex_id))
def update_reminder_in_snex1(snex_id, next_reminder, wrapped_session=None):
'''
Hook to update reminder for sequence in SNEx1.
Runs after continuing a sequence from the scheduling page.
'''
if wrapped_session:
db_session = wrapped_session
else:
db_session = _return_session(settings.SNEX1_DB_URL)
#with _get_session(db_address=_snex1_address) as db_session:
Obsrequests = _load_table('obsrequests', db_address=settings.SNEX1_DB_URL)
snex1_row = db_session.query(Obsrequests).filter(Obsrequests.id==snex_id).first()
now = datetime.now()
snex1_row.nextreminder = datetime.strftime(now + timedelta(days=next_reminder), '%Y-%m-%d %H:%M:%S')
if not wrapped_session:
try:
db_session.commit()
except:
db_session.rollback()
finally:
db_session.close()
else:
db_session.flush()
logger.info('Update reminder in SNEx1 hook: Sequence with SNEx1 ID {} synced'.format(snex_id))
def find_images_from_snex1(targetid, username, allimages=False):
'''
Hook to find filenames of images in SNEx1,
given a target ID
'''
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
# now queries the snex1 database directly as .execute instead of .query, so don't need to load in Photlco as a table
# Photlco = _load_table('photlco', db_address=settings.SNEX1_DB_URL)
Users = _load_table('users', db_address=settings.SNEX1_DB_URL)
Targets = _load_table('targets', db_address=settings.SNEX1_DB_URL)
this_user = db_session.query(Users).filter(Users.name==username).first()
this_target = db_session.query(Targets).filter(Targets.id==targetid).first()
if not allimages:
query = db_session.execute(
text("SELECT * FROM photlco WHERE targetid = :tid AND filetype = 1 "
"AND BIT_COUNT(COALESCE(groupidcode, :target_perm) & :user_groupid) > 0 ORDER BY id DESC LIMIT 8"),
{'tid':targetid, 'target_perm': this_target.groupidcode, 'user_groupid': this_user.groupidcode}).all()
else:
query = db_session.execute(
text("SELECT * FROM photlco WHERE targetid = :tid AND filetype = 1 "
"AND BIT_COUNT(COALESCE(groupidcode, :target_perm) & :user_groupid) > 0 ORDER BY id DESC"),
{'tid':targetid, 'target_perm': this_target.groupidcode, 'user_groupid': this_user.groupidcode}).all()
filepaths = [q.filepath.replace(settings.LSC_DIR, '').replace('/supernova/data/', '') for q in query]
if len(filepaths)==0:
raise IndexError(f"No images found for target {targetid}")
filenames = [q.filename.replace('.fits', '') for q in query]
dates = [date.strftime(q.dateobs, '%m/%d/%Y') for q in query]
teles = [q.telescope[:3] for q in query]
instr = [q.instrument for q in query]
filters = [q.filter for q in query]
exptimes = [str(round(float(q.exptime))) + 's' for q in query]
psfxs = [int(round(q.psfx)) for q in query]
psfys = [int(round(q.psfy)) for q in query]
logger.info('Found file names for target {}'.format(targetid))
return filepaths, filenames, dates, teles, instr, filters, exptimes, psfxs, psfys
def change_interest_in_snex1(targetid, username, status):
'''
Hook to change the status of an interested person
in SNEx1
'''
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
Interests = _load_table('interests', db_address=settings.SNEX1_DB_URL)
Users = _load_table('users', db_address=settings.SNEX1_DB_URL)
snex1_user = db_session.query(Users).filter(Users.name==username).first()
now = datetime.strftime(datetime.utcnow(), '%Y-%m-%d %H:%M:%S')
if status == 'interested':
oldinterest = db_session.query(Interests).filter(and_(Interests.userid==snex1_user.id, Interests.targetid==targetid)).first()
if not oldinterest:
newinterest = Interests(userid=snex1_user.id,
targetid=targetid,
interested=now)
db_session.add(newinterest)
else:
oldinterest.interested = now
elif status == 'uninterested':
oldinterest = db_session.query(Interests).filter(and_(Interests.userid==snex1_user.id, Interests.targetid==targetid)).first()
oldinterest.uninterested = now
db_session.commit()
logger.info('Synced {} interested in target {} with SNEx1'.format(username, targetid))
def sync_paper_with_snex1(paper):
'''
Hook to ingest a paper into SNEx1
'''
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
papers = _load_table('papers', db_address=settings.SNEX1_DB_URL)
status_dict = {'in prep': 'inprep',
'submitted': 'submitted',
'published': 'published'
}
targetid = paper.target_id
reference = paper.author_last_name + ' et al.'
status = status_dict[paper.status]
contents = paper.description
datecreated = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
newpaper = papers(targetid=targetid, reference=reference, status=status, contents=contents, datecreated=datecreated)
db_session.add(newpaper)
db_session.commit()
logger.info('Synced paper {} with SNEx1'.format(paper.id))
def sync_comment_with_snex1(comment, tablename, userid, targetid, snex1_rowid, mode='create', wrapped_session=None):
'''
Hook to sync an observation sequence submitted through SNEx2
to the obsrequests table in the SNEx1 database
'''
if wrapped_session:
db_session = wrapped_session
else:
db_session = _return_session(settings.SNEX1_DB_URL)
#with _get_session(db_address=_snex1_address) as db_session:
Notes = _load_table('notes', db_address=settings.SNEX1_DB_URL)
Users = _load_table('users', db_address=settings.SNEX1_DB_URL)
if userid != 67:
try:
# Get SNEx1 id corresponding to this user
snex2_user = User.objects.get(id=userid)
snex1_user = db_session.query(Users).filter(Users.name==snex2_user.username).first()
snex1_userid = snex1_user.id
except:
snex1_userid = 67
else:
snex1_userid = 67
existing_comment = db_session.query(Notes).filter(and_(Notes.targetid==targetid, Notes.note==comment, Notes.tablename==tablename, Notes.tableid==snex1_rowid)).first()
logger.info(f'existing comment in {tablename}: {existing_comment}')
if existing_comment and mode == 'delete':
logger.info(f'deleting existing comment {existing_comment.note}')
db_session.delete(existing_comment)
if not existing_comment and mode == 'create':
logger.info(f'creating new comment {comment}')
newcomment = Notes(
targetid=targetid,
note=comment,
tablename=tablename,
tableid=snex1_rowid,
posttime=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'),
userid=snex1_userid,
datecreated=datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
)
db_session.add(newcomment)
if not wrapped_session:
try:
logger.info(f'changes committed')
db_session.commit()
except:
logger.info(f'changes rolled back')
db_session.rollback()
finally:
db_session.close()
else:
db_session.flush()
logger.info('Synced comment for table {} from user {}'.format(tablename, userid))
def get_unreduced_spectra(allspec=True):
'''
Hook to find unreduced spectra for FLOYDS inbox
'''
token = os.environ['LCO_APIKEY']
response = requests.get('https://observe.lco.global/api/proposals?active=True&limit=50/',
headers={'Authorization': 'Token ' + token}).json()
proposals = [prop['id'] for prop in response['results']]
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
speclcoraw = _load_table('speclcoraw', db_address=settings.SNEX1_DB_URL)
targetnames = _load_table('targetnames', db_address=settings.SNEX1_DB_URL)
targets = _load_table('targets', db_address=settings.SNEX1_DB_URL)
classifications = _load_table('classifications', db_address=settings.SNEX1_DB_URL)
spec = _load_table('spec', db_address=settings.SNEX1_DB_URL)
original_filenames = [s.original for s in db_session.query(spec).filter(and_(spec.original!='None', spec.original!=None))]
unreduced_spectra = db_session.query(speclcoraw).join(
targets, speclcoraw.targetid==targets.id
).join(
targetnames, speclcoraw.targetid==targetnames.targetid
).join(
classifications, targets.classificationid==classifications.id, isouter=True
).filter(
and_(
not_(speclcoraw.filename.in_(original_filenames)),
speclcoraw.propid.in_(proposals),
speclcoraw.filename.contains('e00.fits'),
or_(
classifications.name != 'Standard',
classifications.name == None
),
or_(
and_(
speclcoraw.type != 'LAMPFLAT',
speclcoraw.type != 'ARC'
),
speclcoraw.type == None
),
not_(speclcoraw.filepath.contains('bad')),
not_(targetnames.name.contains('test_'))
)
)
targetids = [s.targetid for s in unreduced_spectra]
propids = [s.propid for s in unreduced_spectra]
dateobs = [s.dateobs for s in unreduced_spectra]
paths = [s.filepath for s in unreduced_spectra]
filenames = [s.filename for s in unreduced_spectra]
imgpaths = [os.path.join(s.filepath.replace(settings.FLOYDS_DIR, '/snex2/data/floyds'), s.filename.replace('.fits', '.png')) for s in unreduced_spectra]
return targetids, propids, dateobs, paths, filenames, imgpaths
def get_standards_from_snex1(target_id):
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
photlco = _load_table('photlco', db_address=settings.SNEX1_DB_URL)
#targetnames = _load_table('targetnames', db_address=settings.SNEX1_DB_URL)
targets = _load_table('targets', db_address=settings.SNEX1_DB_URL)
std = aliased(photlco)
obj = aliased(photlco)
standard_info = db_session.query(
std.objname, std.filename, std.filter, std.dateobs,
std.telescope, std.instrument
).distinct().join(
targets, std.targetid==targets.id
).filter(
and_(
obj.telescopeid==std.telescopeid,
obj.instrumentid==std.instrumentid,
targets.classificationid==1,
obj.filter==std.filter,
obj.dayobs==std.dayobs,
obj.quality==127,
std.quality==127,
obj.targetid==target_id
)
)
return [dict(r._mapping) for r in standard_info]
def sync_users_with_snex1(user, delete=False, snex1_pw=''):
"""
Sync a new or updated user with SNEx1
Parameters
----------
user: User database object
delete: boolean, True if the user was just deleted
snex1_pw: str, to pass the snex1 password to supernova db
"""
with _get_session(db_address=settings.SNEX1_DB_URL) as db_session:
Groups = _load_table('groups', db_address=settings.SNEX1_DB_URL)
Users = _load_table('users', db_address=settings.SNEX1_DB_URL)
group_names = [g.name for g in user.groups.all()]
snex1_groups = db_session.query(Groups).filter(Groups.name.in_(group_names)).all()
groupidcode = sum(int(g.idcode) for g in snex1_groups)
if delete:
db_session.query(Users).filter(Users.name == user.username).delete()
logger.info(f'User {user.username} deleted from SNEx1')
else:
existing_user = db_session.query(Users).filter(Users.name == user.username).first()
if existing_user:
existing_user.pw = snex1_pw
existing_user.firstname = user.first_name
existing_user.lastname = user.last_name
existing_user.email = user.email
existing_user.groupidcode = groupidcode
logger.info(f'User {user.username} updated in SNEx1')
else:
new_user = Users(
name=user.username,
pw=snex1_pw,
groupidcode=groupidcode,
firstname=user.first_name,
lastname=user.last_name,
email=user.email,
datecreated=user.date_joined
)
db_session.add(new_user)
logger.info(f'User {user.username} created in SNEx1')
db_session.commit()
def download_test_image_from_archive():
"""
Download a test image from the LCO archive to test image thumbnails.
NOTE: Only runs in dev
Creates any directories needed to store the image and thumbnail.
Checks if the image exists and if not, downloads it from the archive.
Returns the image parameters needed to display its thumbnail.
"""
### Check if thumbnail directory exists, and if not make it
thumbnail_directory = settings.FITS_DIR
if not os.path.isdir(thumbnail_directory):
os.makedirs(os.path.join(settings.BASE_DIR, thumbnail_directory))
if not os.path.isdir(settings.THUMB_DIR):
os.mkdir(os.path.join(settings.BASE_DIR, settings.THUMB_DIR))
### Check if test image already exists in thumbnail directory,
### and if not download it
# 4 test images, first 3 are public, last is of 23ixf
test_thumbnail_basenames = ["elp1m008-fa16-20250725-0103-e91","elp0m414-sq31-20250713-0229-e00","ogg0m455-sq30-20250712-0249-e91","tfn0m436-sq33-20250718-0265-e91"]
for test_thumbnail_basename in test_thumbnail_basenames:
if not any([test_thumbnail_basename in f for f in os.listdir(thumbnail_directory)]):
### GET it from the archive
token = settings.FACILITIES['LCO']['api_key']
url = settings.FACILITIES['LCO']['archive_url']
results = requests.get(url,
headers={'Authorization': f'Token {token}'},
params={'basename': test_thumbnail_basename}).json()["results"]
thumbnail_url = results[0]["url"]
thumbnail_filename = results[0]["filename"]
# Download image and funpack it