GetClimateWindSoil.java [tools/GetClimateWindSoil/src/getclimatewindsoil] Revision: 7c1f1f8848cbd1aea45f9edcc27ec1f783d215b9  Date: Wed Oct 09 14:50:30 MDT 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[10][2];

            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) {
                            coordinates[count][0] = Double.parseDouble(latLon[0]);
                            coordinates[count][1] = Double.parseDouble(latLon[1]);
                            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 = new ServiceCallThread(coordinates[i][0], coordinates[i][1], latch);
                    tServiceCall.start();
                }

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

}