001/*
002 * Copyright (c) 2004-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.java_gis;
031
032import java.io.File;
033import java.util.Hashtable;
034
035import ptolemy.actor.lib.SequenceActor;
036import ptolemy.actor.lib.Transformer;
037import ptolemy.data.BooleanToken;
038import ptolemy.data.DoubleToken;
039import ptolemy.data.StringToken;
040import ptolemy.data.expr.FileParameter;
041import ptolemy.data.expr.Parameter;
042import ptolemy.data.type.BaseType;
043import ptolemy.kernel.CompositeEntity;
044import ptolemy.kernel.Port;
045import ptolemy.kernel.util.IllegalActionException;
046import ptolemy.kernel.util.NameDuplicationException;
047
048/**
049 * This actor is for transforming some range of values in a grid to some other
050 * value. If the current value of a grid cell is within the given range (minval,
051 * maxval) and a 'newval' is given, the values in the range are set to 'newval'.
052 * If 'newval' is empty or the input string is an invalid representation of a
053 * double, then the current value is multiplied by the multiplicationFactor and
054 * the additonParameter is added and the new value is the result.
055 * 
056 * (Actually, a new grid is returned, rather than revising the current grid.)
057 * 
058 * @author Dan Higgins NCEAS UC Santa Barbara
059 */
060public class GridReset extends Transformer implements SequenceActor {
061
062        /**
063         * The minumum value in a range where the grid pixel value is to be reset.
064         */
065        public Parameter minvalParameter;
066        /**
067         * The maximum value in a range where the grid pixel value is to be reset.
068         */
069        public Parameter maxvalParameter;
070        /**
071         * The new value of pixels in the (minval,maxval) range. If absent,
072         * transform the existing value
073         */
074        public Parameter newvalParameter;
075        /**
076         * if newvalParameter is empty, multiply the existing value by this factor
077         */
078        public Parameter multiplicationFactor;
079        /**
080         * if newvalParameter is empty, add this to the existing value
081         */
082        public Parameter additionParameter;
083
084        /**
085         * Boolean setting to determine whether or not to use disk for storing grid
086         * data rather than putting all data in RAM arrays
087         */
088        public Parameter useDisk;
089
090        /**
091         * The name to be given to the output File
092         */
093        private FileParameter outputFilename;
094
095        private Grid inputGrid;
096        private Grid newg;
097
098        private boolean finished;
099        private String prevAscFilename = "";
100        private String prevOutFilename = "";
101        private String cachedLine = "";
102        private Hashtable header = null;
103
104        /**
105         * constructor
106         * 
107         *@param container
108         *            The container.
109         *@param name
110         *            The name of this actor.
111         *@exception IllegalActionException
112         *                If the actor cannot be contained by the proposed
113         *                container.
114         *@exception NameDuplicationException
115         *                If the container already has an actor with this name.
116         */
117        public GridReset(CompositeEntity container, String name)
118                        throws NameDuplicationException, IllegalActionException {
119                super(container, name);
120
121                minvalParameter = new Parameter(this, "minvalParameter");
122                minvalParameter.setDisplayName("Minumum Value");
123                maxvalParameter = new Parameter(this, "maxvalParameter");
124                maxvalParameter.setDisplayName("Maximum Value");
125                newvalParameter = new Parameter(this, "newvalParameter");
126                newvalParameter.setDisplayName("New Value");
127                multiplicationFactor = new Parameter(this, "multiplicationFactor");
128                multiplicationFactor.setDisplayName("Multiplication Factor");
129                additionParameter = new Parameter(this, "additionParameter");
130                additionParameter.setDisplayName("Addition Parameter");
131
132                outputFilename = new FileParameter(this, "outputFileName");
133
134                useDisk = new Parameter(this, "useDisk");
135                useDisk.setDisplayName("Use disk storage (for large grids)");
136                useDisk.setTypeEquals(BaseType.BOOLEAN);
137                useDisk.setToken(BooleanToken.TRUE);
138        }
139
140        public void connectionsChanged(Port port) {
141                super.connectionsChanged(port);
142        }
143
144        /**
145         * 
146         *@exception IllegalActionException
147         *                If there is no director.
148         */
149        public void fire() throws IllegalActionException {
150                super.fire();
151                try {
152                        String ascfilename = null;
153                        String ascOutfilename = null;
154                        String ascfileshortname = null;
155                        String temp = "";
156                        ascfilename = ((StringToken) input.get(0)).stringValue();
157                        boolean useDiskValue = ((BooleanToken) useDisk.getToken())
158                                        .booleanValue();
159                        System.out.println("ascfilename: " + ascfilename);
160                        if (ascfilename.indexOf("/") > -1) {
161                                ascfileshortname = ascfilename.substring(ascfilename
162                                                .lastIndexOf("/") + 1, ascfilename.length());
163                        } else if (ascfilename.indexOf("\\") > -1) {
164                                ascfileshortname = ascfilename.substring(ascfilename
165                                                .lastIndexOf("\\") + 1, ascfilename.length());
166                        } else {
167                                ascfileshortname = ascfilename;
168                        }
169                        System.out.println("ascfileshortname: " + ascfileshortname);
170                        temp = outputFilename.stringValue();
171                        if (temp.trim().length() == 0) {
172                                temp = ascfilename;
173                                int dotloc = temp.lastIndexOf(".");
174                                temp = temp.substring(0, dotloc);
175                                temp = temp + ".out";
176                                outputFilename.setExpression(temp);
177                                ascOutfilename = outputFilename.asFile().getPath();
178                        } else { // check to see if the output file is a dir
179                                File of = outputFilename.asFile();
180                                if (of.isDirectory()) {
181                                        temp = ascfileshortname;
182                                        int dotloc = temp.lastIndexOf(".");
183                                        temp = temp.substring(0, dotloc);
184                                        temp = temp + ".out";
185                                        ascOutfilename = of.getPath() + "/" + temp;
186                                } else {
187                                        ascOutfilename = outputFilename.asFile().getPath();
188                                }
189                        }
190
191                        System.out.println("ascOutfilename: " + ascOutfilename);
192
193                        File file = new File(ascfilename);
194
195                        if (file.exists()) {
196                                inputGrid = new Grid(file, !useDiskValue); // a 'false'
197                                                                                                                        // !useDiskValue
198                                                                                                                        // forces the use of
199                                                                                                                        // disk rather than
200                                                                                                                        // RAM
201                                double minval;
202                                try {
203                                        minval = ((DoubleToken) minvalParameter.getToken())
204                                                        .doubleValue();
205                                } catch (Exception w) {
206                                        minval = -1.0e-9;
207                                }
208
209                                double maxval;
210                                try {
211                                        maxval = ((DoubleToken) maxvalParameter.getToken())
212                                                        .doubleValue();
213                                } catch (Exception w) {
214                                        maxval = 1.0e+9;
215                                }
216                                boolean newvalFlag = true;
217                                double newval = 0.0;
218                                try {
219                                        newval = ((DoubleToken) newvalParameter.getToken())
220                                                        .doubleValue();
221                                } catch (Exception w) {
222                                        // if newval is not a number (e.g. empty) set flag
223                                        // this will often be the case when we want to scale or
224                                        // shift values
225                                        newvalFlag = false;
226                                }
227
228                                double multFactor;
229                                try {
230                                        multFactor = ((DoubleToken) multiplicationFactor.getToken())
231                                                        .doubleValue();
232                                } catch (Exception w) {
233                                        multFactor = 1.0;
234                                }
235                                double addFactor;
236                                try {
237                                        addFactor = ((DoubleToken) additionParameter.getToken())
238                                                        .doubleValue();
239                                } catch (Exception w) {
240                                        addFactor = 0.0;
241                                }
242                                if (newvalFlag) {
243                                        newg = inputGrid.reset(minval, maxval, newval);
244                                } else {
245                                        newg = inputGrid.transform(minval, maxval, multFactor,
246                                                        addFactor);
247                                }
248                                newg.createAsc(ascOutfilename);
249                                System.out.println("ready to send: " + ascOutfilename);
250
251                                output.broadcast(new StringToken(ascOutfilename));
252                        } else {
253                                throw new IllegalActionException("Input file " + ascfilename
254                                                + " does not exist.");
255                        }
256                } catch (Exception eee) {
257                        throw new IllegalActionException("Problem Reading File");
258                }
259        }
260
261        /**
262         * Post fire the actor. Return false to indicate that the process has
263         * finished. If it returns true, the process will continue indefinitely.
264         * 
265         *       */
266        public boolean postfire() throws IllegalActionException {
267                inputGrid.delete();
268                newg.delete();
269                return super.postfire();
270        }
271
272        /**
273         * Pre fire the actor. Calls the super class's prefire in case something is
274         * set there.
275         * 
276         *       *@exception IllegalActionException
277         */
278        public boolean prefire() throws IllegalActionException {
279                return super.prefire();
280        }
281
282}