package org.cass; import java.io.*; import java.util.*; /** A utility class containing various functions for writing GRID files. @see Fits Fits @author Zachary Vaughan @version 1.1, 06/30/2006 */ public final class Grid { /** The default minimum Z-axis value for GRID output. */ public static final double ZMIN = 0; /** The default maximum Z-axis value for GRID output. */ public static final double ZMAX = 1000; /** Writes the given image map to the given stream, assuming default axis ranges and binary output format.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. @see #writeMap(OutputStream,float[][],double,double,double,double,double,double,boolean) writeMap(out,map,xmin,xmax,ymin,ymax,zmin,zmax,ascii) */ public static void writeMap(OutputStream out, float[][] map) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, ZMIN, ZMAX, false); } /** Writes the given image map to the given stream, assuming default axis ranges.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @param ascii true for ASCII output format or false for binary output format. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. @see #writeMap(OutputStream,float[][],double,double,double,double,double,double,boolean) writeMap(out,map,xmin,xmax,ymin,ymax,zmin,zmax,ascii) */ public static void writeMap(OutputStream out, float[][] map, boolean ascii) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, ZMIN, ZMAX, ascii); } /** Writes the given image map to the given stream using the given limits, assuming default X-axis and Y-axis ranges and binary output format.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @param zmin the minimum Z-axis value. @param zmax the maximum Z-axis value. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. @see #writeMap(OutputStream,float[][],double,double,double,double,double,double,boolean) writeMap(out,map,xmin,xmax,ymin,ymax,zmin,zmax,ascii) */ public static void writeMap(OutputStream out, float[][] map, double zmin, double zmax) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, zmin, zmax, false); } /** Writes the given image map to the given stream using the given limits, assuming default X-axis and Y-axis ranges.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @param zmin the minimum Z-axis value. @param zmax the maximum Z-axis value. @param ascii true for ASCII output format or false for binary output format. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. @see #writeMap(OutputStream,float[][],double,double,double,double,double,double,boolean) writeMap(out,map,xmin,xmax,ymin,ymax,zmin,zmax,ascii) */ public static void writeMap(OutputStream out, float[][] map, double zmin, double zmax, boolean ascii) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, zmin, zmax, ascii); } /** Writes the given image map to the given stream using the given limits, assuming the default Z-axis range and binary output format.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @param xmin the minimum X-axis value. @param xmax the maximum X-axis value. @param ymin the minimum Y-axis value. @param ymax the maximum Y-axis value. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. @see #writeMap(OutputStream,float[][],double,double,double,double,double,double,boolean) writeMap(out,map,xmin,xmax,ymin,ymax,zmin,zmax,ascii) */ public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax) throws IOException { writeMap(out, map, xmin, xmax, ymin, ymax, ZMIN, ZMAX, false); } /** Writes the given image map to the given stream using the given limits, assuming the default Z-axis range.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @param xmin the minimum X-axis value. @param xmax the maximum X-axis value. @param ymin the minimum Y-axis value. @param ymax the maximum Y-axis value. @param ascii true for ASCII output format or false for binary output format. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. @see #writeMap(OutputStream,float[][],double,double,double,double,double,double,boolean) writeMap(out,map,xmin,xmax,ymin,ymax,zmin,zmax,ascii) */ public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax, boolean ascii) throws IOException { writeMap(out, map, xmin, xmax, ymin, ymax, ZMIN, ZMAX, ascii); } /** Writes the given image map to the given stream using the given limits, assuming binary output format.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @param xmin the minimum X-axis value. @param xmax the maximum X-axis value. @param ymin the minimum Y-axis value. @param ymax the maximum Y-axis value. @param zmin the minimum Z-axis value. @param zmax the maximum Z-axis value. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. @see #writeMap(OutputStream,float[][],double,double,double,double,double,double,boolean) writeMap(out,map,xmin,xmax,ymin,ymax,zmin,zmax,ascii) */ public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) throws IOException { writeMap(out, map, xmin, xmax, ymin, ymax, zmin, zmax, false); } /** Writes the given image map to the given stream using the given limits.

This method assumes that system files are read and written in little-endian format.

@param out the given output stream. @param map the given image map. @param xmin the minimum X-axis value. @param xmax the maximum X-axis value. @param ymin the minimum Y-axis value. @param ymax the maximum Y-axis value. @param zmin the minimum Z-axis value. @param zmax the maximum Z-axis value. @param ascii true for ASCII output format or false for binary output format. @throws IOException if there is an I/O error while writing. @throws NullPointerException if either out or map are null. */ public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax, boolean ascii) throws IOException { int width = map.length, height = map[0].length; int i = 0, x, y; if(ascii) { PrintWriter writer = new PrintWriter(new OutputStreamWriter(out)); writer.println("DSAA"); writer.println(xmin + " " + xmax); writer.println(ymin + " " + ymax); writer.print(zmin + " " + zmax); for(y = 0; y < height; y++) for(x = 0; x < width; x++) writer.print((i++ % 8 == 0 ? "\n" : "\t") + map[x][y]); } else { pWriteString(out, "DSBB"); pWriteShort(out, width); pWriteShort(out, height); pWriteDouble(out, xmin); pWriteDouble(out, xmax); pWriteDouble(out, ymin); pWriteDouble(out, ymax); pWriteDouble(out, zmin); pWriteDouble(out, zmax); for(y = 0; y < height; y++) for(x = 0; x < width; x++) pWriteFloat(out, map[x][y]); } out.flush(); } private static void pWriteString(OutputStream out, String s) throws IOException { for(int i = 0; i < s.length(); i++) out.write(s.charAt(i)); } private static void pWriteShort(OutputStream out, int s) throws IOException { out.write(s); out.write(s >> 8); } private static void pWriteDouble(OutputStream out, double d) throws IOException { long b = Double.doubleToRawLongBits(d); int b1 = (int) (b >> 32), b2 = (int) b; out.write(b2); out.write(b2 >> 8); out.write(b2 >> 16); out.write(b2 >> 24); out.write(b1); out.write(b1 >> 8); out.write(b1 >> 16); out.write(b1 >> 24); } private static void pWriteFloat(OutputStream out, float f) throws IOException { int b = Float.floatToRawIntBits(f); out.write(b); out.write(b >> 8); out.write(b >> 16); out.write(b >> 24); } private Grid() {} }