Skip to content

Commit 6ffa4e9

Browse files
committed
add dynamic colorbar is plotting and add bottom temp animation
1 parent 9381652 commit 6ffa4e9

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

.github/workflows/postprocess_croco.yml

+14
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ jobs:
146146
--skip_time 6 \
147147
--ref_date '2000-01-01 00:00:00'
148148
sudo chown somisana:somisana ${{ env.RUN_DIR }}/postprocess/croco_avg_temp_surf.gif
149+
- name: animate bottom temperature and currents
150+
continue-on-error: true
151+
run: |
152+
docker run \
153+
--rm \
154+
-v ${{ env.RUN_DIR }}:/tmp \
155+
ghcr.io/saeon/somisana-croco_cli_${{ inputs.BRANCH_REF }}:latest \
156+
crocplot \
157+
--fname /tmp/output/croco_avg.nc \
158+
--level 0 \
159+
--gif_out /tmp/postprocess/croco_avg_temp_bot.gif \
160+
--skip_time 6 \
161+
--ref_date '2000-01-01 00:00:00'
162+
sudo chown somisana:somisana ${{ env.RUN_DIR }}/postprocess/croco_avg_temp_bot.gif
149163
- name: animate 100m temperature and currents
150164
continue-on-error: true
151165
run: |

cli.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ def reformat_gfs_atm_handler(args):
190190
help='do a plot/animation of a croco output file(s)')
191191
parser_crocplot.add_argument('--fname', required=True, type=str, help='input native CROCO filename (can handle wildcards to animate over multiple files)')
192192
parser_crocplot.add_argument('--var', required=False, default='temp', type=str, help='the variable name to plot')
193-
parser_crocplot.add_argument('--gif_out', required=True, type=str, help='the output gif filename')
193+
parser_crocplot.add_argument('--gif_out', required=False, type=str, help='the output gif filename')
194+
parser_crocplot.add_argument('--mp4_out', required=False, type=str, help='the output mp4 filename')
194195
parser_crocplot.add_argument('--level', required=False, default=None, type=parse_int, help='level to plot. If >=0, then a sigma level is plotted. If <0 then a z level (in m) is plotted. Default behaviour will plot the surface layer')
195196
parser_crocplot.add_argument('--ticks', required=False, type=parse_list,
196-
default=[12,13,14,15,16,17,18,19,20,21,22],
197+
default=None,
197198
help='contour ticks to use in plotting the variable')
198199
parser_crocplot.add_argument('--cbar_label', required=False, default='temperature ($\degree$C)', type=str, help='the label used for the colorbar')
199200
parser_crocplot.add_argument('--isobaths', required=False, type=parse_list,
@@ -220,7 +221,8 @@ def crocplot_handler(args):
220221
add_vectors = True,
221222
skip_time = args.skip_time,
222223
isobaths=args.isobaths,
223-
gif_out=args.gif_out
224+
gif_out=args.gif_out,
225+
mp4_out=args.mp4_out
224226
)
225227
parser_crocplot.set_defaults(func=crocplot_handler)
226228

crocotools_py/plotting.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ def plot(fname,
242242
ax=None, # allowing for adding to an existing axis
243243
var='temp', # croco variable to plot
244244
grdname=None, # option croco grid file (if grid variables arem't in the croco output file)
245-
time=None, # see post.get_var() for 'time' format. If a single value, then a plot is made, if two values, then an animation between those times is made
245+
time=slice(None), # see post.get_var() for 'time' format. If a single value, then a plot is made, if two values, then an animation between those times is made
246246
level=None, # see post.get_var() for 'level' format. Has to be a single value for this function to do a plot
247-
ticks = np.linspace(12,22,num=11), # the ticks to plot (should default to None and determine automatically)
247+
ticks = None, #np.linspace(12,22,num=11), (gets set automatically if None)
248248
cmap = 'Spectral_r',
249249
extents = None, # [lon0,lon1,lat0,lat1] whole domain plotted if None
250250
ref_date = None, # datetime, from CROCO model setup
251251
add_cbar = True, # add a colorbar?
252252
cbar_loc = None, # [left, bottom, width, height] (gets set automatically if None)
253-
cbar_label = 'temperature ($\degree$C)',
253+
cbar_label = None, # 'temperature ($\degree$C)', we just use 'var' is None
254254
add_vectors = True, # add vectors?
255255
scale_uv = None, # define the vector scaling (gets set automatically if None)
256256
ref_vector = None, # value of reference vector
@@ -260,6 +260,7 @@ def plot(fname,
260260
isobaths = None, # optional list of isobaths to overlay over plot
261261
jpg_out=None, # full path to jpg output
262262
gif_out=None, # full path to gif output
263+
mp4_out=None, # option to rather write an mp4
263264
):
264265
'''
265266
this is a convenience function for doing a quick 2D plot with minimal coding.
@@ -293,7 +294,22 @@ def plot(fname,
293294
else:
294295
# this will be an animation, starting with the first time-step
295296
data_plt=da_var.isel(time=0).values
296-
297+
298+
if ticks is None:
299+
# get the range of the data to plot (using 5th and 95th percentiles)
300+
vmin=np.nanpercentile(da_var, 1)
301+
vmax=np.nanpercentile(da_var, 99)
302+
# round these to two significant figures
303+
vmin=round(vmin, 2 - int(np.floor(np.log10(abs(vmin)))) - 1)
304+
vmax=round(vmax, 2 - int(np.floor(np.log10(abs(vmax)))) - 1)
305+
num_ticks = 10
306+
step = (vmax - vmin) / num_ticks
307+
step = round(step, 2 - int(np.floor(np.log10(abs(step)))) - 1)
308+
# update vmax based on the rounded step
309+
vmax = vmin + num_ticks * step
310+
# Generate the ticks using the rounded step size
311+
ticks = np.arange(vmin, vmax + step/10, step) # Add a small value to ensure new_vmax is included
312+
297313
# compute the extents from the grid if not explicitly defined
298314
if extents is None:
299315
lon_min = min(np.ravel(lon))
@@ -346,6 +362,8 @@ def plot(fname,
346362
time_plt = plot_time(ax,time_var[0])
347363

348364
if add_cbar:
365+
if cbar_label is None:
366+
cbar_label = var
349367
plot_cbar(ax,var_plt,label=cbar_label,ticks=ticks,loc=cbar_loc,aspect_ratio=aspect_ratio)
350368

351369
if add_vectors:
@@ -417,6 +435,9 @@ def plot_tstep(i):
417435
if gif_out is not None:
418436
print('writing '+gif_out)
419437
anim.save(gif_out, writer='imagemagick')
438+
if mp4_out is not None:
439+
print('writing '+mp4_out)
440+
anim.save(mp4_out, writer="ffmpeg")
420441

421442
def plot_blk(croco_grd, # the croco grid file - needed as not saved in the blk file
422443
croco_blk_file, # the croco blk file

0 commit comments

Comments
 (0)