def list_targets(hit, target_no=1, limit=-1, adc_path=None, stitch_targets=None): if stitch_targets is None: stitch_targets = app.config[STITCH] if adc_path is None: adc_path = resolve_adc(hit.bin_pid) targets = read_targets(adc_path, target_no, limit, hit.schema_version) if stitch_targets: targets = list_stitched_targets(targets) targets = add_bin_pid(targets, hit.bin_pid) return targets
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