Skip to content

Instantly share code, notes, and snippets.

@tritemio
Last active August 29, 2015 13:57
Show Gist options
  • Save tritemio/9825667 to your computer and use it in GitHub Desktop.
Save tritemio/9825667 to your computer and use it in GitHub Desktop.
An IPython Notebook that shows how to read the old LabView .sm format for nsALEX and save it in an HDF5 file.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#nsALEX - Old LV format"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load and decode functions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def load_nsalex_sm(fname, magic=184, magic_end=26, debug=False):\n",
" f = open(fname, 'rb')\n",
" \n",
" ## Read the header\n",
" \n",
" # Read all the lines until '*END is found', repeat 3 times\n",
" header = []\n",
" for i in range(3):\n",
" skip = True\n",
" while skip:\n",
" header.append(f.readline())\n",
" if header[-1] == '*END\\r\\n': skip = False\n",
" if debug: print f.tell()\n",
" \n",
" # Read the last part of header\n",
" header.append(f.readline())\n",
" assert header[-1] == '\\r\\n'\n",
" header.append(f.read(magic))\n",
" \n",
" ## Read the data\n",
" buffer = f.read()\n",
" \n",
" custom_dtype = np.dtype([('times', '>u8'), ('nanot', '>u4'), ('det', '>u4')])\n",
" assert np.mod(len(buffer) - magic_end, (8+4+4)) == 0\n",
" \n",
" data = np.ndarray(shape=((len(buffer) - magic_end)/(8+4+4),), \n",
" buffer=buffer, dtype=custom_dtype)\n",
" \n",
" assert (data['nanot'] < 4096).all()\n",
" assert (data['det'] < 8).all()\n",
" return data, header, buffer"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def decode_header_BH(header):\n",
" \n",
" ## Split the header chunks separated by '\\r\\n'\n",
" header_chunks = [[]]\n",
" for line in header:\n",
" if line == '\\r\\n':\n",
" header_chunks.append([])\n",
" continue\n",
" header_chunks[-1].append(line.strip())\n",
" \n",
" ## Make a dictionary of system parameters\n",
" start = False\n",
" sys_para = {}\n",
" for line in header_chunks[1]:\n",
" if line == 'SYS_PARA_BEGIN:':\n",
" start = True\n",
" continue\n",
" if line == 'SYS_PARA_END:':\n",
" break\n",
" if start and line.startswith('#'):\n",
" fields = line[5:-1].split(',')\n",
" \n",
" if fields[1] == 'B':\n",
" value = bool(fields[2])\n",
" elif fields[1] in ['I', 'U', 'L']:\n",
" value = int(fields[2])\n",
" elif fields[1] == 'F':\n",
" value = float(fields[2])\n",
" elif fields[1] == 'S':\n",
" value = fields[2]\n",
" else:\n",
" value = fields[1:]\n",
" \n",
" sys_para[fields[0]] = value\n",
" \n",
" return header_chunks, sys_para "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data load"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fn = \"0m gu 000.sm\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the full file name:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dir_ = r\"C:\\Data\\Antonio\\data\\Eitan/\"\n",
"fname = dir_+fn\n",
"fname"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"'C:\\\\Data\\\\Antonio\\\\data\\\\Eitan/0m gu 000.sm'"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load the measurement:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data, header, buff = load_nsalex_sm(fname)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inside `data` there are 3 arrays of same length: macrotimes (`times`), nanotimes (`nanot`) and detector (`det`):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"times = data['times']\n",
"nanot = data['nanot']\n",
"det = data['det']"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let check the array size (i.e. the number of photons):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data.size"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"10028872"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Data save"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Save the 3 arrays in an HDF5 file:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import h5py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fname_new = fname[:-2] + 'hdf5'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"with h5py.File(fname_new, \"w\") as f:\n",
" f.create_dataset(name='timestamps', data=times)\n",
" f.create_dataset(name='nanotime', data=nanot)\n",
" f.create_dataset(name='detector', data=det.astype('uint8'))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Reload"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just to check, reload the arrays from the HDF5 file:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"with h5py.File(fname_new, \"r\") as f:\n",
" timestamps = f['timestamps'][:]\n",
" nanotime = f['nanotime'][:]\n",
" detector = f['detector'][:]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"timestamps"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"array([ 34991, 36291, 36296, ..., 35842824771,\n",
" 35842833192, 35842841249], dtype=uint64)"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nanotime"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
"array([2137, 1413, 1511, ..., 894, 3055, 1691], dtype=uint32)"
]
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"detector"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
"array([2, 6, 2, ..., 0, 6, 2], dtype=uint8)"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Metadata"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Partial decoding of header infos:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"header_chunks, sys_params = decode_header_BH(header)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 206
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All the system parameters are in the dictionary `sys_params`:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sys_params"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 226,
"text": [
"{'DI_2DC1': True,\n",
" 'DI_2DC1C': 128,\n",
" 'DI_2DC1S': 0,\n",
" 'DI_2DC2': True,\n",
" 'DI_2DC2C': 8388736,\n",
" 'DI_2DC2S': 0,\n",
" 'DI_2DDIS': 0,\n",
" 'DI_2DTRNO': 4,\n",
" 'DI_3DC1C': 128,\n",
" 'DI_3DC2C': 8388736,\n",
" 'DI_3DCOL': 16777215,\n",
" 'DI_3DINCX': 0,\n",
" 'DI_3DMODE': 3,\n",
" 'DI_3DOFFX': 3,\n",
" 'DI_3DOFFY': 1,\n",
" 'DI_3DSTYL': 0,\n",
" 'DI_ASCALE': 0,\n",
" 'DI_BLINE': 0,\n",
" 'DI_BOD_C': 32896,\n",
" 'DI_COL2': 255,\n",
" 'DI_COL3': 65535,\n",
" 'DI_COL4': 65280,\n",
" 'DI_COL5': 16776960,\n",
" 'DI_COLMAX': 16711680,\n",
" 'DI_COLMIN': 0,\n",
" 'DI_COL_NO': 6,\n",
" 'DI_CUR': True,\n",
" 'DI_DETAIL': True,\n",
" 'DI_GCOL_B': 0,\n",
" 'DI_GCOL_F': 8421504,\n",
" 'DI_GRID': True,\n",
" 'DI_HICOL': 16777215,\n",
" 'DI_INTCOL': 1,\n",
" 'DI_INTPIX': 1,\n",
" 'DI_LBLINE': 1,\n",
" 'DI_MAXCNT': 65535,\n",
" 'DI_PFREQ': 1,\n",
" 'DI_PSTYLE': 9,\n",
" 'DI_RATE': True,\n",
" 'DI_SCALE': 1,\n",
" 'DI_SIZE': 0,\n",
" 'DI_SXWIN': 1,\n",
" 'DI_SYWIN': 1,\n",
" 'DI_TRACE': 0,\n",
" 'DI_TWIN': 1,\n",
" 'DI_XREV': True,\n",
" 'DI_XWIN': 1,\n",
" 'DI_YREV': True,\n",
" 'DI_YWIN': 1,\n",
" 'PR_PAUTO': True,\n",
" 'PR_PDEV': 2,\n",
" 'PR_PEJECT': True,\n",
" 'PR_PF': True,\n",
" 'PR_PFNAME': 'C:\\\\USER\\\\TED\\\\DATA\\\\10-11-02\\\\IMAGE.PRT',\n",
" 'PR_PFULL': True,\n",
" 'PR_PHEIGH': 120.0,\n",
" 'PR_PORIEN': 0,\n",
" 'PR_PPORT': 0,\n",
" 'PR_PWHAT': 0,\n",
" 'PR_PWIDTH': 180.0,\n",
" 'PR_SAVE_T': 0,\n",
" 'PR_SLEEP': 15,\n",
" 'PR_STP_FN': 'C:\\\\PROGRAM FILES\\\\BH\\\\SPCM\\\\STP.CFG',\n",
" 'SP_ACCUM': True,\n",
" 'SP_ADC_RE': 4096,\n",
" 'SP_ADC_ZOOM': 0,\n",
" 'SP_ADD_SEL': 0,\n",
" 'SP_ASAVE': 2,\n",
" 'SP_BORD_L': 0,\n",
" 'SP_BORD_U': 0,\n",
" 'SP_CFD_HF': 5.0,\n",
" 'SP_CFD_LH': 80.0,\n",
" 'SP_CFD_LL': -54.901962,\n",
" 'SP_CFD_ZC': 0.0,\n",
" 'SP_COL_T': 100.0,\n",
" 'SP_CYCLES': 1,\n",
" 'SP_DAEC': True,\n",
" 'SP_DAES': True,\n",
" 'SP_DIS_T': 1.0,\n",
" 'SP_DITH': 32,\n",
" 'SP_DLIM': 20480,\n",
" 'SP_DTCOMP': True,\n",
" 'SP_EAL_DE': 0,\n",
" 'SP_EN_DLIM': True,\n",
" 'SP_EN_INT': True,\n",
" 'SP_EPX_DIV': 1,\n",
" 'SP_EXTST': True,\n",
" 'SP_FCYCLES': 1,\n",
" 'SP_FIF_FNO': 1,\n",
" 'SP_FIF_TYP': 1,\n",
" 'SP_FLB_X': 0,\n",
" 'SP_FLB_Y': 0,\n",
" 'SP_INCR': 1,\n",
" 'SP_LDIV': 0,\n",
" 'SP_MEM_BANK': 0,\n",
" 'SP_MODE': 10,\n",
" 'SP_MTCLK': 0,\n",
" 'SP_NCX': 8,\n",
" 'SP_NCY': 1,\n",
" 'SP_OFFSET': 0.0,\n",
" 'SP_OVERFL': ['C', 'N'],\n",
" 'SP_PAGE': 1,\n",
" 'SP_PHOT_F': 50010000,\n",
" 'SP_PH_BUF': 20480,\n",
" 'SP_PIX': 0,\n",
" 'SP_PIX_CLK': 0,\n",
" 'SP_PIX_T': 2e-07,\n",
" 'SP_POL_F': 0,\n",
" 'SP_POL_L': 0,\n",
" 'SP_POL_P': 0,\n",
" 'SP_REPEAT': True,\n",
" 'SP_REP_T': 36000.0,\n",
" 'SP_ROUT': True,\n",
" 'SP_SCAN_RX': 1,\n",
" 'SP_SCAN_RY': 1,\n",
" 'SP_SCAN_X': 32,\n",
" 'SP_SCAN_Y': 32,\n",
" 'SP_SCF': 0,\n",
" 'SP_SPE_FN': 'c:\\\\user\\\\xiangxu\\\\data\\\\08_11_04.ci2.1_53+10.g120r60.4ns\\\\0m gu 000.spc',\n",
" 'SP_STEPS': 1,\n",
" 'SP_STOPT': True,\n",
" 'SP_SXWIN_N': 8,\n",
" 'SP_SX_EQU': True,\n",
" 'SP_SYN_FD': 4,\n",
" 'SP_SYN_FQ': -100.0,\n",
" 'SP_SYN_HF': 4.0,\n",
" 'SP_SYN_ZC': 0.0,\n",
" 'SP_SYWIN_N': 8,\n",
" 'SP_SY_EQU': True,\n",
" 'SP_TAC_EH': 10.0,\n",
" 'SP_TAC_G': 1,\n",
" 'SP_TAC_LH': 100.0,\n",
" 'SP_TAC_LL': 0.0,\n",
" 'SP_TAC_OF': 0.0,\n",
" 'SP_TAC_R': 7.0054369e-08,\n",
" 'SP_TAC_TC': 1.7103118e-11,\n",
" 'SP_TAC_TD': 7.0054371e-09,\n",
" 'SP_TRIGGER': 0,\n",
" 'SP_TWIN_N': 8,\n",
" 'SP_T_EQU': True,\n",
" 'SP_WL_STA': 1.0,\n",
" 'SP_WL_STE': 1.0,\n",
" 'SP_WL_STO': 128.0,\n",
" 'SP_XWIN_N': 8,\n",
" 'SP_X_EQU': True,\n",
" 'SP_YWIN_N': 8,\n",
" 'SP_Y_EQU': True}"
]
}
],
"prompt_number": 226
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The dictionary can be filtered as usual:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for k, v in sys_params.iteritems():\n",
" if 'TAC' in k:\n",
" print k, v"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"SP_TAC_G 1\n",
"SP_TAC_R 7.0054369e-08\n",
"SP_TAC_LH 100.0\n",
"SP_TAC_LL 0.0\n",
"SP_TAC_EH 10.0\n",
"SP_TAC_TC 1.7103118e-11\n",
"SP_TAC_TD 7.0054371e-09\n",
"SP_TAC_OF 0.0\n"
]
}
],
"prompt_number": 220
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"filter(lambda item: 'TAC' in item[0], sys_params.iteritems())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 221,
"text": [
"[('SP_TAC_G', 1),\n",
" ('SP_TAC_R', 7.0054369e-08),\n",
" ('SP_TAC_LH', 100.0),\n",
" ('SP_TAC_LL', 0.0),\n",
" ('SP_TAC_EH', 10.0),\n",
" ('SP_TAC_TC', 1.7103118e-11),\n",
" ('SP_TAC_TD', 7.0054371e-09),\n",
" ('SP_TAC_OF', 0.0)]"
]
}
],
"prompt_number": 221
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import HTML\n",
"HTML(open(\"./styles/custom2.css\", \"r\").read())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<script src=\"http://use.edgefonts.net/source-code-pro.js\"></script>\n",
"\n",
"<style>\n",
"\t/* Import the \"Computer Modern\" font */\n",
" @font-face {\n",
" font-family: \"Computer Modern\";\n",
" src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
" }\n",
"\t\n",
"\t/* Style for all the cells */\n",
"\t/*div.cell{\n",
" width:900px;\n",
" margin-left:8% !important;\n",
" margin-right:auto;\n",
" }*/\n",
"\n",
"\t/* Text in markdown cells */\n",
" div.text_cell_render{\n",
" font-family: Computer Modern, Liberation Sans, Arial, Helvetica, sans-serif;\n",
" line-height: 130%;\n",
" font-size: 120%;\n",
" /* width:800px;\n",
" margin-left:auto;\n",
" margin-right:auto;*/\n",
" }\n",
"\n",
"\t/* Quoted text in markdown cells */\n",
"\t/*blockquote p {\n",
"\tmargin: 1em 2em;\n",
"\tline-height: 140%;\n",
" }*/\n",
"\n",
"\t/* Input and output prompt (In[1], Out[1], ...) */\n",
"\tdiv.prompt{\n",
"\t\tfont-family: \"Source Code Pro\", source-code-pro, Computer Modern, Consolas, monospace;\n",
"\t}\n",
"\n",
"\t/* Output text (i.e. output of print or text display) */\n",
"\tdiv.output_area pre{\n",
"\t\tfont-family: \"Source Code Pro\", source-code-pro, Computer Modern, Consolas, monospace;\n",
"\t}\n",
"\n",
"\t/* Text in code markup, within markdown cells */\n",
" .rendered_html code {\n",
"\tfont-family: \"Source Code Pro\", source-code-pro, Computer Modern, Consolas, monospace;\n",
"\tbackground-color: #f7f7f7;\n",
"\tborder: 1px solid #dddddd;\n",
"\tcolor: black;\n",
"\tline-height: 1.1em;\n",
"\tpadding: 0.15em;\n",
"\tfont-size: 80%;\n",
" }\n",
"\n",
"\t/* Text in code cells */\n",
" .CodeMirror{\n",
" font-family: \"Source Code Pro\", source-code-pro, Consolas, monospace;\n",
" }\n",
"\t\n",
" /* Whether to displays In[] and Out[] prompt */\n",
"\t/*.prompt{\n",
" display: None;\n",
" }*/\n",
" \n",
"</style>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 224,
"text": [
"<IPython.core.display.HTML at 0xddc95c0>"
]
}
],
"prompt_number": 224
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment