Opened 2 months ago

Closed 5 days ago

#3255 closed help (fixed)

saving iris cubes

Reported by: NoelClancy Owned by: pmcguire
Component: Land Surface Modelling Keywords: Python
Cc: Platform: JASMIN
UM Version:

Description

When you run a long python script, and you are only making minor changes to the plotting script at the end of the python file.

Is there a way that you can save the iris cubes without having to run the entire python file again just because you need to change a label in the plot?

Change History (8)

comment:1 Changed 2 months ago by pmcguire

  • Component changed from JULES to Land Surface Modelling
  • Keywords Python added
  • Status changed from new to accepted

Hi Noel:
Are you running the Python script from the command-line or from an interactive session?

If you're running from an interactive session, then maybe it can work. This is how to run a script from an interactive session, if you weren't doing it before:
https://stackoverflow.com/questions/4624416/is-there-a-possibility-to-execute-a-python-script-while-being-in-interactive-mod

Then in the interactive session, after the script runs, you can type other commands, to save cubes or make modifications to plots, etc.

Does this help?
Patrick McGuire

Last edited 2 months ago by pmcguire (previous) (diff)

comment:2 Changed 2 months ago by NoelClancy

I'm running on jasmin-sci2.ceda.ac.uk

comment:3 Changed 2 months ago by pmcguire

  • Platform set to JASMIN

comment:4 Changed 2 months ago by pmcguire

OK.
Thanks.

Are you running Python interactively?
Or are you running Python from the command-line?

Patrick McGuire

comment:5 Changed 5 days ago by pmcguire

Thu, May 21, 11:38 AM

Hi Patrick,

I tried your previous suggestion but with no success.
"This is how to run a script from an interactive session, if you weren't doing it before:
https://stackoverflow.com/questions/4624416/is-there-a-possibility-to-execute-a-python-script-while-being-in-interactive-mod
"

If I could save the cubes then, running plotting scripts would be so much faster for me.

Another approach would be to implement a time constraint as well as a variable constraint. Obviously, I can constrain the iris cube for one variable, where v= variable as this is in my code.


var_name_constraint = iris.Constraint(cube_func=lambda x: x.var_name == '{v}'.format(site=site, A_expt= A_expt, v=v))

A_expt_cubelist = jules.load(filename_ A_expt_hr, var_name_constraint, conv_to_grid=False, callback=jules_output_cube)

A_expt_cube = A_expt_cubelist.concatenate_cube()

A_expt_cube = iris.util.squeeze(A_expt_cube)


But sometimes I would like to constrain the cube for time period also. For example in some instances I’m only plotting the first 72 hours of July so that I can see what the diurnal cycle looks like.

But at fluxnet sites where there are years of data, the cube has no time constraint and there could be 160000 hours of data and then I only plot 72 hours.

qplt.plot(A_expt_cube[4380:4451]

It takes too much time to run my plotting scripts. PLV recommended trying to find sample code on the internet and using that as my starting point but I couldn’t implement what I found.

Noel

comment:6 Changed 5 days ago by pmcguire

May 21, 2020, 12:42 PM

Hi Patrick,

I think I figured out how to save an iris cube as a netCDF, but I can’t constrain the cube for a time range. There’s an error in my code at the end of the attached file.

Maybe I’m nearly there.

Noel

comment:7 Changed 5 days ago by pmcguire

Jun 2, 2020, 9:58 AM

Hi Patrick,

I managed to debug and fix my code to apply a time constraint and then saving the iris cube as a netCDF in a simple file.

However, I will need to embed this code within loops in other python files. I am hopeful that I can do this alone since I managed it on a simple file.

Can we close the ticket #3255: saving iris cubes for now at least?

Noel

comment:8 Changed 5 days ago by pmcguire

  • Resolution set to fixed
  • Status changed from accepted to closed
Note: See TracTickets for help on using tickets.