Skip to content

Commit 661c951

Browse files
committed
cleaned up code a bit --- mostly comments but some small tweaks too
1 parent 2e3ab0a commit 661c951

File tree

2 files changed

+18
-151
lines changed

2 files changed

+18
-151
lines changed

tracpy/inout.py

+5-132
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import numpy as np
1717
from datetime import datetime, timedelta
1818
import pdb
19-
# from mpl_toolkits.basemap import Basemap
2019
from matplotlib import delaunay
2120
import octant
2221
import time
@@ -51,14 +50,9 @@ def setupROMSfiles(loc,date,ff,tout, tstride=1):
5150
# http://code.google.com/p/netcdf4-python/issues/detail?id=170
5251
netCDF._set_default_format(format='NETCDF3_64BIT')
5352

54-
# pdb.set_trace()
53+
# For thredds server where all information is available in one place
5554
if type(loc) == str:
5655
nc = netCDF.Dataset(loc)
57-
# if 'http' in loc or (len(loc) == 2 and '.nc' in loc[0]): # use just input file
58-
# if len(loc) == 2:
59-
# nc = netCDF.Dataset(loc[0])
60-
# else:
61-
# nc = netCDF.Dataset(loc)
6256
if ff == 1: #forward in time
6357
dates = nc.variables['ocean_time'][:] # don't stride here, need all times to make index determinations
6458
ilow = date >= dates
@@ -78,7 +72,6 @@ def setupROMSfiles(loc,date,ff,tout, tstride=1):
7872
files = np.sort(glob.glob(loc[0] + 'ocean_his_????.nc')) # sorted list of file names
7973
else:
8074
files = np.sort(glob.glob(loc + 'ocean_his_????.nc')) # sorted list of file names
81-
# files = np.sort(glob.glob(loc + 'ocean_his_*_tochange.nc')) # this is for idealized tests
8275

8376
# Find the list of files that cover the desired time period
8477
# First, check to see if there is one or more than one time index in each file because
@@ -114,12 +107,9 @@ def setupROMSfiles(loc,date,ff,tout, tstride=1):
114107
# of finding the necessary files a little more general
115108
# Start by opening two files
116109
i = 1
117-
# pdb.set_trace()
118110
fname = [files[ifile]]
119111

120112
nc = netCDF.MFDataset(fname) # files in fname are in chronological order
121-
# # number of indices included in opened files so far
122-
# ninds = nc.variables['ocean_time'][:].size
123113
# Find which output in ifile is closest to the user-input start time (choose lower index)
124114
# Dates for drifters from this start date
125115
dates = nc.variables['ocean_time'][:]
@@ -171,7 +161,7 @@ def setupROMSfiles(loc,date,ff,tout, tstride=1):
171161

172162
# model output files together containing all necessary model outputs
173163
nc = netCDF.MFDataset(fname) # reopen since needed to close things in loop
174-
# pdb.set_trace()
164+
175165
return nc, tinds
176166

177167
def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
@@ -238,14 +228,11 @@ def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
238228
if keeptime: starttime = time.time()
239229

240230
# Read in grid parameters and find x and y in domain on different grids
241-
# if len(loc) == 2:
242-
# gridfile = netCDF.Dataset(loc[1])
243231
# use full dataset to get grid information
244232
# This addresses an issue in netCDF4 that was then fixed, but
245233
# this line makes updating unnecessary. Issue described here:
246234
# http://code.google.com/p/netcdf4-python/issues/detail?id=170
247235
netCDF._set_default_format(format='NETCDF3_64BIT')
248-
# pdb.set_trace()
249236
gridfile = netCDF.Dataset(grid_filename)
250237

251238
# # Read in whether grid is spherical or not
@@ -333,8 +320,6 @@ def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
333320
pm = gridfile.variables['pm'][:]
334321
pn = gridfile.variables['pn'][:]
335322
h = gridfile.variables['h'][:]
336-
# angle = gridfile.variables['angle'][:]
337-
# pdb.set_trace()
338323
if keeptime:
339324
hgridtime = time.time()
340325
print "horizontal grid time ", hgridtime - basemaptime
@@ -350,7 +335,6 @@ def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
350335
Vstretching = gridfile.variables['Vstretching'][0]
351336
# Still want vertical grid metrics, but are in separate file
352337
elif vert_filename is not None:
353-
# elif nc is not None: # for if running off local grid/nc files
354338
nc = netCDF.Dataset(vert_filename)
355339
sc_r = nc.variables['s_w'][:] # sigma coords, 31 layers
356340
Cs_r = nc.variables['Cs_w'][:] # stretching curve in sigma coords, 31 layers
@@ -389,7 +373,6 @@ def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
389373
pm = np.asfortranarray(pm.T)
390374
pn = np.asfortranarray(pn.T)
391375
h = np.asfortranarray(h.T)
392-
# pdb.set_trace()
393376

