import numpy from multiprocessing import Process, Queue import keras from keras import backend as K from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D import tensorflow as tf from keras.datasets import cifar10 from oct2py import octave octave.addpath(octave.genpath("gpml-matlab-v4.0-2016-10-19")) def run_in_separate_process(method, args): def queue_wrapper(q, params): r = method(*params) q.put(r) q = Queue() p = Process(target=queue_wrapper, args=(q, args)) p.start() return_val = q.get() p.join() if type(return_val) is Exception: raise return_val return return_val
def test_verify_AOTIM5_2018(self, parameters): #-- model parameters for AOTIM-5-2018 modelpath = os.path.join(filepath, 'Arc5km2018') grid_file = os.path.join(modelpath, parameters['grid']) model_file = os.path.join(modelpath, parameters['model']) TYPE = parameters['type'] GRID = 'OTIS' EPSG = 'PSNorth' #-- open Arctic Tidal Current Atlas list of records with open(os.path.join(filepath, 'List_of_records.txt'), 'r') as f: file_contents = f.read().splitlines() #-- skip 2 header rows count = 2 #-- iterate over number of stations arctic_stations = len(file_contents) - count stations = [None] * arctic_stations shortname = [None] * arctic_stations station_lon = np.zeros((arctic_stations)) station_lat = np.zeros((arctic_stations)) for s in range(arctic_stations): line_contents = file_contents[count + s].split() stations[s] = line_contents[1] shortname[s] = line_contents[2] station_lat[s] = np.float64(line_contents[10]) station_lon[s] = np.float64(line_contents[11]) #-- calculate daily results for a time period #-- convert time to days since 1992-01-01T00:00:00 tide_time = np.arange( pyTMD.time.convert_calendar_dates(2000, 1, 1), pyTMD.time.convert_calendar_dates(2000, 12, 31) + 1) #-- serial dates for matlab program (days since 0000-01-01T00:00:00) SDtime = np.arange(convert_calendar_serial(2000, 1, 1), convert_calendar_serial(2000, 12, 31) + 1) #-- presently not converting times to dynamic times for model comparisons deltat = np.zeros_like(tide_time) #-- number of days ndays = len(tide_time) #-- extract amplitude and phase from tide model amp, ph, D, c = pyTMD.read_tide_model.extract_tidal_constants( station_lon, station_lat, grid_file, model_file, EPSG, TYPE=TYPE, METHOD='spline', GRID=GRID) #-- calculate complex phase in radians for Euler's cph = -1j * ph * np.pi / 180.0 #-- will verify differences between model outputs are within tolerance eps = np.finfo(np.float16).eps #-- compare daily outputs at each station point invalid_list = ['KS14'] #-- remove coastal stations from the list valid_stations = [ i for i, s in enumerate(shortname) if s not in invalid_list ] #-- compute validation data from Matlab TMD program using octave #-- https://github.com/EarthAndSpaceResearch/TMD_Matlab_Toolbox_v2.5 TMDpath = os.path.join(filepath, '..', 'TMD_Matlab_Toolbox', 'TMD') octave.addpath(octave.genpath(os.path.normpath(TMDpath))) octave.addpath(filepath) octave.addpath(modelpath) #-- turn off octave warnings octave.warning('off', 'all') #-- input control file for model CFname = os.path.join(filepath, 'Model_Arc5km2018') assert os.access(CFname, os.F_OK) #-- run Matlab TMD program with octave #-- MODE: OB time series validation, _ = octave.tmd_tide_pred_plus(CFname, SDtime, station_lat[valid_stations], station_lon[valid_stations], TYPE, nout=2) #-- for each valid station for i, s in enumerate(valid_stations): #-- calculate constituent oscillation for station hc = amp[s, None, :] * np.exp(cph[s, None, :]) #-- allocate for out tides at point tide = np.ma.zeros((ndays)) tide.mask = np.zeros((ndays), dtype=bool) #-- predict tidal elevations at time and infer minor corrections tide.mask[:] = np.any(hc.mask) tide.data[:] = pyTMD.predict_tidal_ts(tide_time, hc, c, DELTAT=deltat, CORRECTIONS=GRID) minor = pyTMD.infer_minor_corrections(tide_time, hc, c, DELTAT=deltat, CORRECTIONS=GRID) tide.data[:] += minor.data[:] #-- calculate differences between matlab and python version difference = np.ma.zeros((ndays)) difference.data[:] = tide.data - validation[:, i] difference.mask = (tide.mask | np.isnan(validation[:, i])) difference.data[difference.mask] = 0.0 if not np.all(difference.mask): assert np.all(np.abs(difference) < eps)
def test_tidal_ellipse(self): #-- model parameters for CATS2008 modelpath = os.path.join(filepath, 'CATS2008') grid_file = os.path.join(modelpath, 'grid_CATS2008') model_file = os.path.join(modelpath, 'uv.CATS2008.out') TYPES = ['u', 'v'] GRID = 'OTIS' EPSG = 'CATS2008' #-- open Antarctic Tide Gauge (AntTG) database with open(os.path.join(filepath, 'AntTG_ocean_height_v1.txt'), 'r') as f: file_contents = f.read().splitlines() #-- counts the number of lines in the header count = 0 HEADER = True #-- Reading over header text while HEADER: #-- check if file line at count starts with matlab comment string HEADER = file_contents[count].startswith('%') #-- add 1 to counter count += 1 #-- rewind 1 line count -= 1 #-- iterate over number of stations antarctic_stations = (len(file_contents) - count) // 10 stations = [None] * antarctic_stations shortname = [None] * antarctic_stations station_type = [None] * antarctic_stations station_lon = np.zeros((antarctic_stations)) station_lat = np.zeros((antarctic_stations)) for s in range(antarctic_stations): i = count + s * 10 stations[s] = file_contents[i + 1].strip() shortname[s] = file_contents[i + 3].strip() lat, lon, _, _ = file_contents[i + 4].split() station_type[s] = file_contents[i + 6].strip() station_lon[s] = np.float64(lon) station_lat[s] = np.float64(lat) #-- compare daily outputs at each station point invalid_list = [ 'Ablation Lake', 'Amery', 'Bahia Esperanza', 'Beaver Lake', 'Cape Roberts', 'Casey', 'Doake Ice Rumples', 'EE4A', 'EE4B', 'Eklund Islands', 'Gerlache C', 'Groussac', 'Gurrachaga', 'Half Moon Is.', 'Heard Island', 'Hobbs Pool', 'Mawson', 'McMurdo', 'Mikkelsen', 'Palmer', 'Primavera', 'Rutford GL', 'Rutford GPS', 'Rothera', 'Scott Base', 'Seymour Is', 'Terra Nova Bay' ] #-- remove coastal stations from the list i = [i for i, s in enumerate(shortname) if s not in invalid_list] valid_stations = len(i) #-- will verify differences between model outputs are within tolerance eps = np.finfo(np.float16).eps #-- save complex amplitude for each current hc1, hc2 = ({}, {}) #-- iterate over zonal and meridional currents for TYPE in TYPES: #-- extract amplitude and phase from tide model amp, ph, D, c = pyTMD.read_tide_model.extract_tidal_constants( station_lon[i], station_lat[i], grid_file, model_file, EPSG, TYPE=TYPE, METHOD='spline', GRID=GRID) #-- calculate complex phase in radians for Euler's cph = -1j * ph * np.pi / 180.0 #-- calculate constituent oscillation for station hc1[TYPE] = amp * np.exp(cph) #-- compute validation data from Matlab TMD program using octave #-- https://github.com/EarthAndSpaceResearch/TMD_Matlab_Toolbox_v2.5 TMDpath = os.path.join(filepath, '..', 'TMD_Matlab_Toolbox', 'TMD') octave.addpath(octave.genpath(os.path.normpath(TMDpath))) octave.addpath(filepath) octave.addpath(modelpath) #-- turn off octave warnings octave.warning('off', 'all') #-- input control file for model CFname = os.path.join(filepath, 'Model_CATS2008') assert os.access(CFname, os.F_OK) #-- extract tidal harmonic constants out of a tidal model amp, ph, D, cons = octave.tmd_extract_HC(CFname, station_lat[i], station_lon[i], TYPE, nout=4) #-- calculate complex phase in radians for Euler's cph = -1j * ph * np.pi / 180.0 #-- calculate constituent oscillation for station hc2[TYPE] = amp * np.exp(cph) #-- compute tidal ellipse parameters for python program test = {} test['umajor'],test['uminor'],test['uincl'],test['uphase'] = \ pyTMD.tidal_ellipse(hc1['u'],hc1['v']) #-- compute tidal ellipse parameters for TMD matlab program valid = {} valid['umajor'],valid['uminor'],valid['uincl'],valid['uphase'] = \ octave.TideEl(hc2['u'],hc2['v'],nout=4) #-- calculate differences between matlab and python version for key in ['umajor', 'uminor', 'uincl', 'uphase']: difference = np.ma.zeros((valid_stations, len(c))) difference.data[:] = test[key].data - valid[key].T difference.mask = (test[key].mask | np.isnan(valid[key].T)) difference.data[difference.mask] = 0.0 if not np.all(difference.mask): assert np.all(np.abs(difference) < eps)
def test_verify_CATS2008(self, parameters): #-- model parameters for CATS2008 modelpath = os.path.join(filepath, 'CATS2008') grid_file = os.path.join(modelpath, parameters['grid']) model_file = os.path.join(modelpath, parameters['model']) TYPE = parameters['type'] GRID = 'OTIS' EPSG = 'CATS2008' #-- open Antarctic Tide Gauge (AntTG) database with open(os.path.join(filepath, 'AntTG_ocean_height_v1.txt'), 'r') as f: file_contents = f.read().splitlines() #-- counts the number of lines in the header count = 0 HEADER = True #-- Reading over header text while HEADER: #-- check if file line at count starts with matlab comment string HEADER = file_contents[count].startswith('%') #-- add 1 to counter count += 1 #-- rewind 1 line count -= 1 #-- iterate over number of stations antarctic_stations = (len(file_contents) - count) // 10 stations = [None] * antarctic_stations shortname = [None] * antarctic_stations station_type = [None] * antarctic_stations station_lon = np.zeros((antarctic_stations)) station_lat = np.zeros((antarctic_stations)) for s in range(antarctic_stations): i = count + s * 10 stations[s] = file_contents[i + 1].strip() shortname[s] = file_contents[i + 3].strip() lat, lon, _, _ = file_contents[i + 4].split() station_type[s] = file_contents[i + 6].strip() station_lon[s] = np.float64(lon) station_lat[s] = np.float64(lat) #-- calculate daily results for a time period #-- convert time to days since 1992-01-01T00:00:00 tide_time = np.arange( pyTMD.time.convert_calendar_dates(2000, 1, 1), pyTMD.time.convert_calendar_dates(2000, 12, 31) + 1) #-- serial dates for matlab program (days since 0000-01-01T00:00:00) SDtime = np.arange(convert_calendar_serial(2000, 1, 1), convert_calendar_serial(2000, 12, 31) + 1) #-- presently not converting times to dynamic times for model comparisons deltat = np.zeros_like(tide_time) #-- number of days ndays = len(tide_time) #-- extract amplitude and phase from tide model amp, ph, D, c = pyTMD.read_tide_model.extract_tidal_constants( station_lon, station_lat, grid_file, model_file, EPSG, TYPE=TYPE, METHOD='spline', GRID=GRID) #-- calculate complex phase in radians for Euler's cph = -1j * ph * np.pi / 180.0 #-- will verify differences between model outputs are within tolerance eps = np.finfo(np.float16).eps #-- compare daily outputs at each station point invalid_list = [ 'Ablation Lake', 'Amery', 'Bahia Esperanza', 'Beaver Lake', 'Cape Roberts', 'Casey', 'Doake Ice Rumples', 'EE4A', 'EE4B', 'Eklund Islands', 'Gerlache C', 'Groussac', 'Gurrachaga', 'Half Moon Is.', 'Heard Island', 'Hobbs Pool', 'Mawson', 'McMurdo', 'Mikkelsen', 'Palmer', 'Primavera', 'Rutford GL', 'Rutford GPS', 'Rothera', 'Scott Base', 'Seymour Is', 'Terra Nova Bay' ] #-- remove coastal stations from the list valid_stations = [ i for i, s in enumerate(shortname) if s not in invalid_list ] #-- compute validation data from Matlab TMD program using octave #-- https://github.com/EarthAndSpaceResearch/TMD_Matlab_Toolbox_v2.5 TMDpath = os.path.join(filepath, '..', 'TMD_Matlab_Toolbox', 'TMD') octave.addpath(octave.genpath(os.path.normpath(TMDpath))) octave.addpath(filepath) octave.addpath(modelpath) #-- turn off octave warnings octave.warning('off', 'all') #-- input control file for model CFname = os.path.join(filepath, 'Model_CATS2008') assert os.access(CFname, os.F_OK) #-- run Matlab TMD program with octave #-- MODE: OB time series validation, _ = octave.tmd_tide_pred_plus(CFname, SDtime, station_lat[valid_stations], station_lon[valid_stations], TYPE, nout=2) #-- for each valid station for i, s in enumerate(valid_stations): #-- calculate constituent oscillation for station hc = amp[s, None, :] * np.exp(cph[s, None, :]) #-- allocate for out tides at point tide = np.ma.zeros((ndays)) tide.mask = np.zeros((ndays), dtype=bool) #-- predict tidal elevations at time and infer minor corrections tide.mask[:] = np.any(hc.mask) tide.data[:] = pyTMD.predict_tidal_ts(tide_time, hc, c, DELTAT=deltat, CORRECTIONS=GRID) minor = pyTMD.infer_minor_corrections(tide_time, hc, c, DELTAT=deltat, CORRECTIONS=GRID) tide.data[:] += minor.data[:] #-- calculate differences between matlab and python version difference = np.ma.zeros((ndays)) difference.data[:] = tide.data - validation[:, i] difference.mask = (tide.mask | np.isnan(validation[:, i])) difference.data[difference.mask] = 0.0 if not np.all(difference.mask): assert np.all(np.abs(difference) < eps)
from __future__ import division import os import unittest import numpy as np from oct2py import octave #from pandas import read_csv import seawater as sw from seawater.constants import c3515 path = os.path.join(os.path.dirname(__file__), 'seawater_v3_3') _ = octave.addpath(octave.genpath(path)) def compare_results(name, function, args, values): args = [values.get(arg) for arg in args] res = function(*args) # Python call. # FIXME: Test only the first output when multiple outputs are present. if isinstance(res, tuple): nout = len(res) res = res[0] else: nout = 1 val = octave.call('sw_%s' % name, *args, nout=nout) # Octave call. if nout > 1: val = val[0] val, res = val.squeeze(), res.squeeze()
import numpy as np from oct2py import octave from pandas import read_csv import seawater as sw from seawater.constants import c3515 try: path = sys.argv[1] except IndexError: path = "./seawater_v3_3" if not os.path.exists(path): raise ValueError("matlab seawater path %s not found" % path) _ = octave.addpath(octave.genpath(path)) kw = dict(comment='#', header=5, index_col=0) st61 = read_csv('Endeavor_Cruise-88_Station-61.csv', **kw) st64 = read_csv('Endeavor_Cruise-88_Station-64.csv', **kw) latst = 36. + 40.03 / 60., 37. + 39.93 / 60. lonst = -(70. + 59.59 / 60.), -71. Sal=np.c_[st61['S'].values, st64['S'].values] Temp=np.c_[st61['t'].values, st64['t'].values] Pres=np.c_[st61.index.values.astype(float), st64.index.values.astype(float)] Gpan = sw.gpan(Sal, Temp, Pres) def compare_results(name, function, args): args = [values.get(arg) for arg in args]