/** * */ package fr.unistra.pelican.algorithms.io; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import fr.unistra.pelican.Algorithm; import fr.unistra.pelican.AlgorithmException; import fr.unistra.pelican.DoubleImage; import fr.unistra.pelican.Image; import fr.unistra.pelican.algorithms.visualisation.MViewer; /** * Load an image save as text as if it was generated by wtext in dataio package in IRAF without any header. User must provide x and y dimensions as they are not stored in the file. * * @author Benjamin Perret * */ public class IRAFTextImageLoad extends Algorithm { /** * Path to the file to be read */ public String filename; /** * x dimension of result */ public int xdim; /** * y dimension of result */ public int ydim; /** * Result */ public Image output; /** * */ public IRAFTextImageLoad() { super(); super.inputs="filename,xdim,ydim"; super.outputs="output"; } /* (non-Javadoc) * @see fr.unistra.pelican.Algorithm#launch() */ @Override public void launch() throws AlgorithmException { File f=new File(filename); try { BufferedReader br= new BufferedReader(new FileReader(f)); String line; int i=0; output=new DoubleImage(xdim,ydim,1,1,1); int size=xdim*ydim; while((line=br.readLine()) != null) { String [] toks=line.split(" "); for(String t:toks) if(!"".equals(t)) { if(i==size) throw new AlgorithmException("Dimension errors with file " +f + ". User specified xdim = " + xdim +" and ydim = " + ydim +" but file contains more than " + size + " values." ); double v=Double.parseDouble(t); output.setPixelDouble(i++, v); } } if(i!=size) throw new AlgorithmException("Dimension errors with file " +f + ". User specified xdim = " + xdim +" and ydim = " + ydim +" but file contains less than " + size + " values." ); } catch (FileNotFoundException e) { throw new AlgorithmException("Error opening file " +f,e); } catch (IOException e) { throw new AlgorithmException("Error reading file " +f,e); }catch (NumberFormatException e) { throw new AlgorithmException("Error reading file " +f + " : value can not be parsed as a double.",e); } } public static Image exec(String filename, int xdim, int ydim) { return (Image)(new IRAFTextImageLoad().process(filename,xdim,ydim)); } public static void main(String [] args) { Image im=IRAFTextImageLoad.exec("d:\\perret\\aims\\thegood.txt", 150, 150); MViewer.exec(im); } }