Created
March 20, 2015 16:25
-
-
Save seumasmorrison/3dad672e81a7a2de5a7b to your computer and use it in GitHub Desktop.
Helper function for processing the historical spectral and wave statistics files as well as data exported from DIWASP via write_csv in Datawell_to_DIWASP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from datetime import datetime | |
from scipy.stats.stats import pearsonr | |
his_columns = ['date_time', 'tp', 'dirp', 'sprp', 'tz', 'hm0', 'ti', 't1', | |
'tc', 'tdw2', 'tdw1', 'tpc', 'nu','eps','qp','ss','tref','tsea', | |
'bat'] | |
hiw_columns = ['date_time','% no reception errors','hmax','tmax','h1_10', | |
't1_10','h1_3','t1_3','Hav','Tav','Eps','#Waves'] | |
def apply_date_time_index(df): | |
df = df[pd.notnull(df.date_time)] | |
date_times = [] | |
for index,date_time_string in enumerate(df['date_time'].values): | |
if type(date_time_string) == type(''): | |
date_time = datetime.strptime(date_time_string[:-5], | |
"%Y-%m-%dT%H:%M:%S") | |
date_times.append(date_time) | |
df.index = pd.DatetimeIndex(date_times) | |
return df | |
def process_hist(hist_df): | |
hist_df = apply_date_time_index(hist_df) | |
hist_df = hist_df.drop('date_time', axis = 1) | |
hist_df = hist_df.resample('30Min') | |
return hist_df | |
def read_hist_csv_and_time_index(filepath, columns= his_columns): | |
his_df = pd.read_csv(filepath, names = columns) | |
return process_hist(his_df) | |
# Reads in csv files output from Matlab DIWASP toolbox | |
# and outputs timestamped Pandas Dataframes | |
def read_diwasp_csv(file_path, date_format="%Y-%m-%dT%Hh%MZ"): | |
"""Function for reading csv files created by the write_csv.m matlab file | |
which output file_names, Hm0 and peak period | |
""" | |
df = pd.read_csv(file_path) | |
date_time_array = [] | |
for date_time_string in df['file_name'].values: | |
date_time_array.append(datetime.strptime(date_time_string[-21:-4], | |
date_format)) | |
df.index = pd.DatetimeIndex(date_time_array) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment