Last active
September 16, 2015 21:34
-
-
Save j6k4m8/467e4036635207aeb047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Requires ocpy, h5py, PIL (all but PIL are pip-installable) | |
""" | |
import ocpy.access.download | |
from ocpy.convert import png | |
from ocpy.access.Request import * | |
import os, glob | |
import h5py | |
def download_token_and_convert_to_png(tk): | |
DATA_LOCATION = tk | |
starting_directory = os.getcwd() | |
# Save downloaded files to the {{token}}/ directory | |
downloaded_files, failed_files = \ | |
ocpy.access.download.get_data( | |
token=tk, channel='image', resolution=0, | |
x_start=0, x_stop=8192, | |
y_start=0, y_stop=8192, | |
z_start=0, z_stop=194, | |
location=DATA_LOCATION) | |
# Write error log | |
with open(tk + '/errors', 'a') as f: | |
for fail in failed_files: | |
f.write(fail) | |
os.mkdir(DATA_LOCATION + "/png") | |
os.chdir(DATA_LOCATION + "/hdf5") | |
for filename in glob.glob("*.hdf5"): | |
# First get the actual parameters from the HDF5 file. | |
req = Request(filename) | |
i = int(req.z_start) | |
print("Slicing " + filename) | |
f = h5py.File(filename, "r") | |
# OCP stores data inside the 'cutout' h5 dataset | |
data_layers = f.get('CUTOUT') | |
out_files = [] | |
for layer in data_layers: | |
# Filename is formatted like the request URL but `/` is `-` | |
png_file = filename + "." + str(i).zfill(6) + ".png" | |
out_files.append( | |
png.export_png("../png/" + png_file, layer)) | |
i += 1 | |
# if you want, you have access to the out_files array here. | |
os.chdir(starting_directory) | |
data = png.import_png_collection("png/*.png") | |
hdf5.export(data, "volume.h5") | |
download_token_and_convert_to_png('kharris15apical') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment