Unverified Commit ecde918e authored by jdha's avatar jdha Committed by GitHub
Browse files

Merge pull request #47 from jdha/PyNEMO3

Py nemo3
parents 083ed633 298e9b48
PyNEMO Description
===================
\ No newline at end of file
......@@ -40,7 +40,7 @@
!------------------------------------------------------------------------------
! I/O
!------------------------------------------------------------------------------
sn_src_dir = '/Users/jdha/Projects/GitHub/PyNEMO/inputs/src_data.ncml' ! src_files/'
sn_src_dir = '/Users/jdha/Projects/GitHub/PyNEMO3/inputs/src_data_local.ncml' ! src_files/'
sn_dst_dir = './outputs'
sn_fn = 'NNA_R12' ! prefix for output files
nn_fv = -1e20 ! set fill value for output files
......
......@@ -6,13 +6,13 @@ Created on 21 Jan 2015
# pylint: disable=E1103
# pylint: disable=no-name-in-module
# pylint: disable=E1002
from PyQt4 import QtGui
from PyQt5 import QtGui, QtWidgets
from .nemo_bdy_namelist_edit import NameListEditor
from .nemo_bdy_mask_gui import MatplotlibWidget
from PyQt4.QtGui import QSizePolicy
from PyQt4.Qt import Qt
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.Qt import Qt
class InputWindow(QtGui.QDialog):
class InputWindow(QtWidgets.QDialog):
'''
Input Window for editing pyNEMO settings
'''
......@@ -43,15 +43,15 @@ class InputWindow(QtGui.QDialog):
self.mpl_widget.set_mask_settings(float(setup.settings['mask_max_depth']), float(setup.settings['mask_shelfbreak_dist']))
splitter = QtGui.QSplitter(Qt.Horizontal)
splitter = QtWidgets.QSplitter(Qt.Horizontal)
splitter.addWidget(self.nl_editor)
splitter.addWidget(self.mpl_widget)
hbox = QtGui.QHBoxLayout()
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(splitter)
self.setLayout(hbox)
#set the Dialog title
self.setWindowTitle("PyNEMO Settings Editor")
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create('Cleanlooks'))
#show the window
self.show()
......@@ -12,8 +12,8 @@ import logging
from scipy import ndimage
import matplotlib.pyplot as plt
from pynemo.utils import gcoms_break_depth
from PyQt4.QtGui import QMessageBox
from PyQt4 import QtCore
from PyQt5.QtWidgets import QMessageBox
from PyQt5 import QtCore
class Mask(object):
"""This is a Mask holder. which reads from a netCDF bathymetry file and
......@@ -199,7 +199,7 @@ class Mask(object):
mask_data[index] = self.data[index]
#connected components
label_mask, num_labels = ndimage.label(mask_data)
mean_values = ndimage.sum(np.ones(self.data.shape),label_mask,range(1, num_labels+1))
mean_values = ndimage.sum(np.ones(self.data.shape),label_mask,list(range(1, num_labels+1)))
max_area_mask = None
if mean_values.size != 0:
max_area_index = np.argmax(mean_values)+1
......@@ -228,7 +228,7 @@ class Mask(object):
label_mask, num_labels = ndimage.label(mask_data)
if num_labels == 0: #if mask is empty/clear
return
mean_values = ndimage.sum(np.ones(self.data.shape),label_mask,range(1, num_labels+1))
mean_values = ndimage.sum(np.ones(self.data.shape),label_mask,list(range(1, num_labels+1)))
max_area_mask = None
if mean_values.size != 0:
max_area_index = np.argmax(mean_values)+1
......
......@@ -10,15 +10,15 @@ from mpl_toolkits.basemap import Basemap, cm
import numpy as np
from .selection_editor import PolygonEditor, BoxEditor
import os.path
from PyQt4.QtCore import pyqtSignal, pyqtSlot, Qt
from nemo_bdy_mask import Mask
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt
from .nemo_bdy_mask import Mask
import logging
from PyQt4.QtGui import QSizePolicy
from PyQt5.QtWidgets import QSizePolicy
from matplotlib.colors import Normalize
mask_alpha = 0.3
from PyQt4 import QtGui
from PyQt5 import QtGui, QtWidgets
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.path import Path
......@@ -26,7 +26,7 @@ from matplotlib.transforms import Bbox
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
# pylint: disable=E1002
class MatplotlibWidget(QtGui.QWidget):
class MatplotlibWidget(QtWidgets.QWidget):
"""This class is a QWidget for pyNEMO mask plot"""
min_depth = 200.0
shelfbreak_dist = 200.0
......@@ -34,7 +34,7 @@ class MatplotlibWidget(QtGui.QWidget):
def __init__(self, parent=None, mask=None, min_depth = 200.0, shelfbreak_dist = 200.0,*args, **kwargs):
""" Initialises the mask, matplot and the navigation toolbar """
super(MatplotlibWidget, self).__init__(parent)
#QtGui.QWidget.__init__(self, parent)
#QtWidgets.QWidget.__init__(self, parent)
self.figure = Figure(*args, **kwargs)
self.canvas = FigureCanvas(self.figure)
self.mask = mask
......@@ -51,7 +51,7 @@ class MatplotlibWidget(QtGui.QWidget):
self.toolbar.drawing_tool.connect(self.drawing_tool_callback)
self.axes = self.figure.add_subplot(111)
self.cbar = None
layout = QtGui.QVBoxLayout()
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
self.setLayout(layout)
......@@ -126,7 +126,7 @@ class MatplotlibWidget(QtGui.QWidget):
x = np.arange(0, self.mask.lon.shape[0])
y = np.arange(0, self.mask.lon.shape[1])
x_vals, y_vals = np.meshgrid(y, x)
grid = zip(x_vals.ravel(), y_vals.ravel())
grid = list(zip(x_vals.ravel(), y_vals.ravel()))
self._drawing_tool.polygon.set_linewidth(1.0)
p_path = Path(self._drawing_tool.polygon.xy)
......@@ -146,7 +146,7 @@ class MatplotlibWidget(QtGui.QWidget):
x = np.arange(0, self.mask.lon.shape[0])
y = np.arange(0, self.mask.lon.shape[1])
x_vals, y_vals = np.meshgrid(y, x)
grid = zip(x_vals.ravel(), y_vals.ravel()) #check for the index
grid = list(zip(x_vals.ravel(), y_vals.ravel())) #check for the index
self._drawing_tool.polygon.set_linewidth(1.0)
p_path = Path(self._drawing_tool.polygon.xy)
......
......@@ -10,10 +10,10 @@ from mpl_toolkits.basemap import Basemap, cm
import numpy as np
from .selection_editor import PolygonEditor, BoxEditor
import os.path
from PyQt4.QtCore import pyqtSignal, pyqtSlot, Qt
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt
from nemo_bdy_mask import Mask
import logging
from PyQt4.QtGui import QSizePolicy
from PyQt5.QtGui import QSizePolicy
mask_alpha = 0.3
......
......@@ -6,13 +6,14 @@ Editor for namelist.bdy file
# pylint: disable=E1103
# pylint: disable=no-name-in-module
# pylint: disable=E1002
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSignal, Qt, QRect, QPoint
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtCore import pyqtSignal, Qt, QRect, QPoint
import ast
from PyQt4.QtGui import QMessageBox, QRegion, QIcon, QToolTip, QCursor
from PyQt5.QtGui import QRegion, QIcon, QCursor
from PyQt5.QtWidgets import QMessageBox, QToolTip
class NameListEditor(QtGui.QWidget):
class NameListEditor(QtWidgets.QWidget):
'''
This class creates a gui for the Namelist file options
'''
......@@ -34,25 +35,25 @@ class NameListEditor(QtGui.QWidget):
'''
Initialises the UI components of the GUI
'''
client = QtGui.QWidget(self)
client = QtWidgets.QWidget(self)
# Create the Layout to Grid
grid = QtGui.QGridLayout()
grid = QtWidgets.QGridLayout()
# Loop through the settings and create widgets for each setting
index = 0
for setting in self.settings:
# initialises setting Widget
label = QtGui.QLabel(setting)
qlabel = QtGui.QPushButton("")
qlabel.setIcon(self.style().standardIcon(QtGui.QStyle.SP_MessageBoxQuestion))
label = QtWidgets.QLabel(setting)
qlabel = QtWidgets.QPushButton("")
qlabel.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MessageBoxQuestion))
if type(self.settings[setting]).__name__ in ['str', 'float', 'double',
'int', 'time', 'dict']:
text = QtGui.QLineEdit(self)
text = QtWidgets.QLineEdit(self)
text.setText(str(self.settings[setting]))
text.textChanged.connect(lambda value=setting,\
var_name=setting: self.label_changed(value, var_name))
if self.bool_settings.has_key(setting):
chkbox = QtGui.QCheckBox(self)
if setting in self.bool_settings:
chkbox = QtWidgets.QCheckBox(self)
chkbox.setChecked(self.bool_settings[setting])
chkbox.stateChanged.connect(lambda value=setting,\
var_name=setting:\
......@@ -60,7 +61,7 @@ class NameListEditor(QtGui.QWidget):
grid.addWidget(chkbox, index, 0)
elif type(self.settings[setting]).__name__ == 'bool':
text = QtGui.QComboBox(self)
text = QtWidgets.QComboBox(self)
text.insertItem(0, 'True')
text.insertItem(1, 'False')
if self.settings[setting]:
......@@ -83,22 +84,22 @@ class NameListEditor(QtGui.QWidget):
client.setLayout(grid)
#scrollbars
scroll_area = QtGui.QScrollArea(self)
scroll_area = QtWidgets.QScrollArea(self)
#scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scroll_area.setWidget(client)
#save cancel buttons
btn_widget = QtGui.QWidget(self)
hbox_layout = QtGui.QHBoxLayout(self)
btn_save = QtGui.QPushButton('Save')
btn_widget = QtWidgets.QWidget(self)
hbox_layout = QtWidgets.QHBoxLayout(self)
btn_save = QtWidgets.QPushButton('Save')
btn_save.clicked.connect(self._btn_save_callback)
self.btn_cancel = QtGui.QPushButton('Close')
self.btn_cancel = QtWidgets.QPushButton('Close')
self.btn_cancel.clicked.connect(self._btn_cancel_callback)
hbox_layout.addWidget(btn_save)
hbox_layout.addWidget(self.btn_cancel)
btn_widget.setLayout(hbox_layout)
box_layout = QtGui.QVBoxLayout(self)
box_layout = QtWidgets.QVBoxLayout(self)
box_layout.addWidget(scroll_area)
box_layout.addWidget(btn_widget)
btn_widget.setMaximumWidth(400)
......@@ -109,7 +110,7 @@ class NameListEditor(QtGui.QWidget):
def label_changed(self, value, name):
""" callback when the text is changed in the text box"""
self.new_settings[name] = unicode(value).encode('utf_8')
self.new_settings[name] = str(value).encode('utf_8')
def combo_index_changed(self, value, name):
""" callback when the True/False drop down for the settings which has boolean value
......@@ -153,7 +154,7 @@ class NameListEditor(QtGui.QWidget):
try:
self.mask_settings_update.emit(float(self.settings['mask_max_depth']), float(self.settings['mask_shelfbreak_dist']))
except KeyError:
print 'Set the mask setting mask_max_depth and mask_shelfbreak_dist'
print('Set the mask setting mask_max_depth and mask_shelfbreak_dist')
if self.bool_settings['mask_file']:
self.bathymetry_update.emit(self.settings['bathy'],self.settings['mask_file'])
......
This diff is collapsed.
This diff is collapsed.
......@@ -9,7 +9,7 @@ import numpy as np
from matplotlib.lines import Line2D
from matplotlib.patches import Polygon
from matplotlib.artist import Artist
from matplotlib.mlab import dist_point_to_segment
from pynemo.utils.nemo_bdy_lib import dist_point_to_segment
from matplotlib.widgets import RectangleSelector
......@@ -58,12 +58,12 @@ class PolygonEditor(object):
self.reset_line()
self.reset_polygon()
elif self.xy_values.shape[0] <= 2: # point or line for 1 or 2 points
xval, yval = zip(*self.xy_values)
xval, yval = list(zip(*self.xy_values))
if self.line == None:
self.line = Line2D(xval, yval, marker='o', markerfacecolor='r', animated=True)
self.axis.add_line(self.line)
else:
self.line.set_data(zip(*self.xy_values))
self.line.set_data(list(zip(*self.xy_values)))
self.reset_polygon()
else: # more than 2 points if polygon is not created then creates one and draws
if self.polygon == None:
......@@ -72,7 +72,7 @@ class PolygonEditor(object):
self.axis.add_patch(self.polygon)
else:
self.polygon.xy = self.xy_values
self.line.set_data(zip(*self.xy_values))
self.line.set_data(list(zip(*self.xy_values)))
self.draw_callback(None)
self.canvas.draw()
......
......@@ -38,12 +38,12 @@ import numpy as np
import scipy.spatial as sp
from calendar import monthrange, isleap
from scipy.interpolate import interp1d
from netcdftime import datetime, utime
from cftime import datetime, utime
from pynemo import nemo_bdy_ncgen as ncgen
from pynemo import nemo_bdy_ncpop as ncpop
# Local Imports
import nemo_bdy_grid_angle as ga
from . import nemo_bdy_grid_angle as ga
from pynemo.reader.factory import GetFile
from pynemo.utils.nemo_bdy_lib import rot_rep, sub2ind
......@@ -233,12 +233,12 @@ class Extract:
# Ann Query substitute
source_tree = None
try:
source_tree = sp.cKDTree(zip(SC.lon.ravel(order='F'),
SC.lat.ravel(order='F')), balanced_tree=False,compact_nodes=False)
source_tree = sp.cKDTree(list(zip(SC.lon.ravel(order='F'),
SC.lat.ravel(order='F'))), balanced_tree=False,compact_nodes=False)
except TypeError: #added this fix to make it compatible with scipy 0.16.0
source_tree = sp.cKDTree(zip(SC.lon.ravel(order='F'),
SC.lat.ravel(order='F')))
dst_pts = zip(dst_lon[:].ravel(order='F'), dst_lat[:].ravel(order='F'))
source_tree = sp.cKDTree(list(zip(SC.lon.ravel(order='F'),
SC.lat.ravel(order='F'))))
dst_pts = list(zip(dst_lon[:].ravel(order='F'), dst_lat[:].ravel(order='F')))
nn_dist, nn_id = source_tree.query(dst_pts, k=1)
# Find surrounding points
......@@ -280,8 +280,8 @@ class Extract:
ind[p, :] = ind[p, dist_ind[p, :]]
if self.key_vec:
self.gcos = self.gcos.flatten(1)[ind].reshape(ind.shape, order='F')
self.gsin = self.gsin.flatten(1)[ind].reshape(ind.shape, order='F')
self.gcos = self.gcos.flatten('F')[ind].reshape(ind.shape, order='F')
self.gsin = self.gsin.flatten('F')[ind].reshape(ind.shape, order='F')
sc_ind = {}
sc_ind['ind'] = ind
......@@ -301,14 +301,14 @@ class Extract:
tmp_lat[r_id] = -9999
source_tree = None
try:
source_tree = sp.cKDTree(zip(tmp_lon.ravel(order='F'),
tmp_lat.ravel(order='F')), balanced_tree=False,compact_nodes=False)
source_tree = sp.cKDTree(list(zip(tmp_lon.ravel(order='F'),
tmp_lat.ravel(order='F'))), balanced_tree=False,compact_nodes=False)
except TypeError: #fix for scipy 0.16.0
source_tree = sp.cKDTree(zip(tmp_lon.ravel(order='F'),
tmp_lat.ravel(order='F')))
source_tree = sp.cKDTree(list(zip(tmp_lon.ravel(order='F'),
tmp_lat.ravel(order='F'))))
dst_pts = zip(dst_lon[rr_id].ravel(order='F'),
dst_lat[rr_id].ravel(order='F'))
dst_pts = list(zip(dst_lon[rr_id].ravel(order='F'),
dst_lat[rr_id].ravel(order='F')))
junk, an_id = source_tree.query(dst_pts, k=3,
distance_upper_bound=fr)
id_121[rr_id, :] = an_id
......@@ -347,11 +347,11 @@ class Extract:
z_ind = np.zeros((num_bdy * dst_len_z, 2), dtype=np.int64)
source_tree = None
try:
source_tree = sp.cKDTree(zip(sc_z.ravel(order='F')), balanced_tree=False,compact_nodes=False)
source_tree = sp.cKDTree(list(zip(sc_z.ravel(order='F'))), balanced_tree=False,compact_nodes=False)
except TypeError: #fix for scipy 0.16.0
source_tree = sp.cKDTree(zip(sc_z.ravel(order='F')))
source_tree = sp.cKDTree(list(zip(sc_z.ravel(order='F'))))
junk, nn_id = source_tree.query(zip(dst_dep_rv), k=1)
junk, nn_id = source_tree.query(list(zip(dst_dep_rv)), k=1)
# WORKAROUND: the tree query returns out of range val when
# dst_dep point is NaN, causing ref problems later.
......@@ -453,7 +453,7 @@ class Extract:
# Get first and last date within range, init to cover entire range
first_date = 0
last_date = len(sc_time.time_counter) - 1
rev_seq = range(len(sc_time.time_counter))
rev_seq = list(range(len(sc_time.time_counter)))
rev_seq.reverse()
for date in rev_seq:
if src_date_seconds[date] < dst_start:
......@@ -571,11 +571,11 @@ class Extract:
for dep in range(sc_z_len):
tmp_arr = [None, None]
# Consider squeezing
tmp_arr[0] = sc_array[0][0,dep,:,:].flatten(1) #[:,:,dep]
tmp_arr[0] = sc_array[0][0,dep,:,:].flatten('F') #[:,:,dep]
if not self.key_vec:
sc_bdy[vn, dep, :, :] = self._flat_ref(tmp_arr[0], ind)
else:
tmp_arr[1] = sc_array[1][0,dep,:,:].flatten(1) #[:,:,dep]
tmp_arr[1] = sc_array[1][0,dep,:,:].flatten('F') #[:,:,dep]
# Include in the collapse the rotation from the
# grid to real zonal direction, ie ij -> e
sc_bdy[vn, dep, :] = (tmp_arr[0][ind[:]] * self.gcos -
......@@ -669,11 +669,11 @@ class Extract:
# Apply 1-2-1 filter along bdy pts using NN ind self.id_121
if self.first:
tmp_valid = np.invert(np.isnan(
dst_bdy.flatten(1)[self.id_121]))
dst_bdy.flatten('F')[self.id_121]))
# Finished first run operations
self.first = False
dst_bdy = (np.nansum(dst_bdy.flatten(1)[self.id_121] *
dst_bdy = (np.nansum(dst_bdy.flatten('F')[self.id_121] *
self.tmp_filt, 2) / np.sum(self.tmp_filt *
tmp_valid, 2))
# Set land pts to zero
......@@ -688,7 +688,7 @@ class Extract:
# If we have depth dimension
if not self.isslab:
# If all else fails fill down using deepest pt
dst_bdy = dst_bdy.flatten(1)
dst_bdy = dst_bdy.flatten('F')
dst_bdy += ((dst_bdy == 0) *
dst_bdy[data_ind].repeat(sc_z_len))
# Weighted averaged on new vertical grid
......@@ -732,7 +732,7 @@ class Extract:
alpha -- input array
beta -- index array
"""
return alpha.flatten(1)[beta.flatten(1)].reshape(
return alpha.flatten('F')[beta.flatten('F')].reshape(
beta.shape, order='F')
# Convert numeric date from source to dest
......@@ -752,7 +752,7 @@ class Extract:
"""
vals = {'gregorian': 365. + isleap(year), 'noleap':
365., '360_day': 360.}
if source not in vals.keys():
if source not in list(vals.keys()):
raise ValueError('Unknown source calendar type: %s' %source)
# Get month length
if dest == '360_day':
......
......@@ -13,7 +13,7 @@ import numpy as np
import logging
#Local Imports
from utils.nemo_bdy_lib import sub2ind
from .utils.nemo_bdy_lib import sub2ind
class Boundary:
# Bearings for overlays
......@@ -102,8 +102,8 @@ class Boundary:
bdy_i = np.transpose(bdy_i, (1, 2, 0))
bdy_i = np.reshape(bdy_i,
(bdy_i.shape[0],bdy_i.shape[1]*bdy_i.shape[2]))
bdy_r = bdy_r.flatten(1)
bdy_r = bdy_r.flatten('F')
## Remove duplicate and open sea points ##
bdy_i, bdy_r = self._remove_duplicate_points(bdy_i, bdy_r)
......@@ -210,11 +210,11 @@ class Boundary:
t -- input 2D array
"""
sh = np.shape(t)
if (len(sh)> 2) or (sh[0] ==0) or (sh[1] == 0):
print 'Warning: Shape of expected 2D array:', sh
if (len(sh)> 2) or (sh[0] ==0) or (sh[1] == 0):
print('Warning: Shape of expected 2D array:', sh)
tlist = t.tolist()
sortt = []
indx = zip(*sorted([(val, i) for i,val in enumerate(tlist)]))[1]
indx = list(zip(*sorted([(val, i) for i,val in enumerate(tlist)])))[1]
indx = np.array(indx)
for i in indx:
sortt.append(tlist[i])
......@@ -224,7 +224,7 @@ class Boundary:
indx[i] = -1
# all the rows are identical, set the first as the unique row
if sortt[0] == sortt[-1]:
indx[0] = 0
indx[0] = 0
return indx[indx != -1]
......@@ -13,7 +13,7 @@
# cd_type: define the nature of pt2d grid points
import numpy as np
from reader.factory import GetFile
from .reader.factory import GetFile
import logging
# pylint: disable=E1101
......
......@@ -23,13 +23,13 @@ def write_data_to_file(filename, variable_name, data):
if variable_name in three_dim_variables:
if len(count) == 3:
count += (1L, )
count += (1, )
ncid.variables[variable_name][:, :, :, :] = np.reshape(data, count)[:, :, :, :]
elif variable_name in two_dim_variables:
if len(count) == 2:
count += (1L, )
count += (1, )
elif len(count) == 1:
count += (1L, 1L, )
count += (1, 1, )
ncid.variables[variable_name][:, :, :] = np.reshape(data, count)[:, :, :]
elif variable_name == 'time_counter':
ncid.variables[variable_name][:] = data[:]
......
......@@ -204,11 +204,11 @@ def _get_val(vars_dictionary, bool_vars_dictionary, line):
if name_prefix == 'ln':
if value.find('true') is not -1:
if vars_dictionary.has_key(name) != True:
if (name in vars_dictionary) != True:
vars_dictionary[name] = True
bool_vars_dictionary[name] = True
elif value.find('false') is not -1:
if vars_dictionary.has_key(name) != True:
if (name in vars_dictionary) != True:
vars_dictionary[name] = False
bool_vars_dictionary[name] = False
else:
......
......@@ -29,7 +29,7 @@ class SourceTime:
dir_list.sort()
return filter(None, dir_list)
return [_f for _f in dir_list if _f]
# Returns list of dicts of date/time info
# I assume there is only one date per file
......
......@@ -14,12 +14,12 @@ Initialise with bdy t, u and v grid attributes (Grid.bdy_i)
and settings dictionary
"""
from reader.factory import GetFile
from .reader.factory import GetFile
import numpy as np
import logging
from utils.nemo_bdy_lib import sub2ind
from utils.e3_to_depth import e3_to_depth
from .utils.nemo_bdy_lib import sub2ind
from .utils.e3_to_depth import e3_to_depth
# pylint: disable=E1101
# Query name
class Depth:
......@@ -52,6 +52,7 @@ class Depth:
# Check inputs
# FIX ME? Errors for wrong obj arg len. probably better to work around
print(settings)
if settings['sco']:
# hc = ... FIX ME??
# Depth of water column at t-point
......@@ -68,7 +69,7 @@ class Depth:
v_ind = sub2ind(mbathy.shape, bdy_v[:,0], bdy_v[:,1])
v_ind2 = sub2ind(mbathy.shape, bdy_v[:,0], bdy_v[:,1] + 1)
[tmp_zt, tmp_zw] = e3_to_depth(np.squeeze(nc['e3t'][:,:,:,:]), np.squeeze(nc['e3w'][:,:,:,:]), nz)
[tmp_zt, tmp_zw] = e3_to_depth(np.squeeze(nc['e3t'][:,:,:,:]), np.squeeze(nc['e3w'][:,:,:,:]), nz)
# This is very slow
self.logger.debug( 'starting nc reads loop' )
for k in range(nz):
......@@ -95,12 +96,12 @@ class Depth:
# Set u and v grid point depths
zshapes = {}
for p in self.zpoints.keys():
for p in list(self.zpoints.keys()):
zshapes[p] = self.zpoints[p].shape
wshapes = []
wshapes.append(wrk1.shape)
wshapes.append(wrk2.shape)
wrk1, wrk2 = wrk1.flatten(1), wrk2.flatten(1)
wrk1, wrk2 = wrk1.flatten('F'), wrk2.flatten('F')
self.zpoints['t'][k,:] = wrk1[t_ind]
self.zpoints['wt'][k,:] = wrk2[t_ind]
......@@ -111,7 +112,7 @@ class Depth:
self.zpoints['v'][k,:] = 0.5 * (wrk1[v_ind] + wrk1[v_ind2])
self.zpoints['wv'][k,:] = 0.5 * (wrk2[v_ind] + wrk2[v_ind2])
for p in self.zpoints.keys():
for p in list(self.zpoints.keys()):
self.zpoints[p] = self.zpoints[p].reshape(zshapes[p])
self.logger.debug( 'Done loop, zpoints: %s ', self.zpoints['t'].shape)
......
#
# The loop from nemo_bdy_extr_tm3
#
#
#
from calendar import monthrange
from datetime import datetime
from netCDF4 import Dataset, netcdftime
class Enter:
def __init__(self, settings, sc_time, dir_list, dst_cal_type, year, month):
var_nam = ['votemper', 'vosaline']
sc_fields = source_fields
#self.setup = settings # dict
# define src/dst cals
sf, ed = cal_trans(sc_time, dst_cal_type, year, month)
# W
DstCal = utime('seconds since %d-1-1' %year, dst_cal_type)
dst_start = DstCal.date2num(datetime(year, month, 1))
dst_end = DstCal.date2num(datetime(year, month, ed, 23, 59, 59))
self.S_cal = utime(sc_time[0]['units'], sc_time[0]['calendar'])
self.D_cal = utime('seconds since %d-1-1' %settings['year_000'],
settings['dst_calendar'])
for date in sc_time:
date['date_num'] = DstCal.date2num(date['date']) * sf
# Get first and last date within range
first_date, last_date = None, None
rev_seq = range(len_sc_time)
rev_seq.reverse()
# Multiple values.. might be broken..
for date in rev_seq:
if sc_time[date]['date_num'] < dst_start:
first_date = date #number..
break
for date in range(len_sc_time):
if sc_time[date]['date_num'] > dst_end:
last_date = date
break
for date in range(first_date, last_date + 1):
nc = Dataset(sc_time[date], 'r')
if key_vec:
pass
#nc_2 = Dataset
# FIX ME
# We shouldnt have to worry about counters
sc_bdy = np.zeros(nvar, sc_z_len, source_ind['ind'].shape[0],
source_ind['ind'].shape[1])
ind_vec = {}
# distinctive variable name since it gets lost in the huge loop
for shoggoth in range(nvar):
varid = nc.variables[var_nam[shoggoth]]
i, ii = source_ind['imin'], source_ind['imax']
j, jj = source_ind['jmin'], source_ind['jmax']
sc_arrays = []
col_sc_arrays = []
if key_vec:
varid_2 = nc_2.variables[var_nam[shoggoth + 1]]
if not isslab and not key_vec:
# NOTE: 0 v 1 indexing may problemate
sc_arrays.append(varid[i-1:ii, j-1:jj, :sc_z_len, :1])
elif key_vec:
sc_arrays.append(varid[i-2:ii, j-1:jj, :sc_z_len, :1])
sc_arrays.append(varid_2[i-1:ii, j-2:jj, :sc_z_len, :1])
for x in 0,1:
# tidy up src array - replace missing val
for y in 'mv', 'fv':
if not np.isnan(sc_fields[y][x]):
ind_vec[y] = sc_arrays[x] == sc_fields[y][x]
sc_arrays[x][ind_vec[y]] = 0
else:
sc_arrays[x][np.isnan(scarr)] = 0
# Adjust for scaling or offsets
if not np.isnan(sc_fields['sf'][x]):
sc_arrays[x] *= sc_fields['sf'][x]
if not np.isnan(sc_fields['os'][x]):
sc_arrays[x] += sc_fields['os'][x]
# Colocate velocity points on T grid prior to rotation
axis = [1, None]
col = 0.5 * (sc_arrays[x][:-axis[0],:-axis[1],:] +
sc_arrays[x][axis[0]:,axis[1]:,:])
col[col == 0] = np.NaN
col_sc_arrays.append(col)
axis.reverse()
# This is a slab
else:
sc_arrays.append(varid[i-1:ii, j-1:jj, :1])
#query var names
if msk and first and shoggoth==0:
pass
# Open another magic file and do stuff
nc3 = Dataset(source_mask, 'r')
varid_3 = nc3.variables['tmaskutil']
msk_array = varid_3[i-1:ii, j-1:jj, :1]
if msk: #must be true for msk array ??...
sc_arrays[0][msk_array == 0] = np.NaN
# Finished reading Source data
#for depth_val in range(sc_z_len):
# tmp_arrays = []
# if not key_vec:
# tmp_arrays.append(sc_arrays[0][:,:depth_val]
def _fv_mv_to_zero(self, scarr, indvec, sc_fields, pos):
for x in 'mv', 'fv':
if not np.isnan(sc_fields[x][pos]):
ind_vec[x] = scarr == sc_fields[x][pos]
scarr[ind_vec[x]] = 0
else:
scarr[np.isnan(scarr)] = 0
return scarr, indvec
# Convert numeric date from source to dest
def convert_date(self, date):
val = self.S_cal.num2date(date)
return self.D_cal.date2num(val)
def cal_trans(self, source, dest, year, month):
vals = {'gregorian': [monthrange(year, month)[1], 31], 'noleap':
[365., 31],'360_day': [360., 30]}
if source not in vals.keys():
raise ValueError('Unknown calendar type: %s' %source)
sf = val[source][0] / val[dest][0]
return sf, vals[dest][1]
......@@ -28,7 +28,7 @@ class Coord:
self.logger = logging.getLogger(__name__)
self.logger.debug( fname )
if not fname:
print 'need some error handling in here or is this redundant?' # TODO
print('need some error handling in here or is this redundant?') # TODO
# Enter define mode
self.ncid = Dataset(fname, 'w', clobber=True, format='NETCDF4')
......
......@@ -34,7 +34,7 @@ $Last commit on:$
import time
import logging
import numpy as np
from PyQt4.QtGui import QMessageBox
from PyQt5.QtWidgets import QMessageBox
#Local imports
from pynemo import pynemo_settings_editor
......@@ -245,13 +245,13 @@ def process_bdy(setup_filepath=0, mask_gui=False):
'values in input bdy file')
return
yrs = range(yr_000, yr_end+1)
yrs = list(range(yr_000, yr_end+1))
if yr_end - yr_000 >= 1:
if range(mn_000, mn_end+1) < 12:
if list(range(mn_000, mn_end+1)) < 12:
logger.info('Warning: All months will be extracted as the number '+
'of years is greater than 1')
mns = range(1,13)
mns = list(range(1,13))
else:
mn_000 = settings['month_000']
mn_end = settings['month_end']
......@@ -259,7 +259,7 @@ def process_bdy(setup_filepath=0, mask_gui=False):
logging.error('Please check the nn_month_000 and nn_month_end '+
'values in input bdy file')
return
mns = range(mn_000, mn_end+1)
mns = list(range(mn_000, mn_end+1))
# Enter the loop for each year and month extraction
......@@ -312,7 +312,7 @@ def process_bdy(setup_filepath=0, mask_gui=False):
# Initialise the mapping indices for each grid
for key, val in emap.items():
for key, val in list(emap.items()):
extract_obj[key] = extract.Extract(Setup.settings,
SourceCoord, DstCoord,
......@@ -325,7 +325,7 @@ def process_bdy(setup_filepath=0, mask_gui=False):
for year in yrs:
for month in mns:
for key, val in emap.items():
for key, val in list(emap.items()):
# Extract the data for a given month and year
......@@ -380,7 +380,7 @@ def write_tidal_data(setup_var, dst_coord_var, grid, tide_cons, cons):
const_name = setup_var.settings['clname'][tide_con]
const_name = const_name.replace("'", "").upper()
for key,val in tmap.items():
for key,val in list(tmap.items()):
fout_tide = setup_var.settings['dst_dir']+ \
setup_var.settings['fn']+ \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment