Instruction:
Can you write a function in Python 3 where it
does a query of the CADC Megacam table.

Response:
def cfht_megacam_tap_query(ra_deg=180.0, dec_deg=0.0, width=1, height=1, date=None):
    """Do a query of the CADC Megacam table.

    Get all observations inside the box (right now it turns width/height into a radius, should not do this).

    @rtype : Table
    @param ra_deg: center of search region, in degrees
    @param dec_deg: center of search region in degrees
    @param width: width of search region in degrees
    @param height: height of search region in degrees
    @param date: ISO format date string.  Query will be +/- 0.5 days from date given.
    """

    radius = min(90, max(width, height) / 2.0)

    query = ("SELECT "
             "COORD1(CENTROID(Plane.position_bounds)) AS RAJ2000,"
             "COORD2(CENTROID(Plane.position_bounds)) AS DEJ2000,"
             "target_name "
             "FROM "
             "caom2.Observation as o "
             "JOIN caom2.Plane as Plane on o.obsID=Plane.obsID "
             "WHERE o.collection = 'CFHT' "
             "AND o.instrument_name = 'MegaPrime' "
             "AND INTERSECTS( CIRCLE('ICRS', %f, %f, %f), Plane.position_bounds ) = 1")

    query = query % (ra_deg, dec_deg, radius)

    if date is not None:
        mjd = Time(date, scale='utc').mjd
        query += " AND Plane.time_bounds_lower <= {} AND {} <= Plane.time_bounds_upper ".format(mjd+0.5, mjd-0.5)

    data = {"QUERY": query,
            "REQUEST": "doQuery",
            "LANG": "ADQL",
            "FORMAT": "votable"}

    url = "http://www.cadc.hia.nrc.gc.ca/tap/sync"

    warnings.simplefilter('ignore')
    ff = StringIO(requests.get(url, params=data).content)
    ff.seek(0)
    table = votable.parse(ff).get_first_table().to_table()
    assert isinstance(table, Table)
    return table


Instruction:
Can you tell what is the following Python 3 function doing
def validate(self, size):
        msg = 'scale and array size must match, ' \
              'but were scale: {self.scale.n_bands},  array size: {size}'

        if size != len(self.scale):
            raise ValueError(msg.format(**locals()))

Response: Raises a ValueError if the size of the dimension doesn t match the number of bands in the array


Instruction:
Create a Python 3 function for
creating a basic processing pipeline that can resample all incoming audio to a normalized sampling rate for downstream processing.

Response:
def resampled(
        chunksize_bytes=DEFAULT_CHUNK_SIZE,
        resample_to=SR44100(),
        store_resampled=False):
    """
    Create a basic processing pipeline that can resample all incoming audio
    to a normalized sampling rate for downstream processing, and store a
    convenient, compressed version for playback

    :param chunksize_bytes: The number of bytes from the raw stream to process
    at once
    :param resample_to: The new, normalized sampling rate
    :return: A simple processing pipeline
    """

    class Resampled(BaseModel):
        meta = JSONFeature(
            MetaData,
            store=True,
            encoder=AudioMetaDataEncoder)

        raw = ByteStreamFeature(
            ByteStream,
            chunksize=chunksize_bytes,
            needs=meta,
            store=False)

        ogg = OggVorbisFeature(
            OggVorbis,
            needs=raw,
            store=True)

        pcm = AudioSamplesFeature(
            AudioStream,
            needs=raw,
            store=False)

        resampled = AudioSamplesFeature(
            Resampler,
            needs=pcm,
            samplerate=resample_to,
            store=store_resampled)

    return Resampled


Instruction:
How would you explain what the following Python 3 function does
def audio_graph(
        chunksize_bytes=DEFAULT_CHUNK_SIZE,
        resample_to=SR44100(),
        store_fft=False):
    """
    Produce a base class suitable as a starting point for many audio processing
    pipelines.  This class resamples all audio to a common sampling rate, and
    produces a bark band spectrogram from overlapping short-time fourier
    transform frames.  It also compresses the audio into ogg vorbis format for
    compact storage.
    """

    band = FrequencyBand(20, resample_to.nyquist)

    class AudioGraph(BaseModel):
        meta = JSONFeature(
            MetaData,
            store=True,
            encoder=AudioMetaDataEncoder)

        raw = ByteStreamFeature(
            ByteStream,
            chunksize=chunksize_bytes,
            needs=meta,
            store=False)

        ogg = OggVorbisFeature(
            OggVorbis,
            needs=raw,
            store=True)

        pcm = AudioSamplesFeature(
            AudioStream,
            needs=raw,
            store=False)

        resampled = AudioSamplesFeature(
            Resampler,
            needs=pcm,
            samplerate=resample_to,
            store=False)

        windowed = ArrayWithUnitsFeature(
            SlidingWindow,
            needs=resampled,
            wscheme=HalfLapped(),
            wfunc=OggVorbisWindowingFunc(),
            store=False)

        dct = ArrayWithUnitsFeature(
            DCT,
            needs=windowed,
            store=True)

        fft = ArrayWithUnitsFeature(
            FFT,
            needs=windowed,
            store=store_fft)

        bark = ArrayWithUnitsFeature(
            BarkBands,
            needs=fft,
            frequency_band=band,
            store=True)

        centroid = ArrayWithUnitsFeature(
            SpectralCentroid,
            needs=bark,
            store=True)

        chroma = ArrayWithUnitsFeature(
            Chroma,
            needs=fft,
            frequency_band=band,
            store=True)

        bfcc = ArrayWithUnitsFeature(
            BFCC,
            needs=fft,
            store=True)

    return AudioGraph

Response: Generates a base class suitable as a starting point for many audio processing of a single object.


Instruction:
How would you implement a function in Python 3 that
produces a mixin class that extracts onsets from a short - time fourier transform feature

Response:
def with_onsets(fft_feature):
    """
    Produce a mixin class that extracts onsets
    :param fft_feature: The short-time fourier transform feature
    :return: A mixin class that extracts onsets
    """

    class Onsets(BaseModel):
        onset_prep = ArrayWithUnitsFeature(
            SlidingWindow,
            needs=fft_feature,
            wscheme=HalfLapped() * Stride(frequency=1, duration=3),
            store=False)

        complex_domain = ArrayWithUnitsFeature(
            ComplexDomain,
            needs=onset_prep,
            store=False)

        sliding_detection = ArrayWithUnitsFeature(
            SlidingWindow,
            needs=complex_domain,
            wscheme=HalfLapped() * Stride(frequency=1, duration=11),
            padwith=5,
            store=False)

        slices = TimeSliceFeature(
            MovingAveragePeakPicker,
            needs=sliding_detection,
            aggregate=np.median,
            store=True)

    return Onsets


Instruction:
Can you tell what is the following Python 3 function doing
def keplerian_sheared_field_locations(ax, kbos, date, ras, decs, names, elongation=False, plot=False):
    seps = {'dra': 0., 'ddec': 0.}
    for kbo in kbos:
        ra = kbo.ra
        dec = kbo.dec
        kbo.compute(date)
        seps['dra'] += kbo.ra - ra
        seps['ddec'] += kbo.dec - dec

    seps['dra'] /= float(len(kbos))
    seps['ddec'] /= float(len(kbos))

    print date, seps, len(kbos)

    for idx in range(len(ras)):
        name = names[idx]
        ra = ras[idx] + seps['dra']
        dec = decs[idx] + seps['ddec']
        if plot:
            ax.add_artist(Rectangle(xy=(math.degrees(ra) - camera_dimen / 2.0, math.degrees(dec) - camera_dimen / 2.0),
                                    height=camera_dimen,
                                    width=camera_dimen,
                                    edgecolor='b',
                                    facecolor='b',
                                    lw=0.5, fill=True, alpha=0.2))
        if elongation:
            # For each field centre, plot the elongation onto the field at that date.
            elong = field_elongation(ephem.degrees(ra), ephem.degrees(dec), date)
            ax.annotate(name, (math.degrees(ra) + camera_dimen / 2., math.degrees(dec)), size=3)
            ax.annotate("%0.1f" % elong, (math.degrees(ra) + camera_dimen / 4., math.degrees(dec) - camera_dimen / 4.),
                        size=5)

    return ax

Response: Plot the Keplerian sheared field locations.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def field_elongation(ra, dec, date):
    sun = ephem.Sun()
    sun.compute(date)
    sep = ephem.separation((ra, dec), sun)
    retval = 180. - math.degrees(sep)

    return retval

Response: Calculates the solar elongation at the given date.


Instruction:
Implement a Python 3 function for
converting string value to timedelta instance according to given format.

Response:
def str_to_timedelta(value: str,
                     fmt: str = None) -> Optional[datetime.timedelta]:
    """
    Convert string value to timedelta instance according to the given format.

    If format not set function tries to load timedelta using default
    ``TIMEDELTA_FORMAT`` and then both of magic "full" formats.

    You should also specify list of formats and function tries to convert
    to timedelta using each of formats in list. First matched format would
    return the converted timedelta instance.

    If user specified format, but function cannot convert string to
    new timedelta instance - ``ValueError`` would be raised. But if user did
    not specify the format, function would be fail silently and return ``None``
    as result.

    :param value: String representation of timedelta.
    :param fmt: Format to use for conversion.
    """
    def timedelta_kwargs(data: DictStrInt) -> DictStrInt:
        """
        Convert day_hours, hour_minutes, minute_seconds, week_days and weeks to
        timedelta seconds.
        """
        seconds = data.get('seconds', 0)
        seconds += data.get('day_hours', 0) * 3600
        seconds += data.pop('hour_minutes', 0) * 60
        seconds += data.pop('minute_seconds', 0)
        seconds += data.pop('week_days', 0) * SECONDS_PER_DAY
        seconds += data.pop('weeks', 0) * SECONDS_PER_WEEK
        data.update({'seconds': seconds})
        return data

    if not isinstance(value, str):
        raise ValueError(
            'Value should be a "str" instance. You use {0}.'
            .format(type(value)))

    user_fmt = fmt

    if isinstance(fmt, (list, tuple)):
        formats = list(fmt)
    elif fmt is None:
        formats = [TIMEDELTA_FORMAT, 'F', 'f']
    else:
        formats = [fmt]

    locale_data = {
        'days_label': '({0}|{1})'.format('day', 'days'),
        'short_days_label': 'd',
        'short_week_days_label': 'd',
        'short_weeks_label': 'w',
        'week_days_label': '({0}|{1})'.format('day', 'days'),
        'weeks_label': '({0}|{1})'.format('week', 'weeks'),
    }
    regexps = []

    for item in formats:
        processed = r'^'

        for part in item:
            if part in TIMEDELTA_FORMATS:
                part = TIMEDELTA_FORMATS[part][1] % locale_data
            else:
                part = re.escape(part)
            processed += part

        processed += r'$'
        regexps.append(processed)

    for regexp in regexps:
        timedelta_re = re.compile(regexp)
        matched = timedelta_re.match(value)

        if matched:
            data = {
                key: to_int(value) or 0
                for key, value in matched.groupdict().items()}
            return datetime.timedelta(**timedelta_kwargs(data))

    if user_fmt:
        raise ValueError(
            'Cannot convert {0!r} to timedelta instance, using {1!r} format.'
            .format(value, user_fmt))

    return None


Instruction:
Explain what the following Python 3 code does
def timedelta_div(first: datetime.timedelta,
                  second: datetime.timedelta) -> Optional[float]:
    """Implement divison for timedelta instances.

    :param first: First timedelta instance.
    :param second: Second timedelta instance.
    """
    first_seconds = timedelta_seconds(first)
    second_seconds = timedelta_seconds(second)

    if not second_seconds:
        return None

    return first_seconds / second_seconds

