Beispiel #1
0
    def parse_mag(self, data, **kwargs):
        """Return a `Photometry` object from a `PhotometryMag` marshmallow
        schema.

        Parameters
        ----------
        data : dict
            The instance of the PhotometryMag schema to convert to Photometry.

        Returns
        -------
        Photometry
            The Photometry object generated from the PhotometryMag dict.
        """

        from skyportal.models import Instrument, Obj, PHOT_SYS, PHOT_ZP, Photometry
        from sncosmo.photdata import PhotometricData

        # check that mag and magerr are both null or both not null, not a mix
        ok = any([
            all([op(field, None) for field in [data['mag'], data['magerr']]])
            for op in [operator.is_, operator.is_not]
        ])

        if not ok:
            raise ValidationError(f'Error parsing packet "{data}": mag '
                                  f'and magerr must both be null, or both be '
                                  f'not null.')

        # get the instrument
        instrument = Instrument.query.get(data['instrument_id'])
        if not instrument:
            raise ValidationError(
                f'Invalid instrument ID: {data["instrument_id"]}')

        # get the object
        obj = Obj.query.get(
            data['obj_id'])  # TODO: implement permissions checking
        if not obj:
            raise ValidationError(f'Invalid object ID: {data["obj_id"]}')

        if data["filter"] not in instrument.filters:
            raise ValidationError(
                f"Instrument {instrument.name} has no filter "
                f"{data['filter']}.")

        # determine if this is a limit or a measurement
        hasmag = data['mag'] is not None

        if hasmag:
            flux = 10**(-0.4 * (data['mag'] - PHOT_ZP))
            fluxerr = data['magerr'] / (2.5 / np.log(10)) * flux
        else:
            nsigflux = 10**(-0.4 * (data['limiting_mag'] - PHOT_ZP))
            flux = None
            fluxerr = nsigflux / PHOT_DETECTION_THRESHOLD

        # convert flux to microJanskies.
        table = Table([{
            'flux': flux,
            'fluxerr': fluxerr,
            'magsys': data['magsys'],
            'zp': PHOT_ZP,
            'filter': data['filter'],
            'mjd': data['mjd'],
        }])
        if flux is None:
            # this needs to be non-null for the conversion step
            # will be replaced later with null
            table['flux'] = 0.0

        # conversion happens here
        photdata = PhotometricData(table).normalized(zp=PHOT_ZP,
                                                     zpsys=PHOT_SYS)

        # replace with null if needed
        final_flux = None if flux is None else photdata.flux[0]

        p = Photometry(
            obj_id=data['obj_id'],
            mjd=data['mjd'],
            flux=final_flux,
            fluxerr=photdata.fluxerr[0],
            instrument_id=data['instrument_id'],
            assignment_id=data['assignment_id'],
            filter=data['filter'],
            ra=data['ra'],
            dec=data['dec'],
            ra_unc=data['ra_unc'],
            dec_unc=data['dec_unc'],
        )
        if 'alert_id' in data and data['alert_id'] is not None:
            p.alert_id = data['alert_id']
        return p
Beispiel #2
0
    def parse_flux(self, data, **kwargs):
        """Return a `Photometry` object from a `PhotometryFlux` marshmallow
        schema.

        Parameters
        ----------
        data : dict
            The instance of the PhotometryFlux schema to convert to Photometry.

        Returns
        -------
        Photometry
            The Photometry object generated from the PhotometryFlux object.
        """

        from skyportal.models import Instrument, Obj, PHOT_SYS, PHOT_ZP, Photometry
        from sncosmo.photdata import PhotometricData

        # get the instrument
        instrument = Instrument.query.get(data['instrument_id'])
        if not instrument:
            raise ValidationError(
                f'Invalid instrument ID: {data["instrument_id"]}')

        # get the object
        obj = Obj.query.get(
            data['obj_id'])  # TODO : implement permissions checking
        if not obj:
            raise ValidationError(f'Invalid object ID: {data["obj_id"]}')

        if data["filter"] not in instrument.filters:
            raise ValidationError(
                f"Instrument {instrument.name} has no filter "
                f"{data['filter']}.")

        # convert flux to microJanskies.
        table = Table([data])
        if data['flux'] is None:
            # this needs to be non-null for the conversion step
            # will be replaced later with null
            table['flux'] = 0.0

        # conversion happens here
        photdata = PhotometricData(table).normalized(zp=PHOT_ZP,
                                                     zpsys=PHOT_SYS)

        # replace with null if needed
        final_flux = None if data['flux'] is None else photdata.flux[0]

        p = Photometry(
            obj_id=data['obj_id'],
            mjd=data['mjd'],
            flux=final_flux,
            fluxerr=photdata.fluxerr[0],
            instrument_id=data['instrument_id'],
            assignment_id=data['assignment_id'],
            filter=data['filter'],
            ra=data['ra'],
            dec=data['dec'],
            ra_unc=data['ra_unc'],
            dec_unc=data['dec_unc'],
        )
        if 'alert_id' in data and data['alert_id'] is not None:
            p.alert_id = data['alert_id']
        return p