Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from __future__ import with_statement 

from builtins import zip 

import json 

import os 

import numpy as np 

 

__all__ = ["jsonFromCatalog"] 

 

def jsonFromCatalog(obs_list, cat_class, db, json_dir): 

 

""" 

Parameters 

---------- 

obs_list is a list of ObservationMetaData 

 

cat_class is the class of InstanceCatalog from which we are generating 

our DIASources 

 

db is a CatalogDBObject connecting to the database that is the source 

of our DIASources 

 

json_dir is the directory where we will write out our output 

""" 

 

# taken from 

# https://bugs.python.org/issue24313 

def json_default(o): 

if isinstance(o, np.integer): return int(o) 

raise TypeError("Cannot JSON serialize %s" % o) 

 

for obs in obs_list: 

source_dict = {} 

cat = cat_class(db, obs_metadata=obs) 

obshistid = obs.OpsimMetaData['obsHistID'] 

for data in cat.iter_catalog(): 

source = dict(zip(cat._column_outputs, data)) 

#chipNum = str(source['ccdVisitId'])[-4:] 

chipNum = source['ccdVisitId']%10000 

if chipNum>0: 

tag = '%d_%d' % (chipNum, obshistid) 

if tag not in source_dict: 

source_dict[tag] = [] 

source_json = json.dumps(source, default=json_default) 

source_dict[tag].append(source_json) 

for tag in source_dict: 

file_name = os.path.join(json_dir, 'diaSources_%s.txt' % tag) 

with open(file_name, 'w') as file_handle: 

for source in source_dict[tag]: 

file_handle.write('%s\n' % source)