394377
if keeptime:
395378
fortranflippingtime = time.time()
@@ -399,7 +382,6 @@ def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
399382
# Grid sizes
400383
imt = h.shape[0] # 671
401384
jmt = h.shape[1] # 191
402-
# km = sc_r.shape[0] # 31
403385
if 'sc_r' in dir():
404386
km = sc_r.shape[0]-1 # 30 NOT SURE ON THIS ONE YET
405387

@@ -427,16 +409,9 @@ def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
427409

428410
# tracmass ordering.
429411
# Not sure how to convert this to pm, pn with appropriate shift
430-
dxv = 1/pm #.copy() # pm is 1/\Delta x at cell centers
431-
dyu = 1/pn #.copy() # pn is 1/\Delta y at cell centers
432-
433-
# dxv = xr.copy()
434-
# dxv[0:imt-2,:] = dxv[1:imt-1,:] - dxv[0:imt-2,:]
435-
# dxv[imt-1:imt,:] = dxv[imt-3:imt-2,:]
436-
# # pdb.set_trace()
437-
# dyu = yr.copy()
438-
# dyu[:,0:jmt-2] = dyu[:,1:jmt-1] - dyu[:,0:jmt-2]
439-
# dyu[:,jmt-1] = dyu[:,jmt-2]
412+
dxv = 1/pm # pm is 1/\Delta x at cell centers
413+
dyu = 1/pn # pn is 1/\Delta y at cell centers
414+
440415
dxdy = dyu*dxv
441416

