Skip to content

Instantly share code, notes, and snippets.

@lukecampbell
Last active August 29, 2015 14:19
Show Gist options
  • Save lukecampbell/832fd094d875e60a02c4 to your computer and use it in GitHub Desktop.
Save lukecampbell/832fd094d875e60a02c4 to your computer and use it in GitHub Desktop.
Binary wrapper for running the OTPS model
#!/bin/bash
set -x
OTPS_URL=ftp://ftp.oce.orst.edu/dist/tides/OTPS.tar.Z
TPXO_URL=ftp://ftp.oce.orst.edu/dist/tides/Global/tpxo.tar.Z
MODEL_RUN_URL=https://gist.githubusercontent.com/lukecampbell/832fd094d875e60a02c4/raw/eb86dfa1c7c0921fbb47236919592649235dba8b/model_run.py
wget $OTPS_URL
zcat OTPS.tar.Z | tar -xv
cd OTPS
make
wget $MODEL_RUN_URL
wget $TPXO_URL
zcat tpxo.tar.Z | tar -xv
cat <<EOF > DATA/Model_tpxo7
$PWD/DATA/h_tpxo7.2
$PWD/DATA/u_tpxo7.2
$PWD/DATA/grid_tpxo7.2
EOF
cd ..
#!/usr/bin/env python
'''
'''
from sh import Command
import time
from datetime import datetime, timedelta
from dateutil.parser import parse as dateparse
import json
import os
import tempfile
predict_tide_path = '/Users/lcampbell/Documents/Dev/code/OTPS/predict_tide'
model_path = '/Users/lcampbell/Documents/Dev/code/OTPS/DATA/Model_tpxo7'
predict_tide = Command(predict_tide_path)
def main(args):
lat = args.lat
lon = args.lon
output, records = run(lat, lon)
if args.verbose:
print output
print json.dumps({'records':records, 'length':len(records)})
def run(lat, lon):
now = datetime.utcnow()
lines = []
for i in xrange(24 * 5):
forecast = now + timedelta(hours=i)
date_string = ' '.join([str(s) for s in [forecast.year, forecast.month, forecast.day, forecast.hour, forecast.minute, forecast.second]])
lat_lon_time_string = '%(lat)s %(lon)s %(date_string)s' % locals()
lines.append(lat_lon_time_string)
fd, coordinates_file_path = tempfile.mkstemp()
os.close(fd)
fd, output_file_path = tempfile.mkstemp()
os.close(fd)
with open(coordinates_file_path, 'w') as f:
f.write('\n'.join(lines))
f.write('\n')
input_file = '%s\n%s\nz\n\nAP\noce\n1\n%s' % (model_path, coordinates_file_path, output_file_path)
output = predict_tide(_in=input_file)
records = run_parser(output_file_path)
return output, records
def run_parser(output_file_path):
with open(output_file_path, 'r') as f:
buf = f.readlines()[6:]
records = []
for line in buf:
lat, lon, date_str, time_str, z, depth = line.split()
line_date = dateparse('%s %s' % (date_str, time_str))
record = {
'lat' : float(lat),
'lon' : float(lon),
'time' : line_date.isoformat(),
'z' : float(z),
'depth' : float(depth)
}
records.append(record)
return records
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser(description='Run the OTPS model')
parser.add_argument('lat', type=float, help='Latitude')
parser.add_argument('lon', type=float, help='Longitude')
parser.add_argument('-v', '--verbose', action='store_true', help='Include verbose output')
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment