GetClimateWindSoil.java [tools/GetClimateWindSoil/src/getclimatewindsoil] Revision: af24fed480b2b8d52b03aa714e30b7a762e256e0  Date: Tue Dec 03 15:27:33 MST 2019
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package getclimatewindsoil;

import csip.ServiceException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.CountDownLatch;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class GetClimateWindSoil {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ServiceException, FileNotFoundException, IOException, InterruptedException {
        if (args.length >= 1) {
            boolean fileDone = false;
            int lineNumber = 0;
            int maxThreads = 10;

            if (args.length >= 2) {
                maxThreads = Integer.parseInt(args[1]);
            }

            File inputFile = new File(args[0]);
            BufferedReader inputStream = Files.newBufferedReader(inputFile.toPath());
            double[][] coordinates = new double[maxThreads][2];
            String[] sCoordinates = new String[maxThreads];
            boolean gotString = false;

            while (!fileDone) {
                int count = 0;
                while ((count < maxThreads) && (!fileDone)) {
                    String inLine = inputStream.readLine();

                    if (null != inLine) {
                        lineNumber++;
                        String[] latLon = inLine.split(",");
                        if (latLon.length == 2) {
                            gotString = false;
                            coordinates[count][0] = Double.parseDouble(latLon[0]);
                            coordinates[count][1] = Double.parseDouble(latLon[1]);
                            sCoordinates[count] = "";
                            count++;
                        } else {
                            if (latLon.length > 2) {
                                gotString = true;
                                sCoordinates[count] = inLine;
                                coordinates[count][0] = Double.NaN;
                                coordinates[count][1] = Double.NaN;
                                count++;
                            } else {
                                throw new ServiceException("Malformed input file, invalid number of coordinates on line: " + lineNumber);
                            }
                        }
                    } else {
                        fileDone = true;
                    }
                }
                CountDownLatch latch = new CountDownLatch(count);
                for (int i = 0; i < count; i++) {
                    ServiceCallThread tServiceCall = ((!gotString && (!Double.isNaN(coordinates[i][0])) && (!Double.isNaN(coordinates[i][1]))) 
                            ? new ServiceCallThread(coordinates[i][0], coordinates[i][1], latch)
                            : new ServiceCallThread(sCoordinates[i], latch));
                    tServiceCall.start();
                }

                latch.await();
            }
        } else {
            System.err.println("Please specify an input file");
        }
    }

}