Skip to content

Instantly share code, notes, and snippets.

@the-arkhive
Created June 15, 2015 20:13
Show Gist options
  • Save the-arkhive/60f9fa39002e55f1a8d0 to your computer and use it in GitHub Desktop.
Save the-arkhive/60f9fa39002e55f1a8d0 to your computer and use it in GitHub Desktop.
Radar processing program. We'll call this one version 2.1. It adds various user prompts to set up the program and allow for more comprehensive use.
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileData {
public static void main(String[] args) throws IOException {
System.out.println("How many files do you have to process?");
Scanner FileNum = new Scanner(System.in);
int numOfFiles = FileNum.nextInt();
//The default output file location
String file_path_out = "/Users/Thomas_Williams/Desktop/DataReader v2.1/FinalData.csv";
//Default coordinates for the desired rectangle, Lat1 will be the southern bound, Lat2 the northern, Lon1 the western and Lon2 the eastern.
double Lat1 = 76.00;
double Lat2 = 76.8;
double Lon1 = -69.17;
double Lon2 = -65.93;
for (int FilesProcessed = 0; FilesProcessed < numOfFiles; FilesProcessed++) {
System.out.println("");
System.out.println("\\\\----------------------------------------------------------------------------//");
System.out.println("");
//Creates the scanners and varaibles needed to take user input for the source file.
System.out.println("Please drag and drop the source data file into the colsole now.");
Scanner Input = new Scanner(System.in);
String file_path_in = Input.nextLine();
//Creates the scanners and varaibles needed to take user input for the output file.
Scanner Output = new Scanner(System.in);
if (FilesProcessed == 0) {
System.out.println("");
System.out.println("\\\\----------------------------------------------------------------------------//");
System.out.println("");
System.out.println("The default output file is:");
System.out.println(file_path_out);
System.out.println("");
System.out.println("If you would like to change the output file enter '1' if not enter '0'");
int Output_check = Input.nextInt();
if (Output_check == 1) {
System.out.println("");
System.out.println("Please drag and drop the new output file into the console now.");
file_path_out = Output.nextLine();
} else if (Output_check == 0) {
file_path_out = file_path_out;
}
} else {
file_path_out = file_path_out;
}
if (FilesProcessed == 0) {
System.out.println("");
System.out.println("\\\\----------------------------------------------------------------------------//");
System.out.println("");
System.out.println("The default coordinates are:");
System.out.println(" Northern Bound: " + Lat2);
System.out.println(" Southern Bound: " + Lat1);
System.out.println(" Eastern Bound: " + Lon1);
System.out.println(" Western Bound: " + Lon2);
System.out.println("");
System.out.println("If you would like to change these coordinates enter '1' if not enter '0'");
int Output_check = Input.nextInt();
if (Output_check == 1) {
System.out.println("Please enter your coordinates separated by typing the northern bound then hitting enter, then the southern bound, enter and so on.");
Lat2 = Output.nextInt();
Lat1 = Output.nextInt();
Lon1 = Output.nextInt();
Lon2 = Output.nextInt();
} else if (Output_check == 0) {
Lat1 = Lat1;
Lat2 = Lat2;
Lon1 = Lon1;
Lon2 = Lon2;
}
} else {
Lat1 = Lat1;
Lat2 = Lat2;
Lon1 = Lon1;
Lon2 = Lon2;
}
System.out.println("");
System.out.println("\\\\----------------------------------------------------------------------------//");
System.out.println("");
//If for some reason you want to empty the output file each time you process a source file set this to false, otherwise leave it as true.
Boolean append = true;
//Executes ReadFile if able otherwise throws an exception
try {
ReadFile file = new ReadFile(file_path_in, file_path_out, append, Lat1, Lat2, Lon1, Lon2);
String[] aryLines = file.OpenFile();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("");
System.out.println("\\\\----------------------------------------------------------------------------//");
System.out.println("");
System.out.println("Data processing complete.");
}
}
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class ReadFile {
//Variable declarations
private int processedCount = 0;
private double ThicknessBound = -9999.00;
private String path;
private String path_out;
private boolean append;
private double botLat;
private double topLat;
private double botLon;
private double topLon;
public ReadFile (String file_path_in, String file_path_out , boolean append_value, double lowerLat, double upperLat, double lowerLon, double upperLon) {
path = file_path_in;
path_out = file_path_out;
append = append_value;
botLat = lowerLat;
topLat = upperLat;
botLon = lowerLon;
topLon = upperLon;
}
//Method to read the file and store it in an array
public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for (int i = 0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();
//Extracts the first two pieces of data and line number
String fullData = textData[i];
String[] splitData = fullData.split(",");
String textLat = splitData[0];
String textLon = splitData[1];
Double numLat = new Double("0");
Double numLon = new Double("0");
String testTickness = splitData[3];
Double numThickness = new Double("0");
//Converts the contents of the text data into doubles
double Lat = numLat.valueOf(textLat);
double Lon = numLon.valueOf(textLon);
double Thickness = numThickness.valueOf(testTickness);
//Checks for data that falls within the user defined bounds
if (Lat <= topLat && Lat >= botLat && Lon <= topLon && Lon >= botLon && Thickness != ThicknessBound) {
WriteFile output = new WriteFile(path_out, append);
output.writeToFile(textData[i]);
processedCount++;
System.out.println(Lat + " " + Lon);
} else {
processedCount++;
}
if (processedCount == numberOfLines) {
System.out.println("");
System.out.println("There is no more data to process in this file.");
}
}
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null) {
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class WriteFile{
private String path;
private Boolean append_to_file = true;
public WriteFile(String file_path_out , boolean append_value) {
path = file_path_out;
append_to_file = append_value;
}
public void writeToFile(String textData) throws IOException {
FileWriter write = new FileWriter(path , append_to_file);
PrintWriter print_line = new PrintWriter(write);
//Appends the text file
print_line.printf( "%s" + "%n" , textData);
print_line.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment