{"nbformat_minor": 1, "nbformat": 4, "metadata": {"kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "language_info": {"name": "python", "version": "3.6.1", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "file_extension": ".py", "codemirror_mode": {"name": "ipython", "version": 3}, "mimetype": "text/x-python"}}, "cells": [{"source": ["These are (unofficial) functions to read and view the competition files. Most of the information in the file header is identical across scans and thus not relevant to the competition."], "cell_type": "markdown", "metadata": {"_execution_state": "idle", "_cell_guid": "86c3aef9-19ab-4720-b5b8-5b58b292a3ee", "_uuid": "a49a92d4732cae422c49c40cd5fc269dda1397f4"}}, {"source": ["## Read header"], "cell_type": "markdown", "metadata": {"_execution_state": "idle", "_cell_guid": "e69d0f57-8a32-4bfd-b505-2a29e6404f7d", "_uuid": "87d8411dc21ac9edf0fc0f0dbc0f037fd3c9f34a"}}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true, "_cell_guid": "b669e9d4-389e-471b-a26e-7b9f59724af4", "_uuid": "d2bbd58d7b33d594459aa6bbb8fc4147668a1ee1"}, "source": ["import numpy as np\n", "import os\n", "import matplotlib\n", "\n", "def read_header(infile):\n", "    \"\"\"Read image header (first 512 bytes)\n", "    \"\"\"\n", "    h = dict()\n", "    fid = open(infile, 'r+b')\n", "    h['filename'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 20))\n", "    h['parent_filename'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 20))\n", "    h['comments1'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 80))\n", "    h['comments2'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 80))\n", "    h['energy_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['config_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['file_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['trans_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['scan_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['data_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['date_modified'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 16))\n", "    h['frequency'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['mat_velocity'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['num_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n", "    h['num_polarization_channels'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['spare00'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['adc_min_voltage'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['adc_max_voltage'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['band_width'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['spare01'] = np.fromfile(fid, dtype = np.int16, count = 5)\n", "    h['polarization_type'] = np.fromfile(fid, dtype = np.int16, count = 4)\n", "    h['record_header_size'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['word_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['word_precision'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['min_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['max_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['avg_data_value'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['data_scale_factor'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['data_units'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['surf_removal'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n", "    h['edge_weighting'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n", "    h['x_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n", "    h['y_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n", "    h['z_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n", "    h['t_units'] = np.fromfile(fid, dtype = np.uint16, count = 1)\n", "    h['spare02'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['x_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_return_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['scan_orientation'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['scan_direction'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['data_storage_order'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['scanner_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['x_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['t_inc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['num_x_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n", "    h['num_y_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n", "    h['num_z_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n", "    h['num_t_pts'] = np.fromfile(fid, dtype = np.int32, count = 1)\n", "    h['x_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_speed'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['x_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_acc'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['x_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_motor_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['x_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_encoder_res'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['date_processed'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 8))\n", "    h['time_processed'] = b''.join(np.fromfile(fid, dtype = 'S1', count = 8))\n", "    h['depth_recon'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['x_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['elevation_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['roll_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_max_travel'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['azimuth_offset_angle'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['adc_type'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['spare06'] = np.fromfile(fid, dtype = np.int16, count = 1)\n", "    h['scanner_radius'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['x_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['y_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['z_offset'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['t_delay'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['range_gate_start'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['range_gate_end'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['ahis_software_version'] = np.fromfile(fid, dtype = np.float32, count = 1)\n", "    h['spare_end'] = np.fromfile(fid, dtype = np.float32, count = 10)\n", "    return h"]}, {"source": ["## Read image data"], "cell_type": "markdown", "metadata": {"_execution_state": "idle", "_cell_guid": "32371ee0-c472-4247-8ab7-bb5b12c164ce", "_uuid": "81bc02741351c9f640d41ab18b60663721e82246"}}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_execution_state": "idle", "_cell_guid": "220f1039-77ee-4d0a-a7ce-a340f23f961b", "_uuid": "c27f70b9e82bfb7de2329bffb79965586f126d22"}, "source": ["def read_data(infile):\n", "    \"\"\"Read any of the 4 types of image files, returns a numpy array of the image contents\n", "    \"\"\"\n", "    extension = os.path.splitext(infile)[1]\n", "    h = read_header(infile)\n", "    nx = int(h['num_x_pts'])\n", "    ny = int(h['num_y_pts'])\n", "    nt = int(h['num_t_pts'])\n", "    fid = open(infile, 'rb')\n", "    fid.seek(512) #skip header\n", "    if extension == '.aps' or extension == '.a3daps':\n", "        if(h['word_type']==7): #float32\n", "            data = np.fromfile(fid, dtype = np.float32, count = nx * ny * nt)\n", "        elif(h['word_type']==4): #uint16\n", "            data = np.fromfile(fid, dtype = np.uint16, count = nx * ny * nt)\n", "        data = data * h['data_scale_factor'] #scaling factor\n", "        data = data.reshape(nx, ny, nt, order='F').copy() #make N-d image\n", "    elif extension == '.a3d':\n", "        if(h['word_type']==7): #float32\n", "            data = np.fromfile(fid, dtype = np.float32, count = nx * ny * nt)\n", "        elif(h['word_type']==4): #uint16\n", "            data = np.fromfile(fid, dtype = np.uint16, count = nx * ny * nt)\n", "        data = data * h['data_scale_factor'] #scaling factor\n", "        data = data.reshape(nx, nt, ny, order='F').copy() #make N-d image\n", "    elif extension == '.ahi':\n", "        data = np.fromfile(fid, dtype = np.float32, count = 2* nx * ny * nt)\n", "        data = data.reshape(2, ny, nx, nt, order='F').copy()\n", "        real = data[0,:,:,:].copy()\n", "        imag = data[1,:,:,:].copy()\n", "    fid.close()\n", "    if extension != '.ahi':\n", "        return data\n", "    else:\n", "        return real, imag"]}, {"source": ["## Example plotting function: .aps file animation"], "cell_type": "markdown", "metadata": {"_execution_state": "idle", "_cell_guid": "3542ab26-0712-4caf-9e6b-d88c9e27d925", "_uuid": "d9856629c0f9df190227e19f6d2352571b24d060"}}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"_execution_state": "idle", "_cell_guid": "8adccb4c-fbdb-4285-a00d-50b9dd710990", "_uuid": "c09380d28fe20fe9d6b0a2af82cea9f9323b28df"}, "source": ["def save_images(path):\n", "    savepath= os.path.splitext('sample/00360f79fd6e02781457eda48f85da90.aps')[0]+'/'\n", "    if os.path.exists(savepath)==False:\n", "        os.mkdir(savepath)\n", "    data = read_data(path)\n", "    for i in range(data.shape[-1]):\n", "        img = np.flipud(data[:,:,i].transpose())\n", "        matplotlib.pyplot.imsave(savepath+str(i)+'.png',img)"]}, {"outputs": [], "execution_count": null, "cell_type": "code", "metadata": {"collapsed": true}, "source": ["save_images('sample/00360f79fd6e02781457eda48f85da90.aps')"]}]}