Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save taldcroft/a13b670ab15db5684f49 to your computer and use it in GitHub Desktop.

Select an option

Save taldcroft/a13b670ab15db5684f49 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:ef1ab81b589158bbde596a2e5ff48379a882e322da548c902ce325289beca160"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from astropy.io import ascii\n",
"from astropy.table import Table\n",
"from collections import OrderedDict\n",
"import astropy.units as u\n",
"import numpy as np\n",
"from StringIO import StringIO"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ascii.Dtif"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"astropy.io.ascii.dtif.Dtif"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t = Table([[1], [2]], names=['a', 'b'], dtype=(np.float32, np.uint8))\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table id=\"table4393912016\"><thead><tr><th>a</th><th>b</th></tr></thead><tr><td>1.0</td><td>2</td></tr></table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"<Table rows=1 names=('a','b')>\n",
"array([(1.0, 2)], \n",
" dtype=[('a', '<f4'), ('b', 'u1')])"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fh = StringIO()\n",
"ascii.write(t, fh, format='dtif')\n",
"print(fh.getvalue())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"# %DTIF-1.0\n",
"# columns:\n",
"# - {name: a, type: float32}\n",
"# - {name: b, type: uint8}\n",
"a b\n",
"1.0 2\n",
"\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t_r = ascii.read(fh.getvalue(), format='dtif', guess=False)\n",
"t_r"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table id=\"table4356018000\"><thead><tr><th>a</th><th>b</th></tr></thead><tr><td>1.0</td><td>2</td></tr></table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"<Table rows=1 names=('a','b')>\n",
"array([(1.0, 2)], \n",
" dtype=[('a', '<f4'), ('b', 'u1')])"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t_r.dtype"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"dtype([('a', '<f4'), ('b', 'u1')])"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t2 = t.copy()\n",
"t2.meta['keywords'] = OrderedDict([('z_key1', 'val1'), ('a_key2', 'val2')])\n",
"t2.meta['comments'] = ['Comment 1', 'Comment 2', 'Comment 3', 'Long comment ,' * 10]\n",
"t2['a'].unit = u.m / u.s\n",
"t2['a'].format = '%5.2f'\n",
"t2['a'].description = 'Column A'\n",
"t2['b'].meta = dict(column_meta={'a':1, 'b': 2})"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table id=\"table4396836368\"><thead><tr><th>a</th><th>b</th></tr></thead><thead><tr><th>m / s</th><th></th></tr></thead><tr><td>1.00</td><td>2</td></tr></table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"<Table rows=1 names=('a','b') units=('m / s',None)>\n",
"array([(1.0, 2)], \n",
" dtype=[('a', '<f4'), ('b', 'u1')])"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fh = StringIO()\n",
"ascii.write(t2, fh, format='dtif')\n",
"print(fh.getvalue())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"# %DTIF-1.0\n",
"# columns:\n",
"# - {name: a, unit: m / s, type: float32, format: '%5.2f', description: Column A}\n",
"# - name: b\n",
"# type: uint8\n",
"# meta:\n",
"# column_meta: {a: 1, b: 2}\n",
"# table_meta: !!omap\n",
"# - keywords: !!omap\n",
"# - {z_key1: val1}\n",
"# - {a_key2: val2}\n",
"# - comments: [Comment 1, Comment 2, Comment 3, 'Long comment ,Long comment ,Long comment\n",
"# ,Long comment ,Long comment ,Long comment ,Long comment ,Long comment ,Long\n",
"# comment ,Long comment ,']\n",
"a b\n",
"1.00 2\n",
"\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t2_r = ascii.read(fh.getvalue(), format='dtif')\n",
"t2_r['b'].meta"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"OrderedDict([('column_meta', {'a': 1, 'b': 2})])"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t2_r"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<table id=\"table4396834960\"><thead><tr><th>a</th><th>b</th></tr></thead><thead><tr><th>m / s</th><th></th></tr></thead><tr><td>1.00</td><td>2</td></tr></table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"<Table rows=1 names=('a','b') units=('m / s',None)>\n",
"array([(1.0, 2)], \n",
" dtype=[('a', '<f4'), ('b', 'u1')])"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t2_r.meta"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"OrderedDict([('keywords', OrderedDict([('z_key1', 'val1'), ('a_key2', 'val2')])), ('comments', ['Comment 1', 'Comment 2', 'Comment 3', 'Long comment ,Long comment ,Long comment ,Long comment ,Long comment ,Long comment ,Long comment ,Long comment ,Long comment ,Long comment ,'])])"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment