Ejemplo n.º 1
0
def json_to_sdss_dlasurvey(json_file, sdss_survey, add_pf=True, debug=False):
    """ Convert JSON output file to a DLASurvey object
    Assumes SDSS bookkeeping for sightlines (i.e. PLATE, FIBER)

    Parameters
    ----------
    json_file : str
      Full path to the JSON results file
    sdss_survey : DLASurvey
      SDSS survey, usually human (e.g. JXP for DR5)
    add_pf : bool, optional
      Add plate/fiber to DLAs in sdss_survey

    Returns
    -------
    ml_survey : LLSSurvey
      Survey object for the LLS

    """
    print("Loading SDSS Survey from JSON file {:s}".format(json_file))
    # imports
    from pyigm.abssys.dla import DLASystem
    from pyigm.abssys.lls import LLSSystem
    # Fiber key
    for fkey in ['FIBER', 'FIBER_ID', 'FIB']:
        if fkey in sdss_survey.sightlines.keys():
            break
    # Read
    ml_results = ltu.loadjson(json_file)
    use_platef = False
    if 'plate' in ml_results[0].keys():
        use_platef = True
    else:
        if 'id' in ml_results[0].keys():
            use_id = True
    # Init
    #idict = dict(plate=[], fiber=[], classification_confidence=[],  # FOR v2
    #             classification=[], ra=[], dec=[])
    idict = dict(ra=[], dec=[])
    if use_platef:
        for key in ['plate', 'fiber', 'mjd']:
            idict[key] = []
    ml_tbl = Table()
    ml_survey = LLSSurvey()
    systems = []
    in_ml = np.array([False]*len(sdss_survey.sightlines))
    # Loop
    for obj in ml_results:
        # Sightline
        for key in idict.keys():
            idict[key].append(obj[key])
        # DLAs
        #if debug:
        #    if (obj['plate'] == 1366) & (obj['fiber'] == 614):
        #        sv_coord = SkyCoord(ra=obj['ra'], dec=obj['dec'], unit='deg')
        #        print("GOT A MATCH IN RESULTS FILE")
        for idla in obj['dlas']:
            """
            dla = DLASystem((sdss_survey.sightlines['RA'][mt[0]],
                             sdss_survey.sightlines['DEC'][mt[0]]),
                            idla['spectrum']/(1215.6701)-1., None,
                            idla['column_density'])
            """
            if idla['z_dla'] < 1.8:
                continue
            isys = LLSSystem((obj['ra'],obj['dec']),
                    idla['z_dla'], None, NHI=idla['column_density'], zem=obj['z_qso'])
            isys.confidence = idla['dla_confidence']
            if use_platef:
                isys.plate = obj['plate']
                isys.fiber = obj['fiber']
            elif use_id:
                plate, fiber = [int(spl) for spl in obj['id'].split('-')]
                isys.plate = plate
                isys.fiber = fiber
            # Save
            systems.append(isys)
    # Connect to sightlines
    ml_coord = SkyCoord(ra=idict['ra'], dec=idict['dec'], unit='deg')
    s_coord = SkyCoord(ra=sdss_survey.sightlines['RA'], dec=sdss_survey.sightlines['DEC'], unit='deg')
    idx, d2d, d3d = match_coordinates_sky(s_coord, ml_coord, nthneighbor=1)
    used = d2d < 1.*u.arcsec
    for iidx in np.where(~used)[0]:
        print("Sightline RA={:g}, DEC={:g} was not used".format(sdss_survey.sightlines['RA'][iidx],
                                                                sdss_survey.sightlines['DEC'][iidx]))
    # Add plate/fiber to statistical DLAs
    if add_pf:
        dla_coord = sdss_survey.coord
        idx2, d2d, d3d = match_coordinates_sky(dla_coord, s_coord, nthneighbor=1)
        if np.min(d2d.to('arcsec').value) > 1.:
            raise ValueError("Bad match to sightlines")
        for jj,igd in enumerate(np.where(sdss_survey.mask)[0]):
            dla = sdss_survey._abs_sys[igd]
            try:
                dla.plate = sdss_survey.sightlines['PLATE'][idx2[jj]]
            except IndexError:
                pdb.set_trace()
            dla.fiber = sdss_survey.sightlines[fkey][idx2[jj]]
    # Finish
    ml_survey._abs_sys = systems
    if debug:
        ml2_coord = ml_survey.coord
        minsep = np.min(sv_coord.separation(ml2_coord))
        minsep2 = np.min(sv_coord.separation(s_coord))
        tmp = sdss_survey.sightlines[used]
        t_coord = SkyCoord(ra=tmp['RA'], dec=tmp['DEC'], unit='deg')
        minsep3 = np.min(sv_coord.separation(t_coord))
        pdb.set_trace()
    ml_survey.sightlines = sdss_survey.sightlines[used]
    for key in idict.keys():
        ml_tbl[key] = idict[key]
    ml_survey.ml_tbl = ml_tbl
    # Return
    return ml_survey