29
29
import os
30
30
import numpy as np
31
31
import math
32
- import itertools
32
+ # import itertools
33
33
34
34
from bgcval2 .bgcvaltools .dataset import dataset
35
35
from bgcval2 .bgcvaltools .bv2tools import maenumerate
87
87
88
88
def maenumerate (marr ):
89
89
mask = ~ marr .mask .ravel ()
90
- for i , m in itertools . izip (np .ndenumerate (marr ), mask ):
90
+ for i , m in zip (np .ndenumerate (marr ), mask ):
91
91
if m : yield i
92
92
93
93
def myhaversine (lon1 , lat1 , lon2 , lat2 ):
@@ -96,12 +96,12 @@ def myhaversine(lon1, lat1, lon2, lat2):
96
96
on the earth (specified in decimal degrees)
97
97
"""
98
98
# convert decimal degrees to radians
99
- [lon1 , lat1 , lon2 , lat2 ] = [math .radians [ l ] for l in [lon1 , lat1 , lon2 , lat2 ]]
99
+ [lon1 , lat1 , lon2 , lat2 ] = [math .radians ( l ) for l in [lon1 , lat1 , lon2 , lat2 ]]
100
100
101
101
# haversine formula
102
102
dlon = lon2 - lon1
103
103
dlat = lat2 - lat1
104
- a = math .sin (dlat / 2. )** 2 + math .cos (lat1 ) * math .cos (lat2 ) * mathsin (dlon / 2. )** 2
104
+ a = math .sin (dlat / 2. )** 2 + math .cos (lat1 ) * math .cos (lat2 ) * math . sin (dlon / 2. )** 2
105
105
c = 2. * math .asin (math .sqrt (a ))
106
106
dist = 6367000. * c
107
107
@@ -201,7 +201,7 @@ def loadAtlanticMask_full(altmaskfile, maskname='tmaskatl', grid = 'eORCA1'):
201
201
else :
202
202
raise ValueError ("Grid not recognised in this calculation: %s" , grid )
203
203
nc = dataset (altmaskfile , 'r' )
204
- alttmask = nc .variables [maskname ][latslice26Nnm , :]
204
+ alttmask = nc .variables [maskname ][:]
205
205
nc .close ()
206
206
loadedAltMask_full = True
207
207
@@ -383,7 +383,7 @@ def fov_sa(nc, keys, **kwargs):
383
383
thkcello = nc .variables ['thkcello' ][:].squeeze ()
384
384
385
385
lats = np .ma .masked_outside (lats , - 30. , - 34. )
386
- lons = np .ma .masked_outside (lons , - 100 . , 20. )
386
+ lons = np .ma .masked_outside (lons , - 65 . , 20. )
387
387
388
388
# lons = nc.variables['nav_lon'][:]
389
389
lons_bounds_0 = nc .variables ['bounds_lon' ][:].min (2 )
@@ -394,23 +394,23 @@ def fov_sa(nc, keys, **kwargs):
394
394
395
395
lons_diff = lons_bounds_1 - lons_bounds_0
396
396
if lons_diff .min () != lons_diff .max ():
397
- print ('Can not assume that grid is even' )
397
+ print ('Can not assume that longitude grid is even' )
398
398
assert 0
399
399
400
400
# Load Atlantic Mask
401
401
if not loadedAltMask_full :
402
+ altmaskfile = get_kwarg_file (kwargs , 'altmaskfile' , default = 'bgcval2/data/basinlandmask_eORCA1.nc' )
402
403
loadAtlanticMask_full (altmaskfile , maskname = 'tmaskatl' , grid = grid )
403
404
404
405
# Load and mask vso
405
406
vso = np .ma .array (nc .variables ['vso' ][:]).squeeze () # #vso in PSU m/s
406
407
vo = np .ma .array (nc .variables ['vo' ][:]).squeeze () # #vso in PSU m/s
407
408
sal0 = vso / vo - sal_ref
408
409
409
- mask_2d = alttmask + lats .mask + lons .mask
410
-
410
+ mask_2d = lats .mask + lons .mask
411
+ #print(alttmask, alttmask.shape)
411
412
unique_lats = {la :True for la in np .ma .masked_where (mask_2d , lats ).compressed ()}
412
413
zonal_distances = {la : myhaversine (0 , la , 1. , la ) for la in unique_lats .keys ()}
413
-
414
414
415
415
mask_3d = [mask_2d for _ in range (75 )]
416
416
mask_3d = np .stack (mask_3d , axis = 0 )
@@ -423,19 +423,44 @@ def fov_sa(nc, keys, **kwargs):
423
423
sal0 = np .ma .masked_where (vso .mask + mask_3d + (sal0 == 0. ), sal0 ) # shape alignment?
424
424
xarea = np .ma .masked_where (sal0 .mask , thkcello )
425
425
426
+ # from matplotlib import pyplot
427
+ # for name, dat in zip(
428
+ # ['vo', 'sal0', 'xarea', 'lats', 'lons', 'mask2d', 'mask3d', 'alttmask',],
429
+ # [vo, sal0, xarea, lats, lons, mask_2d, mask_3d, alttmask]):
430
+ # print('plotting', name)
431
+ # if name in ['lats', 'lons', 'mask2d', 'alttmask']:
432
+ # plot = dat
433
+ # else:
434
+ # plot = dat.mean(0)
435
+ # pyplot.pcolormesh(plot)
436
+ # pyplot.title(name)
437
+ # pyplot.colorbar()
438
+ # pyplot.savefig('images/'+name+'.png')
439
+ # pyplot.close()
440
+
426
441
# calculate cross sectional area by multiplying zonal cell length by cell depth thickness
427
442
for (z , y , x ), thk in maenumerate (xarea ):
428
443
la = lats [y , x ]
429
444
xarea [z , y , x ] = thk * zonal_distances [la ]
430
- xarea_sum = xarea .sum (axis = (0 ,1 ))
445
+ xarea_sum = xarea .sum (axis = (0 ,2 ))
431
446
447
+ #print('xarea', {f:True for f in xarea_sum.compressed()}.keys()) # should be 4 or 5 values.
448
+ #assert 0
432
449
# Take the zonal mean of the meridional velocity
450
+
433
451
total = vo * sal0 * xarea
434
- total = total .sum (axis = (0 ,1 ))/ xarea_sum
452
+ total = total .sum (axis = (0 , 2 ))/ xarea_sum
453
+ #print('total', {f:True for f in total.compressed()}.keys())
454
+ #pyplot.pcolormesh(vo[0] * sal0[0] * xarea[0])
455
+ #pyplot.title('total')
456
+ #pyplot.colorbar()
457
+ #pyplot.savefig('images/total.png')
458
+ #pyplot.close()
459
+
435
460
total = total .mean ()
436
- output = (- 1. / sal_rf ) * total
461
+ output = (- 1. / sal_ref ) * total
437
462
print (output )
438
- assert 0
463
+ # assert 0
439
464
return output
440
465
441
466
0 commit comments