Jump to content

This must be easy , pulling the last record of the day, every day, from a txt


regg001

Recommended Posts

I have a txt file logging weather data every minutes (so 1 record per minute). I want to extract the data from the (and only that one) last record for every single day in the log file.

 

Each record as a $date , $time, and other $data fields.

 

In all my projects i use all the data to create graphs (jpgraphs), but for that one i only need the data from the last record of the day. What php function do i need to read the file and only select the last $time of the $date and pickup every last $time on all $date from the file. Of course when that record is ''the one'' for that day , i need to pull the other $data fields (they'll be use to create the graph).

 

I tried several things, but i'm not able (don't know how) to segregate records looking at different fields in the array and of course pick the right one.

 

I'm sure it's an easy one, but i just don't get the logic to get it done  :confused:

I have my own station and i'm running Cumulus to feed my log file. The files looks like that :

 

.

.

2010-01-08 21:49:11 -8.3 63 -14.1 11.9 12.9 11 0.0 0.0 1011.89 ...

.

.

 

2010-01-08 is $date

21:39:11 is $time

All the other fields have there own $field name

 

There's a new record every minute containing all the data from the station at that moment, and a year of data in the file. Each field of the record is parsed into an array. On most of my graphs i'm using a simple foreach $rawdata as $key and pick up the informations i need to from selected fields (previously parse in an array). With that i can plot the x y y2 coordinates in the graph and come up with the desired results (e.g. 1 data plot per record).

 

The problem i have, i cannot use the same foreach technique to pick up the field's data for that new graph, as i have to segregate / choose / select the last record of each day in the file, and only then pickup the desire fields value to populate the x y y2 coordinates. And i don't want to rely on a condition looking for a timestamp of 23:59 because he might be missing (for whatever reason). The goal is to have 1 data plot per day (and from the last record of that day).

 

The logic is something like : while $date is the same as previous record, look for the highest $time in all the records matching that day ($date) - when found, keep the $date + $fielda + $fieldb - then move to the next $date and repeat the process to find the last record of that other $date. After each last $time for that $date, populate arrays with values from it, where $date will become the x coordinate, and $fielda and $fieldb will become the y1 and y2 coordinates. But as i said, i don't have problem with plotting the data, i have a problem selecting the right record from the file to get only one record for each day from the file , to plot.

 

Thanks for your help.

Go through the file in a foreach.

For each line save the date as old_date and line as old_line and match it against the present date. So all you are really doing is swapping dates after each line loops and finding where the date changes and pushing the previous line to an array so you should end up with an array of the last line of each day.

 

foreach($array as $line){
list(date,$extra)=explode(" ",$line);
if ($old_date != $new_date)
{
array_push($save,$old_line);
}
$old_date=$date;
$old_line=$line;
}

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.