[fitsbits] Resetting Cursor

Tom McGlynn Thomas.A.McGlynn at nasa.gov
Wed Dec 17 10:46:28 EST 2008


lsitongia wrote:
 > I'm using Cursor to iterate over a Header.  I find that nextCard
 > starts returning cards at the location in the Header at which my last
 > get found a card.  For example, if I getIntValue for something in the
 > Header, then the Cursor starts from where that card was found.
 >
 > How do I reset the Cursor to the beginning of the Header?
 >
 > Thanks,
 > ==Leonard
 > _______________________________________________
 > fitsbits mailing list
 > fitsbits at listmgr.cv.nrao.edu
 > http://listmgr.cv.nrao.edu/mailman/listinfo/fitsbits
 >


Hi Leonard,

While I suspect that you are referring to use of the Java FITS package 
that I wrote (or one derived from it), you might want to be a little 
more specific about the context of the software and version you are using.

If you are using the nom.tam.fits package then you should be able to 
iterate over the entire header using your own cursors rather than the 
one owned by the Header object.  E.g., the program below

   import nom.tam.fits.*;
   import nom.tam.util.*;

   public class Test {

     public static void main(String[] args) throws Exception {
         Fits      f   = new Fits(args[0]);
	BasicHDU  hdu = f.readHDU();
	Header    hdr = hdu.getHeader();
	
	double cv2 = hdr.getDoubleValue("CRVAL2");
	System.out.println("CRVAL2 is:" + cv2);
	
	Cursor c1 = hdr.iterator();
	cv2 = hdr.getDoubleValue("CRVAL2");
	System.out.println("CRVAL2 is:" + cv2);
	
	Cursor c2 = hdr.iterator();
	while (c1.hasNext()) {
	    System.out.println("C1: " + c1.next());
	}
	System.out.println("\n\n***  second ****\n\n");
	while(c2.hasNext()) {
	    System.out.println("C2: " + c2.next());
	}
     }
   }

yields the following input (where I've suppressed most of the header 
lines and replaced them with a ...).  Both iterators run over the entire
header despite the getDoubleValue() call.

   CRVAL2 is:90.0
   CRVAL2 is:90.0
   C1: SIMPLE  =                    T / Written by SkyView Thu ...
   C1: BITPIX  =                  -64 / 8 byte floating point 

   ...
   C1: HISTORY 

   C1: END 



   ***  second ****


   C2: SIMPLE  =                    T / Written by SkyView Thu ...
   BITPIX  =                  -64 / 8 byte floating point 

   ...
   C2: HISTORY 

   C2: END 


The header object uses its own cursor to keep track of the last row that 
the user asked for.  That's to handle a situation where, e.g., a user 
wants to look at the comment cards that immediately precede or follow a 
given keyword.  So if you are going to use getXXXValue calls, the best 
way to independently iterate over the entire header is something like:

    Cursor curs = hdr.iterator();
    while (curs.hasNext()) {
       HeaderCard hc = (HeaderCard) curs.next();
       ...
    }

The casts are a bit ugly, but all of this was written long before Java 
generics were available.

With regard to your previous question regarding the behavior of Cursors 
at the end of the header:  I'm not quite sure what you are saying here. 
  I wonder if you are confused by the action of nextCard() which returns 
null if you are beyond the end of the header.  You can use this via a 
loop like:

     HeaderCard c = null;
     while ( (c=hdr.nextCard()) != null) {
          ... process card c ...
     }

If you have further questions, please feel free to write.

      Regards,
      Tom McGlynn
      Tom.McGlynn at nasa.gov




More information about the fitsbits mailing list