Response: Implement divison for timedelta instances.


Instruction:
Implement a function in Python 3 to
return the number of seconds from a timedelta instance.

Response:
def timedelta_seconds(value: datetime.timedelta) -> int:
    """Return full number of seconds from timedelta.

    By default, Python returns only one day seconds, not all timedelta seconds.

    :param value: Timedelta instance.
    """
    return SECONDS_PER_DAY * value.days + value.seconds


Instruction:
Given the following Python 3 function, write the documentation
def timedelta_to_str(value: datetime.timedelta, fmt: str = None) -> str:
    # Only ``datetime.timedelta`` instances allowed for this function
    if not isinstance(value, datetime.timedelta):
        raise ValueError(
            'Value should be a "datetime.timedelta" instance. You use {0}.'
            .format(type(value)))

    # Generate total data
    days = value.days
    microseconds = value.microseconds
    seconds = timedelta_seconds(value)

    hours = seconds // 3600
    minutes = seconds // 60
    weeks = days // 7

    # Generate collapsed data
    day_hours = hours - days * 24
    hour_minutes = minutes - hours * 60
    minute_seconds = seconds - minutes * 60
    week_days = days - weeks * 7

    days_label = 'day' if days % 10 == 1 else 'days'
    short_days_label = 'd'
    short_week_days_label = 'd'
    short_weeks_label = 'w'
    week_days_label = 'day' if week_days % 10 == 1 else 'days'
    weeks_label = 'week' if weeks % 10 == 1 else 'weeks'

    # Collect data
    data = locals()

    fmt = fmt or TIMEDELTA_FORMAT
    processed = ''

    for part in fmt:
        if part in TIMEDELTA_FORMATS:
            is_full_part = part in ('f', 'F')
            is_repr_part = part in ('r', 'R')

            part = TIMEDELTA_FORMATS[part][0]

            if is_full_part or is_repr_part:
                if is_repr_part and not days:
                    part = part.replace('%(days)d', '')
                    part = part.replace('%(days_label)s,', '')
                    part = part.replace('%(short_days_label)s', '')

                if is_full_part and not minute_seconds:
                    part = part.replace(':%(minute_seconds)02d', '')

                if is_full_part and not weeks:
                    part = part.replace('%(weeks)d', '')
                    part = part.replace('%(short_weeks_label)s', '')
                    part = part.replace('%(weeks_label)s,', '')

                if is_full_part and not week_days:
                    part = part.replace('%(week_days)d', '')
                    part = part.replace('%(short_week_days_label)s', '')
                    part = part.replace('%(week_days_label)s,', '')

                part = part.strip()
                part = ' '.join(part.split())

        processed += part

    return processed % data

Response: Display the timedelta formatted according to the given string.


Instruction:
Can you create a Python 3 function that
creates a shifts file that transforms the space and flux time scale of all images to the first image.

Response:
def align(expnums, ccd, version='s', dry_run=False):
    """Create a 'shifts' file that transforms the space/flux/time scale of all images to the first image.

    This function relies on the .fwhm, .trans.jmp, .phot and .zeropoint.used files for inputs.
    The scaling we are computing here is for use in planting sources into the image at the same sky/flux locations
    while accounting for motions of sources with time.

    :param expnums: list of MegaPrime exposure numbers to add artificial KBOs to,
                    the first frame in the list is the reference.
    :param ccd: which ccd to work on.
    :param version: Add sources to the 'o', 'p' or 's' images
    :param dry_run: don't push results to VOSpace.
    """

    # Get the images and supporting files that we need from the VOSpace area
    # get_image and get_file check if the image/file is already on disk.
    # re-computed fluxes from the PSF stars and then recompute x/y/flux scaling.

    # some dictionaries to hold the various scale
    pos = {}
    apcor = {}
    mags = {}
    zmag = {}
    mjdates = {}

    for expnum in expnums:
        filename = storage.get_image(expnum, ccd=ccd, version=version)
        zmag[expnum] = storage.get_zeropoint(expnum, ccd, prefix=None, version=version)
        mjdates[expnum] = float(fits.open(filename)[0].header.get('MJD-OBS'))
        apcor[expnum] = [float(x) for x in open(storage.get_file(expnum,
                                                                 ccd=ccd,
                                                                 version=version,
                                                                 ext=storage.APCOR_EXT)).read().split()]
        keys = ['crval1', 'cd1_1', 'cd1_2', 'crval2', 'cd2_1', 'cd2_2']
        # load the .trans.jmp values into a 'wcs' like dictionary.
        # .trans.jmp maps current frame to reference frame in pixel coordinates.
        # the reference frame of all the frames supplied must be the same.
        shifts = dict(zip(keys, [float(x) for x in open(storage.get_file(expnum,
                                                                         ccd=ccd,
                                                                         version=version,
                                                                         ext='trans.jmp')).read().split()]))
        shifts['crpix1'] = 0.0
        shifts['crpix2'] = 0.0
        # now create a wcs object based on those transforms, this wcs links the current frame's
        # pixel coordinates to the reference frame's pixel coordinates.
        w = get_wcs(shifts)

        # get the PHOT file that was produced by the mkpsf routine
        logging.debug("Reading .phot file {}".format(expnum))
        phot = ascii.read(storage.get_file(expnum, ccd=ccd, version=version, ext='phot'), format='daophot')

        # compute the small-aperture magnitudes of the stars used in the PSF
        import daophot
        logging.debug("Running phot on {}".format(filename))
        mags[expnum] = daophot.phot(filename,
                                    phot['XCENTER'],
                                    phot['YCENTER'],
                                    aperture=apcor[expnum][0],
                                    sky=apcor[expnum][1] + 1,
                                    swidth=apcor[expnum][0],
                                    zmag=zmag[expnum])

        # covert the x/y positions to positions in Frame 1 based on the trans.jmp values.
        logging.debug("Doing the XY translation to refrence frame: {}".format(w))
        (x, y) = w.wcs_pix2world(mags[expnum]["XCENTER"], mags[expnum]["YCENTER"], 1)
        pos[expnum] = numpy.transpose([x, y])
        # match this exposures PSF stars position against those in the first image of the set.
        logging.debug("Matching lists")
        idx1, idx2 = util.match_lists(pos[expnums[0]], pos[expnum])

        # compute the magnitdue offset between the current frame and the reference.
        dmags = numpy.ma.array(mags[expnums[0]]["MAG"] - apcor[expnums[0]][2] -
                               (mags[expnum]["MAG"][idx1] - apcor[expnum][2]),
                               mask=idx1.mask)
        dmags.sort()
        logging.debug("Computed dmags between input and reference: {}".format(dmags))
        error_count = 0

        error_count += 1
        logging.debug("{}".format(error_count))

        # compute the median and determine if that shift is small compared to the scatter.
        try:
            midx = int(numpy.sum(numpy.any([~dmags.mask], axis=0)) / 2.0)
            dmag = float(dmags[midx])
            logging.debug("Computed a mag delta of: {}".format(dmag))
        except Exception as e:
            logging.error(str(e))
            logging.error("Failed to compute mag offset between plant and found using: {}".format(dmags))
            dmag = 99.99

        error_count += 1
        logging.debug("{}".format(error_count))

        try:
            if math.fabs(dmag) > 3 * (dmags.std() + 0.01):
                logging.warning("Magnitude shift {} between {} and {} is large: {}".format(dmag,
                                                                                expnums[0],
                                                                                expnum,
                                                                                shifts))
        except Exception as e:
            logging.error(str(e))

        error_count += 1
        logging.debug("{}".format(error_count))

        shifts['dmag'] = dmag
        shifts['emag'] = dmags.std()
        shifts['nmag'] = len(dmags.mask) - dmags.mask.sum()
        shifts['dmjd'] = mjdates[expnums[0]] - mjdates[expnum]
        shift_file = os.path.basename(storage.get_uri(expnum, ccd, version, '.shifts'))

        error_count += 1
        logging.debug("{}".format(error_count))

        try:
            fh = open(shift_file, 'w')
            fh.write(json.dumps(shifts, sort_keys=True, indent=4, separators=(',', ': ')))
            fh.write('\n')
            fh.close()
        except Exception as e:
            logging.error("Creation of SHIFTS file failed while trying to write: {}".format(shifts))
            raise e

        error_count += 1
        logging.debug("{}".format(error_count))

        if not dry_run:
            storage.copy(shift_file, storage.get_uri(expnum, ccd, version, '.shifts'))


Instruction:
Create a Python 3 function to
return the distance between the values in range uniformly or via some function.

Response:
def dist(self):
        """
        How should the values in range be sampled, uniformly or via some function.
        :return:
        """
        if self.func is None:
            return random.uniform(self.min, self.max)
        if self._dist is None:
            x = numpy.arange(self.min, self.max, (self.max-self.min)/1000.0)
            p = self.func(x).cumsum()
            p -= p.min()
            p /= p.max()
            self._dist = interpolate.interp1d(p, x)
        return self._dist(random.random())


Instruction:
Can you implement a function in Python 3 that
returns the next set of values that can be used for an planted object builder.

Response:
def next(self):
        """
        :return: a set of values that can be used for an planted object builder.
        """
        #        x          y        mag   pix rate      angle  ''/h rate     id
        # 912.48     991.06      22.01      57.32     -45.23      10.60      0
        self._n -= 1
        if self._n < 0:
            raise StopIteration()
        return {'x': self.x(), 'y': self.y(), 'mag': self.mag(), 'sky_rate': self.rate(), 'angle': self.angle(),
                'id': self.id}


Instruction:
How would you explain what the following Python 3 function does
def send_command(self, cmd, params=None, raw=False):
        '''
        Send command to foscam.
        '''
        paramstr = ''
        if params:
            paramstr = urlencode(params)
            paramstr = '&' + paramstr if paramstr else ''
        cmdurl = 'http://%s/cgi-bin/CGIProxy.fcgi?usr=%s&pwd=%s&cmd=%s%s' % (
                                                                  self.url,
                                                                  self.usr,
                                                                  self.pwd,
                                                                  cmd,
                                                                  paramstr,
                                                                  )
        if self.ssl and ssl_enabled:
            cmdurl = cmdurl.replace('http:','https:')

        # Parse parameters from response string.
        if self.verbose:
            print ('Send Foscam command: %s' % cmdurl)
        try:
            raw_string = ''
            if self.ssl and ssl_enabled:
                gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)  # disable cert
                raw_string = urlopen(cmdurl,context=gcontext, timeout=5).read()
            else:
                raw_string = urlopen(cmdurl,timeout=5).read()
            if raw:
                if self.verbose:
                    print ('Returning raw Foscam response: len=%d' % len(raw_string))
                return FOSCAM_SUCCESS, raw_string
            root = ET.fromstring(raw_string)
        except:
            if self.verbose:
                print ('Foscam exception: ' + raw_string)
            return ERROR_FOSCAM_UNAVAILABLE, None
        code = ERROR_FOSCAM_UNKNOWN
        params = OrderedDict()
        for child in root.iter():
            if child.tag == 'result':
                code = int(child.text)

            elif child.tag != 'CGI_Result':
                params[child.tag] = unquote(child.text)

        if self.verbose:
            print ('Received Foscam response: %s, %s' % (code, params))
        return code, params

Response: Send a command to foscam.


Instruction:
Can you implement a function in Python 3 that
executes a command and return a parsed response.

Response:
def execute_command(self, cmd, params=None, callback=None, raw=False):
        '''
        Execute a command and return a parsed response.
        '''
        def execute_with_callbacks(cmd, params=None, callback=None, raw=False):
            code, params = self.send_command(cmd, params, raw)
            if callback:
                callback(code, params)
            return code, params

        if self.daemon:
            t = Thread(target=execute_with_callbacks,
                    args=(cmd, ), kwargs={'params':params, 'callback':callback, 'raw':raw})
            t.daemon = True
            t.start()
        else:
            return execute_with_callbacks(cmd, params, callback, raw)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_ip_info(self, is_dhcp, ip='', gate='', mask='',
                                dns1='', dns2='', callback=None):
        '''
        isDHCP: 0(False), 1(True)
        System will reboot automatically to take effect after call this CGI command.
        '''
        params = {'isDHCP': is_dhcp,
                  'ip': ip,
                  'gate': gate,
                  'mask': mask,
                  'dns1': dns1,
                  'dns2': dns2,
                 }

        return self.execute_command('setIpInfo', params, callback=callback)

Response: Set the IP information of the current system.


Instruction:
Can you generate the documentation for the following Python 3 function
def set_port_info(self, webport, mediaport, httpsport,
                                      onvifport, callback=None):
        '''
        Set http port and media port of camera.
        '''
        params = {'webPort'   : webport,
                  'mediaPort' : mediaport,
                  'httpsPort' : httpsport,
                  'onvifPort' : onvifport,
                 }
        return self.execute_command('setPortInfo', params, callback=callback)

Response: Set http port and media port of camera.


Instruction:
How would you code a function in Python 3 to
get the wifi list around after refreshWifiList.

Response:
def get_wifi_list(self, startno, callback=None):
        '''
        Get the aps around after refreshWifiList.
        Note: Only 10 aps will be returned one time.
        '''
        params = {'startNo': startno}
        return self.execute_command('getWifiList', params, callback=callback)


Instruction:
Can you create a Python 3 function that
sets wifi config. Camera will not connect to AP unless you enject your cable.

Response:
def set_wifi_setting(self, ssid, psk, isenable, isusewifi, nettype,
                            encryptype, authmode, keyformat, defaultkey,
                            key1='', key2='', key3='', key4='',
                            key1len=64, key2len=64, key3len=64, key4len=64,
                            callback=None):
        '''
        Set wifi config.
        Camera will not connect to AP unless you enject your cable.
        '''
        params = {'isEnable'   : isenable,
                  'isUseWifi'  : isusewifi,
                  'ssid'       : ssid,
                  'netType'    : nettype,
                  'encryptType': encryptype,
                  'psk'        : psk,
                  'authMode'   : authmode,
                  'keyFormat'  : keyformat,
                  'defaultKey' : defaultkey,
                  'key1'       : key1,
                  'key2'       : key2,
                  'key3'       : key3,
                  'key4'       : key4,
                  'key1Len'    : key1len,
                  'key2Len'    : key2len,
                  'key3Len'    : key3len,
                  'key4Len'    : key4len,
                  }
        return self.execute_command('setWifiSetting', params, callback=callback)


Instruction:
Explain what the following Python 3 code does
def set_upnp_config(self, isenable, callback=None):
        '''
        Set UPnP config
        '''
        params = {'isEnable': isenable}
        return self.execute_command('setUPnPConfig', params, callback=callback)

Response: Set UPnP config for the current set of UPnP entries.


Instruction:
How would you explain what the following Python 3 function does
def set_sub_video_stream_type(self, format, callback=None):
        '''
        Set the stream fromat of sub stream.
        Supported format: (1) H264 : 0
                          (2) MotionJpeg 1
        '''
        params = {'format': format}
        return self.execute_command('setSubVideoStreamType',
                                        params, callback=callback)

Response: Set the stream fromat of sub stream.


Instruction:
How would you implement a function in Python 3 that
sets the format of the sub stream

Response:
def set_sub_stream_format(self, format, callback=None):
        '''
        Set the stream fromat of sub stream????
        '''
        params = {'format': format}
        return self.execute_command('setSubStreamFormat',
                                        params, callback=callback)


Instruction:
Can you write a function in Python 3 where it
sets the stream type of main stream

Response:
def set_main_video_stream_type(self, streamtype, callback=None):
        '''
        Set the stream type of main stream
        '''
        params = {'streamType': streamtype}
        return self.execute_command('setMainVideoStreamType',
                                        params, callback=callback)


Instruction:
Make a summary of the following Python 3 code
def set_video_stream_param(self, streamtype, resolution, bitrate,
            framerate, gop, isvbr, callback=None):
        '''
        Set the video stream param of stream N
        streamtype(0~3): Stream N.
        resolution(0~4): 0 720P,
                         1 VGA(640*480),
                         2 VGA(640*360),
                         3 QVGA(320*240),
                         4 QVGA(320*180).
        bitrate: Bit rate of stream type N(20480~2097152).
        framerate: Frame rate of stream type N.
        GOP: P frames between 1 frame of stream type N.
             The suggest value is: X * framerate.
        isvbr: 0(Not in use currently), 1(In use).
        '''
        params = {'streamType': streamtype,
                  'resolution': resolution,
                  'bitRate'   : bitrate,
                  'frameRate' : framerate,
                  'GOP'       : gop,
                  'isVBR'     : isvbr
                 }
        return self.execute_command('setVideoStreamParam',
                                    params, callback=callback)

Response: Set the video stream param of the current session.


Instruction:
Make a summary of the following Python 3 code
def mirror_video(self, is_mirror, callback=None):
        '''
        Mirror video
       ``is_mirror``: 0 not mirror, 1 mirror
        '''
        params = {'isMirror': is_mirror}
        return self.execute_command('mirrorVideo', params, callback=callback)

Response: Mirror the video in the current node.


Instruction:
Create a Python 3 function to
flip video ``is_flip``: 0 Not flip, 1 Flip

Response:
def flip_video(self, is_flip, callback=None):
        '''
        Flip video
        ``is_flip``: 0 Not flip, 1 Flip
        '''
        params = {'isFlip': is_flip }
        return self.execute_command('flipVideo', params, callback=callback)


Instruction:
How would you explain what the following Python 3 function does
def change_password(self, usrname, oldpwd, newpwd, callback=None):
        '''
        Change password.
        '''
        params = {'usrName': usrname,
                  'oldPwd' : oldpwd,
                  'newPwd' : newpwd,
                 }
        return self.execute_command('changePassword',
                                    params, callback=callback)

Response: Change password of a resource.


Instruction:
Write a Python 3 script to
set the state of the Infrared LED to the specified mode.

Response:
def set_infra_led_config(self, mode, callback=None):
        '''
        Set Infrared LED configuration
        cmd: setInfraLedConfig
        mode(0,1): 0=Auto mode, 1=Manual mode
        '''
        params = {'mode': mode}
        return self.execute_command('setInfraLedConfig', params, callback=callback)


Instruction:
Implement a function in Python 3 to
set the motion detection on or off of the current config.

Response:
def set_motion_detection(self, enabled=1):
        '''
        Get the current config and set the motion detection on or off
        '''
        result, current_config = self.get_motion_detect_config()
        if result != FOSCAM_SUCCESS:
            return result
        current_config['isEnable'] = enabled
        self.set_motion_detect_config(current_config)
        return FOSCAM_SUCCESS


Instruction:
How would you code a function in Python 3 to
set the motion detection on or off for one of the available resources.

Response:
def set_motion_detection1(self, enabled=1):
        '''
        Get the current config and set the motion detection on or off
        '''
        result, current_config = self.get_motion_detect_config1()
        if result != FOSCAM_SUCCESS:
            return result
        current_config['isEnable'] = enabled
        self.set_motion_detect_config1(current_config)


Instruction:
How would you code a function in Python 3 to
set alarm record config

Response:
def set_alarm_record_config(self, is_enable_prerecord=1,
                                prerecord_secs=5, alarm_record_secs=300, callback=None):
        '''
        Set alarm record config
        Return: set result(0-success, -1-error)
        '''
        params = {'isEnablePreRecord': is_enable_prerecord,
                  'preRecordSecs'    : prerecord_secs,
                  'alarmRecordSecs'  : alarm_record_secs
                 }
        return self.execute_command('setAlarmRecordConfig', params, callback=callback)


Instruction:
Can you tell what is the following Python 3 function doing
def set_local_alarm_record_config(self, is_enable_local_alarm_record = 1,
                                      local_alarm_record_secs = 30, callback=None):
        '''
        Set local alarm-record config
        `is_enable_local_alarm_record`: 0 disable, 1 enable
        '''
        params = {'isEnableLocalAlarmRecord': is_enable_local_alarm_record,
                  'localAlarmRecordSecs'    : local_alarm_record_secs}
        return self.execute_command('setLocalAlarmRecordConfig', params, callback=callback)

Response: Set local alarm - record configuration


Instruction:
Can you write a function in Python 3 where it
sets the frame shipping reference mode of H264 encode stream.

Response:
def set_h264_frm_ref_mode(self, mode=1, callback=None):
        '''
        Set frame shipping reference mode of H264 encode stream.
        params:
            `mode`: see docstr of meth::get_h264_frm_ref_mode
        '''
        params = {'mode': mode}
        return self.execute_command('setH264FrmRefMode', params, callback)


Instruction:
Implement a Python 3 function for
setting schedule record config. cmd: setScheduleRecordConfig args: See docstring of meth::get_schedule_record_config

Response:
def set_schedule_record_config(self, is_enable, record_level,
                                   space_full_mode, is_enable_audio,
                                   schedule0 = 0, schedule1 = 0, schedule2 = 0,
                                   schedule3 = 0, schedule4 = 0, schedule5 = 0,
                                   schedule6 = 0, callback=None):
        '''
        Set schedule record config.
        cmd: setScheduleRecordConfig
        args: See docstring of meth::get_schedule_record_config
        '''

        params = {'isEnable'     : is_enable,
                  'isEnableAudio': is_enable_audio,
                  'recordLevel'  : record_level,
                  'spaceFullMode': space_full_mode,
                  'schedule0'    : schedule0,
                  'schedule1'    : schedule1,
                  'schedule2'    : schedule2,
                  'schedule3'    : schedule3,
                  'schedule4'    : schedule4,
                  'schedule5'    : schedule5,
                  'schedule6'    : schedule6,
                  }
        return self.execute_command('setScheduleRecordConfig', params, callback=callback)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_record_path(self, path, callback=None):
        '''
        Set Record path: sd/ftp
        cmd: setRecordPath
        param:
             path: (0,SD), (2, FTP)
        '''
        params = {'Path': path}
        return self.execute_command('setRecordPath', params, callback=callback)

Response: Set the path of the record


Instruction:
Write a Python 3 script to
retrieve log records from camera.

Response:
def get_log(self, offset, count=10, callback=None):
        '''
        Retrieve log records from camera.
        cmd: getLog
        param:
           offset: log offset for first record
           count: number of records to return
        '''
        params = {'offset': offset, 'count': count}
        return self.execute_command('getLog', params, callback=callback)


Instruction:
Can you tell what is the following Python 3 function doing
def get_apcor(expnum, ccd, version='p', prefix=None):
    uri = get_uri(expnum, ccd, ext=APCOR_EXT, version=version, prefix=prefix)
    apcor_file_name = tempfile.NamedTemporaryFile()
    client.copy(uri, apcor_file_name.name)
    apcor_file_name.seek(0)
    return [float(x) for x in apcor_file_name.readline().split()]

Response: retrieve the aperture correction for this exposure


Instruction:
Write a Python 3 script to
do a QUERY on the TAP service for all observations that are part of OSSOS (*P05/*P016) where taken after mjd and have calibration 'observable'. @param ra: RA center of search cont @type ra: Quantity @param dec: float degrees @type dec: Quantity @param dra: float degrees @type dra: Quantity @param ddec: float degrees @type ddec: Quantity @param calibration_level: What calibration level must the found image have, @param mjdate: what data must the observation be to @param collection: name of the data collection to be searched. @param use_ssos: USE the SSOIS server to find comparison? False -> Use CAOM2 TAP query. ke on. this is a CAOM2 parameter of CADC, 2 means calibrated data.

Response:
def cone_search(ra, dec, dra=0.01, ddec=0.01, mjdate=None, calibration_level=2, use_ssos=True,
                collection='CFHTMEGAPIPE'):
    """Do a QUERY on the TAP service for all observations that are part of OSSOS (*P05/*P016)
    where taken after mjd and have calibration 'observable'.

    @param ra: RA center of search cont
    @type ra: Quantity
    @param dec: float degrees
    @type dec: Quantity
    @param dra: float degrees
    @type dra: Quantity
    @param ddec: float degrees
    @type ddec: Quantity
    @param calibration_level: What calibration level must the found image have,
    @param mjdate: what data must the observation be to
    @param collection: name of the data collection to be searched.
    @param use_ssos: USE the SSOIS server to find comparison?  False -> Use CAOM2 TAP query.
    ke on.
    this is a CAOM2 parameter of CADC, 2 means calibrated data.
    """

    if use_ssos:
        ssois_server = "http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/cadcbin/ssos/fixedssos.pl"
        params = dict(pos="{0:f},{1:f}".format(ra.to(units.degree).value, dec.to(units.degree).value))
        result = requests.get(ssois_server, params=params)
        table = ascii.read(result.text, format='tab')
        table = table[table['Telescope/Instrument'] == 'CFHT/MegaCam']
        column_name_mapping = {'Image': 'collectionID',
                               'Filter': 'filter',
                               'Exptime': 'exptime'}
        # rename the columns
        for key in column_name_mapping:
            table[key].name = column_name_mapping[key]
        table['collectionID'] = [x[:-1] for x in table['collectionID']]
        # compute the mjdate from the time string.
        table['mjdate'] = Time(table['Date/Time']).mjd
        return table

    data = dict(QUERY=(" SELECT Observation.observationID as collectionID, "
                       " Plane.time_bounds_lower AS mjdate, "
                       " Plane.time_exposure AS exptime, "
                       " Plane.energy_bandpassName as filter"
                       " FROM caom2.Observation AS Observation "
                       " JOIN caom2.Plane AS Plane "
                       " ON Observation.obsID = Plane.obsID "
                       " WHERE  ( Observation.collection = '{}' ) "
                       " AND Plane.calibrationLevel > {} "
                       " AND ( Plane.energy_bandpassName LIKE 'r.%' OR Plane.energy_bandpassName LIKE 'gri.%' ) "
                       " AND ( Observation.proposal_id LIKE '%P05' or Observation.proposal_id LIKE '%P06' )"
                       " AND Observation.target_name NOT LIKE 'WP%'"),
                REQUEST="doQuery",
                LANG="ADQL",
                FORMAT="tsv")

    data["QUERY"] = data["QUERY"].format(calibration_level, collection)
    data["QUERY"] += (" AND  "
                      " CONTAINS( BOX('ICRS', {}, {}, {}, {}), "
                      " Plane.position_bounds ) = 1 ").format(ra.to(units.degree).value, dec.to(units.degree).value,
                                                              dra.to(units.degree).value, ddec.to(units.degree).value)
    if mjdate is not None:
        data["QUERY"] += " AND Plane.time_bounds_lower < {} AND Plane.time_bounds_cval2 > {} ".format(
            mjdate + 1.0 / 24.0,
            mjdate - 1 / 24.0)

    result = requests.get(TAP_WEB_SERVICE, params=data, verify=False)

    logger.debug("Doing TAP Query using url: %s" % (str(result.url)))

    table_reader = ascii.get_reader(Reader=ascii.Basic)
    table_reader.header.splitter.delimiter = '\t'
    table_reader.data.splitter.delimiter = '\t'
    table = table_reader.read(result.text)

    logger.debug(str(table))
    return table


Instruction:
Given the following Python 3 function, write the documentation
def populate(dataset_name, data_web_service_url=DATA_WEB_SERVICE + "CFHT"):

    data_dest = get_uri(dataset_name, version='o', ext=FITS_EXT)
    data_source = "%s/%so.{}" % (data_web_service_url, dataset_name, FITS_EXT)

    mkdir(os.path.dirname(data_dest))

    try:
        client.link(data_source, data_dest)
    except IOError as e:
        if e.errno == errno.EEXIST:
            pass
        else:
            raise e

    header_dest = get_uri(dataset_name, version='o', ext='head')
    header_source = "%s/%so.fits.fz?cutout=[0]" % (
        data_web_service_url, dataset_name)
    try:
        client.link(header_source, header_dest)
    except IOError as e:
        if e.errno == errno.EEXIST:
            pass
        else:
            raise e

    header_dest = get_uri(dataset_name, version='p', ext='head')
    header_source = "%s/%s/%sp.head" % (
        'http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/data/pub', 'CFHTSG', dataset_name)
    try:
        client.link(header_source, header_dest)
    except IOError as e:
        if e.errno == errno.EEXIST:
            pass
        else:
            raise e

    return True

Response: Populate the DBIMAGES and HEADERS of a CFHT dataset.


Instruction:
Given the following Python 3 function, write the documentation
def get_cands_uri(field, ccd, version='p', ext='measure3.cands.astrom', prefix=None,
                  block=None):
    """
        return the nominal URI for a candidate file.

    @param field: the OSSOS field name
    @param ccd: which CCD are the candidates on
    @param version: either the 'p', or 's' (scrambled) candidates.
    @param ext: Perhaps we'll change this one day.
    @param prefix: if this is a 'fake' dataset then add 'fk'
    @param block: Which BLOCK of the field are we looking at? eg. 15BS+1+1
    @return:
    """

    if prefix is None:
        prefix = ""
    if len(prefix) > 0:
        prefix += "_"
    if len(field) > 0:
        field += "_"

    if ext is None:
        ext = ""
    if len(ext) > 0 and ext[0] != ".":
        ext = ".{}".format(ext)

    measure3_dir = MEASURE3
    if block is not None:
        measure3_dir + "/{}".format(block)
    return "{}/{}{}{}{}{}".format(measure3_dir, prefix, field, version, ccd, ext)

Response: Returns the nominal URI for a candidate file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_uri(expnum, ccd=None,
            version='p', ext=FITS_EXT,
            subdir=None, prefix=None):
    """
    Build the uri for an OSSOS image stored in the dbimages
    containerNode.

    :rtype : basestring
    expnum: CFHT exposure number
    ccd: CCD in the mosaic [0-35]
    version: one of p,s,o etc.
    dbimages: dbimages containerNode.
    @type subdir: str
    @param expnum: int
    @param ccd:
    @param version:
    @param ext:
    @param subdir:
    @param prefix:
    """
    if subdir is None:
        subdir = str(expnum)
    if prefix is None:
        prefix = ''
    uri = os.path.join(DBIMAGES, subdir)

    if ext is None:
        ext = ''
    elif len(ext) > 0 and ext[0] != '.':
        ext = '.' + ext

    if version is None:
        version = ''

    if ccd is None:
        uri = os.path.join(uri,
                           '%s%s%s%s' % (prefix, str(expnum),
                                         version,
                                         ext))
    else:
        ccd = str(ccd).zfill(2)
        uri = os.path.join(uri,
                           'ccd{}'.format(ccd),
                           '%s%s%s%s%s' % (prefix, str(expnum),
                                           version,
                                           ccd,
                                           ext))

    return uri

Response: Build the URI for an OSSOS image stored in the dbimages file.


Instruction:
How would you explain what the following Python 3 function does
def get_tag(expnum, key):

    uri = tag_uri(key)
    force = uri not in get_tags(expnum)
    value = get_tags(expnum, force=force).get(uri, None)
    return value

Response: given a key return the vospace tag value.


Instruction:
How would you code a function in Python 3 to
return a string that represents the process tag for a given CCD number and version.

Response:
def get_process_tag(program, ccd, version='p'):
    """
    make a process tag have a suffix indicating which ccd its for.
    @param program: Name of the process that a tag is built for.
    @param ccd: the CCD number that this process ran on.
    @param version: The version of the exposure (s, p, o) that the process ran on.
    @return: The string that represents the processing tag.
    """
    return "%s_%s%s" % (program, str(version), str(ccd).zfill(2))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_status(task, prefix, expnum, version, ccd, return_message=False):
    key = get_process_tag(prefix+task, ccd, version)
    status = get_tag(expnum, key)
    logger.debug('%s: %s' % (key, status))
    if return_message:
        return status
    else:
        return status == SUCCESS

Response: Report back status of a given program.


Instruction:
Implement a Python 3 function for
setting the processing status of the given program.

Response:
def set_status(task, prefix, expnum, version, ccd, status):
    """
    set the processing status of the given program.
    @param task: name of the processing task
    @param prefix: was there a prefix on the exposure number processed?
    @param expnum: exposure number processed.
    @param version: which version of the exposure? (p, s, o)
    @param ccd: the number of the CCD processing.
    @param status: What status to record:  "SUCCESS" we hope.
    @return: Success?
    """
    return set_tag(expnum, get_process_tag(prefix+task, ccd, version), status)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _cutout_expnum(observation, sky_coord, radius):

    uri = observation.get_image_uri()
    cutout_filehandle = tempfile.NamedTemporaryFile()
    disposition_filename = client.copy(uri + "({},{},{})".format(sky_coord.ra.to('degree').value,
                                                                 sky_coord.dec.to('degree').value,
                                                                 radius.to('degree').value),
                                       cutout_filehandle.name,
                                       disposition=True)
    cutouts = decompose_content_decomposition(disposition_filename)

    cutout_filehandle.seek(0)
    hdulist = fits.open(cutout_filehandle)
    hdulist.verify('silentfix+ignore')
    logger.debug("Initial Length of HDUList: {}".format(len(hdulist)))

    # Make sure here is a primaryHDU
    if len(hdulist) == 1:
        phdu = fits.PrimaryHDU()
        phdu.header['ORIGIN'] = "OSSOS"
        hdulist.insert(0, phdu)

    logger.debug("Final Length of HDUList: {}".format(len(hdulist)))

    if len(cutouts) != len(hdulist) - 1:
        raise ValueError("Wrong number of cutout structures found in Content-Disposition response.")

    for hdu in hdulist[1:]:
        cutout = cutouts.pop(0)
        if 'ASTLEVEL' not in hdu.header:
            print("WARNING: ******* NO ASTLEVEL KEYWORD ********** for {0} ********".format(observation.get_image_uri))
            hdu.header['ASTLEVEL'] = 0
        hdu.header['EXTNO'] = cutout[0]
        naxis1 = hdu.header['NAXIS1']
        naxis2 = hdu.header['NAXIS2']
        default_datasec = "[{}:{},{}:{}]".format(1, naxis1, 1, naxis2)
        datasec = hdu.header.get('DATASEC', default_datasec)
        datasec = datasec_to_list(datasec)
        corners = datasec
        for idx in range(len(corners)):
            try:
                corners[idx] = int(cutout[idx+1])
            except Exception:
                pass

        hdu.header['DATASEC'] = reset_datasec("[{}:{},{}:{}]".format(corners[0],
                                                                     corners[1],
                                                                     corners[2],
                                                                     corners[3]),
                                              hdu.header.get('DATASEC', default_datasec),
                                              hdu.header['NAXIS1'],
                                              hdu.header['NAXIS2'])
        hdu.header['XOFFSET'] = int(corners[0]) - 1
        hdu.header['YOFFSET'] = int(corners[2]) - 1
        hdu.converter = CoordinateConverter(hdu.header['XOFFSET'], hdu.header['YOFFSET'])
        try:
                hdu.wcs = WCS(hdu.header)
        except Exception as ex:
            logger.error("Failed trying to initialize the WCS for {}".format(uri))
            raise ex
    logger.debug("Sending back {}".format(hdulist))
    return hdulist

Response: Get a cutout from an exposure based on the RA and DEC location.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def frame2expnum(frameid):
    result = {}
    parts = re.search('(?P<expnum>\d{7})(?P<type>\S)(?P<ccd>\d\d)', frameid)
    assert parts is not None
    result['expnum'] = parts.group('expnum')
    result['ccd'] = parts.group('ccd')
    result['version'] = parts.group('type')
    return result

Response: Given an OSSOS frameid return the expnum version and ccdnum as a dictionary.


Instruction:
Implement a Python 3 function for
getting a FITS file for this exposure number and CCD.

Response:
def get_image(expnum, ccd=None, version='p', ext=FITS_EXT,
              subdir=None, prefix=None, cutout=None, return_file=True, flip_image=True):
    """Get a FITS file for this expnum/ccd  from VOSpace.

    @param cutout: (str)
    @param return_file: return an filename (True) or HDUList (False)
    @param expnum: CFHT exposure number (int)
    @param ccd:     @param ccd:
    @param version: [p, s, o]  (char)
    @param ext:
    @param subdir:
    @param prefix:
    @param flip_image: Should the image be x/y flipped after being retrieved?
    @return: astropy.io.fits.PrimaryHDU
    """

    filename = os.path.basename(get_uri(expnum, ccd, version, ext=ext, subdir=subdir, prefix=prefix))
    if os.access(filename, os.F_OK) and return_file and cutout is None:
        return filename

    cutout_string = cutout
    try:
        if os.access(filename, os.F_OK) and cutout:
            cutout = datasec_to_list(cutout)
            hdulist = fits.open(filename)
            if len(hdulist) > 1:
                raise ValueError("Local cutout access not designed to work on MEFs yet.")
            header = hdulist[0].header
            cutout[0] = cutout[0] < 0 and header['NAXIS1'] - cutout[0] + 1 or cutout[0]
            cutout[1] = cutout[1] < 0 and header['NAXIS1'] - cutout[1] + 1 or cutout[1]
            cutout[2] = cutout[2] < 0 and header['NAXIS2'] - cutout[2] + 1 or cutout[2]
            cutout[3] = cutout[3] < 0 and header['NAXIS2'] - cutout[3] + 1 or cutout[3]
            logger.debug("DATA array shape: {}".format(hdulist[0].data.shape))
            logger.debug("CUTOUT array: {} {} {} {}".format(cutout[0],
                                                            cutout[1],
                                                            cutout[2],
                                                            cutout[3]))
            flip = cutout[0] < cutout[1] and 1 or -1
            flop = cutout[2] < cutout[3] and 1 or -1
            header['CRPIX1'] = (header.get("CRPIX1", 1) - cutout[0]) * flip + 1
            header['CRPIX2'] = (header.get("CRPIX2", 1) - cutout[2]) * flop + 1
            header['CD1_1'] = header.get("CD1_1", 1) * flip
            header['CD2_1'] = header.get("CD2_1", 1) * flip
            header['CD2_2'] = header.get("CD2_2", 1) * flop
            header['CD1_2'] = header.get("CD1_2", 1) * flop

            data = hdulist[0].data[cutout[2]-1:cutout[3], cutout[0]-1:cutout[1]]
            hdulist[0].data = data
            header['DATASEC'] = reset_datasec(cutout_string,
                                              header['DATASEC'],
                                              header['NAXIS1'],
                                              header['NAXIS2'])
            if return_file:
                cutout_filename = os.path.splitext(filename)[0]+"_{}_{}_{}_{}.fits".format(cutout[0],
                                                                                cutout[1],
                                                                                cutout[2],
                                                                                cutout[3])
                hdulist.writeto(cutout_filename)
                return cutout_filename
            else:
                return hdulist
    except Exception as e:
        logger.debug(str(e))
        logger.debug("Failed trying to access local copy: {} with cutout [{}:{}, {}:{}], using VOSpace".format(
            filename, cutout[2]-1, cutout[3], cutout[0]-1, cutout[1]))

    cutout = cutout_string

    if not subdir:
        subdir = str(expnum)

    logger.debug("Building list of possible uri locations")
    # # here is the list of places we will look, in order
    if version != 'p':
        locations = [(get_uri(expnum, ccd, version, ext=ext, subdir=subdir, prefix=prefix),
                      cutout)]
    else:
        locations = []
    logger.debug(str(locations))
    if ccd is not None:
        try:
            for this_ext in [ext]:
                ext_no = int(ccd) + 1
                # extension 1 -> 18 +  37,36 should be flipped.
                flip_these_extensions = range(1, 19)
                flip_these_extensions.append(37)
                flip_these_extensions.append(38)
                flip = (cutout is None and "fits" in ext and (
                    (ext_no in flip_these_extensions and flip_image) and "[-*,-*]" or "[*,*]")) or cutout
                locations.append((get_uri(expnum, version=version, ext=this_ext, subdir=subdir),
                                  "[{}]{}".format(ext_no, flip)))
        except Exception as e:
            logger.error(str(e))
            pass
    else:
        uri = get_uri(expnum, ccd, version, ext=ext, subdir=subdir, prefix=prefix)
        locations.append((uri, cutout))
        # uri = get_uri(expnum, ccd, version, ext=ext + ".fz", subdir=subdir, prefix=prefix)
        # locations.append((uri, cutout))

    err = errno.EFAULT
    while len(locations) > 0:
        (uri, cutout) = locations.pop(0)
        try:
            hdu_list = get_hdu(uri, cutout)
            if return_file:
                hdu_list.writeto(filename)
                del hdu_list
                return filename
            else:
                return hdu_list
        except Exception as e:
            err = getattr(e, 'errno', errno.EAGAIN)
            logger.debug("{}".format(type(e)))
            logger.debug("Failed to open {} cutout:{}".format(uri, cutout))
            logger.debug("vos sent back error: {} code: {}".format(str(e), getattr(e, 'errno', 0)))

    if err == errno.EAGAIN:
        time.sleep(5)
        return get_image(expnum, ccd=ccd, version=version, ext=ext,
                         subdir=subdir, prefix=prefix, cutout=cutout, return_file=return_file, flip_image=flip_image)
    raise IOError(err, "Failed to get image at uri: {} using {} {} {} {}.".format(uri, expnum, version, ccd, cutout))


Instruction:
Given the following Python 3 function, write the documentation
def reset_datasec(cutout, datasec, naxis1, naxis2):

    if cutout is None or cutout == "[*,*]":
        return datasec
    try:
        datasec = datasec_to_list(datasec)
    except:
        return datasec

    # check for '-*' in the cutout string and replace is naxis:1
    cutout = cutout.replace(" ", "")
    cutout = cutout.replace("[-*,", "{}:1,".format(naxis1))
    cutout = cutout.replace(",-*]", ",{}:1]".format(naxis2))
    cutout = cutout.replace("[*,", "[1:{},".format(naxis1))
    cutout = cutout.replace(",*]", ",1:{}]".format(naxis1))

    try:
        cutout = [int(x) for x in re.findall(r"([-+]?[*\d]+?)[:,\]]+", cutout)]
    except:
        logger.debug("Failed to processes the cutout pattern: {}".format(cutout))
        return datasec

    if len(cutout) == 5:
        # cutout likely starts with extension, remove
        cutout = cutout[1:]

    # -ve integer offsets indicate offset from the end of array.
    for idx in [0, 1]:
        if cutout[idx] < 0:
            cutout[idx] = naxis1 - cutout[idx] + 1
    for idx in [2, 3]:
        if cutout[idx] < 0:
            cutout[idx] = naxis2 - cutout[idx] + 1

    flip = cutout[0] > cutout[1]
    flop = cutout[2] > cutout[3]

    logger.debug("Working with cutout: {}".format(cutout))

    if flip:
        cutout = [naxis1 - cutout[0] + 1, naxis1 - cutout[1] + 1, cutout[2], cutout[3]]
        datasec = [naxis1 - datasec[1] + 1, naxis1 - datasec[0] + 1, datasec[2], datasec[3]]

    if flop:
        cutout = [cutout[0], cutout[1], naxis2 - cutout[2] + 1, naxis2 - cutout[3] + 1]
        datasec = [datasec[0], datasec[1], naxis2 - datasec[3] + 1, naxis2 - datasec[2] + 1]

    datasec = [max(datasec[0] - cutout[0] + 1, 1),
               min(datasec[1] - cutout[0] + 1, naxis1),
               max(datasec[2] - cutout[2] + 1, 1),
               min(datasec[3] - cutout[2] + 1, naxis2)]

    return "[{}:{},{}:{}]".format(datasec[0], datasec[1], datasec[2], datasec[3])

Response: reset the datasec to account for a possible cutout


Instruction:
Can you generate the documentation for the following Python 3 function
def get_hdu(uri, cutout=None):
    try:
        # the filename is based on the Simple FITS images file.
        filename = os.path.basename(uri)
        if os.access(filename, os.F_OK) and cutout is None:
            logger.debug("File already on disk: {}".format(filename))
            hdu_list = fits.open(filename, scale_back=True)
            hdu_list.verify('silentfix+ignore')

        else:
            logger.debug("Pulling: {}{} from VOSpace".format(uri, cutout))
            fpt = tempfile.NamedTemporaryFile(suffix='.fits')
            cutout = cutout is not None and cutout or ""
            copy(uri+cutout, fpt.name)
            fpt.seek(0, 2)
            fpt.seek(0)
            logger.debug("Read from vospace completed. Building fits object.")
            hdu_list = fits.open(fpt, scale_back=False)
            hdu_list.verify('silentfix+ignore')

            logger.debug("Got image from vospace")
            try:
                hdu_list[0].header['DATASEC'] = reset_datasec(cutout, hdu_list[0].header['DATASEC'],
                                                              hdu_list[0].header['NAXIS1'],
                                                              hdu_list[0].header['NAXIS2'])
            except Exception as e:
                logging.debug("error converting datasec: {}".format(str(e)))

        for hdu in hdu_list:
            logging.debug("Adding converter to {}".format(hdu))
            hdu.converter = CoordinateConverter(0, 0)
            try:
                hdu.wcs = WCS(hdu.header)
            except Exception as ex:
                logger.error("Failed trying to initialize the WCS: {}".format(ex))
    except Exception as ex:
        raise ex
    return hdu_list

Response: Get a HDU from the given URI from the VOSpace.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_fwhm_tag(expnum, ccd, prefix=None, version='p'):
    uri = get_uri(expnum, ccd, version, ext='fwhm', prefix=prefix)
    if uri not in fwhm:
        key = "fwhm_{:1s}{:02d}".format(version, int(ccd))
        fwhm[uri] = get_tag(expnum, key)
    return fwhm[uri]

Response: Get the FWHM from the VOSpace annotation.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_fwhm(expnum, ccd, prefix=None, version='p'):

    uri = get_uri(expnum, ccd, version, ext='fwhm', prefix=prefix)
    filename = os.path.basename(uri)

    try:
        return fwhm[uri]
    except:
        pass

    try:
        fwhm[uri] = float(open(filename, 'r').read())
        return fwhm[uri]
    except:
        pass

    try:
        fwhm[uri] = float(open_vos_or_local(uri).read())
        return fwhm[uri]
    except Exception as ex:
        logger.error(str(ex))
        fwhm[uri] = 4.0
        return fwhm[uri]

Response: Get the FWHM computed for the given expnum and ccd.


Instruction:
How would you code a function in Python 3 to
retrieve the zeropoint stored in the tags associated with this image.

Response:
def _get_zeropoint(expnum, ccd, prefix=None, version='p'):
    """
    Retrieve the zeropoint stored in the tags associated with this image.
    @param expnum: Exposure number
    @param ccd: ccd of the exposure
    @param prefix: possible prefix (such as 'fk')
    @param version: which version: p, s, or o ?
    @return: zeropoint
    """
    if prefix is not None:
        DeprecationWarning("Prefix is no longer used here as the 'fk' and 's' have the same zeropoint.")

    key = "zeropoint_{:1s}{:02d}".format(version, int(ccd))
    return get_tag(expnum, key)


Instruction:
How would you explain what the following Python 3 function does
def get_zeropoint(expnum, ccd, prefix=None, version='p'):
    uri = get_uri(expnum, ccd, version, ext='zeropoint.used', prefix=prefix)
    try:
        return zmag[uri]
    except:
        pass

    try:
        zmag[uri] = float(open_vos_or_local(uri).read())
        return zmag[uri]
    except:
        pass

    zmag[uri] = 0.0
    return zmag[uri]

Response: Get the zeropoint for this exposure using the zeropoint. used file created during source planting..


Instruction:
How would you explain what the following Python 3 function does
def mkdir(dirname):
    dir_list = []

    while not client.isdir(dirname):
        dir_list.append(dirname)
        dirname = os.path.dirname(dirname)
    while len(dir_list) > 0:
        logging.info("Creating directory: %s" % (dir_list[-1]))
        try:
            client.mkdir(dir_list.pop())
        except IOError as e:
            if e.errno == errno.EEXIST:
                pass
            else:
                raise e

Response: make directory tree in vospace.


Instruction:
Here you have a function in Python 3, explain what it does
def vofile(filename, **kwargs):
    basename = os.path.basename(filename)
    if os.access(basename, os.R_OK):
        return open(basename, 'r')
    kwargs['view'] = kwargs.get('view', 'data')
    return client.open(filename, **kwargs)

Response: Open and return a handle on a VOSpace data connection


Instruction:
Write a Python 3 function that can
open a file which can either be in VOSpace or the local filesystem.

Response:
def open_vos_or_local(path, mode="rb"):
    """
    Opens a file which can either be in VOSpace or the local filesystem.

    @param path:
    @param mode:
    @return:
    """
    filename = os.path.basename(path)
    if os.access(filename, os.F_OK):
        return open(filename, mode)
    if path.startswith("vos:"):
        primary_mode = mode[0]
        if primary_mode == "r":
            vofile_mode = os.O_RDONLY
        elif primary_mode == "w":
            vofile_mode = os.O_WRONLY
        elif primary_mode == "a":
            vofile_mode = os.O_APPEND
        else:
            raise ValueError("Can't open with mode %s" % mode)

        return vofile(path, mode=vofile_mode)
    else:
        return open(path, mode)


Instruction:
How would you code a function in Python 3 to
copy a file from source to dest

Response:
def copy(source, dest):
    """
    use the vospace service to get a file.

    @param source:
    @param dest:
    @return:
    """
    logger.info("copying {} -> {}".format(source, dest))

    return client.copy(source, dest)


Instruction:
Implement a function in Python 3 to
make a link between two versions of a file

Response:
def vlink(s_expnum, s_ccd, s_version, s_ext,
          l_expnum, l_ccd, l_version, l_ext, s_prefix=None, l_prefix=None):
    """make a link between two version of a file.

    @param s_expnum:
    @param s_ccd:
    @param s_version:
    @param s_ext:
    @param l_expnum:
    @param l_ccd:
    @param l_version:
    @param l_ext:
    @param s_prefix:
    @param l_prefix:
    @return:
    """
    source_uri = get_uri(s_expnum, ccd=s_ccd, version=s_version, ext=s_ext, prefix=s_prefix)
    link_uri = get_uri(l_expnum, ccd=l_ccd, version=l_version, ext=l_ext, prefix=l_prefix)

    return client.link(source_uri, link_uri)


Instruction:
Create a Python 3 function to
delete a file, no error on does not exist @param expnum: @param ccd: @param version: @param ext: @param prefix: @return:

Response:
def delete(expnum, ccd, version, ext, prefix=None):
    """
    delete a file, no error on does not exist

    @param expnum:
    @param ccd:
    @param version:
    @param ext:
    @param prefix:
    @return:
    """
    uri = get_uri(expnum, ccd=ccd, version=version, ext=ext, prefix=prefix)
    remove(uri)


Instruction:
How would you code a function in Python 3 to
get a listing matching pattern @param pattern: @return:

Response:
def my_glob(pattern):
    """
    get a listing matching pattern

    @param pattern:
    @return:
    """
    result = []
    if pattern[0:4] == 'vos:':
        dirname = os.path.dirname(pattern)
        flist = listdir(dirname)
        for fname in flist:
            fname = '/'.join([dirname, fname])
            if fnmatch.fnmatch(fname, pattern):
                result.append(fname)
    else:
        result = glob(pattern)
    return result


Instruction:
Write a Python 3 function that can
check if a node in VOSpace has the specified property.

Response:
def has_property(node_uri, property_name, ossos_base=True):
    """
    Checks if a node in VOSpace has the specified property.

    @param node_uri:
    @param property_name:
    @param ossos_base:
    @return:
    """
    if get_property(node_uri, property_name, ossos_base) is None:
        return False
    else:
        return True


Instruction:
Write a Python 3 function that can
retrieve the value associated with a property on a node in VOSpace.

Response:
def get_property(node_uri, property_name, ossos_base=True):
    """
    Retrieves the value associated with a property on a node in VOSpace.

    @param node_uri:
    @param property_name:
    @param ossos_base:
    @return:
    """
    # Must use force or we could have a cached copy of the node from before
    # properties of interest were set/updated.
    node = client.get_node(node_uri, force=True)
    property_uri = tag_uri(property_name) if ossos_base else property_name

    if property_uri not in node.props:
        return None

    return node.props[property_uri]


Instruction:
Can you generate the documentation for the following Python 3 function
def set_property(node_uri, property_name, property_value, ossos_base=True):
    node = client.get_node(node_uri)
    property_uri = tag_uri(property_name) if ossos_base else property_name

    # If there is an existing value, clear it first
    if property_uri in node.props:
        node.props[property_uri] = None
        client.add_props(node)

    node.props[property_uri] = property_value
    client.add_props(node)

Response: Sets the value of a property on a node in VOSpace.


Instruction:
Can you generate the documentation for the following Python 3 function
def build_counter_tag(epoch_field, dry_run=False):
    logger.info("Epoch Field: {}, OBJECT_COUNT {}".format(str(epoch_field), str(OBJECT_COUNT)))
    tag = epoch_field[1] + "-" + OBJECT_COUNT

    if dry_run:
        tag += "-DRYRUN"

    return tag

Response: Builds the tag for the counter of a given epoch field.


Instruction:
Can you tell what is the following Python 3 function doing
def read_object_counter(node_uri, epoch_field, dry_run=False):
    return get_property(node_uri, build_counter_tag(epoch_field, dry_run),
                        ossos_base=True)

Response: Reads the object counter for the given epoch and field.


Instruction:
How would you explain what the following Python 3 function does
def increment_object_counter(node_uri, epoch_field, dry_run=False):
    current_count = read_object_counter(node_uri, epoch_field, dry_run=dry_run)

    if current_count is None:
        new_count = "01"
    else:
        new_count = coding.base36encode(coding.base36decode(current_count) + 1,
                                        pad_length=2)

    set_property(node_uri,
                 build_counter_tag(epoch_field, dry_run=dry_run),
                 new_count,
                 ossos_base=True)

    return new_count

Response: Increment the object counter for a node.


Instruction:
Can you create a Python 3 function that
retrieves the mopheader from cache or local file.

Response:
def get_mopheader(expnum, ccd, version='p', prefix=None):
    """
    Retrieve the mopheader, either from cache or from vospace

    @param expnum:
    @param ccd:
    @param version:
    @param prefix:
    @return: Header
    """
    prefix = prefix is None and "" or prefix
    mopheader_uri = dbimages_uri(expnum=expnum,
                                 ccd=ccd,
                                 version=version,
                                 prefix=prefix,
                                 ext='.mopheader')
    if mopheader_uri in mopheaders:
        return mopheaders[mopheader_uri]

    filename = os.path.basename(mopheader_uri)

    if os.access(filename, os.F_OK):
        logger.debug("File already on disk: {}".format(filename))
        mopheader_fpt = StringIO(open(filename, 'r').read())
    else:
        mopheader_fpt = StringIO(open_vos_or_local(mopheader_uri).read())

    with warnings.catch_warnings():
        warnings.simplefilter('ignore', AstropyUserWarning)
        mopheader = fits.open(mopheader_fpt)

        # add some values to the mopheader so it can be an astrom header too.
        header = mopheader[0].header
        try:
            header['FWHM'] = get_fwhm(expnum, ccd)
        except IOError:
            header['FWHM'] = 10
        header['SCALE'] = mopheader[0].header['PIXSCALE']
        header['NAX1'] = header['NAXIS1']
        header['NAX2'] = header['NAXIS2']
        header['MOPversion'] = header['MOP_VER']
        header['MJD_OBS_CENTER'] = str(Time(header['MJD-OBSC'],
                                            format='mjd',
                                            scale='utc', precision=5).replicate(format='mpc'))
        header['MAXCOUNT'] = MAXCOUNT
        mopheaders[mopheader_uri] = header
        mopheader.close()
    return mopheaders[mopheader_uri]


Instruction:
How would you implement a function in Python 3 that
gets the astrometric header for a given CFHT exposure number.

Response:
def _get_sghead(expnum):
    """
    Use the data web service to retrieve the stephen's astrometric header.

    :param expnum: CFHT exposure number you want the header for
    :rtype : list of astropy.io.fits.Header objects.
    """
    version = 'p'

    key = "{}{}".format(expnum, version)
    if key in sgheaders:
        return sgheaders[key]

    url = "http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/data/pub/CFHTSG/{}{}.head".format(expnum, version)
    logging.getLogger("requests").setLevel(logging.ERROR)
    logging.debug("Attempting to retrieve {}".format(url))
    resp = requests.get(url)
    if resp.status_code != 200:
        raise IOError(errno.ENOENT, "Could not get {}".format(url))

    header_str_list = re.split('END      \n', resp.content)

    # # make the first entry in the list a Null
    headers = [None]
    for header_str in header_str_list:
        headers.append(fits.Header.fromstring(header_str, sep='\n'))
        logging.debug(headers[-1].get('EXTVER', -1))
    sgheaders[key] = headers
    return sgheaders[key]


Instruction:
How would you implement a function in Python 3 that
gets a FITS header from the given URI.

Response:
def get_header(uri):
    """
    Pull a FITS header from observation at the given URI

    @param uri:  The URI of the image in VOSpace.
    """
    if uri not in astheaders:
        astheaders[uri] = get_hdu(uri, cutout="[1:1,1:1]")[0].header
    return astheaders[uri]


Instruction:
Make a summary of the following Python 3 code
def get_astheader(expnum, ccd, version='p', prefix=None):
    logger.debug("Getting ast header for {}".format(expnum))
    if version == 'p':
        try:
            # Get the SG header if possible.
            sg_key = "{}{}".format(expnum, version)
            if sg_key not in sgheaders:
                _get_sghead(expnum)
            if sg_key in sgheaders:
                for header in sgheaders[sg_key]:
                        if header.get('EXTVER', -1) == int(ccd):
                            return header
        except:
            pass

    try:
       ast_uri = dbimages_uri(expnum, ccd, version=version, ext='.fits')
       if ast_uri not in astheaders:
           hdulist = get_image(expnum, ccd=ccd, version=version, prefix=prefix,
                               cutout="[1:1,1:1]", return_file=False, ext='.fits')
           assert isinstance(hdulist, fits.HDUList)
           astheaders[ast_uri] = hdulist[0].header
    except:
       ast_uri = dbimages_uri(expnum, ccd, version=version, ext='.fits.fz')
       if ast_uri not in astheaders:
           hdulist = get_image(expnum, ccd=ccd, version=version, prefix=prefix,
                               cutout="[1:1,1:1]", return_file=False, ext='.fits.fz')
           assert isinstance(hdulist, fits.HDUList)
           astheaders[ast_uri] = hdulist[0].header
    return astheaders[ast_uri]

Response: Retrieve the header for a given expnum and ccd.


Instruction:
Write a Python 3 script to
return the string representation of the tag used to annotate the status in VOSpace.

Response:
def tag(self):
        """
        Get the string representation of the tag used to annotate the status in VOSpace.
        @return: str
        """
        return "{}{}_{}{:02d}".format(self.target.prefix,
                                      self,
                                      self.target.version,
                                      self.target.ccd)


Instruction:
Write a Python 3 function for
scrambling the image files for the given set of exposures

Response:
def scramble(expnums, ccd, version='p', dry_run=False):
    """run the plant script on this combination of exposures"""

    mjds = []
    fobjs = []
    for expnum in expnums:
        filename = storage.get_image(expnum, ccd=ccd, version=version)
        fobjs.append(fits.open(filename))
        # Pull out values to replace in headers.. must pull them
        # as otherwise we get pointers...
        mjds.append(fobjs[-1][0].header['MJD-OBS'])

    order = [0, 2, 1]
    for idx in range(len(fobjs)):
        logging.info("Flipping %d to %d" % (fobjs[idx][0].header['EXPNUM'],
                                            expnums[order[idx]]))
        fobjs[idx][0].header['EXPNUM'] = expnums[order[idx]]
        fobjs[idx][0].header['MJD-OBS'] = mjds[order[idx]]
        uri = storage.get_uri(expnums[order[idx]],
                              ccd=ccd,
                              version='s',
                              ext='fits')
        fname = os.path.basename(uri)
        if os.access(fname, os.F_OK):
            os.unlink(fname)
        fobjs[idx].writeto(fname)
        if dry_run:
            continue
        storage.copy(fname, uri)

    return


Instruction:
Write a Python 3 function that can
create an OSSOS mopheader file for the specified image.

Response:
def mk_mopheader(expnum, ccd, version, dry_run=False, prefix=""):
    """Run the OSSOS mopheader script.

    """
    ## confirm destination directory exists.
    destdir = os.path.dirname(
        storage.dbimages_uri(expnum, ccd, prefix=prefix, version=version, ext='fits'))
    if not dry_run:
        storage.mkdir(destdir)

    ## get image from the vospace storage area
    filename = storage.get_image(expnum, ccd, version=version, prefix=prefix)
    logging.info("Running mopheader on %s %d" % (expnum, ccd))
    ## launch the mopheader script
    ## launch the makepsf script
    expname = os.path.basename(filename).strip('.fits')
    logging.info(util.exec_prog(['stepZjmp',
                                 '-f',
                                 expname]))

    mopheader_filename = expname+".mopheader"

    # mopheader_filename = mopheader.main(filename)

    if dry_run:
        return

    destination = storage.dbimages_uri(expnum, ccd, prefix=prefix, version=version, ext='mopheader')
    source = mopheader_filename
    storage.copy(source, destination)

    return


Instruction:
How would you explain what the following Python 3 function does
def read_cands(filename):
    import sre

    lines=file(filename).readlines()
    exps=[]
    cands=[]
    coo=[]
    for line in lines:
        if ( line[0:2]=="##" ) :
            break
        exps.append(line[2:].strip())

    for line in lines:
        if ( line[0]=="#" ) :
            continue
        if len(line.strip())==0:
            if len(coo)!=0:
                cands.append(coo)
            coo=[]
            continue
        vals=line.split()
        cols=['x','y','x_0','y_0','flux','size','max_int','elon']
        values={}
        for j in range(len(cols)):
            col=cols.pop().strip()
            val=vals.pop().strip()
            values[col]=float(val)
        coo.append(values)
    cands.append(coo)
    return {'fileId': exps, 'cands': cands}

Response: Read in the contents of a cands comb file


Instruction:
Implement a Python 3 function for
displaying the candidates contained in a candidate record list

Response:
def discands(record):
    """Display the candidates contained in a candidate record list"""
    import pyraf, pyfits

    pyraf.iraf.tv()
    display = pyraf.iraf.tv.display

    width=128

    cands = record['cands']
    exps=   record['fileId']

    ### load some header info from the mophead file
    headers={}
    for exp in exps:
        f = pyfits.open(exp+".fits")
        headers[exp]={}
        for key in ['MJDATE', 'NAXIS1', 'NAXIS2', 'EXPTIME', 'FILTER']:
            headers[exp][key]=f[0].header[key]
        headers[exp]['MJD-OBSC']=headers[exp]['MJDATE']+headers[exp]['EXPTIME']/2.0/3600.0/24.0
        f.close()

    import math,os
    for cand in cands:
        for i in range(len(exps)):
            x2=[]
            y2=[]
            y1=[]
            x1=[]
            fileId=exps[i]
            x2.append(int(min(math.floor(cand[i]['x'])+width,headers[fileId]['NAXIS1'])))
            y2.append(int(min(math.floor(cand[i]['y'])+width,headers[fileId]['NAXIS2'])))
            x1.append(int(max(math.floor(cand[i]['x'])-width,1)))
            y1.append(int(max(math.floor(cand[i]['y'])-width,1)))
        x_1 = min(x1)
        y_1 = min(y1)
        x_2 = max(x2)
        y_2 = max(y2)
        for i in range(len(exps)):
            tvmark=open('tv.coo','w')
            xshift=cand[i]['x']-cand[i]['x_0']
            yshift=cand[i]['y']-cand[i]['y_0']
            tvmark.write('%f %f\n' % ( cand[i]['x'], cand[i]['y']))
            x1=x_1 + xshift
            y1=y_1 + yshift
            x2=x_2 + xshift
            y2=y_2 + yshift
            cutout = "[%d:%d,%d:%d]" % (x1,x2,y1,y2)
            fileId=exps[i]
            display(fileId+cutout,i+1)
            tvmark.close()
            pyraf.iraf.tv.tvmark(i+1,'tv.coo',mark='circle',radii=15)
            os.unlink('tv.coo')


Instruction:
Can you generate the documentation for the following Python 3 function
def query_for_observations(mjd, observable, runid_list):

    data = {"QUERY": ("SELECT Observation.target_name as TargetName, "
                      "COORD1(CENTROID(Plane.position_bounds)) AS RA,"
                      "COORD2(CENTROID(Plane.position_bounds)) AS DEC, "
                      "Plane.time_bounds_lower AS StartDate, "
                      "Plane.time_exposure AS ExposureTime, "
                      "Observation.instrument_name AS Instrument, "
                      "Plane.energy_bandpassName AS Filter, "
                      "Observation.observationID AS dataset_name, "
                      "Observation.proposal_id AS ProposalID, "
                      "Observation.proposal_pi AS PI "
                      "FROM caom2.Observation AS Observation "
                      "JOIN caom2.Plane AS Plane ON "
                      "Observation.obsID = Plane.obsID "
                      "WHERE  ( Observation.collection = 'CFHT' ) "
                      "AND Plane.time_bounds_lower > %d "
                      "AND Plane.calibrationLevel=%s "
                      "AND Observation.proposal_id IN %s ") % (mjd, observable, str(runid_list)),
            "REQUEST": "doQuery",
            "LANG": "ADQL",
            "FORMAT": "votable"}

    result = requests.get(storage.TAP_WEB_SERVICE, params=data, verify=False)
    assert isinstance(result, requests.Response)
    logging.debug("Doing TAP Query using url: %s" % (str(result.url)))
    temp_file = tempfile.NamedTemporaryFile()
    with open(temp_file.name, 'w') as outfile:
        outfile.write(result.text)
    try:
        vot = parse(temp_file.name).get_first_table()
    except Exception as ex:
        logging.error(str(ex))
        logging.error(result.text)
        raise ex

    vot.array.sort(order='StartDate')
    t = vot.array
    temp_file.close()

    logging.debug("Got {} lines from tap query".format(len(t)))

    return t

Response: Query the TAP service for all observations that are part of runid and have calibration observable.


Instruction:
Can you tell what is the following Python 3 function doing
def create_ascii_table(observation_table, outfile):

    logging.info("writing text log to %s" % outfile)

    stamp = "#\n# Last Updated: " + time.asctime() + "\n#\n"
    header = "| %20s | %20s | %20s | %20s | %20s | %20s | %20s |\n" % (
        "EXPNUM", "OBS-DATE", "FIELD", "EXPTIME(s)", "RA", "DEC", "RUNID")
    bar = "=" * (len(header) - 1) + "\n"

    if outfile[0:4] == "vos:":
        temp_file = tempfile.NamedTemporaryFile(suffix='.txt')
        fout = temp_file
    else:
        fout = open(outfile, 'w')

    t2 = None
    fout.write(bar + stamp + bar + header)

    populated = storage.list_dbimages()
    for i in range(len(observation_table) - 1, -1, -1):
        row = observation_table.data[i]
        if row['dataset_name'] not in populated:
            storage.populate(row['dataset_name'])
        str_date = str(ephem.date(row.StartDate +
                                  2400000.5 -
                                  ephem.julian_date(ephem.date(0))))[:20]
        t1 = time.strptime(str_date, "%Y/%m/%d %H:%M:%S")
        if t2 is None or math.fabs(time.mktime(t2) - time.mktime(t1)) > 3 * 3600.0:
            fout.write(bar)
        t2 = t1
        ra = str(ephem.hours(math.radians(row.RA)))
        dec = str(ephem.degrees(math.radians(row.DEC)))
        line = "| %20s | %20s | %20s | %20.1f | %20s | %20s | %20s |\n" % (
            str(row.dataset_name),
            str(ephem.date(row.StartDate + 2400000.5 -
                           ephem.julian_date(ephem.date(0))))[:20],
            row.TargetName[:20],
            row.ExposureTime, ra[:20], dec[:20], row.ProposalID[:20])
        fout.write(line)

    fout.write(bar)

    if outfile[0:4] == "vos:":
        fout.flush()
        storage.copy(fout.name, outfile)
    fout.close()

    return

Response: Create an ascii log file for easy parsing.


Instruction:
Here you have a function in Python 3, explain what it does
def create_sky_plot(observation_table, outfile):

    # camera dimensions
    width = 0.98
    height = 0.98
    ax = None

    if outfile[0:4] == 'vos:':
        temp_file = tempfile.NamedTemporaryFile(suffix='.pdf')
        pdf = PdfPages(temp_file.name)
    else:
        pdf = PdfPages(outfile)

    saturn = ephem.Saturn()
    uranus = ephem.Uranus()

    t2 = None
    fig = None
    proposal_id = None
    limits = {'13A': (245, 200, -20, 0),
              '13B': (0, 45, 0, 20)}
    for row in reversed(observation_table.data):
        date = ephem.date(row.StartDate + 2400000.5 - ephem.julian_date(ephem.date(0)))
        str_date = str(date)
        # Saturn only a problem in 2013A fields
        saturn.compute(date)
        sra = math.degrees(saturn.ra)
        sdec = math.degrees(saturn.dec)
        uranus.compute(date)
        ura = math.degrees(uranus.ra)
        udec = math.degrees(uranus.dec)
        t1 = time.strptime(str_date, "%Y/%m/%d %H:%M:%S")
        if t2 is None or (math.fabs(time.mktime(t2) - time.mktime(
                t1)) > 3 * 3600.0 and opt.stack) or proposal_id is None or proposal_id != row.ProposalID:
            if fig is not None:
                pdf.savefig()
                close()
            proposal_id = row.ProposalID
            fig = figure(figsize=(7, 2))
            ax = fig.add_subplot(111, aspect='equal')
            ax.set_title("Data taken on %s-%s-%s" % (t1.tm_year, t1.tm_mon, t1.tm_mday), fontdict={'fontsize': 8})
            ax.axis(limits.get(row.ProposalID[0:3], (0, 20, 0, 20)))  # appropriate only for 2013A fields
            ax.grid()
            ax.set_xlabel("RA (deg)", fontdict={'fontsize': 8})
            ax.set_ylabel("DEC (deg)", fontdict={'fontsize': 8})
        t2 = t1
        ra = row.RA - width / 2.0
        dec = row.DEC - height / 2.0
        color = 'b'
        if 'W' in row['TargetName']:
            color = 'g'
        ax.add_artist(Rectangle(xy=(ra, dec), height=height, width=width,
                                edgecolor=color, facecolor=color,
                                lw=0.5, fill='g', alpha=0.33))
        ax.add_artist(Rectangle(xy=(sra, sdec), height=0.3, width=0.3,
                                edgecolor='r',
                                facecolor='r',
                                lw=0.5, fill='k', alpha=0.33))
        ax.add_artist(Rectangle(xy=(ura, udec), height=0.3, width=0.3,
                                edgecolor='b',
                                facecolor='b',
                                lw=0.5, fill='b', alpha=0.33))

    if ax is not None:
        ax.axis((270, 215, -20, 0))
        pdf.savefig()
        close()
    pdf.close()
    if outfile[0:4] == "vos:":
        storage.copy(pdf.name, outfile)
        pdf.close()

    return

Response: Create a sky coverge plot for the given observation table.


Instruction:
Here you have a function in Python 3, explain what it does
def load_observations((observations, regex, rename), path, filenames):

    for filename in filenames:
        if re.search(regex, filename) is None:
            logging.warning("Skipping {}".format(filename))
            continue
        print os.path.join(path,filename)
        obs = mpc.MPCReader().read(os.path.join(path,filename))
        for ob in obs:
            if rename:
                new_provisional_name = os.path.basename(filename)
                new_provisional_name = new_provisional_name[0:new_provisional_name.find(".")]
                ob.provisional_name = new_provisional_name
                # ob.comment.name = new_provisional_name
            key1 = "{:.5f}".format(ob.date.mjd)
            key2 = ob.provisional_name
            if key1 not in observations:
                 observations[key1] = {}
            if key2 in observations[key1]:
               if observations[key1][key2]:
                   continue
               if not observation.null_observation:
                   logger.error(filename)
                   logger.error(line)
                   logger.error(str(observations[key1][key2]))
                   raise ValueError("conflicting observations for {} in {}".format(key2, key1))
            observations[key1][key2] = ob

Response: Load the MPC file and store the observations into the observations dictionary.


Instruction:
Can you implement a function in Python 3 that
writes the header to a FITS file.

Response:
def writeto(self, filename, **kwargs):
        """
        Write the header to a fits file.
        :param filename:
        :return:
        """
        fits.PrimaryHDU(header=self).writeto(filename, output_verify='ignore', **kwargs)


Instruction:
Make a summary of the following Python 3 code
def crpix(self):
        try:
            return self.wcs.crpix1, self.wcs.crpix2
        except Exception as ex:
            logging.debug("Couldn't get CRPIX from WCS: {}".format(ex))
            logging.debug("Switching to use DATASEC for CRPIX value computation.")

        try:
            (x1, x2), (y1, y2) = util.get_pixel_bounds_from_datasec_keyword(self['DETSEC'])
            dx = float(self['NAXIS1'])
            dy = float(self['NAXIS2'])
        except KeyError as ke:
            raise KeyError("Header missing keyword: {}, required for CRPIX[12] computation".format(ke.args[0]))

        crpix1 = self._DET_X_CEN - (x1 + x2) / 2. + dx / 2.
        crpix2 = self._DET_Y_CEN - (y1 + y2) / 2. + dy / 2.

        return crpix1, crpix2

Response: Return the location of the reference coordinate in the pixel frame.


Instruction:
How would you explain what the following Python 3 function does
def mjd_obsc(self):
        # TODO Check if this exposure was taken afer or before correction needed.

        try:
            utc_end = self['UTCEND']
            exposure_time = float(self['EXPTIME'])
            date_obs = self['DATE-OBS']
        except KeyError as ke:
            raise KeyError("Header missing keyword: {}, required for MJD-OBSC computation".format(ke.args[0]))

        utc_end = Time(date_obs + "T" + utc_end)
        utc_cen = utc_end - TimeDelta(0.73, format='sec') - TimeDelta(exposure_time / 2.0, format='sec')
        return round(utc_cen.mjd, 7)

Response: Compute the MJD of the Megaprime image header.


Instruction:
Can you create a Python 3 function that
gets the world coordinate of the reference pixel.

Response:
def crval(self):
        """
        Get the world coordinate of the reference pixel.

        @rtype: float, float
        """
        try:
            return self.wcs.crval1, self.wcs.crval2
        except Exception as ex:
            logging.debug("Couldn't get CRVAL from WCS: {}".format(ex))
            logging.debug("Trying RA/DEC values")

        try:
            return (float(self['RA-DEG']),
                    float(self['DEC-DEG']))
        except KeyError as ke:
            KeyError("Can't build CRVAL1/2 missing keyword: {}".format(ke.args[0]))


Instruction:
Write a Python 3 script for
returning the pixel scale of the detector in arcseconds.

Response:
def pixscale(self):
        """Return the pixel scale of the detector, in arcseconds.

        This routine first attempts to compute the size of a pixel using the WCS.  Then looks for PIXSCAL in header.

        @return: pixscal
        @rtype: float
        """
        try:
            (x, y) = self['NAXIS1'] / 2.0, self['NAXIS2'] / 2.0
            p1 = SkyCoord(*self.wcs.xy2sky(x, y) * units.degree)
            p2 = SkyCoord(*self.wcs.xy2sky(x + 1, y + 1) * units.degree)
            return round(p1.separation(p2).to(units.arcsecond).value / math.sqrt(2), 3)
        except Exception as ex:
            logging.debug("Failed to compute PIXSCALE using WCS: {}".format(ex))

        return float(self['PIXSCAL'])


Instruction:
Create a Python 3 function for
computing the magnitude of the star at location x y

Response:
def iraf_phot(f,x,y,zmag=26.5,apin=10,skyin=15,skywidth=10):
    """Compute the magnitude of the star at location x/y"""
    import pyfits
    import re
    infits=pyfits.open(f,'update')
    f=re.sub(r'.fits$','',f)

    ### Get my python routines
    from pyraf import iraf
    from pyraf.irafpar import IrafParList

    ### keep all the parameters locally cached.
    iraf.set(uparm="./")
    iraf.set(imtype="fits")

    ### Load the required IRAF packages
    iraf.digiphot()
    iraf.apphot()
    iraf.daophot()

    ### temp file name hash.
    tfile={}

    iraf.datapars.datamax=60000
    iraf.datapars.datamin=-1000
    iraf.datapars.airmass='AIRMASS'
    iraf.datapars.filter='FILTER'
    iraf.datapars.obstime='TIME-OBS'
    iraf.datapars.exposure='EXPTIME'
    iraf.datapars.gain='GAIN'
    iraf.datapars.ccdread='RDNOISE'
    iraf.datapars.fwhmpsf=5.0

    iraf.centerpars.calgorithm='centroid'
    iraf.photpars.zmag=zmag
    iraf.photpars.apertures=apin
    iraf.fitskypars.annulus=skyin
    iraf.fitskypars.dannulus=skywidth
    iraf.daophot.verbose=iraf.no
    iraf.daophot.verify=iraf.no
    iraf.daophot.update=iraf.no

    iraf.psf.interactive=iraf.no
    iraf.pstselect.interactive=iraf.no

    iraf.datapars.saveParList()
    iraf.fitskypars.saveParList()
    iraf.centerpars.saveParList()
    iraf.findpars.saveParList()
    iraf.photpars.saveParList()

    tfiles = ['coo','mag']

    for file in tfiles:
        extname=f
        tfile[file]=extname+"."+file
        if ( os.access(tfile[file],os.F_OK) ):
            os.unlink(tfile[file])

    this_image=f

    fd = open(tfile['coo'],'w')
    fd.write('%f %f\n' % ( x, y) )
    fd.close()

    print "Measuring photometry psf star in "+tfile['coo']
    iraf.daophot.phot(image=this_image,
                      coords=tfile['coo'],
                      output=tfile['mag'])

    import string
    a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1)
    (mag,x,y)=string.split(a[0])

    inhdu=infits[0].header

    inhdu.update("PSFMAG",float(mag),comment="PSF Magnitude")
    inhdu.update("PSF_X",float(x),comment="PSF Magnitude")
    inhdu.update("PSF_Y",float(y),comment="PSF Magnitude")
    inhdu.update("ZMAG",zmag,comment="ZMAG of PSF ")

    ## now measure using a smaller aperture to get aper correction
    iraf.photpars.apertures=apin*3.0
    os.unlink(tfile['mag'])
    iraf.daophot.phot(image=this_image,
                      coords=tfile['coo'],
                      output=tfile['mag'])
    a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1)
    (magout,x,y)=string.split(a[0])
    inhdu.update("APCOR",float(magout)-float(mag),comment="AP_OUT - AP_IN")
    inhdu.update("AP_IN",apin*3.0,comment="Small aperature")
    inhdu.update("AP_OUT",apin,comment="Large aperture")

    #    ### append this psf to the output images....

    infits.close()

    ### remove the temp file we used for this computation.
    #for tf in tfile.keys():
    #    if os.access(tfile[tf],os.F_OK):
    #        os.unlink(tfile[tf])
    return 0


Instruction:
Write a Python 3 function that can
use the rates program to determine the minimum and maximum bounds for planting

Response:
def get_rates(file,au_min=25,au_max=150):
    """ Use the rates program to determine the minimum and maximum bounds for planting"""
    import os, string

    rate_command='rate.pl --file %s %d ' % ( file, au_min)
    rate=os.popen(rate_command)
    line=rate.readline()
    print line
    rate.close()
    (min_rate, min_ang, min_aw, min_rmin, min_rmax)=string.split(line)
    rate_command='rate.pl --file %s %d ' % ( file, au_min)
    rate=os.popen(rate_command)
    line=rate.readline()
    rate.close()
    (max_rate, max_ang, max_aw, max_rmin, max_rmax)=string.split(line)
    rmin=float(min(max_rmin, min_rmin))
    rmax=float(max(max_rmax, min_rmax))
    aw=float(max(max_aw,min_aw))
    angle=(float(max_ang)+float(min_ang))/2.0
    rates={'rmin': rmin, 'rmax': rmax, 'angle': angle, 'aw': aw}
    return rates