Skip to content

Commit 566422d

Browse files
GKDE calcs
1 parent bc2cbf2 commit 566422d

8 files changed

Lines changed: 268 additions & 29 deletions

Code/Calculations/kde_func.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# conda activate venv_kde_xarray
2+
3+
## Written on 18/01/2021 by Laura Gomez Navarro
4+
5+
# Modules:
6+
7+
from glob import glob
8+
import xarray as xr
9+
import numpy as np
10+
from scipy.stats import gaussian_kde
11+
12+
def rem_nans(ds):
13+
"""
14+
This renders lon and lat variables without nans for the last timestep.
15+
"""
16+
bad_indices = np.isnan(ds['lon'][:,-1]) | np.isnan(ds['lat'][:,-1])
17+
good_indices = ~bad_indices
18+
lon_end_nonans = ds['lon'][:,-1][good_indices]
19+
lat_end_nonans = ds['lat'][:,-1][good_indices]
20+
21+
return lon_end_nonans, lat_end_nonans
22+
23+
def kde_vals(lon_end_nonans, lat_end_nonans):
24+
"""
25+
TO DO
26+
"""
27+
x = lon_end_nonans.copy()
28+
y = lat_end_nonans.copy()
29+
30+
xy = np.vstack([x, y])
31+
z = gaussian_kde(xy)(xy)
32+
33+
idx = z.argsort()
34+
x, y, z = x[idx], y[idx], z[idx]
35+
36+
return x, y, z
37+
38+
def kde_parcels(nfile):
39+
"""
40+
Inputs
41+
nfile : filedir + filename
42+
43+
Outputs
44+
KDE for end fields of lon and lat
45+
"""
46+
47+
ds = xr.open_dataset(nfile)
48+
49+
# Remove nans:
50+
lon_end_nonans, lat_end_nonans = rem_nans(ds)
51+
52+
kde_x, kde_y, kde_z = kde_vals(lon_end_nonans, lat_end_nonans)
53+
54+
savedir = '/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/kde_calcs/'
55+
savename = savedir + 'KDE_' + nfile.split('/')[-1].split('.nc')[0] + '.npz'
56+
np.savez(savename, kde_x=kde_x, kde_y=kde_y, kde_z=kde_z)
57+
58+
return kde_x, kde_y, kde_z
59+
60+
def kde_parcels_bws(nfile, bw_set):
61+
"""
62+
bw_set = np.arange(0.01, 0.26, 0.01)
63+
"""
64+
ds = xr.open_dataset(nfile)
65+
66+
# Remove nans:
67+
lon_end_nonans, lat_end_nonans = rem_nans(ds)
68+
69+
x = lon_end_nonans.copy()
70+
y = lat_end_nonans.copy()
71+
72+
xy = np.vstack([x, y])
73+
74+
KDE_dict = {}
75+
for ii in range(0, len(bw_set)):
76+
KDE_dict["z%02d" %ii] = gaussian_kde(xy, bw_method=bw_set[ii])(xy)
77+
78+
# Saving:
79+
savedir = '/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/kde_calcs/'
80+
savename = savedir + 'KDE_' + nfile.split('/')[-1].split('.nc')[0] + '_bws.npz'
81+
np.savez(savename, bw_set=bw_set, KDE_dict=KDE_dict)
82+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#/bin/sh
2+
3+
# SGE: the job name
4+
#$ -N Azores_eNATL60_KDE_weekly_Dec_Jun
5+
#
6+
# The requested run-time, expressed as (xxxx sec or hh:mm:ss)
7+
#$ -l h_rt=02:00:00
8+
#
9+
# SGE: your Email here, for job notification
10+
#$ -M l.gomeznavarro@uu.nl
11+
#
12+
# SGE: when do you want to be notified (b : begin, e : end, s : error)?
13+
#$ -m es
14+
#
15+
# SGE: output in the current working dir
16+
#$ -cwd
17+
#
18+
cd /nethome/gomez023/parcels_Azores/kde_calcs/
19+
#python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_no_tides_1201_hourly_MONTH.nc")'
20+
#python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_tides_1201_hourly_MONTH.nc")'
21+
22+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_no_tides_0601_hourly_MONTH.nc")'
23+
#python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_tides_0601_hourly_MONTH.nc")'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#/bin/sh
2+
3+
# SGE: the job name
4+
#$ -N Azores_eNATL60_KDE_MONTH_nT_wT_Apr_May
5+
#
6+
# The requested run-time, expressed as (xxxx sec or hh:mm:ss)
7+
#$ -l h_rt=02:00:00
8+
#
9+
# SGE: your Email here, for job notification
10+
#$ -M l.gomeznavarro@uu.nl
11+
#
12+
# SGE: when do you want to be notified (b : begin, e : end, s : error)?
13+
#$ -m es
14+
#
15+
# SGE: output in the current working dir
16+
#$ -cwd
17+
#
18+
19+
cd /nethome/gomez023/parcels_Azores/kde_calcs/
20+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/ntides/monthly/Particle_AZO_grid100000p_ntides_0401_hourly_MONTH.nc")'
21+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/wtides/monthly/Particle_AZO_grid100000p_wtides_0401_hourly_MONTH.nc")'
22+
23+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/ntides/monthly/Particle_AZO_grid100000p_ntides_0501_hourly_MONTH.nc")'
24+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/wtides/monthly/Particle_AZO_grid100000p_wtides_0501_hourly_MONTH.nc")'
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#/bin/sh
2+
3+
# SGE: the job name
4+
#$ -N Azores_eNATL60_KDE_MONTH_nT_wT_Jan_Feb
5+
#
6+
# The requested run-time, expressed as (xxxx sec or hh:mm:ss)
7+
#$ -l h_rt=02:00:00
8+
#
9+
# SGE: your Email here, for job notification
10+
#$ -M l.gomeznavarro@uu.nl
11+
#
12+
# SGE: when do you want to be notified (b : begin, e : end, s : error)?
13+
#$ -m es
14+
#
15+
# SGE: output in the current working dir
16+
#$ -cwd
17+
#
18+
19+
cd /nethome/gomez023/parcels_Azores/kde_calcs/
20+
#python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/input_data/eNATL60/Azores/outputs_parcels/Particle_AZO_grid100000p_no_tides_0101_hourly_MONTH.nc")'
21+
#python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/input_data/eNATL60/Azores/outputs_parcels/Particle_AZO_grid100000p_tides_0101_hourly_MONTH.nc")'
22+
23+
#python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/input_data/eNATL60/Azores/outputs_parcels/Particle_AZO_grid100000p_no_tides_0201_hourly_MONTH.nc")'
24+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/input_data/eNATL60/Azores/outputs_parcels/Particle_AZO_grid100000p_tides_0201_hourly_MONTH.nc")'
25+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#/bin/sh
2+
3+
# SGE: the job name
4+
#$ -N Azores_eNATL60_KDE_weekly_nT_wT_Jul_Aug
5+
#
6+
# The requested run-time, expressed as (xxxx sec or hh:mm:ss)
7+
#$ -l h_rt=02:00:00
8+
#
9+
# SGE: your Email here, for job notification
10+
#$ -M l.gomeznavarro@uu.nl
11+
#
12+
# SGE: when do you want to be notified (b : begin, e : end, s : error)?
13+
#$ -m es
14+
#
15+
# SGE: output in the current working dir
16+
#$ -cwd
17+
#
18+
cd /nethome/gomez023/parcels_Azores/kde_calcs/
19+
20+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_no_tides_0701_hourly_MONTH.nc")'
21+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_tides_0701_hourly_MONTH.nc")'
22+
23+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_no_tides_0801_hourly_MONTH.nc")'
24+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/scratch/gomez023/outputs_parcels/Particle_AZO_grid100000p_tides_0801_hourly_MONTH.nc")'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#/bin/sh
2+
3+
# SGE: the job name
4+
#$ -N Azores_eNATL60_KDE_MONTH_nT_wT_Oct_Nov
5+
#
6+
# The requested run-time, expressed as (xxxx sec or hh:mm:ss)
7+
#$ -l h_rt=02:00:00
8+
#
9+
# SGE: your Email here, for job notification
10+
#$ -M l.gomeznavarro@uu.nl
11+
#
12+
# SGE: when do you want to be notified (b : begin, e : end, s : error)?
13+
#$ -m es
14+
#
15+
# SGE: output in the current working dir
16+
#$ -cwd
17+
#
18+
19+
cd /nethome/gomez023/parcels_Azores/kde_calcs/
20+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/ntides/monthly/Particle_AZO_grid100000p_ntides_1001_hourly_MONTH.nc")'
21+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/wtides/monthly/Particle_AZO_grid100000p_wtides_1001_hourly_MONTH.nc")'
22+
23+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/ntides/monthly/Particle_AZO_grid100000p_ntides_1101_hourly_MONTH.nc")'
24+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/Azores/eNATL60/wtides/monthly/Particle_AZO_grid100000p_wtides_1101_hourly_MONTH.nc")'
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#/bin/sh
2+
3+
# SGE: the job name
4+
#$ -N Azores_eNATL60_KDE_MONTH_nT_wT_Sep_Mar
5+
#
6+
# The requested run-time, expressed as (xxxx sec or hh:mm:ss)
7+
#$ -l h_rt=02:00:00
8+
#
9+
# SGE: your Email here, for job notification
10+
#$ -M l.gomeznavarro@uu.nl
11+
#
12+
# SGE: when do you want to be notified (b : begin, e : end, s : error)?
13+
#$ -m es
14+
#
15+
# SGE: output in the current working dir
16+
#$ -cwd
17+
#
18+
19+
cd /nethome/gomez023/parcels_Azores/kde_calcs/
20+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/ntides/monthly/Particle_AZO_grid100000p_ntides_0901_hourly_MONTH.nc")'
21+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/wtides/monthly/Particle_AZO_grid100000p_wtides_0901_hourly_MONTH.nc")'
22+
23+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/ntides/monthly/Particle_AZO_grid100000p_ntides_0301_hourly_MONTH.nc")'
24+
python3 -c 'from kde_func import *; _,_,_=kde_parcels("/data/oceanparcels/output_data/data_LauraGN/outputs_parcels/wtides/monthly/Particle_AZO_grid100000p_wtides_0301_hourly_MONTH.nc")'
25+

0 commit comments

Comments
 (0)