示例#1
0
文件: webapi.py 项目: LouisK130/oii
def serve_bin(hit,mimetype):
    """Serve a sample bin in some format"""
    # for raw files, simply pass the file through
    if hit.extension == ADC:
        return Response(file(resolve_adc(hit.bin_pid)), direct_passthrough=True, mimetype='text/csv')
    elif hit.extension == ROI:
        return Response(file(resolve_roi(hit.bin_pid)), direct_passthrough=True, mimetype='application/octet-stream')
    # at this point we need to resolve the HDR file
    hdr_path = resolve_hdr(hit.bin_pid)
    if hit.extension == HDR:
        return Response(file(hdr_path), direct_passthrough=True, mimetype='text/plain')
    props = read_hdr(LocalFileSource(hdr_path))
    bin_props = get_bin_props(hit.time_series).get_props(hit.bin_lid)
    context = props[CONTEXT]
    del props[CONTEXT]
    # sort properties according to their order in the header schema
    props = [(k,props[k]) for k,_ in HDR_SCHEMA if k in props]
    # then add database props
    props += [(k,bin_props[k]) for k in sorted(bin_props.keys())]
    # get a list of all targets, taking into account stitching
    if hit.product != 'short':
        targets = list_targets(hit)
        target_pids = ['%s_%05d' % (hit.bin_pid, target['targetNumber']) for target in targets]
    else:
        targets = []
        target_pids = []
    template = dict(hit=hit,context=context,properties=props,targets=targets,target_pids=target_pids,static=app.config[STATIC])
    if minor_type(mimetype) == 'xml':
        return template_response('bin.xml', **template)
    elif minor_type(mimetype) == 'rdf+xml':
        return template_response('bin.rdf', mimetype='text/xml', **template)
    elif minor_type(mimetype) == 'csv':
        return bin2csv_response(hit, targets)
    elif mimetype == 'text/html':
        return template_response('bin.html', **template)
    elif mimetype == 'application/json':
        properties = dict(props)
        properties['context'] = context
        properties['targets'] = targets
        properties['date'] = hit.date
        if hit.product == 'short':
            del properties['targets']
        if hit.product == 'medium':
            properties['targets'] = target_pids
        return jsonr(properties)
    elif mimetype == 'application/zip':
        return Response(bin_zip(hit,targets,template), mimetype=mimetype, headers=max_age())
    else:
        abort(404)
示例#2
0
def bin_zip(hit, hdr_path, adc_path, roi_path, outfile):
    """hit should be the hit on the mvco resolver for pid=bin_pid on the 'pid' resolver.
    outfile must be a filelike object open for writing, not a pathname. StringIO will work"""
    props = read_hdr(LocalFileSource(hdr_path))
    context = props[CONTEXT]
    del props[CONTEXT]
    props = [(k,props[k]) for k,_ in HDR_SCHEMA if k in props]

    raw_targets = list(read_adc(LocalFileSource(adc_path), 1, -1, hit.schema_version))
    raw_targets = add_bin_pid(raw_targets, hit.bin_pid)
    stitched_targets = deepcopy(raw_targets)
    stitched_targets = list_stitched_targets(stitched_targets)
    stitched_targets = add_bin_pid(stitched_targets, hit.bin_pid)
    target_pids = ['%s_%05d' % (hit.bin_pid, target['targetNumber']) for target in stitched_targets]
    template = dict(hit=hit,context=context,properties=props,targets=stitched_targets,target_pids=target_pids)

    with tempfile.SpooledTemporaryFile() as temp:
        z = ZipFile(temp,'w',ZIP_DEFLATED)
        csv_out = '\n'.join(bin2csv(stitched_targets, hit.schema_version))
        z.writestr(hit.bin_lid + '.csv', csv_out)
        # xml as well, including header info
        z.writestr(hit.bin_lid + '.xml', bin2xml(template))
        pairs = list(find_pairs(raw_targets))
        with open(roi_path,'rb') as roi_file:
            for target in stitched_targets:
                im = None
                for (a,b) in pairs:
                    if a[TARGET_NUMBER] == target[TARGET_NUMBER]:
                        images = list(read_rois((a,b),roi_file=roi_file)) # read the images
                        (im, _) = stitch((a,b), images) # stitch them
                if im is None:
                    im = list(read_rois([target],roi_file=roi_file))[0] # read the image
                # need LID here
                target_lid = re.sub(r'.*/','',target[PID]) # FIXME resolver should do this
                z.writestr(target_lid + '.png', im2bytes(im))
        z.close()
        temp.seek(0)
        shutil.copyfileobj(temp, outfile)

    return stitched_targets
示例#3
0
def check_hdr(hdr_source):
    try:
        read_hdr(hdr_source)
    except Exception, e:
        raise IntegrityException('.hdr failed: ' + str(e)), None, sys.exc_info()[2]