Skip to content
Snippets Groups Projects
Commit 9351e174 authored by Dominik Zobel's avatar Dominik Zobel
Browse files

Add postprocessing example

parent a90b8456
No related branches found
No related tags found
1 merge request!56Debugging Strategies lecture notes
Pipeline #66815 passed
......@@ -227,6 +227,90 @@ $\Rightarrow$_Show use of `gdb`_
- Check prerequisites/environment
- Use debugger
# Errors due to data access and communication
## Postprocessing example (1/3)
- Extract data from netCDF file and calculate interpolation weights
- File located [here](static/vmro3_input4MIPs_ozone_1850-1855_rev.nc)
- Works for the active time stamps, but not for the ones in the comment
```python
filename = 'vmro3_input4MIPs_ozone_1850-1855_rev.nc'
timestamp_start = '1851-01-01T00:00:00.000' # '1850-01-01T00:00:00.000'
timestamp_end = '1851-03-31T00:00:00.000' # '1850-03-31T00:00:00.000'
timestep_days = 10
Calculate_Weights(filename, timestamp_start, timestamp_end, timestep_days)
```
## Postprocessing example (2/3)
:::{.smaller}
```{.python startFrom="23"}
def Calculate_Weights(filename, timestamp_start, timestamp_end, timestep_days):
start_date, end_date, timestep = Get_Time_Objects(
timestamp_start=timestamp_start, timestamp_end=timestamp_end,
timestep_days=10)
ds = Open_File(filename=filename)
model_date = start_date
while ( model_date < end_date ):
o3_prev_date = Select_Date(ds=ds, model_date=model_date, method='ffill')
o3_next_date = Select_Date(ds=ds, model_date=model_date, method='bfill')
if ( o3_next_date == o3_prev_date ):
prev_weight = 0.5
else :
delta_step_sec = (o3_next_date - o3_prev_date).total_seconds()
delta_sec = (model_date - o3_prev_date).total_seconds()
prev_weight = 1.0 - delta_sec/delta_step_sec
next_weight = 1.0 - prev_weight
print('weights for', model_date, 'are', prev_weight, 'and', next_weight)
model_date = model_date + timestep
```
:::
## Postprocessing example (3/3)
```python
def Open_File(filename):
import xarray as xr
return xr.open_dataset(filename)\
.convert_calendar('standard', use_cftime=True)
def Select_Date(ds, model_date, method='ffill'):
import datetime as dt
ds_elem = ds.sel(time=model_date, method=method)
return dt.datetime.strptime(str(ds_elem['time'].values),
'%Y-%m-%d %H:%M:%S')
```
:::{.fragment}
```{.python startFrom="14"}
def Get_Time_Objects(timestamp_start, timestamp_end, timestep_days):
import datetime as dt
time_format = '%Y-%m-%dT%H:%M:%S.%f'
start_date = dt.datetime.strptime(timestamp_start, time_format)
end_date = dt.datetime.strptime(timestamp_end, time_format)
timestep = dt.timedelta(days=timestep_days)
return [start_date, end_date, timestep]
```
:::
## Parallel programs
......
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment