Skip to content

Instantly share code, notes, and snippets.

@embray
Created December 7, 2012 18:12
Show Gist options
  • Save embray/4235177 to your computer and use it in GitHub Desktop.
Save embray/4235177 to your computer and use it in GitHub Desktop.
update_card_with_long_comment() PyFITS example
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "long_comment"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import pyfits\n",
"header = pyfits.Header()\n",
"\n",
"\n",
"def update_card_with_long_comment(header, keyword, value, comment):\n",
" \"\"\"\n",
" Append/update a keyword in the given header such that if the comment\n",
" is too long to fit in a single card, instead append the comment in a\n",
" subsequent card with a blank keyword.\n",
" \"\"\"\n",
" \n",
" # Go ahead and add a keyword to the header *without* the comment\n",
" header[keyword] = value\n",
" \n",
" # Now we use the header.cards accessor to get the card object itself\n",
" # and find out how long it is\n",
" # The reason for the .rstrip() is to remove trailing spaces; otherwise\n",
" # the length will always be reported as 80\n",
" card = header.cards[keyword]\n",
" cardlen = len(str(card).rstrip())\n",
"\n",
" # We need len(comment) + 3 spaces left in the card.\n",
" # The +3 is for ' / '\n",
" if pyfits.Card.length - (cardlen + 3) >= len(comment):\n",
" card.comment = comment\n",
" else:\n",
" # A single blank card can only hold up to 72 characters of comment\n",
" # text, so split the comment into 72 character chunks\n",
" idx = header.index(keyword)\n",
" while comment:\n",
" header.set('', comment[:72], after=idx)\n",
" comment = comment[72:]\n",
" idx += 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# First test a keyword with a short comment to see that it works normally\n",
"update_card_with_long_comment(header, 'TEST1', 'VALUE1', 'A short comment')\n",
"header"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 2,
"text": [
"TEST1 = 'VALUE1 ' / A short comment "
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Now let's test it out with a keyword that has a long comment\n",
"update_card_with_long_comment(header, 'TEST2', 'VALUE2', 'Long comment ' * 5)\n",
"header"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 3,
"text": [
"TEST1 = 'VALUE1 ' / A short comment \n",
"TEST2 = 'VALUE2 ' \n",
" Long comment Long comment Long comment Long comment Long comment "
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Really long comment\n",
"update_card_with_long_comment(header, 'TEST3', 'VALUE3', 'Long comment ' * 20)\n",
"header"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": [
"TEST1 = 'VALUE1 ' / A short comment \n",
"TEST2 = 'VALUE2 ' \n",
"TEST3 = 'VALUE3 ' \n",
" Long comment Long comment Long comment Long comment Long comment Long co\n",
" mment Long comment Long comment Long comment Long comment Long comment L\n",
" ong comment Long comment Long comment Long comment Long comment Long com\n",
" ment Long comment Long comment Long comment \n",
" Long comment Long comment Long comment Long comment Long comment "
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# One shortcoming of this is that there is now no way to be certain\n",
"# that the blank keyword after TEST2 is actually tied to TEST2, so if\n",
"# we call\n",
"\n",
"# update_card_with_long_comment(header, 'TEST2', 'VALUE2', 'Long comment ' * 5)\n",
"\n",
"# again the 'Long comment' card will be duplicated. There's no perfect way\n",
"# around that I don't think. But if you're just writing the header once this\n",
"# shouldn't be a problem."
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment