Skip to content

Instantly share code, notes, and snippets.

@selimnairb
Last active October 13, 2015 01:31
Show Gist options
  • Save selimnairb/fe6a9f204d1e18ee0310 to your computer and use it in GitHub Desktop.
Save selimnairb/fe6a9f204d1e18ee0310 to your computer and use it in GitHub Desktop.
Python/Pandas code for converting an irregular timeseries of tipping bucket rain gage data to hourly rainfall accumulations
#!/usr/bin/env python
import sys
import pandas as pd
df = pd.read_csv(sys.argv[1], delimiter='|', index_col=0, parse_dates=True, header=0,
names=['timestamp', 'tips_in', 'qual', 'interp', 'remarks'])
monthly_precip_in = df.tips_in.resample('H', how='sum')
monthly_precip_mm = monthly_precip_in * 25.4
monthly_precip_mm.name = 'precip_mm'
monthly_precip_mm.to_csv(sys.argv[2], header=True)
@selimnairb
Copy link
Author

Input data are of format:

TimeStamp|Tips (in)|Quality code|interpolation|Remarks
2009-04-11 03:56:58|0.00|200|1| 
2009-04-11 03:58:38|0.01|200|1| 
2009-04-11 04:10:24|0.01|200|1|

Where Tips (in) is a tip equal to 0.01 inches.

Output data are of format:

timestamp,precip_mm
2009-04-11 03:00:00,0.254
2009-04-11 04:00:00,1.27
2009-04-11 05:00:00,3.048

Where precip_mm is rainfall accumulation for the hour, in millimeters.

To run:

python tipping_bucket_to_hourly.py PATH/TO/TIPPING_BUCKET_DATA.txt MYOUTPUTFILE.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment