-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvProcessing.php
More file actions
99 lines (89 loc) · 2.12 KB
/
csvProcessing.php
File metadata and controls
99 lines (89 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
include('dataPaths.inc');
$UTC_timezone = new DateTimeZone('UTC');
$currentDate = date("mdY");
/*
Opens a CSV file and outputs an entire column
*/
function processCSV($date, $ID, $columns) {
global $DATA_DIR;
$dir_handle = @opendir($DATA_DIR.$ID) or die("Error: Cannot open the data folder for detector id: ".$ID);
$handle = fopen($DATA_DIR.$ID.'/'.$date.'.csv',"r");
$skipFirstHeader = TRUE;
$useSecondHeaderInput = TRUE;
while($data = fgetcsv($handle, 0)) {
if ($skipFirstHeader)
$skipFirstHeader = FALSE;
else {
foreach($columns as $value) {
$outputData[] = $data[$value];
}
$output = implode(',', $outputData);
$meta["data"][] = $output;
if ($useSecondHeaderInput) {
$useSecondHeaderInput = FALSE;
$meta["time"][] = "Time in UTC";
}
else
$meta["time"][] = date("Y-m-d H:i", strtotime($data[0]));
unset($outputData);
}
}
fclose($handle);
return $meta;
}
/*
Checks to see if there is a data file on this date
in the detector directories
*/
function doesDateFileExist($ID, $date) {
global $DATA_DIR;
$handle = fopen($DATA_DIR.$ID.'/'.$date.'.csv',"r");
if ($handle === FALSE) {
fclose($handle);
return FALSE;
}
else {
fclose($handle);
return TRUE;
}
}
/*
Reads in the detector directories and outputs a
an array of ID's.
*/
function getDetectorIDs() {
global $DATA_DIR;
if ($handle = opendir($DATA_DIR)) {
while (FALSE !== ($file = readdir($handle))) {
if (strlen($file) == 4 && is_numeric(substr($file, -1))) {
$meta[] = $file;
}
}
}
closedir($handle);
return $meta;
}
function getData($date) {
$columns = func_get_args();
array_shift($columns);
$meta = buildDataArray($date, $columns);
}
/*
Main function to retrieve an aurora array based on date.
@param the date to be used.
@param... the columns to output.
*/
function buildDataArray($date, $columns) {
$directoriesForDetectors = getDetectorIDs();
foreach ($directoriesForDetectors as $ID) {
if (doesDateFileExist($ID, $date)) {
$activeDetector[] = $ID;
}
}
foreach ($activeDetector as $ID) {
$outputColumns[$ID] = processCSV($date, $ID, $columns);
}
return $outputColumns;
}
?>