Python for PDS, FITS, etc.By Bill Allen
|
Bippy PullSDTS Python Notes Tkinter Notes File Formats SDTS Notes Glossary Index out of date. |
First, a note about Bippy: It doesn't exist yet in a form suitable for public distribution, but it is planned to display and convert FITS, PDS, and Wavefront OBJ file formats using only Python 2+ and its Tkinter package, and all in original code that isn't bound by any copyright or copyleft (GNU) restrictions. Its components have been in long use by the author in several unpublished alpha versions.
Ahead of a full beta Bippy complete with a Tkinter GUI, the page you are reading has been posted beginning 22 October 2004 with a few first modules needed to handle NASA Planetary Data System (PDS) image files. This will be mainly of interest to experienced Python users who want to import PDS files into their own scripts. A great first example of such usage is Daniel Crotty's MER Multispectral Color Imagery page. He spotted a reference to Python PDS handling on the Asteroid/Comet Connection's June 2003 Stardust 5535 Annefrank Flyby page and asked the author if the code was available for others to use. And so the related modules were put into publishable form with his testing and feedback, and with additional testing by Cecilia Ziemer. Thanks to both for their help and enthusiasm.
You can get a taste of how Bippy will handle OBJ files (commonly used for asteroid shape models) by looking at the "Tkinter 3D" page on this site. And results from the alpha FITS code have been used at A/CC to create many Major News About Minor Objects "cover" images, such as those of September 26th and July 29th. (All A/CC links given on this page are local to the A/CC backup site. The primary site's news page is here.)
As provided, the pds.py PDS module also requires the raster.py raster image data module, and makes use of the common.py module.
Usage: parsePDS(pn,fn='',keephead=0,showpad=0,verbose=0,vals=[],headonly=0)
pn = path name (directory/folder with or without file name)
fn = file name (must be blank if file name is part of path)
keephead = 1 to store the full header in the returned object
showpad = 1 to keep image data padding
verbose = 1 to report script progress
vals = [] to return no header values, ['all'] for all, or ['name1','name2'] values
headonly = 1 to return header values but no image data, use query file info
#!/usr/bin/python2
fileName = 'pds.img' # <<- put your file's name here
dirName = 'c:\\pds-files' # & the directory where it is located
print '\nSimple Bippy PDS module test...'
import os, sys
thisDir = os.path.split(sys.argv[0])[0]
if not thisDir in sys.path: sys.path.append(thisDir)
import common, pds
pname = common.fixPath(dirName,fileName)
if os.path.exists(pname):
headObj,msg = pds.parsePDS(pn=pname,vals=['TARGET_NAME'],headonly=1)
if msg: print ' headObj msg:',msg
if headObj:
print ' file',pname,'target name =',headObj.values['TARGET_NAME']
rasObj,msg = pds.parsePDS(pn=pname)
if msg: print ' rasObj msg:',msg
if rasObj:
pn = pname[:-3]+'raw'
f = open(pn,'wb') #write binary!
rasObj.array().tofile(f) #write data to headerless RAW file in same dir
f.flush()
f.close()
print ' saved',pn
else: print ' ',pname,'not found'
print ' Done'
What these modules can do: The PDS module simply loads header information and/or image data into an object which can be queried for header details and which can be used to pass the image data array to another script, such as to the third-party Python Imaging Library (PIL). It also can be used to write out to 8- or 16-bit RAW or 8-bit PGM (portable graphics grayscale) image file formats, which can then be read by some graphics applications (12-bit PDS can be saved as 16-bit RAW). The sample test-pds-tk.py script illustrates the basics of using Python Tkinter to display image files and to save them to 8-bit GIF format.
Note: It is the responsibility of the calling script to determine that a file path exists before passing that path to pds.py to open.
Platform issues: These modules and test scripts have been tested on Windows ME and XP and on Mac OS9.
Mac OS9 users may need to File/Open newly downloaded Python scripts in the Python IDE and save them to get Mac-style line endings that will allow drag-and drop. To be double-clickable in Mac OS9 requires changing a script's file type to "TEXT" and creator code to "Pyth." A utility such as ResEdit or BBedit may be helpful, and the Mac Python distribution comes with a fixfiletypes.py script.
If you encounter a platform issue or other problem, please tell the the author, but do NOT attach files to E-mail without checking first.
Although this entire page, accompanying pages, and their presentation are ©Copyright by Columbine, Inc. with all rights reserved, the Python modules and test scripts linked here are hereby put into the public domain without limitation. These modules consist only of original code written by Bill Allen, and as an informational public service are provided without warranty; use them at your own risk. The author may not be able to answer all E-mail, but will endeavor to investigate and fix reported problems. Acknowledgement and a link back would be appreciated if you use this code (or a derivative in any programming language) in a project or product.
These files must be all in the same folder.
| File | Dated | Purpose & notes | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Files that MUST be in the folder:
Other files you may find in the same folder (leave them): *.pyc - "compiled" modules (e.g., pds.pyc) created by Python to boot faster temp folder - used by test-pds-tk.py to hold its temporary files | ||||||||||||||||||||