Keep Fits Headers i...
 
Share:
Notifications
Clear all

2022-05-29: APP 2.0.0-beta2 has been released !

Release notes

Download links per platform:

windows 2.0.0-beta2

macOS x86_64 2.0.0-beta2

macOS arm64 M1 2.0.0-beta2

Linux DEB 2.0.0-beta2

Linux RPM 2.0.0-beta2

Keep Fits Headers in Calibrated Frames


(@pmneo)
Hydrogen Atom Customer
Joined: 5 months ago
Posts: 2
Topic starter  

Hey! 

It would be realy nice if all Fits Headers will be kept in saved calibrated frames.

Currently some worthfull informations like Rotation Angle and Coords are lost :'(

 

Thanks

Philip


ReplyQuote
(@wvreeven)
Galaxy Admin
Joined: 4 years ago
Posts: 1723
 

@pmneo Thanks for the request. I asked Mabula to reply.


ReplyQuote
(@mabula-admin)
Quasar Admin
Joined: 5 years ago
Posts: 3158
 

Hi @pmneo,

Thank you very much for your suggestion. It is on our ToDo list to keep as much info as possible in the Fits headers 😉

Mabula


ReplyQuote
(@pmneo)
Hydrogen Atom Customer
Joined: 5 months ago
Posts: 2
Topic starter  

Thanks @mabula-admin

 

Until then, i have wrote a simple Java helper to copy all not present FITS Headers:

 

package sandbox;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import nom.tam.fits.Fits;
import nom.tam.fits.FitsException;
import nom.tam.fits.Header;
import nom.tam.fits.HeaderCard;
import nom.tam.util.Cursor;

public class CopyFitsHeaders {

	public static void main(String[] args) throws IOException, FitsException {
		
		File root = new File("C:\\ASI1600\\Nikkor");
		
		for( File base : root.listFiles() ) {
			
			if( base.isDirectory() == false ) {
				continue;
			}
			
			System.out.println( "Processing " + base );
			
			LinkedList lights = collectFits( new File( base, "Light" ) );
			LinkedList calibratedLights = collectFits( base );
			
			calibratedLights.removeAll( lights );
			
			for( File light : lights ) {
				final LinkedList matchingCalibratedLights = new LinkedList<>();
				for( File c : calibratedLights ) {
					if( c.getName().startsWith( light.getName().substring(0, light.getName().length() - ".fits".length() ) ) ) {
						matchingCalibratedLights.add( c );
						break;
					}
				}
				
				if( matchingCalibratedLights.size() == 0 ) {
					System.err.println( "No calbirated light found for " + light );
					continue;
				}
				
				Fits f = new Fits( light );
				try {
					for( File calibratedLight : matchingCalibratedLights ) {
						Fits t = new Fits( calibratedLight );
						try {
							Header header = f.getHDU(0).getHeader();
							Header targetHeader = t.getHDU(0).getHeader();
							
							Map<String, HeaderCard> targetHeaders = new HashMap<>();
							Cursor<String, HeaderCard> ti = targetHeader.iterator();
							while( ti.hasNext() ) {
								HeaderCard h = ti.next();
								
								
								targetHeaders.put( h.getKey(), h );
							}
							
							int written = 0;
							Cursor<String, HeaderCard> i = header.iterator();
							while( i.hasNext() ) {
								HeaderCard h = i.next();
								
								if( targetHeaders.get( h.getKey() ) == null ) {
									//System.err.println( h );
									//System.err.flush();
									
									ti.add( h.copy() );
									written++;
								}
							}
							if( written > 0 ) {
								System.out.println( "Updating " + written + " FITS headers to " + calibratedLight );
								t.write( calibratedLight );
							}
						}
						finally {
							t.close();
						}
					}
				}
				finally {
					f.close();
				}
			}
		}
			
		System.out.println( "Finished" );
	}

	private static LinkedList collectFits(File lights) {
		LinkedList allLights = new LinkedList( listFilesAsList(lights) );
		LinkedList allFits = new LinkedList( );
		
		while( allLights.size() > 0 ) {
			File light = allLights.removeFirst();
			
			if( light.isDirectory() ) {
				allLights.addAll( listFilesAsList(light) );
			}
			else if( light.getName().endsWith( ".fits" ) ) {
				allFits.add( light );
			}
		}
		return allFits;
	}

	private static List listFilesAsList(File lights) {
		File files[] = lights.listFiles();
		if( files == null ) {
			return Collections.emptyList();
		}
		else {
			return Arrays.asList( files );
		}
	}
}

CS


ReplyQuote
Share: