001/*
002 *
003 *  Copyright (C) 2001 Numeric Solutions.  All Rights Reserved.
004 *
005 *  Permission is hereby granted, without written agreement and without license 
006 *  or royalty fees, to use, copy, modify, and distribute this software and its 
007 *  documentation for any purpose, provided that the above copyright notice 
008 *  appears in all copies of this software.
009 *  
010 *  Contact information: Numeric Solutions support@numericsolutions.com, or:
011 *
012 *  http://www.numericsolutions.com
013 *
014 */
015package com.numericsolutions.geomodeltools;
016
017import java.io.BufferedReader;
018import java.io.FileOutputStream;
019import java.io.FileReader;
020import java.io.PrintStream;
021import java.util.Vector;
022
023public class GeomodelGlue {
024
025        static {
026                try {
027                        System.out.println("LIBPATH:  {"
028                                        + System.getProperty("java.library.path") + "}");
029                        System.loadLibrary("nsgeotoolsjniglue");
030                } catch (Exception e) {
031                        e.printStackTrace();
032                }
033        }
034
035        /**
036         * function in the nsgrid.cpp code
037         * 
038         * @param grid
039         *            String[]
040         * @param data
041         *            String[]
042         * @param radius
043         *            float
044         * @return String[]
045         */
046        public static native String[] gridDensityByArray(String grid[],
047                        String data[], float radius);
048
049        public static native String[] gridDensityByArrayDimension(String data[],
050                        float radius, float xmin, float xmax, float ymin, float ymax,
051                        float dx, float dy, float pval);
052
053        public String runGridDensityByArrayDimension(String inDataFile, double rad,
054                        double xmin, double xmax, double ymin, double ymax, double dx,
055                        double dy, double pVal) {
056                try {
057                        BufferedReader indata = new BufferedReader(new FileReader(
058                                        inDataFile));
059                        Vector testGridVec = new Vector();
060                        Vector testDataVec = new Vector();
061                        String s;
062                        while ((s = indata.readLine()) != null) {
063                                testDataVec.addElement(s);
064                        }
065
066                        int size = testDataVec.size();
067                        String d[] = new String[size];
068                        for (int i = 0; i < size; i++) {
069                                d[i] = (String) testDataVec.elementAt(i);
070                        }
071
072                        String out[] = GeomodelGlue.gridDensityByArrayDimension(d,
073                                        (float) rad, (float) xmin, (float) xmax, (float) ymin,
074                                        (float) ymax, (float) dx, (float) dy, (float) pVal);
075                        StringBuffer sb = new StringBuffer();
076                        for (int i = 0; i < out.length; i++) {
077                                sb.append(out[i]);
078                                sb.append("\n");
079                        }
080
081                        indata.close();
082                        return sb.toString();
083                } catch (Exception e) {
084                        e.printStackTrace();
085                        return null;
086                }
087        }
088
089        public void runGridDensityByArray(String inGridFile, String inDataFile,
090                        double rad) {
091                try {
092                        BufferedReader ingrid = new BufferedReader(new FileReader(
093                                        inGridFile));
094                        BufferedReader indata = new BufferedReader(new FileReader(
095                                        inDataFile));
096                        Vector testGridVec = new Vector();
097                        Vector testDataVec = new Vector();
098                        String s;
099
100                        while ((s = ingrid.readLine()) != null) {
101                                testGridVec.addElement(s);
102                        }
103                        s = null;
104                        while ((s = indata.readLine()) != null) {
105                                testDataVec.addElement(s);
106                        }
107
108                        int size = testGridVec.size();
109                        String g[] = new String[size];
110                        for (int i = 0; i < size; i++) {
111                                g[i] = (String) testGridVec.elementAt(i);
112                        }
113
114                        size = testDataVec.size();
115                        String d[] = new String[size];
116                        for (int i = 0; i < size; i++) {
117                                d[i] = (String) testDataVec.elementAt(i);
118                        }
119
120                        String out[] = GeomodelGlue.gridDensityByArray(g, d, (float) rad);
121                        // ** TEMPORARILR WRITE THE RESULTS TO STD OUT **//
122                        PrintStream outfile = new PrintStream(new FileOutputStream(
123                                        "out.ns2grid"));
124                        for (int i = 0; i < out.length; i++) {
125                                outfile.println(out[i]);
126                        }
127                        outfile.close();
128                        ingrid.close();
129                        indata.close();
130                } catch (Exception e) {
131                        e.printStackTrace();
132                }
133        }
134
135}