Re: php unpack function

From: Date: Wed, 23 May 2001 22:42:58 +0000
Subject: Re: php unpack function
Groups: php.general 
Request: Send a blank email to php-general+get-54127@lists.php.net to get a copy of this message
Addressed to: Chris.Mattmann@jpl.nasa.gov php-general@lists.php.net ** Reply to note from Chris Mattmann <Chris.Mattmann@jpl.nasa.gov> Wed, 23 May 2001 13:28:36 -0700 > For instance, my file is basically just thousands of temperature > values, taken 4 times a day, over a period of 20 years. Let's > say I wanted to get a year's worth of temperatures, from the 10th > year into the file (so I would need the 4x365x10) th temperature > value from the file. I don't quite understand what you are doing, but this might be close. If not, give me more information on the source data, how you want to select which days to inspect, and what you want to do with the output. This code reads from a file that contains thousands of temp records back to back, with no separators and displays all the temps, one day per line for each day within a range of day numbers. Display is to a web page and not very well formatted. # Set these to fit your situation $TempSize = x; # Length of temperature value in bytes. $TempFormat = '???'; # Unpack format string for a single day. $FileName = 'tempfile.data'; $StartDate = 365 * 10; # Start of tenth year $EndDate = 365 * 11; # Start of eleventh year. # End date is not displayed $BlockSize = 20; # Set for good performance/memory use # Here goes... $F = fopen( $FileName, 'rb' ); $DayLength = 4 * $TempSize; for( $I = $StartDate; $I < $EndDate; $I += $BlockSize ) { $SeekPos = $I * $TempSize; if( ( $I + $BlockSize ) > $EndDate ) $BlockSize = $EndDate - $I; fseek( $F, $SeekPos, SEEK_SET ); $Data = fread( $F, $BlockSize * $TempSize ); for( $J = 0; $J < $BlockSize; $J ++ ) { $DayPos = $J * 4 * $TempSize; $DayData = substr( $Data, $DayPos, $DayPos + DayLength ); $DayTemps = unpack( $TempFormat, $DayData ); echo "Day: ", $I + $J, " - ", $DayTemps[ 1 ], " ", $DayTemps[ 2 ], " ", $DayTemps[ 3 ], " ", $DayTemps[ 4 ], "<BR>\n"; } } This is not tested, but should be pretty close. You may have to add or subtract one from some of the control conditions in the ifs or the places where I setup values. Rick Widmer Internet Marketing Specialists http://www.developersdesk.com

« previous php.general (#54127) next »