-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_test.py
executable file
·62 lines (51 loc) · 1.67 KB
/
bin_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import xarray as xr
import numpy as np
#import timing
import sys
memb=sys.argv[1]
#yr=sys.argv[2]
estr=str(memb).zfill(3)
datadir='/ccc/scratch/cont003/gen0727/garciagi/DETREND/'
homedir=''
outdir='/ccc/scratch/cont003/gen0727/garciagi/DETREND/TEMP/'
#outdir=homedir
#fname="ORCA025.L75-OCCITENS."+estr+"_y"+str(yr)+".5d_ssha.nc"
fname='GOM025.GSL301_m'+estr+'_1d_dtrend.nc'
print(fname)
#with xr.open_dataset(homedir+'lonlat.nc') as fic:
#lon=fic.nav_lon.values
#lat=fic.nav_lat.values
#can't deal with NaN so set to value outside of bin range so that they don't get considered
#ssh[np.where(np.isnan(ssh))]=-9999
##quantiles for the file
qfile=xr.open_dataset(datadir+'quantiles010_ssht.nc')
#bwidth=0.05
#brange=np.arange(-200,201,bwidth*1e2)*1e-2
#bhalf=bwidth*0.5
quantiles=qfile.quantile.values
brange=quantiles
print(quantiles.shape)
with xr.open_dataset(datadir+fname) as fic:
ssh=fic.ssht.values
lon=fic.lons.values
lat=fic.lats.values
nt,ny,nx=ssh.shape
nq=quantiles.shape
binned=np.zeros((nq[0],nt,ny,nx))
print(binned.shape)
#ssh[np.where(np.isnan(ssh))]=-9999
work=np.zeros(np.shape(ssh))
for i in range(len(brange)):
ix,iy,iz=np.where((ssh >= brange[i]-bhalf) & \
(ssh < brange[i]+bhalf))
work[ix,iy,iz]=1
binned[i,:,:,:]=work
work=work*0
#create xarray dataset
fic=xr.Dataset({'bin_counts': (['quant','time_counter','y','x'], binned)},
coords={'nav_lon': (['y','x'],lon),
'nav_lat': (['y','x'],lat),
'quantile': (['quant','y','x'],brange)})
fic.to_netcdf(outdir+'GOM025.GS301_m'+estr+'_binned.nc')
fic.close()