001/*
002 * Copyright (c) 2014-2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2018-07-24 17:15:39 +0000 (Tue, 24 Jul 2018) $' 
007 * '$Revision: 34704 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.gis.actor.io;
030
031import java.io.File;
032
033import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
034import org.kepler.gis.actor.CRSActor;
035import org.kepler.gis.data.RasterToken;
036import org.kepler.gis.util.RasterUtilities;
037
038import ptolemy.actor.TypedIOPort;
039import ptolemy.actor.parameters.PortParameter;
040import ptolemy.data.StringToken;
041import ptolemy.data.type.BaseType;
042import ptolemy.kernel.CompositeEntity;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045import ptolemy.kernel.util.SingletonAttribute;
046
047/** An actor to read a raster from a file.
048 * 
049 *  TODO this actor is not finished.
050 * 
051 *  @author Daniel Crawl
052 *  @version $Id: RasterReader.java 34704 2018-07-24 17:15:39Z crawl $
053 */
054public class RasterReader extends CRSActor {
055
056    public RasterReader(CompositeEntity container, String name)
057            throws IllegalActionException, NameDuplicationException {
058        super(container, name);
059
060        input = new PortParameter(this, "input");
061        input.setTypeEquals(BaseType.STRING);
062        input.setStringMode(true);
063        input.getPort().setTypeEquals(BaseType.STRING);
064        new SingletonAttribute(input.getPort(), "_showName");
065    
066        raster = new TypedIOPort(this, "raster", false, true);
067        raster.setTypeEquals(RasterToken.RASTER);
068    }
069
070    @Override
071    public void fire() throws IllegalActionException {
072        
073        super.fire();
074        
075        input.update();
076        String inputStr = ((StringToken)input.getToken()).stringValue();
077
078        RasterToken rasterToken = null;
079
080        // see if local file name
081        File inputFile = new File(inputStr);
082        if(!inputFile.exists()) {
083            throw new IllegalActionException(this, "Input file does not exist: " + inputStr);
084        }
085        
086        try {
087            AbstractGridCoverage2DReader reader = RasterUtilities.getReader(inputFile, _crs);
088            if(reader == null) {
089                //System.out.println("file not natively supported by geotools.");
090                rasterToken = new RasterToken(inputFile);
091            } else {
092                rasterToken = new RasterToken(reader);
093            }
094        } catch (Exception e) {
095            throw new IllegalActionException(this, e,
096                    "Error reading from file.");
097        }
098            
099        
100        /*
101        if(rasterToken == null) {
102            throw new IllegalActionException(this,
103                    "Unable to determine type of input of " + inputStr);
104        }
105        */
106
107        raster.broadcast(rasterToken);
108
109    }
110    
111    @Override
112    public void wrapup() throws IllegalActionException {
113        super.wrapup();
114        RasterUtilities.closeReaders();
115    }
116    
117    /** The name of the file to read. */
118    public PortParameter input;
119
120    /** The raster output. */
121    public TypedIOPort raster;
122
123}