001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
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 */
029
030package org.ecoinformatics.seek.gis.grass;
031
032import java.io.BufferedReader;
033import java.io.IOException;
034import java.io.InputStreamReader;
035
036import ptolemy.actor.TypedAtomicActor;
037import ptolemy.actor.TypedIOPort;
038import ptolemy.data.StringToken;
039import ptolemy.data.expr.Parameter;
040import ptolemy.data.type.BaseType;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045/**
046 * This actor exports a GRASS database object into an ARC file.
047 */
048
049public class GrassExportActor extends TypedAtomicActor {
050        // input ports
051        public TypedIOPort dataType = new TypedIOPort(this, "dataType", true, false);
052        public TypedIOPort outFileName = new TypedIOPort(this, "outFileName", true,
053                        false);
054        public TypedIOPort inputObjectName = new TypedIOPort(this,
055                        "inputObjectName", true, false);
056        public TypedIOPort trigger = new TypedIOPort(this, "trigger", false, true);
057
058        public Parameter grass_gisbase = new Parameter(this, "GRASS_GISBASE");
059        public Parameter location_name = new Parameter(this, "LOCATION_NAME");
060        public Parameter gisdbase = new Parameter(this, "GISDBASE");
061        public Parameter gisrc = new Parameter(this, "GISRC");
062
063        private String GRASS_GISBASE;
064        private String LOCATION_NAME;
065        private String GISDBASE;
066        private String GISRC;
067
068        /**
069         * Grass Export Actor.
070         */
071        public GrassExportActor(CompositeEntity container, String name)
072                        throws NameDuplicationException, IllegalActionException {
073                super(container, name);
074                dataType.setTypeEquals(BaseType.STRING);
075                outFileName.setTypeEquals(BaseType.STRING);
076                inputObjectName.setTypeEquals(BaseType.STRING);
077                trigger.setTypeEquals(BaseType.STRING);
078        }
079
080        /**
081   *
082   */
083        public void initialize() throws IllegalActionException {
084                if (grass_gisbase.getToken() != null) {
085                        GRASS_GISBASE = ((StringToken) grass_gisbase.getToken()).toString();
086                        // get rid of the quotes
087                        GRASS_GISBASE = GRASS_GISBASE.substring(1,
088                                        GRASS_GISBASE.length() - 1);
089                        System.out.println("GRASS_GISBASE: " + GRASS_GISBASE);
090                }
091                if (location_name.getToken() != null) {
092                        LOCATION_NAME = ((StringToken) location_name.getToken()).toString();
093                        // get rid of the quotes
094                        LOCATION_NAME = LOCATION_NAME.substring(1,
095                                        LOCATION_NAME.length() - 1);
096                        System.out.println("LOCATION_NAME: " + LOCATION_NAME);
097                }
098                if (gisdbase.getToken() != null) {
099                        GISDBASE = ((StringToken) gisdbase.getToken()).toString();
100                        // get rid of the quotes
101                        GISDBASE = GISDBASE.substring(1, GISDBASE.length() - 1);
102                        System.out.println("GISDBASE: " + GISDBASE);
103                }
104                if (gisrc.getToken() != null) {
105                        GISRC = ((StringToken) gisrc.getToken()).toString();
106                        // get rid of the quotes
107                        GISRC = GISRC.substring(1, GISRC.length() - 1);
108                        System.out.println("GISRC: " + GISRC);
109                }
110
111                if (GISRC == null || GISDBASE == null || LOCATION_NAME == null
112                                || GRASS_GISBASE == null) {
113                        throw new IllegalActionException(
114                                        "The parameters GISRC, GISBASE, "
115                                                        + "LOCATION_NAME and GRASS_GISBASE must have valid values.  GISRC is "
116                                                        + "the path to your .grassrc file.  GISBASE is the path to your grass5"
117                                                        + "installation directory.  LOCATION_NAME is the name of the database "
118                                                        + "location within GRASS.  GRASS_GISBASE is the base directory for "
119                                                        + "your GRASS database.  Please provide these paths and re-execute.");
120                }
121        }
122
123        /**
124   *
125   */
126        public boolean prefire() throws IllegalActionException {
127                return super.prefire();
128        }
129
130        /**
131   *
132   */
133        public void fire() throws IllegalActionException {
134                System.out.println("firing GrassExportActor");
135                super.fire();
136
137                StringToken outFileToken = (StringToken) outFileName.get(0);
138                String outFileStr = outFileToken.stringValue();
139
140                StringToken inputObjectNameToken = (StringToken) inputObjectName.get(0);
141                String inputObjectNameStr = inputObjectNameToken.stringValue();
142
143                StringToken dataTypeToken = (StringToken) dataType.get(0);
144                String dataTypeStr = dataTypeToken.stringValue();
145
146                String errorout = "";
147                // /////////////////////////// IMPL CODE
148                // //////////////////////////////////
149
150                try {
151                        final Process listener;
152                        String exportCommand = null;
153
154                        if (dataTypeStr.equalsIgnoreCase("asc")) {
155                                exportCommand = "r.out.arc";
156                        } else if (dataTypeStr.equalsIgnoreCase("shp")) {
157                                exportCommand = "r.out.shape";
158                        } else {
159                                exportCommand = "r.in.arc"; // default
160                        }
161
162                        String args[] = { this.GRASS_GISBASE + "/bin/" + exportCommand,
163                                        "input=" + inputObjectNameStr, "output=" + outFileStr };
164
165                        String envvars[] = { "GISBASE=" + this.GRASS_GISBASE,
166                                        "LOCATION_NAME=" + this.LOCATION_NAME,
167                                        "GISDBASE=" + this.GISDBASE, "GISRC=" + this.GISRC };
168
169                        listener = Runtime.getRuntime().exec(args, envvars);
170
171                        new Thread(new Runnable() {
172                                public void run() {
173                                        try {
174                                                BufferedReader br_in = new BufferedReader(
175                                                                new InputStreamReader(listener.getInputStream()));
176                                                BufferedReader br_err_in = new BufferedReader(
177                                                                new InputStreamReader(listener.getErrorStream()));
178                                                String buff = null;
179                                                while ((br_in != null && (buff = br_in.readLine()) != null)) {
180                                                        System.out.println("Process out: " + buff);
181                                                        try {
182                                                                Thread.sleep(1);
183                                                        } catch (Exception e) {
184                                                        }
185                                                }
186                                                br_in.close();
187                                                while ((br_err_in != null && (buff = br_err_in
188                                                                .readLine()) != null)) {
189                                                        System.out.println("Process error out: " + buff);
190                                                        try {
191                                                                Thread.sleep(1);
192                                                        } catch (Exception e) {
193                                                        }
194                                                }
195                                                br_err_in.close();
196                                        } catch (IOException ioe) {
197                                                System.out.println("Exception caught printing result");
198                                                ioe.printStackTrace();
199                                        }
200                                }
201                        }).start();/*
202                                                 * 
203                                                 * BufferedReader br_err_in = new BufferedReader( new
204                                                 * InputStreamReader(listener.getErrorStream())); String
205                                                 * buff = null; while ((br_err_in != null && (buff =
206                                                 * br_err_in.readLine()) != null)) { errorout += buff +
207                                                 * "\n"; } br_err_in.close();
208                                                 */
209
210                        System.out.println("****" + errorout + "****");
211
212                        // thread to wait for grass process to terminate
213                        new Thread(new Runnable() {
214                                public void run() {
215                                        try {
216                                                System.out
217                                                                .println("*Thread waiting for Process to exit");
218                                                listener.waitFor();
219                                                System.out.println("*Thread detected Process exiting");
220                                        } catch (InterruptedException ie) {
221                                                System.out
222                                                                .println("InterruptedException caught whilst waiting "
223                                                                                + "for Mozilla Process to exit: " + ie);
224                                                ie.printStackTrace();
225                                        }
226                                }
227                        }).start();
228                } catch (Exception e) {
229                        e.printStackTrace();
230                        throw new IllegalActionException("Unexpected error: "
231                                        + e.getMessage());
232                }
233
234                // ////////////////////////////////////////////////////////////////////////
235
236                /*
237                 * if(errorout.indexOf("CREATING SUPPORT FILES FOR") != -1) {
238                 * trigger.broadcast(new StringToken("SUCCESS"));
239                 * System.out.println("Grass Import Action done"); } else { throw new
240                 * IllegalActionException("There was a problem running the " +
241                 * "GRASS script: " + errorout); }
242                 */
243        }
244}