[GRASS-user] RE: grass-user Digest, Vol 61, Issue 42 [SEC=UNCLASSIFIED]

Andrew MacIntyre Andrew.MacIntyre at acma.gov.au
Thu May 19 21:36:55 EDT 2011


> Date: Wed, 18 May 2011 14:53:25 +0200
> From: "Johannes Radinger" <JRadinger at gmx.at>
> Subject: Re: [GRASS-user] Re: Using temp files in Grass Script
> To: Glynn Clements <glynn at gclements.plus.com>
> Cc: grass-user at lists.osgeo.org
> Message-ID: <20110518125325.274840 at gmx.net>
> Content-Type: text/plain; charset="utf-8"
> 
> 
> -------- Original-Nachricht --------
> > Datum: Tue, 17 May 2011 13:11:41 +0100
> > Von: Glynn Clements <glynn at gclements.plus.com>
> > An: "Johannes Radinger" <JRadinger at gmx.at>
> > CC: grass-user at lists.osgeo.org
> > Betreff: Re: [GRASS-user] Re: Using temp files in Grass Script
> 
> >
> > Johannes Radinger wrote:
> >
> > > I found that thread about temporary maps...
> > >
> > > e.g in my case in my python script i use:
> > >
> > >     #Convert river from vector to raster format
> > >     grass.run_command("v.to.rast",
> > >                       input = "river_gen",
> > >                       overwrite = True,
> > >                       output = "river_raster",
> > >                       use = "val",
> > >                       value = res)
> > >
> > >     #Thinning the rastarized river
> > >     grass.run_command("r.thin",
> > >                       input = "river_raster",
> > >                       overwrite = True,
> > >                       output = "river_raster_thin")
> > >
> > > and here is the map river_raster only temporary for the thinning
> > > process and the river_raster_thin is used for further calculations.
> > > At the moment I have a g.remove to remove the river raster after
> > > the thinning process but should I handle that with temporary files/maps
> > > and if yes how is that exactly done in my python script?
> >
> > 1. In general, you should make an attempt at ensuring that temporary
> > map names won't conflict with any existing map name. The usual
> > approach is to include both ".tmp" and the PID in the map name, e.g.:
> >
> > 	import os
> > 	...
> > 	global tmpmap
> > 	tmp_map = 'river_raster.tmp.%d' % os.getpid()
> >
> > 2. Deletion should ideally be handled using an exit handler, e.g.:
> >
> > 	import os
> > 	import atexit
> > 	import grass.script as grass
> >
> > 	tmp_map = None
> >
> > 	def cleanup():
> > 	    if tmp_map:
> > 		grass.run_command('g.remove', rast = tmp_map, quiet =
> True)
> >
> > 	def main():
> > 	    global tmpmap
> > 	    tmp_map = 'river_raster.tmp.%d' % os.getpid()
> > 	    ...
> >
> > 	if __name__ == "__main__":
> > 	    options, flags = grass.parser()
> > 	    atexit.register(cleanup)
> > 	    main()
> >
> > Using an exit handler ensures that the temporary map gets removed
> > regardless of how the script terminates (e.g. if it terminates due to
> > an exception).
> 
> Thank you for your nice examples how to do that with tmp files and the exit
> handler.
> 
> Just some questions:
> 1) is the PID also used on windows systems? so can it be integrated in a script
> which also windows users want to use? Is it correct that the PID is the same
> value for running the whole script one time?
> 
> 2) what exactly does the "global tmpmap"?
> 
> 3)I've got a lot of tmp-files which will be created during the process, so is
> there and option to tell the g.remove that all maps containing .tmp.%d' %
> os.getpid() in the end should be removed instead of typing all the tmp map
> files into the list of g.remove.
> 
> 4) so the whole thing works that in the end all the tmp maps are deleted
> after the processing of the script and after an exception etc.

Rather than use a simple global variable as per Glynn's example, use a list:

	tmp_map = []

Any time you create a temporary file name, append it to the list:

	tmp_fn = '...'
	tmp_map.append(tmp_fn)

In your cleanup handler, scan the list:

	def cleanup():
	    while tmp_map:
	        tfn = tmp_map.pop()
	        grass.run_command('g.remove', rast = tfn, quiet = True)

Using a list like this removes the need for the global statements, as you only manipulate the list rather than assign new objects to the global variable.

-------------------------> "These thoughts are mine alone!" <---------
Andrew MacIntyre           Operations Branch
tel:   +61 2 6219 5356     Communications Infrastructure Division
fax:   +61 2 6253 3277     Australian Communications & Media Authority
email: andrew.macintyre at acma.gov.au            http://www.acma.gov.au/



NOTICE: This email message is for the sole use of the intended recipient(s) 
 and may contain confidential and privileged information. Any unauthorized 
 review, use, disclosure or distribution is prohibited. If you are not the 
 intended recipient, please contact the sender by reply email and destroy all 
 copies of the original message.


More information about the grass-user mailing list