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.garp;
031
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.StringToken;
035import ptolemy.data.expr.FileParameter;
036import ptolemy.data.type.BaseType;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040
041/**
042 * GARP is a computer program for predicting species locations based on various
043 * spatial data sets of environment variables and known species locations. GARP
044 * is an acronym for Genetic Algorithm for Rule Set Production. GARP was
045 * originally ceated by David Stockwell. The version in Kepler is based on
046 * 'Desktop GARP', http://www.lifemapper.org/desktopgarp/. The
047 * GarpPresampleLayers actor carries out the first step in a GARP calculation to
048 * be executes as a Kepler workflow. It samples the environment data and known
049 * locations to create a 'CellSet' of information to be used in later steps. As
050 * input, it takes several file names. One is a summary of spatial grids
051 * containing things like environmental data (e.g. temperature, precipitation,
052 * etc.) A number of these spatial grids are summarized in an xml file (named
053 * *.dxl). The second major input is locations file that contains a list of
054 * known spatial locations where the species of interest has been found. This
055 * information is 'presampled' to create a CellSet to be used in subsequent
056 * pieces of the GARP calculation. There is also an input that should give the
057 * file name to be used to store this CellSet data. The output port provides the
058 * file name when the file has been actually written. (The output is thus a
059 * 'trigger'.)
060 * 
061 * This is a JNI-based actor. It requires the following: linux: libgarp.so
062 * windows: garp.dll, libexpat.dll MacOSX - currently not available for the Mac
063 * (3/16/2006)
064 * 
065 * @author Chad Berkeley, Dan Higgins, NCEAS, UC Santa Barbara
066 */
067public class GarpPresampleLayers extends TypedAtomicActor {
068        /**
069         * This parameter is the file name of the xml file that summarizes all the
070         * spatial layers to be used in the GARP calculation. The file name is of
071         * the *.dxl format. Examine samples to see the content.
072         */
073        public FileParameter layersetFilenameParameter = new FileParameter(this,
074                        "layersetFilenameParameter");
075        /**
076         * This parameter is the file name of the file that contains the known
077         * locations to be used to determine a ruleset for predicting other
078         * locations from the spatial environment layers. The file is a text file
079         * with one location per line. Each line contains a point location (x,y)
080         * with the numeric values of x and y separated by a 'tab' character.
081         * Typically the (x,y) is (longitude, latitude).
082         */
083        public FileParameter dataPointFileNameParameter = new FileParameter(this,
084                        "dataPointFileNameParameter");
085        /**
086         * This parameter is the file name to be used in saving the cell set
087         * information. This information is a sample to be used by GARP in creating
088         * the rule set for predicting occurrences.
089         */
090        public FileParameter cellSetFileNameParameter = new FileParameter(this,
091                        "cellSetFileNameParameter");
092        // input ports
093        /**
094         * This input port can be used to supply the file name of the xml file that
095         * summarizes all the spatial layers to be used in the GARP calculation. The
096         * file name is of the *.dxl format. Examine samples to see the content.
097         */
098        public TypedIOPort layersetFilename = new TypedIOPort(this,
099                        "layersetFilename", true, false);
100
101        /**
102         * This input port can be used to supply the file name of the file that
103         * contains the known locations to be used to determine a ruleset for
104         * predicting other locations from the spatial environment layers. The file
105         * is a text file with one location per line. Each line contains a point
106         * location (x,y) with the numeric values of x and separated by a 'tab'
107         * character. Typically the (x,y) is (longitude, latitude).
108         */
109        public TypedIOPort dataPointFileName = new TypedIOPort(this,
110                        "dataPointFileName", true, false);
111
112        /**
113         * input port can be used to supply the file name to be used in saving the
114         * cell set information. This information is a sample to be used by GARP in
115         * creating the rule set for predicting occurrences.
116         */
117        public TypedIOPort cellSetFileName = new TypedIOPort(this,
118                        "cellSetFileName", true, false);
119        // output ports
120        /**
121         * This output port supplies the file name of the cellSetFile. It has the
122         * same name as supplied in the input port, but it is only fired after the
123         * data has been calculated. It is thus used as a 'trigger' for the next
124         * step in the GARP calculation.
125         */
126        public TypedIOPort cellSetFileNameOutput = new TypedIOPort(this,
127                        "cellSetFileNameOutput", false, true);
128
129        /**
130         * Garp presample layer actor
131         */
132        public GarpPresampleLayers(CompositeEntity container, String name)
133                        throws NameDuplicationException, IllegalActionException {
134                super(container, name);
135                layersetFilename.setTypeEquals(BaseType.STRING);
136                dataPointFileName.setTypeEquals(BaseType.STRING);
137                cellSetFileName.setTypeEquals(BaseType.STRING);
138                cellSetFileNameOutput.setTypeEquals(BaseType.STRING);
139
140                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
141                                + "width=\"66\" height=\"42\" " + "style=\"fill:white\"/>\n"
142                                + "<text x=\"12\" y=\"16\" "
143                                + "style=\"font-size:14; fill:blue; font-family:SansSerif\">"
144                                + "GARP</text>\n" + "<text x=\"4\" y=\"34\" "
145                                + "style=\"font-size:12; fill:blue; font-family:SansSerif\">"
146                                + "Presample</text>\n" + "</svg>\n");
147
148        }
149
150        /**
151   *
152   */
153        public void initialize() throws IllegalActionException {
154        }
155
156        /**
157   *
158   */
159        public boolean prefire() throws IllegalActionException {
160                return super.prefire();
161        }
162
163        /**
164   *
165   */
166        public void fire() throws IllegalActionException {
167                System.out.println("Firing GarpPresampleLayers");
168                super.fire();
169                String layersetFilenameStr = "";
170                String dataPointFileNameStr = "";
171                String cellSetFileNameStr = "";
172                if (layersetFilename.numberOfSources() > 0) {
173                        if (!layersetFilename.hasToken(0))
174                                return;
175                        StringToken layerSetFileNameToken = (StringToken) layersetFilename
176                                        .get(0);
177                        layersetFilenameStr = layerSetFileNameToken.stringValue();
178                        layersetFilenameParameter.setExpression(layersetFilenameStr);
179                } else {
180                        layersetFilenameStr = layersetFilenameParameter.asFile().getPath();
181                }
182
183                if (dataPointFileName.numberOfSources() > 0) {
184                        if (!dataPointFileName.hasToken(0))
185                                return;
186                        StringToken dataPointFileNameToken = (StringToken) dataPointFileName
187                                        .get(0);
188                        dataPointFileNameStr = dataPointFileNameToken.stringValue();
189                        dataPointFileNameParameter.setExpression(dataPointFileNameStr);
190                } else {
191                        dataPointFileNameStr = dataPointFileNameParameter.asFile()
192                                        .getPath();
193                }
194
195                if (cellSetFileName.numberOfSources() > 0) {
196                        if (!cellSetFileName.hasToken(0))
197                                return;
198                        StringToken cellSetFileNameToken = (StringToken) cellSetFileName
199                                        .get(0);
200                        cellSetFileNameStr = cellSetFileNameToken.stringValue();
201                        cellSetFileNameParameter.setExpression(cellSetFileNameStr);
202                } else {
203                        cellSetFileNameStr = cellSetFileNameParameter.asFile().getPath();
204                }
205
206                System.out.println("Starting GarpPresampleLayers JNI Code");
207                new GarpJniGlue().PresampleLayers(layersetFilenameStr,
208                                dataPointFileNameStr, cellSetFileNameStr);
209
210                cellSetFileNameOutput.broadcast(new StringToken(cellSetFileNameStr));
211
212                System.out.println("Finished with GarpPresampleLayers JNI Code");
213        }
214}