Does this script run continuously in the background, like a daemon, or is it run via cron or is it manual? If it runs continuously, then you just need to check prior to writing the data out, comparing the current hour value with the last hour value you used during you last processing and use that to create the new data file. If it is run via cron or manually, you'll need to store the last hour value checked when you processed and if it's different, open the new file for writing using the current timestamp. It really depends on how you run the program, which affects how you keep track of the hour value used to determine creation of the new data file.
---
s;;:<).>|\;\;_>?\\^0<|=!]=,|{\$/.'>|<?.|/"&?=#!>%\$|#/\$%{};;y;,'} -/:-@[-`{-};,'}`-{/" -;;s;;$_;see;
Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.