def gen_targets(): (prev_bottom, prev_left, prev_trigger) = (None, None, None) (pairs, colocated_pairs) = (0, 0) for target in read_adc(adc_source, schema_version=schema_version): (bottom, left, trigger) = (target[BOTTOM], target[LEFT], target[TRIGGER]) if trigger == prev_trigger: pairs += 1 if (bottom, left, trigger) == (prev_bottom, prev_left, prev_trigger): colocated_pairs += 1 (prev_bottom, prev_left, prev_trigger) = (bottom, left, trigger) yield target if pairs > 0 and pairs == colocated_pairs: raise IntegrityException('.adc stitching problem')
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
def read_targets(adc_path, target_no=1, limit=-1, schema_version=SCHEMA_VERSION_2): return list(read_adc(LocalFileSource(adc_path), target_no, limit, schema_version=schema_version))