Skip to content

Instantly share code, notes, and snippets.

Created August 19, 2016 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4cde37a8614d1c69cc03ec678d36a9d7 to your computer and use it in GitHub Desktop.
Save anonymous/4cde37a8614d1c69cc03ec678d36a9d7 to your computer and use it in GitHub Desktop.
This class processes an EPWT.csv file into marxian format.
package com.example.a1003137m.profitgraph;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
public class FileProcessor extends Thread {
// This class will process a file requested by the user and save it to the Android internal storage
Context app_context;
URL file_url;
String file_name;
public FileProcessor(URL file, Context activity_context, String guess_file) {
// Constructor
app_context=activity_context;
file_url=file;
file_name=guess_file;
}
public void run() {
processFile();
}
public void processFile() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(file_url.openStream()));
String test;
CSVReader reader = new CSVReader(in, ';');
FileOutputStream file_out = app_context.openFileOutput(file_name, Context.MODE_PRIVATE);
CSVWriter writer = new CSVWriter(, ';');
String[] columns = {"Country", "ID", "qual", "year","pop", "fert", "birth", "mort", "N=workforce", "dN", "K", "c", "v", "s", "gdp", "delta", "gamma", "alpha", "r", "requil", "s/v", "K/v"};
java.util.List<String[]> file = reader.readAll();
reader.close();
ArrayList<String[]> epwt = new ArrayList<>();
epwt.add(columns);
String[][] twodarray = new String[file.size()][31];
DecimalFormat df = new DecimalFormat("0.00");
for (int i = 0; i < twodarray.length; i++) {
if (file.get(i)[3].equals("A") || file.get(i)[3].equals("B") || file.get(i)[0].equals("China")) {
twodarray[i] = file.get(i);
}
}
for (int j = 1; j < twodarray.length; j++) {
String[] line = twodarray[j];
double dn = 0;
int temp_pop = Integer.parseInt(line[4]);
temp_pop=temp_pop*1000;
int temp_worker= Integer.parseInt(line[5]);
if (temp_worker>temp_pop) {
//swap pop and worker figures as some are in wrong place
String temp = line[4];
line[4] = line[5];
line[5] = temp;
}
int pop = Integer.parseInt(line[4]);
pop = pop * 1000;
int n = Integer.parseInt(line[5]);
double fertility = 0;
if (line[6].equals("")) {
//Do nothing
} else {
fertility = Double.parseDouble(line[6]);
}
double birth = 0;
if (line[7].equals("")) {
//Do nothing
} else {
birth = (Double.parseDouble(line[7]) / 1000);
}
double mortality = 0;
if (line[8].equals("")) {
//Do nothing
} else {
mortality = (Double.parseDouble(line[8]) / 1000);
}
long gdp = Long.parseLong(line[9]);
long k = Long.parseLong(line[10]);
long c;
if (line[11].equals("")) {
continue;
} else {
c = Long.parseLong(line[11]);
}
double ws;
if (line[19].equals("")) {
continue;
} else {
ws = Double.parseDouble(line[19]);
}
double ipw = Double.parseDouble(line[22]);
double v = ws * gdp;
double s = gdp - v - c;
double delta;
if (line[12].equals("")) {
continue;
} else {
delta = (Double.parseDouble(line[12]) / 100);
}
double gamma;
if (line[26].equals("")) {
continue;
} else {
gamma = (Double.parseDouble(line[26]) / 100);
}
double acc = n * ipw;
double r = s / k;
double s_v = 0;
double k_v = 0;
if (v > 0) {
s_v = s / v;
k_v = k / v;
}
if (j != (twodarray.length - 1)) {
if (twodarray[j][0].equals(twodarray[j + 1][0])) {
dn = Double.parseDouble(twodarray[j + 1][5]);
dn = (dn - n) / n;
double new_acc = (Double.parseDouble(twodarray[j + 1][10]));
acc = new_acc - k + c;
}
}
double alpha = acc / s;
double requil = (dn + delta + gamma) / alpha;
String[] nextline = new String[]{file.get(j)[0], file.get(j)[1], file.get(j)[2], file.get(j)[3], String.valueOf(pop),
String.valueOf(fertility), String.valueOf(birth), String.valueOf(mortality), String.valueOf(n), String.valueOf(dn), String.valueOf(k),
String.valueOf(c), String.valueOf(v), String.valueOf(s), String.valueOf(gdp), String.valueOf(df.format(delta)), String.valueOf(df.format(gamma)), String.valueOf(df.format(alpha)), String.valueOf(df.format(r)),
String.valueOf(df.format(requil)), String.valueOf(df.format(s_v)), String.valueOf(df.format(k_v))};
epwt.add(nextline);
Log.d("Test", Arrays.toString(nextline));
}
writer.writeAll(epwt, false);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment