package org.cass; import java.io.*; import java.util.*; import java.util.zip.*; /** A program designed to read the image maps of FITS files and output those maps in the format of ASCII or binary GRID files. This program is meant to be referenced through the name 'makegrid' which should be defined as an alias or batch file, depending on the underlying system. @author Zachary Vaughan @version 1.2 06/05/2006 */ public final class MakeGrid { private static final String[] htmext = {"sky", "eq1", "np1", "sp1", "eqn", "npn", "spn", "dsb", "tfo", "psn", "pse", "drm", "dhn", "gtx", "gty", "tim", "bpm"}, equext = {"equ", "eq1", "np1", "sp1", "eqs", "nps", "sps", "equ", "npu", "spu"}; private static final double[] htmlox = {0, -360, -40, -40, -360, -40, -40, -360, -360, -360, -360, -360, -360, -360, -360, -360, -360}, htmhix = {0, 0, 40, 40, 0, 40, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, htmloy = {0, -60, -40, -40, -60, -40, -40, -90, -90, -90, -90, -90, -90, -90, -90, -90, -90}, htmhiy = {0, 60, 40, 40, 60, 40, 40, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90}, equlox = {0, -360, -40, -40, -360, -40, -40, -360, -40, -40}, equhix = {0, 0, 40, 40, 0, 40, 40, 0, 40, 40}, equloy = {0, -60, -40, -40, -60, -40, -40, -60, -40, -40}, equhiy = {0, 60, 40, 40, 60, 40, 40, 60, 40, 40}; private static final boolean[] htmflipx = {false, true, false, true, true, false, true, true, true, true, true, true, true, true, true, true, true}, htmflipy = {false, false, true, true, false, true, true, false, false, false, false, false, false, false, false, false, false}, equflipx = {false, true, false, true, true, false, true, true, false, true}, equflipy = {false, false, true, false, false, true, false, false, true, false}; private static Vector fset = new Vector(); private static boolean ascii = false, flip = false, skymap = false, silent = false; /** The entry point for this program. The method is synchronized to allow only one process or thread to use the program at a time per instance of the Virtual Machine.

This method NEVER RETURNS control to the user; rather it terminates the Virtual Machine with an exit code representing the results of the program. A value of 0 indicates success, and all other values represent some sort of error or failure of the program.

@param args the command-line arguments supplied to this program. */ public static synchronized void main(String[] args) { if(args.length == 0) { printUsage(System.out); System.exit(1); } try { for(int i = 0; i < args.length; i++) { if(args[i].startsWith("-")) { if(args[i].equals("-ascii")) ascii = true; else if(args[i].equals("-flip")) flip = true; else if(args[i].equals("-skymap")) skymap = true; else if(args[i].equals("-silent")) silent = true; else throw new IllegalArgumentException("Illegal argument, " + args[i]); } else parseFiles(args[i]); } } catch(Exception e) { System.err.println("Error: " + e.getMessage()); printUsage(System.err); System.exit(1); } /* check image conversion parameters */ if(ascii) System.out.println("-> ASCII output format"); else System.out.println("-> Binary output format"); if(flip) System.out.println("-> Flipping map coordinate systems"); if(skymap) System.out.println("-> Forcing image type recognition to \'skymap_htm\'"); if(silent) System.out.println("-> Silence mode, minimal output"); /* check file count */ if(fset.isEmpty()) { System.out.println("-> No files found"); System.err.println("Error: No files found"); System.exit(1); } else System.out.println("-> " + fset.size() + " file(s) found"); try { System.out.println(); process(); } catch(Exception e) { System.err.println("Error: " + e); System.exit(1); } /* exit normally */ System.out.println("All done."); System.exit(0); } /** Prints an informative usage message describing command line syntax to the given output stream. @param out the desired output stream to print to. */ public static void printUsage(PrintStream out) { out.println("Usage:"); out.println(" % makegrid [options] files"); out.println(); out.println("Options:"); out.println(" -ascii output GRID files in ASCII format rather than binary"); out.println(" -flip flip the coordinate systems of outputted skymaps"); out.println(" -skymap force image type recognition to \'skymap_htm\' (for older skymaps)"); out.println(" -silent don't display as much information about conversion process"); out.println(); } private static void process() throws IOException { for(Iterator fi = fset.iterator(); fi.hasNext();) { File f = fi.next(); if(f.isDirectory()) { System.err.println("\'" + f + "\' is a directory. Skipping.\n"); continue; } else if(f.canRead() == false) { System.err.println("Cannot read file \'" + f + "\'. Skipping.\n"); continue; } else System.out.println("Converting \'" + f + "\', image 1"); boolean gzip = f.getName().toLowerCase().endsWith(".gz"); InputStream in = new BufferedInputStream(gzip ? (InputStream) new GZIPInputStream(new FileInputStream(f)) : (InputStream) new FileInputStream(f)); String dir = f.getParent(); String base = gzip ? trim(trim(f.getName())) : trim(f.getName()); Map header1 = Fits.readHeader(in), header = null; String type = header1.get("IMG_TYPE"); if(type == null) type = ""; float[][] map = Fits.readMap(in, header1); if(skymap || type.equals("\'skymap_htm\'")) { if(!silent) { System.out.println(" HTM generated skymap" + (skymap ? " (assumed)" : "")); System.out.println(" Camera " + header1.get("CAMERA") + ", Mode " + header1.get("MODE")); } base = header1.get("NAME"); base = base.substring(1, base.length() - 1); if(!silent) printBasics(header1); if(flip) map = flipMap(map, htmflipx[1], htmflipy[1]); File of = new File(dir, base + "_" + htmext[1] + ".grd"); OutputStream out = new BufferedOutputStream(new FileOutputStream(of)); Grid.writeMap(out, map, htmlox[1], htmhix[1], htmloy[1], htmhiy[1], ascii); out.close(); if(!silent) System.out.println(" Output file is \'" + of + "\'"); for(int i = 2; i < 17; i++) { System.out.println("Converting \'" + f + "\', image " + i); header = Fits.readHeader(in); if(!silent) printBasics(header); map = Fits.readMap(in, header); if(flip) map = flipMap(map, htmflipx[i], htmflipy[i]); of = new File(dir, base + "_" + htmext[i] + ".grd"); out = new BufferedOutputStream(new FileOutputStream(of)); Grid.writeMap(out, map, htmlox[i], htmhix[i], htmloy[i], htmhiy[i], ascii); out.close(); if(!silent) System.out.println(" Output file is \'" + of + "\'"); } } else if(type.equals("\'skymap_equ\'")) { if(!silent) { System.out.println(" EQU generated skymap"); System.out.println(" Camera " + header1.get("CAMERA") + ", Mode " + header1.get("MODE")); } base = header1.get("NAME"); base = base.substring(1, base.length() - 1); if(!silent) printBasics(header1); if(flip) map = flipMap(map, equflipx[1], equflipy[1]); File of = new File(dir, base + "_" + equext[1] + ".grd"); OutputStream out = new BufferedOutputStream(new FileOutputStream(of)); Grid.writeMap(out, map, equlox[1], equhix[1], equloy[1], equhiy[1], ascii); out.close(); if(!silent) System.out.println(" Output file is \'" + of + "\'"); for(int i = 2; i < 10; i++) { System.out.println("Converting \'" + f + "\', image " + i); header = Fits.readHeader(in); if(!silent) printBasics(header); map = Fits.readMap(in, header); if(flip) map = flipMap(map, equflipx[i], equflipy[i]); of = new File(dir, base + "_" + equext[i] + ".grd"); out = new BufferedOutputStream(new FileOutputStream(of)); Grid.writeMap(out, map, equlox[i], equhix[i], equloy[i], equhiy[i], ascii); out.close(); if(!silent) System.out.println(" Output file is \'" + of + "\'"); } } else { if(!silent) { System.out.println(" Unspecified image type, using defaults"); printBasics(header1); } header = Fits.readHeader(in); if(header == null) { File of = new File(dir, base + ".grd"); OutputStream out = new BufferedOutputStream(new FileOutputStream(of)); Grid.writeMap(out, map, ascii); out.close(); if(!silent) System.out.println(" Output file is \'" + of + "\'"); } else { File of = new File(dir, base + "_001.grd"); OutputStream out = new BufferedOutputStream(new FileOutputStream(of)); Grid.writeMap(out, map, ascii); out.close(); if(!silent) System.out.println(" Output file is \'" + of + "\'"); for(int i = 2; header != null; header = Fits.readHeader(in)) { String istr = (i > 99 ? "" : i > 9 ? "0" : "00") + i; System.out.println("Converting \'" + f + "\', image " + i); if(!silent) printBasics(header); map = Fits.readMap(in, header); of = new File(dir, base + "_" + istr + ".grd"); out = new BufferedOutputStream(new FileOutputStream(of)); Grid.writeMap(out, map, ascii); out.close(); if(!silent) System.out.println(" Output file is \'" + of + "\'"); i++; } } } System.out.println(); in.close(); } } private static String trim(String file) { int i = file.lastIndexOf('.'); return i <= 0 ? file : file.substring(0, i); } private static void printBasics(Map header) { int b = Integer.parseInt(header.get("BITPIX")); if(header.containsKey("MAP")) System.out.println(" " + header.get("MAP")); System.out.print(" (" + header.get("NAXIS1") + "," + header.get("NAXIS2") + ") at " + Math.abs(b) + " bit "); System.out.println(b > 0 ? "integer precision" : "floating precision"); } private static float[][] flipMap(float[][] map, boolean flipx, boolean flipy) { float[][] rval = new float[map.length][map[0].length]; for(int x = 0; x < map.length; x++) for(int y = 0; y < map[0].length; y++) rval[flipx ? map.length - x - 1 : x] [flipy ? map[0].length - y - 1 : y] = map[x][y]; return rval; } private static void parseFiles(String arg) { File dir = new File(PatternFilter.parseDirectory(arg)); File[] farr = dir.listFiles(new PatternFilter(arg)); if(farr != null) for(int i = 0; i < farr.length; i++) if(farr[i] != null) fset.add(farr[i]); } private MakeGrid() {} }