-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_start_conditions.py
More file actions
66 lines (53 loc) · 2.95 KB
/
_start_conditions.py
File metadata and controls
66 lines (53 loc) · 2.95 KB
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
63
64
65
66
########################################################################################################
# Author: Simon Schäfers
# Date: 2025-01-27
########################################################################################################
# This script contains start condition methods for the particles. The `start_condition` function
# initializes a chosen start condition method. The `from_csv` method reads the particle data from a
# csv file and returns the particle data as a dictionary, containing lon, lat, depth, and optionally
# time of release.
import pandas as pd
def start_condition(set,plot = False):
'''
Main function to initialize the start conditions for the particles.
Initializes the chosen start condition method based on the settings.
## Parameters:
- set: Settings, settings for the advection routine. Important attribute is `start_particles`.
## Returns:
- dict, dictionary containing the particle data (lon, lat, depth, and optionally time)
'''
return globals()[set.start_particles](set,plot = plot)
def from_csv(set,plot = False):
'''
Reads the particle data from a csv file and returns the particle data as a dictionary, containing
lon, lat, depth, and optionally time of release. Longitude is converted to the range [0,360] if the
settings contain `lon_lims` with negative values.
## Parameters:
- set: Settings, settings for the advection routine. Important attribute is `particle_file`.
Further, the settings can contain `lon_lims`, `lat_lims`, and `depth_lims` to mask the particle data.
## Returns:
- dict, dictionary containing the particle data (lon, lat, depth, and optionally time)
'''
particle_data = pd.read_csv(set.particle_file)
# mask particle data based on lon, lat, and depth limits
if hasattr(set,'lon_lims'):
lon_lims = [lon_lim if lon_lim > 0 else lon_lim+360 for lon_lim in set.lon_lims]
if lon_lims[0] < lon_lims[1]:
lon_mask = (particle_data['lon'] >= lon_lims[0]) & (particle_data['lon'] <= lon_lims[1])
else:
lon_mask = (particle_data['lon'] >= lon_lims[0]) | (particle_data['lon'] <= lon_lims[1])
if hasattr(set,'lat_lims'):
lat_mask = (particle_data['lat'] >= set.lat_lims[0]) & (particle_data['lat'] <= set.lat_lims[1])
if hasattr(set,'depth_lims'):
depth_mask = (particle_data['depth'] >= set.depth_lims[0]) & (particle_data['depth'] <= set.depth_lims[1])
mask = lon_mask & lat_mask & depth_mask
# include time if available
if hasattr(particle_data,'time'):
return {'lon':particle_data['lon'][mask],
'lat':particle_data['lat'][mask],
'depth':particle_data['depth'][mask],
'time':particle_data['time'][mask].astype('datetime64[ns]')}
else:
return {'lon':particle_data['lon'][mask],
'lat':particle_data['lat'][mask],
'depth':particle_data['depth'][mask]}