Using CIS within python script

4 posts / 0 new
Last post
KirstyPringle
Using CIS within python script

Hi there,

I'm new to CIS and trying to work out how best to use it with our existing Python routines. We are hoping to develop code that uses CIS to do some plotting and analysis of our model data, but it has to be within a Python script rather than on command line.

There are two ways that I can see of doing this, the first is to simply call a CIS command using syntax like:

os.system("cis info "+path+"/"+campaign+"/"+filename+" --variable SO4")

As far as I can see this would give me the full functionality of CIS, but the syntax is a bit clumsy as the commands all need to be in the wrapper. So I suspect there is a better way of doing it.

The other option that I can see is to import cis as a library, but this seems to give me access to only a section of CIS and with very different syntax? I'm quite new to Python so may be missing something. It seems that using a CIS as a library you need to first read in each variable as a initial step, e.g.

cis.read_data(path+"/"+campaign+"/"+filename, "SO4")

And then plot, but I'm struggling with the syntax here, I tried this:

cis.plotting.heatmap SO4

But that's not working, could you point me in the direction of the correct syntax?

But the bigger question is really some advice on how you would advise us to combine CIS within a python script. We are at the stage of designing a suite of Python code so it would be great to work out the best approach to take,

Thanks for your help,
Kirsty

duncanwp
Re: Using CIS within python script

Hi Kirsty,

Thanks for the interesting question, hopefully I can help. CIS was originally designed just as a command line tool without an easy way of using it in Python, but we are currently working towards making CIS a useful Python library which you can use in your scripts as well.

There are really three options at the moment (two of which you've already mentioned):

1. The os.system calls - which as you rightly say are pretty clunky.

2. Using the Python functions directly (we call this our API, or Application Programming Interface). This has some nice benefits, including being able to combine operations without writing the intermediate files to disk, for some analyses this can significantly speed things up. It's worth pointing out though that we aren't quite happy with the way some of these functions are called, so will be changing them in the near future to make them simpler and more consistent (the plotting in particular). I've written some documentation about this here which might be useful: http://cis.readthedocs.org/en/1.2.1/api/cis.html

3. Finally, there is another option which might not have been obvious, but may be a good compromise for now: You actually call the main function commands from within python directly, so the following should work for your info example:

>>> from cis.cis_main import info_cmd
>>> from cis.parse import parse_args
>>>
>>> main_arguments = parse_args(['info', path+"/"+campaign+"/"+filename, '-v', 'SO4'])
>>> info_cmd(main_arguments)

Hopefully those will help for now, but I'm happy to keep you updated on progress with the API. Using CIS from within Python is also one of the things we'll be covering in our user workshop in January, so if you're able to make it it should be useful for you (see http://www.eventbrite.com/e/cis-user-workshop-tickets-19294904579?aff=we... for details).

Just let me know if you want more information or have any other questions!

Thanks,

Duncan

KirstyPringle
Hi Duncan,

Hi Duncan,

Thanks for your help! I've signed up to the January workshop.

I'm interested in your option 3, but I'm not totally sure that I understand it! This maybe a python novice question, but although the above example works for me, I've no idea how I would use similar syntax in e.g. a plot or a read command. You'd need to know the structure required for each command? And what each command is called, e.g. not just info but info_cmd. Is this something that is standard?

Thanks,
Kirsty

duncanwp
Re: Re: Using CIS within python script

It's true you need to know what each command is called, but handily, these are just whatever the command line command is followed by '_cmd'. So: 'aggregate_cmd', 'subset_cmd', etc.

The syntax is also exactly the same as on the command line, so the documentation should be able to help you put those together.

I'm happy to have a chat about it next week if you're still not sure!

Duncan

Website designed & built by OCC