Need to handle dates to implement a project I am working on mapping the growth of the tea party but javascript is getting in the way
in
Programming Questions
•
1 years ago
I have been trying to write a sketch that will increment over a dataset and progressively map events so that I can stitch them together. Before I do that I am trying to read through a csv and convert date strings to Date objects so that I can store the dates as a uniform number (like number of days since some set time). This will allow me to read the begin date of the file and the end date and help me increment through the data. Having read through some documentation on JavaScript date handling libraries I am a bit frustrated as I cant seem to make what seems like it should be easy work.
Here is my code, it is long but the important part is just the small bit that is highlighted. If anybody has any suggestions I would be immensely grateful.
Here is my code, it is long but the important part is just the small bit that is highlighted. If anybody has any suggestions I would be immensely grateful.
- //date handling modules
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
//indeces for each columnLAT
int UID = 0;
int LATITUDE = 1;
int LONGITUDE = 2;
int GRNAME = 3;
int DATE = 4;
int OCCURANCE = 5;
void setup(){
String[] lines = loadStrings("Tparty_geocoded_slim.csv");
//split the row into pieces allong each comma
String[] firstLine = split(lines[1], '\t');
scrubQuotes(firstLine);
//remove extra whitespace on either side of each column
firstLine = trim(firstLine);
String beginDate = firstLine[DATE];
//split the row into pieces allong each comma
String[] endLine = split(lines[(lines.length-1)], '\t');
scrubQuotes(endLine);
//remove extra whitespace on either side of each column
endLine = trim(endLine);
String endDate = endLine[DATE];
//set min and max values arbitrarily high
float minX = 1;
float maxX = -1;
float minY = 1;
float maxY = -1;
//set up an array for the cleaned data
String[] cleaned = new String[lines.length];
//number of cleaned entries found
int placeCount = 0;
//Start at row 1 because the first row is the column titles.
for (int row = 1; row < lines.length; row++){
//split the row into pieces allong each comma
String[] data = split(lines[row], '\t');
scrubQuotes(data);
//remove extra whitespace on either side of each column
data = trim(data);
// here is where I am trying to parse date[DATE] (a text string) into a date object so that I can store the //dates value as a number of continuous days since ?epoch? later on when I print to a text file. cant seem to //make it work though it seems like it should be easy. I hate the way javascript handles dates. Really //frustrating. I would be enormously grateful for any help anyone could confer.
DateFormat formatter ;
Date days;
try {
formatter = new SimpleDateFormat("MM/dd/yyyy");
days = formatter.parse(data[DATE]);
} catch (ParseException e)
//exclude meetings in non-contiguous US States (hawaii/alaska)
float longitude = Float.parseFloat(data[LONGITUDE]);
if (longitude < -127) continue;
float lat = float(data[LATITUDE]);
float lon = float(data[LONGITUDE]);
float phi0 = 0;
float lambda0 = radians(-96);
float phi1 = radians(29.5f);
float phi2 = radians(45.5f);
float phi = radians(lat);
float lambda = radians(lon);
float n = 0.5f * (sin(phi1) + sin(phi2));
float theta = n * (lambda - lambda0); //radians(lon-labda0)
float c = sq(cos(phi1)) + 2*n*sin(phi1);
float rho = sqrt(c - 2*n*sin(phi))/n;
float rho0 = sqrt(c - 2*n*sin(phi0))/n;
float x = rho * sin(theta);
float y = rho0 - rho*cos(theta);
if (x > maxX) maxX = x;
if (x < minX) minX = x;
if (y > maxY) maxY = y;
if (y < minY) minY = y;
cleaned[placeCount++] = data[UID] + "\t" +
y + "\t" +
x + "\t" +
data[LATITUDE] + "\t" +
data[LONGITUDE] + "\t" +
data[DATE] + "\t" +
data[OCCURANCE] + "\t" +
days;
}
//write to a file called "cleanT.tsv" in the sketch folder
PrintWriter tsv = createWriter("cleanTcontigUSb.tsv");
//use the first line to specify the number of data points in the file,
//allong with the minimum and maximum lat and long using
//# to make the line as different from the other ones.
tsv.println("#" + placeCount + "," + minX + "," + maxX + "," + minY + "," + maxY + "," + beginDate + "," + endDate);
for (int i = 0; i < placeCount; i++) {
tsv.println(cleaned[i]);
}
tsv.flush();
tsv.close();
println("Finished.");
exit();
}
// Parse quotes from CSV or TSV data. Quotes areound a column are common,
//and actual double quotes (") are specified by two double quotes("")
void scrubQuotes(String[] array){
for (int i = 0; i < array.length; i++){
if (array[i].length() > 2){
//Remove quotes from start and end if present
if(array[i].startsWith("\"") && array[i].endsWith("\"")){
array[i] = array[i].substring(1, array[i].length() - 1);
}
}
array[i] = array[i].replaceAll("\"\"", "\"");
}
}
String fixCapitals(String title) {
char[] text = title.toCharArray();
//If set to true, the next letter will nbe capitalized.
boolean capitalizeNext = true;
for (int i = 0; i < text.length; i++) {
if(Character.isSpace(text[i])) {
capitalizeNext = true;
} else if (capitalizeNext) {
text[i] = Character.toUpperCase(text[i]);
capitalizeNext = false;
} else {
text[i] = Character.toLowerCase(text[i]);
}
}
return new String(text);
}
1