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;
033
034import ptolemy.actor.TypedIOPort;
035import ptolemy.actor.lib.Transformer;
036import ptolemy.data.BooleanToken;
037import ptolemy.data.DoubleToken;
038import ptolemy.data.StringToken;
039import ptolemy.data.expr.Parameter;
040import ptolemy.data.expr.StringParameter;
041import ptolemy.data.type.BaseType;
042import ptolemy.kernel.CompositeEntity;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045
046/**
047 * This actor finds the value of a given position in an asc grid. Inputs include
048 * the position point (in gis coordinates) and an *.asc gis file. Output is the
049 * interpreted value using the indicated interpolation algorithm.
050 * 
051 * @author Dan Higgins NCEAS UC Santa Barbara
052 */
053public class AscGridValue extends Transformer {
054
055        /**
056         * 'xLocation' is the xLocation (longitude) where the value is to be
057         * calculated
058         */
059        public TypedIOPort xLocation = new TypedIOPort(this, "xLocation", true,
060                        false);
061
062        /**
063         * 'yLocation' is the yLocation (latitude) where the value is to be
064         * calculated
065         */
066        public TypedIOPort yLocation = new TypedIOPort(this, "yLocation", true,
067                        false);
068
069        /**
070         * Boolean setting to determine whether or not to use disk for storing grid
071         * data rather than putting all data in RAM arrays
072         */
073        public Parameter useDisk;
074
075        /**
076         * This is the algoritm to be used in calculating cell values in the output
077         * grid from the values in the input grid Currently there are two choices:
078         * 'Nearest Neighbor' or 'Inverse Distance Weighted'
079         */
080        public StringParameter algorithm;
081
082        private Grid inputGrid;
083
084        private int algorithmToUse = _NEAREST_NEIGHBOR;
085        private static final int _NEAREST_NEIGHBOR = 0;
086        private static final int _INVERSE_DISTANCE = 1;
087
088        /**
089         * constructor
090         * 
091         *@param container
092         *            The container.
093         *@param name
094         *            The name of this actor.
095         *@exception IllegalActionException
096         *                If the actor cannot be contained by the proposed
097         *                container.
098         *@exception NameDuplicationException
099         *                If the container already has an actor with this name.
100         */
101        public AscGridValue(CompositeEntity container, String name)
102                        throws NameDuplicationException, IllegalActionException {
103                super(container, name);
104
105                algorithm = new StringParameter(this, "algorithm");
106                algorithm.setExpression("Nearest Neighbor");
107                algorithm.addChoice("Nearest Neighbor");
108                algorithm.addChoice("Inverse Distance");
109
110                useDisk = new Parameter(this, "useDisk");
111                useDisk.setDisplayName("Use disk storage (for large grids)");
112                useDisk.setTypeEquals(BaseType.BOOLEAN);
113                useDisk.setToken(BooleanToken.TRUE);
114
115                xLocation.setTypeEquals(BaseType.DOUBLE);
116                yLocation.setTypeEquals(BaseType.DOUBLE);
117                output.setTypeEquals(BaseType.DOUBLE);
118                input.setTypeEquals(BaseType.STRING);
119        }
120
121        /**
122         * 
123         * 
124         *@exception IllegalActionException
125         *                If there is no director.
126         */
127        public void fire() throws IllegalActionException {
128
129                String temp = "";
130                double xloc = 0.0;
131                double yloc = 0.0;
132                double asc_value = 1.0E101;
133
134                super.fire();
135                if (xLocation.getWidth() > 0) { // has a connection
136                        if (xLocation.hasToken(0)) { // has a token
137
138                                try {
139                                        DoubleToken xtoken = (DoubleToken) xLocation.get(0);
140                                        xloc = xtoken.doubleValue();
141                                } catch (Exception w) {
142                                        xloc = 0.0;
143                                }
144                        }
145                }
146
147                if (yLocation.getWidth() > 0) { // has a connection
148                        if (yLocation.hasToken(0)) { // has a token
149
150                                try {
151                                        DoubleToken ytoken = (DoubleToken) yLocation.get(0);
152                                        yloc = ytoken.doubleValue();
153                                } catch (Exception w) {
154                                        xloc = 0.0;
155                                }
156                        }
157                }
158
159                try {
160                        String ascfilename = null;
161                        String ascOutfilename = null;
162                        String ascfileshortname = null;
163                        int width = input.getWidth();
164
165                        boolean useDiskValue = ((BooleanToken) useDisk.getToken())
166                                        .booleanValue();
167
168                        for (int iii = 0; iii < width; iii++) {
169                                ascfilename = ((StringToken) input.get(iii)).stringValue();
170                                System.out.println("ascfilename: " + ascfilename);
171                                if (ascfilename.indexOf("/") > -1) {
172                                        ascfileshortname = ascfilename.substring(ascfilename
173                                                        .lastIndexOf("/") + 1, ascfilename.length());
174                                } else if (ascfilename.indexOf("\\") > -1) {
175                                        ascfileshortname = ascfilename.substring(ascfilename
176                                                        .lastIndexOf("\\") + 1, ascfilename.length());
177                                } else {
178                                        ascfileshortname = ascfilename;
179                                }
180                                // System.out.println("ascfileshortname: "+ascfileshortname);
181                        }
182
183                        File file = new File(ascfilename);
184
185                        if (file.exists()) {
186                                inputGrid = new Grid(file, !useDiskValue); // a 'false'
187                                                                                                                        // !useDiskValue
188                                                                                                                        // forces the use of
189                                                                                                                        // disk rather than
190                                                                                                                        // RAM
191                                // System.out.println("nrows: "+inputGrid.nrows);
192                                // System.out.println("ncols: "+inputGrid.ncols);
193                                // System.out.println("delx: "+inputGrid.delx);
194                                // System.out.println("dely: "+inputGrid.dely);
195
196                                temp = algorithm.stringValue();
197                                if (temp.equals("Inverse Distance")) {
198                                        algorithmToUse = _INVERSE_DISTANCE;
199                                } else {
200                                        algorithmToUse = _NEAREST_NEIGHBOR;
201                                }
202
203                                asc_value = inputGrid.interpValue(xloc, yloc, algorithmToUse);
204
205                                if (inputGrid != null) {
206                                        inputGrid.delete();
207                                }
208
209                                if (asc_value < 1.0E100) {
210                                        output.broadcast(new DoubleToken(asc_value));
211                                } else {
212                                        output.broadcast(DoubleToken.NIL);
213                                }
214                        }
215
216                } catch (Exception eee) {
217                        throw new IllegalActionException("Problem Reading File");
218                }
219        }
220
221}