442417
# Change dxv,dyu to be correct u and v grid size after having
@@ -453,7 +428,6 @@ def readgrid(grid_filename, vert_filename=None, llcrnrlon=-98.5, llcrnrlat=22.5,
453428
print "grid metrics time ", gridmetricstime - delaunaytime
454429

455430
# Adjust masking according to setupgrid.f95 for rutgersNWA example project from Bror
456-
# pdb.set_trace()
457431
if 'sc_r' in dir():
458432
mask2 = mask.copy()
459433
kmt = np.ones((imt,jmt),order='f')*km
@@ -601,37 +575,7 @@ def readfields(tind,grid,nc,z0=None, zpar=None, zparuv=None):
601575
sshread = True
602576
else:
603577
sshread = False
604-
605-
# time_read = time.time()-tic_temp
606578

607-
# tic_temp = time.time()
608-
# # make arrays in same order as is expected in the fortran code
609-
# # ROMS expects [time x k x j x i] but tracmass is expecting [i x j x k x time]
610-
# # change these arrays to be fortran-directioned instead of python
611-
# # u = u.T.copy(order='f')
612-
# # v = v.T.copy(order='f')
613-
# # ssh = ssh.T.copy(order='f')
614-
# # # Flip vertical dimension because in ROMS surface is at k=-1
615-
# # # and in tracmass surface is at 1
616-
# # u = np.flipud(u)
617-
# # v = np.flipud(v)
618-
# time_flip1 = time.time()-tic_temp
619-
620-
# This is code from tracmass itself, which is being replaced by Rob's octant code
621-
# # Copy calculations from rutgersNWA/readfields.f95
622-
# dzt = np.ones((grid['imt'],grid['jmt'],grid['km']))*np.nan
623-
# dzu = np.ones((grid['imt']-1,grid['jmt'],grid['km']))*np.nan
624-
# dzv = np.ones((grid['imt'],grid['jmt']-1,grid['km']))*np.nan
625-
# for k in xrange(grid['km']):
626-
# dzt0 = (grid['sc_r'][k]-grid['Cs_r'][k])*grid['hc'] \
627-
# + grid['Cs_r'][k]*grid['h']
628-
# dzt[:,:,k] = dzt0 + ssh*(1.+dzt0/grid['h'])
629-
630-
# dzt0 = dzt[:,:,grid['km']-1]
631-
# dzt[:,:,0:grid['km']-1] = dzt[:,:,1:grid['km']] - dzt[:,:,0:grid['km']-1]
632-
# dzt[:,:,grid['km']-1] = ssh - dzt0
633-
634-
# tic_temp = time.time()
635579
h = grid['h'].T.copy(order='c')
636580
# Use octant to calculate depths for the appropriate vertical grid parameters
637581
# have to transform a few back to ROMS coordinates and python ordering for this
@@ -642,50 +586,23 @@ def readfields(tind,grid,nc,z0=None, zpar=None, zparuv=None):
642586
zwt = octant.depths.get_zw(grid['Vtransform'], grid['Vstretching'], grid['km']+1, grid['theta_s'], grid['theta_b'],
643587
h, grid['hc'], zeta=0, Hscale=3)
644588
# Change dzt to tracmass/fortran ordering
645-
# zwt = zwt.T.copy(order='f')
646-
# dzt = zwt[:,:,1:] - zwt[:,:,:-1]
647589
dzt = zwt[1:,:,:] - zwt[:-1,:,:]
648-
# pdb.set_trace()
649-
# time_zw = time.time()-tic_temp
650590

651-
# tic_temp = time.time()
652591
# also want depths on rho grid
653592
if sshread:
654593
zrt = octant.depths.get_zrho(grid['Vtransform'], grid['Vstretching'], grid['km'], grid['theta_s'], grid['theta_b'],
655594
h, grid['hc'], zeta=ssh, Hscale=3)
656595
else:
657596
zrt = octant.depths.get_zrho(grid['Vtransform'], grid['Vstretching'], grid['km'], grid['theta_s'], grid['theta_b'],
658597
h, grid['hc'], zeta=0, Hscale=3)
659-
# Change dzt to tracmass/fortran ordering
660-
# zrt = zrt.T.copy(order='f')
661-
# time_zr = time.time()-tic_temp
662598

663-
# tic_temp = time.time()
664599
dzu = .5*(dzt[:,:,0:grid['imt']-1] + dzt[:,:,1:grid['imt']])
665600
dzv = .5*(dzt[:,0:grid['jmt']-1,:] + dzt[:,1:grid['jmt'],:])
666-
# dzu = .5*(dzt[0:grid['imt']-1,:,:] + dzt[1:grid['imt'],:,:])
667-
# dzv = .5*(dzt[:,0:grid['jmt']-1,:] + dzt[:,1:grid['jmt'],:])
668-
# dzu = dzt[0:grid['imt']-1,:,:]*0.5 + dzt[1:grid['imt'],:,:]*0.5
669-
# dzv = dzt[:,0:grid['jmt']-1,:]*0.5 + dzt[:,1:grid['jmt'],:]*0.5
670-
# dzu[0:grid['imt']-1,:,:] = dzt[0:grid['imt']-1,:,:]*0.5 + dzt[1:grid['imt'],:,:]*0.5
671-
# dzv[:,0:grid['jmt']-1,:] = dzt[:,0:grid['jmt']-1,:]*0.5 + dzt[:,1:grid['jmt'],:]*0.5
672-
# pdb.set_trace()
673-
# time_interp = time.time()-tic_temp
674601

675-
# tic_temp = time.time()
676602
# Change order back to ROMS/python for this calculation
677-
# u = u.T.copy(order='c')
678-
# v = v.T.copy(order='c')
679-
# dzu = dzu.T.copy(order='c')
680-
# dzv = dzv.T.copy(order='c')
681-
# dzt = dzt.T.copy(order='c')
682603
dyu = grid['dyu'].T.copy(order='c')
683604
dxv = grid['dxv'].T.copy(order='c')
684-
# zrt = zrt.T.copy(order='c')
685-
# time_flip2 = time.time()-tic_temp
686-
# pdb.set_trace()
687605

688-
# tic_temp = time.time()
689606
# I think I can avoid this loop for the isoslice case
690607
if z0 == None: # 3d case
691608
uflux1 = u*dzu*dyu
@@ -698,79 +615,36 @@ def readfields(tind,grid,nc,z0=None, zpar=None, zparuv=None):
698615
elif z0 == 'rho' or z0 == 'salt' or z0 == 'temp':
699616
# the vertical setup we're selecting an isovalue of
700617
vert = nc.variables[z0][tind,:,:,:]
701-
# pdb.set_trace()
702618
# Calculate flux and then take slice
703619
uflux1 = octant.tools.isoslice(u*dzu*dyu,op.resize(vert,2),zpar)
704620
vflux1 = octant.tools.isoslice(v*dzv*dxv,op.resize(vert,1),zpar)
705621
dzt = octant.tools.isoslice(dzt,vert,zpar)
706622
zrt = octant.tools.isoslice(zrt,vert,zpar)
707-
# pdb.set_trace()
708623
elif z0 == 'z':
709624
# Calculate flux and then take slice
710625
uflux1 = octant.tools.isoslice(u*dzu*dyu,op.resize(zrt,2),zpar)
711626
vflux1 = octant.tools.isoslice(v*dzv*dxv,op.resize(zrt,1),zpar)
712627
dzt = octant.tools.isoslice(dzt,zrt,zpar)
713628
zrt = np.ones(uflux1.shape)*zpar # array of the input desired depth
714-
# time_flux = time.time()-tic_temp
715629

716-
# tic_temp = time.time()
717630
# Change all back to tracmass/fortran ordering if being used again
718-
# tic = time.time()
719-
# uflux1 = uflux1.T.copy(order='f')
720-
# vflux1 = vflux1.T.copy(order='f')
721-
# dzt = dzt.T.copy(order='f')
722-
# zrt = zrt.T.copy(order='f')
723-
# ssh = ssh.T.copy(order='f')
724-
# zwt = zwt.T.copy(order='f')
725-
# print "copy time",time.time()-tic
726-
# tic = time.time()
727631
# This is faster than copying arrays
728632
# Don't bother changing order of these arrays since I have to change it in
729633
# run.py anyway (concatenate appears not to preserve ordering)
730634
uflux1 = uflux1.T
731635
vflux1 = vflux1.T
732636
dzt = np.asfortranarray(dzt.T)
733-
# uflux1 = np.asfortranarray(uflux1.T)
734-
# vflux1 = np.asfortranarray(vflux1.T)
735-
# dzt = np.asfortranarray(dzt.T)
736637
zrt = np.asfortranarray(zrt.T)
737638
if sshread:
738639
ssh = np.asfortranarray(ssh.T)
739640
zwt = np.asfortranarray(zwt.T)
740-
# print "fortran time",time.time()-tic
741-
# time_flip3 = time.time()-tic_temp
742641

743-
744-
# tic_temp = time.time()
745642
# make sure that all fluxes have a placeholder for depth
746643
if is_string_like(z0):
747644
uflux1 = uflux1.reshape(np.append(uflux1.shape,1))
748645
vflux1 = vflux1.reshape(np.append(vflux1.shape,1))
749646
dzt = dzt.reshape(np.append(dzt.shape,1))
750647
zrt = zrt.reshape(np.append(zrt.shape,1))
751-
# time_reshape = time.time()-tic_temp
752-
753-
754-
# # Flip vertical dimension because in ROMS surface is at k=-1
755-
# # and in tracmass surface is at 1
756-
# # This is not true. In tracmass, surface is at k=KM
757-
# uflux1 = uflux1[:,:,::-1]
758-
# vflux1 = vflux1[:,:,::-1]
759-
# dzt = dzt[:,:,::-1]
760-
# uflux1 = np.flipud(uflux1)
761-
# vflux1 = np.flipud(vflux1)
762-
# dzt = np.flipud(dzt)
763-
764-
765-
# print "time to read=",time_read
766-
# # print "time to flip=",time_flip1
767-
# print "time to get zw=",time_zw
768-
# print "time to get zr=",time_zr
769-
# print "time to interp=",time_interp
770-
# print "time to flip2=",time_flip2
771-
# print "time to flux=",time_flux
772-
# print "time to flip3=",time_flip3
773-
# print "time to reshape=",time_reshape
774648

775649
return uflux1, vflux1, dzt, zrt, zwt
776650

@@ -828,7 +702,6 @@ def savetracks(xin, yin ,zpin, tpin, name, nstepsin, Nin, ffin, tseasin,
828702
# Define dimensions
829703
rootgrp.createDimension('ntrac', ntrac)
830704
rootgrp.createDimension('nt', nt)
831-
# pdb.set_trace()
832705
if Uin is not None:
833706
xul = Uin.shape[0]
834707
yul = Uin.shape[1]

tracpy/tracpy_class.py

+13-19
Original file line numberDiff line numberDiff line change
@@ -245,44 +245,38 @@ def prepare_for_model_run(self, date, lon0, lat0):
245245
yend = np.ones((ia.size,(len(tinds)-1)*self.N+1))*np.nan
246246
zend = np.ones((ia.size,(len(tinds)-1)*self.N+1))*np.nan
247247
zp = np.ones((ia.size,(len(tinds)-1)*self.N+1))*np.nan
248-
# iend = np.ones((ia.size,(len(tinds)-1)*self.N))*np.nan
249-
# jend = np.ones((ia.size,(len(tinds)-1)*self.N))*np.nan
250-
# kend = np.ones((ia.size,(len(tinds)-1)*self.N))*np.nan
251248
ttend = np.zeros((ia.size,(len(tinds)-1)*self.N+1))
252249
flag = np.zeros((ia.size),dtype=np.int) # initialize all exit flags for in the domain
253250

254251
# Initialize vertical stuff and fluxes
255252
# Read initial field in - to 'new' variable since will be moved
256253
# at the beginning of the time loop ahead
254+
lx = self.grid['xr'].shape[0]
255+
ly = self.grid['xr'].shape[1]
256+
lk = self.grid['sc_r'].size
257257
if is_string_like(self.z0): # isoslice case
258258
# Now that we have the grid, initialize the info for the two bounding model
259259
# steps using the grid size
260-
self.uf = np.asfortranarray(np.ones((self.grid['xu'].shape[0],
261-
self.grid['xu'].shape[1], 1, 2)))*np.nan
262-
self.vf = np.asfortranarray(np.ones((self.grid['xv'].shape[0],
263-
self.grid['xv'].shape[1], 1, 2)))*np.nan
264-
self.dzt = np.asfortranarray(np.ones((self.grid['xr'].shape[0],
265-
self.grid['xr'].shape[1], 1, 2)))*np.nan
266-
self.zrt = np.asfortranarray(np.ones((self.grid['xr'].shape[0],
267-
self.grid['xr'].shape[1], 1, 2)))*np.nan
268-
self.zwt = np.asfortranarray(np.ones((self.grid['xr'].shape[0],
269-
self.grid['xr'].shape[1], self.grid['sc_r'].size, 2)))*np.nan
270-
self.uf[:,:,:,1], self.vf[:,:,:,1], self.dzt[:,:,:,1], self.zrt[:,:,:,1], self.zwt[:,:,:,1] = tracpy.inout.readfields(tinds[0],self.grid,nc,self.z0,self.zpar,zparuv=self.zparuv)
271-
# self.ufnew,self.vfnew,self.dztnew,self.zrtnew,self.zwtnew = tracpy.inout.readfields(tinds[0],self.grid,nc,self.z0,self.zpar,zparuv=self.zparuv)
260+
self.uf = np.asfortranarray(np.ones((lx-1, ly, lk-1, 2)))*np.nan
261+
self.vf = np.asfortranarray(np.ones((lx, ly-1, lk-1, 2)))*np.nan
262+
self.dzt = np.asfortranarray(np.ones((lx, ly, lk-1, 2)))*np.nan
263+
self.zrt = np.asfortranarray(np.ones((lx, ly, lk-1, 2)))*np.nan
264+
self.zwt = np.asfortranarray(np.ones((lx, ly, lk, 2)))*np.nan
265+
self.uf[:,:,:,1], self.vf[:,:,:,1], \
266+
self.dzt[:,:,:,1], self.zrt[:,:,:,1], \
267+
self.zwt[:,:,:,1] = tracpy.inout.readfields(tinds[0], self.grid, nc, self.z0, self.zpar, zparuv=self.zparuv)
268+
272269
else: # 3d case
273270
# Now that we have the grid, initialize the info for the two bounding model
274271
# steps using the grid size
275-
lx = self.grid['xr'].shape[0]
276-
ly = self.grid['xr'].shape[1]
277-
lk = self.grid['sc_r'].size
278272
self.uf = np.asfortranarray(np.ones((lx-1, ly, lk-1, 2)))*np.nan
279273
self.vf = np.asfortranarray(np.ones((lx, ly-1, lk-1, 2)))*np.nan
280274
self.dzt = np.asfortranarray(np.ones((lx, ly, lk-1, 2)))*np.nan
281275
self.zrt = np.asfortranarray(np.ones((lx, ly, lk-1, 2)))*np.nan
282276
self.zwt = np.asfortranarray(np.ones((lx, ly, lk, 2)))*np.nan
283277
self.uf[:,:,:,1], self.vf[:,:,:,1], \
284278
self.dzt[:,:,:,1], self.zrt[:,:,:,1], \
285-
self.zwt[:,:,:,1] = tracpy.inout.readfields(tinds[0],self.grid,nc)
279+
self.zwt[:,:,:,1] = tracpy.inout.readfields(tinds[0], self.grid, nc)
286280

287281
## Find zstart0 and ka
288282
# The k indices and z grid ratios should be on a wflux vertical grid,

0 commit comments

Comments
 (0)