Skip to content

Instantly share code, notes, and snippets.

View seumasmorrison's full-sized avatar

James Morrison seumasmorrison

View GitHub Profile
@seumasmorrison
seumasmorrison / plot_spectra.py
Created October 21, 2014 15:13
Script for plotting buoy spectra from Datawell Waverider buoy. Pass an absolute file path to plot_spectra function and it will create png files with polar plots for buoy spectra file.
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 21 15:49:56 2014
@author: le12jm
"""
import os
import numpy as np
import matplotlib.cm as cm
@seumasmorrison
seumasmorrison / month_his_to_df.py
Created October 30, 2014 10:17
Takes the path to an Datawell Waverider MKIII HIS(Historical Spectra) file and returns a DateTimeIndexed Dataframe with the option of resampling to 30 minutes.
import pandas as pd
from datetime import datetime
def month_his_to_df(his_file_path, resample=True):
his_columns = ['date_time', 'Tp', 'dirp', 'sprp', 'Tz', 'Hm0', 'TI', 'T1',
'Tc', 'Tdw2', 'Tdw1', 'Tpc', 'nu','eps','QP','Ss','TRef',
'TSea','Bat']
month_dataframe = pd.io.parsers.read_csv(his_file_path, names = his_columns)
date_time_array = []
for date_time_string in month_dataframe['date_time'].values:
@seumasmorrison
seumasmorrison / make_raw_files.py
Created October 30, 2014 14:53
Function for creating raw files from a hebtools displacements DataFrame and creating custom raw files form it.
import pandas as pd
import datetime
import os
def make_raw_files(path_to_raw_df, output_folder):
raw_df = pd.read_pickle(path_to_raw_df)
os.chdir(output_folder)
timestamp = datetime.datetime(2013,2,1,0,17,59)
half_hour_delta = datetime.timedelta(0,1800)
final_timestamp = datetime.datetime(2013,2,28,23,48)
@seumasmorrison
seumasmorrison / polyline_to_polygon.py
Last active August 29, 2015 14:09
Script for taking polyline shapefile to a Polygon shapefile using shapely 1.4.4 - https://pypi.python.org/pypi/Shapely ( Windows binary - http://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely ) and fiona 1.4.8 - https://pypi.python.org/pypi/Fiona. Useful for Ordnance Survey coastline data, the polylines to polygons function in QGIS 2.6 used an olde…
from shapely.geometry import mapping
from shapely.ops import polygonize
import fiona
def polyline_to_polygon(input_file, output_file):
polylines = fiona.open(input_file)
geom = [x['geometry'] for x in polylines]
coords = [x['coordinates']for x in geom[:-1]]
schema = {'geometry': 'Polygon','properties': {'id': 'int'}}
with fiona.open(output_file, 'w', 'ESRI Shapefile', schema) as c:
# translate_multipoint
# Translation of Shapefile record geometries in a functional style.
from fiona import collection
from functools import partial
from itertools import imap
import logging
log = logging.getLogger()
@seumasmorrison
seumasmorrison / dmds_to_decimal_degrees.py
Last active August 29, 2015 14:14
Coordinate conversion from Degrees Minutes Decimal Second to Decimal Degrees on points exported from Leica Geo Office in csv format using LatLon and Pandas. Used for preparation for import to QGIS for creating contours from points with the Contours plugin.
import LatLon
import pandas as pd
import re
leica_points = pd.read_csv('D://project_3.csv',names=['id','type','timestamp','Latitude','Longtitude','Height','Quality'])
coords = []
chars = '[\xb0\'"]'
for index, row in leica_points.iterrows():
lat_dmds = re.sub(chars,'',row.Latitude)
long_dmds = re.sub(chars,'',row.Longtitude)
@seumasmorrison
seumasmorrison / hist_concat.py
Created February 9, 2015 12:53
Module for concatenating, resampling and writing Datawell his/hiw files as Excel spreadsheets, modified from hebtools version
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 06 14:38:11 2015
@author: le12jm
"""
from datetime import datetime
#from hebtools.common import wave_power
import glob
@seumasmorrison
seumasmorrison / lgo_dec_s_to_dec_deg.py
Last active August 29, 2015 14:15
Improved version of previous script ( https://gist.github.com/seumasmorrison/d3f3afa96b350163909b ) for importing and converting post processed data reliably
import LatLon
import pandas as pd
import re
# Function for importing Leica Geo Office created comma separated variables
# files from post processed data, decimal seconds coordinates are converted
# to decimal degrees.
def read_lgo_csv_decimal_seconds_to_decimal_degrees(input_file, output_file):
cols = ['id','type','timestamp', 'Latitude', 'Longtitude', 'Height',
import sys
# Be safe and define a maximum of frames we're trying to walk up
MAX_FRAMES = 20
def save_to_interactive(dct):
n = 0
# Walk up the stack looking for '__name__'
# with a value of '__main__' in frame globals
for n in range(MAX_FRAMES):
@seumasmorrison
seumasmorrison / dict_his.py
Created February 26, 2015 17:19
Python dictionary columns of 'his' file with explanatory descriptions.
{'datetime':'Timestamp: date and time (ISO8601)',
'Tp': 'the peak period (the reciprocal of the peak frequency) [s]',
'Dirp': 'the wave direction at the peak frequency [°]',
'Sprp': 'the directional spread at the peak frequency [°]',
'Tz': 'the zero-upcross period [s]',
'Hs': 'the significant wave height [cm]',
'TI': 'the integral period, or Tm(-2,0) [s]',
'T1': 'the mean period, or Tm(0,1) [s]',
'Tc': 'the crest period, or Tm(2,4) [s]',
'Tdw2': 'wave period Tm(-1,1) [s]',