Reply to topic  [ 1 post ] 
Fits file handler with Oxy objects 
Author Message
Yorick Guru

Joined: Thu May 10, 2007 12:07 pm
Posts: 62
Post Fits file handler with Oxy objects
Hi folks,

I created functions to fill my need : reading (and writing) fits file object easely for quick and in live analysis.
I use oxy objects for that (So yorick 2.2 is required instead of list in E.thiebaut's fits.i). The package allow to:
- read / write fits object ( data array, binary tables, ascii table)
- handle groups of fits file.
- walk and execute any command on HDUs and group of fits files in a very few command lines, no need to write loops.

This is a beta version, I just wrote very very quickly function documentation, they need to be deeply clarified. If somebody understand my writing and is interested by the package he is welcome to contribute in any-way to improve documentation and performances.

The file is there:

The fits object, I have called "ofits", have all the methods you need for basic operations (get/set keywords, write data, walk trough HDUs, ... ).

I put bellow a few examples on what it can do:
> fh = fits_open( "my-fits-file.fits" )
> fh, show, 1:4    // will show the first 4 lines of the hdu header  fh, show (shows all the header)
-----------------------------------> hdu  1 <-----------------------------------
SIMPLE  =                    T / file does conform to FITS standard             
BITPIX  =                   16 / number of bits per data pixel                 
NAXIS   =                    0 / number of data axes                           
EXTEND  =                    T / FITS Extension may be present                 
> fh, next   // go to the next hdu return hdu number if successfull or 0 if failed)
> fh, prev,  // go to the previous hdu
> fh ( hdu, 2) // go to the hdu number 2  (will return 2 if succesfull  or 0 if failed)
> fh ( numberofhdu, )  //return the number of HDUs in the file 
> fh, show , "NAXIS*"       // show the header for NAXIS*  value
-----------------------------------> hdu  2 <-----------------------------------
NAXIS   =                    2 / number of data axes                           
NAXIS1  =                  354 / width of table in bytes                       
NAXIS2  =                   53 / number of rows in table                       
> fh(keys , "NAXIS*")  // return the keys contening the glo pattern NAXIS*   
> fhw = fh(copy, "new_file.fits")   // make a editable copy of fh
> fh, help  // get general help
> fh, help, get   // get help on the method get

Keywords are not key sensitive and can be yorick symbol (like when one get a member of oxy object):
> fh  (get, object);
> fh (get, "ObJect");
> k = "OBJECT";  fh(get, noop(k));

To get the data:
> data = fh (data, );   // will get all the data
> subdata = fh ( data, 1:100, 100:300) // is equivalent to fh(data, )(1:100, 100:300) //except that a small portion of data is red an loaded in Yorick memory  usefull for large 2d arrays
> c = fh(cube,  [34,45,189], 10, [156, 52, 87], 5 )  // will return a "cube" containing portion the data, of dimension 3x(10*2+1)x(5*2+1)  (e.i. in 2d finding chart of 21x11 pixel  centered at x=[34,45,189], y = [156, 52, 87]

To create a new fits:

> fhw = fits_new();
>  fhw = fits_new( simple=1, bitpix=float, naxis=2, naxis1=100, naxis2=200);
-equivalent to-
> fhw = fits_new( "SIMPLE", 'T', "BITPIX", -32, "NAXIS", 2, "NAXIS1", 100, "NAXIS2", 200);
> fhw = fh_template( new,  "comment", "fits header derived from fh_template");  // where fh_template is an other ofits object, the header of fh_template (curent hdu) will be copied  to the new ofits
> fhw, file, "~/target-file-path.fits"   // set the target file of the writable object
> fhw, newhdu     // create a new hdu and go there
> fhw, newhdu , fh_template  // create a new hdu and copy the header of fh_template
> fhw, newhdu, fhw   // will duplicate (only the header) the curent hdu in a new one

> fhw, setdata, float(random(100,200))    // set the data
> fhw, setdata, random( 800, 800) , force=1   // set the data and force to (re-)write silently the header (xtension, bitpix and Naxis#) if needed
> fhw, write    //  write the curent hdu (in the file handler, previous hdu must have been writen before )  so fhw can be discarded securely   fhw=[]
> fits_close, fhw  // close all hdus and write the file. 

Method like walkh or walka allow to execute methods by looping on HDUs or arrays:
> dim = fh (walka ,  get, ["NAXIS", "NAXIS1", "NAXIS2"])  // we walk on all external dimension of array arguments and execute the get method
- will replace all these commands -
> dim = array( int,3) ;  dim(1)= fh(get, "NAXIS"); dim(2) = fh( get, "NAXIS1") ; dim(3)=fh(get, "NAXIS2")
- it works with any methods :
> fhw, walka, set, "NAXIS"+["", "1", "2"],  dimsof( my_data)

> fh( walkh,  get, "NAXIS")   //  will walk through the hdu (starting from the curent) and execute the given method (here is get with "NAXIS" as argument)
- walk's method can be combined -
> fh ( walkh, walka, get, fh(keys, "NAXIS*") )   // will return an array of dim  Nhdu X NAXIS  (in this case NAXIS must be the same for all HDUs)
> cube = fh( walkh, data, 1:100, 300:450)      // will return a cube of  Nhdu X 100 x 150 
> fh, walkh,  show , 1:6                                // will show the first 6 lines of all HDUs

- etc ... -

Finaly ofits object can be grouped in what I have called "gfits" object :
> gf = gfits_new( fh1, fh2, fh3, ...., fhN );
> gf = gfits_open( ["path_1.fits", "path_2.fits", "path_3.fits", ...,  "path_N.fits"] )
> gf = gfits_open(  "@~/list.text" , dir="~/")  // where ~/list.text is a file contening all fits file path

The gfits have also the walk method to walk through the ofits sub members:
> gf( walk, get, object)   // will return value  of keyword "OBJECT" in each ofits members
> gf, walk , show , 3       //  will show the firs 3 lines of each hdu
> func fits_median ( fh) {  return  median(fh(data,*)); }
> gf( walk, exec, fits_median)   // will return the median of the data of each ofits members !
> moy  = gf( walk, data, 1:10, avg) // will get an array  of Nmember X 10  representing the 10 first value of the average in y direction (if image) for each sub-members
> gf (walk, walkh, get, "NAXIS")   // return a array of NAXIS integer of dim   Nmember X Nhdu  (if the number of hdu is the same for each sub-member)

This work alos for writing:
> fh = fits_open ( "~/cube_of_data" ) // let say it contain a cube  of 100x00x4   for instance
> gfw = fits_gen(fh, 4)  // generate 4 writable ofits object with the same header than fh   
> gfw , walka, file,  swrite( format="~/data_%d.fits", indgen(4) )  // set the file names for each of 4 
> gfw, walk, set , "naxis", 2   // reset the NAXIS keyword
> gfw, walk, remove, "naxis3"  // remove NAXIS3 Keyword
> gfw, walka , setdata, fh(data, )   // will walk on the external dimension of fh(data, ); (the 3nd dimension)   -> split the cube in the 4 fits new objects 
> gfw, walk, write   //  write all the ofits 
> gfw =[]    // you are done

Mon Feb 28, 2011 2:28 pm
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.