[GRASS-user] windows format for temporary file path

Hi,
On a google search for building a temporary path in a bash script for win grass I found the manual for g.tempfile which lead me to write the line

temp1=`g.tempfile pid=$$`

which, on echo, generated E:\GRASSdata/newLocation/PERMANENT/.tmp/9168.0

and in use results in the following error:
Illegal filename. Character </> not allowed.
<E:\GRASSdata/newLocation/PERMANENT/.tmp/7512.0> is an illegal file name

So, do I have to run sed or some such to convert the / to \? or is there a windows safe way to generate a temporary file path? Or better yet an OS agnostic way?

Richard

Richard Edmonds wrote:

On a google search for building a temporary path in a bash script for
win grass I found the manual for g.tempfile which lead me to write the line

temp1=`g.tempfile pid=$$`

which, on echo, generated E:\GRASSdata/newLocation/PERMANENT/.tmp/9168.0

and in use results in the following error:
Illegal filename. Character </> not allowed.
<E:\GRASSdata/newLocation/PERMANENT/.tmp/7512.0> is an illegal file name

In what context does the error occur?

So, do I have to run sed or some such to convert the / to \? or is there
a windows safe way to generate a temporary file path? Or better yet an
OS agnostic way?

The problem isn't the backslashes. The error message:

  Illegal filename. Character </> not allowed.

implies that it's expecting a simple file name rather than a complete
path.

The error message matches that generated by G_legal_filename():

  fprintf(stderr,
    _("Illegal filename. Character <%c> not allowed.\n"), *s)

That function is used to validate the names of maps, mapsets,
locations, etc, where the name is used as a single component of the
path. It's possible that it's being used where it isn't appropriate
(i.e. where an absolute path is appropriate), but it's also possible
that you're passing an absolute path where a simple filename (or even
a map name) is actually required.

--
Glynn Clements <glynn@gclements.plus.com>

On 16/03/2012 3:13 a.m., Glynn Clements wrote:

It's possible that it's being used where it isn't appropriate
(i.e. where an absolute path is appropriate), but it's also possible
that you're passing an absolute path where a simple filename (or even
a map name) is actually required.

Ah that makes sense,
I was trying to use it to generate, then remove a temporary layer for use in the v.to.rast command. Apologies for not posting the command which generated the error.

I have changed my bash script with the following which now generates only a name rather than a path and at least now the v.to.rast command runs.

temp1="$$1"
v.to.rast --overwrite input=$GIS_OPT_INPUT output=$temp1 use=attr column=elevation

However I still get that error when I try to remove the layer/file. I had inferred from the g.remove man page that:

g.remove rast=$temp1

would remove the raster layer $temp1 but instead i get the error:
Removing raster <39281>
raster: couldn't be removed

trying it with:

g.remove rast=`g.tempfile pid=$$1`

generates the error I described originally but does look closer to actually removing the layer:

Removing raster <E:\GRASSdata/newLocation/PERMANENT/.tmp/35001.0>
Illegal filename. Character </> not allowed.
Raster map <E:\GRASSdata/newLocation/PERMANENT/.tmp/35001.0> not found
Illegal filename. Character </> not allowed.
raster: couldn't be removed
Illegal filename. Character </> not allowed.
header: couldn't be removed
Illegal filename. Character </> not allowed.
category: couldn't be removed
Illegal filename. Character </> not allowed.
color: couldn't be removed
Illegal filename. Character </> not allowed.
history: couldn't be removed
Illegal filename. Character </> not allowed.
misc: couldn't be removed
Illegal filename. Character </> not allowed.
fcell: couldn't be removed
Illegal filename. Character </> not allowed.
g3dcell: couldn't be removed
Illegal filename. Character </> not allowed.
colr2/PERMANENT: couldn't be removed
<E:\GRASSdata/newLocation/PERMANENT/.tmp/35001.0> nothing removed

Sorry the error dump is so long, but I'm having real trouble dealing with these pesky temporary layers.

Hopefully if I get this sorted then a search for "create and remove temporary layers in bash for GRASS GIS" will turn up a working solution which I couldn't find previously. Perhaps I should start a new thread?

Richard

Hi,

2012/3/15 Glynn Clements <glynn@gclements.plus.com>:

which, on echo, generated E:\GRASSdata/newLocation/PERMANENT/.tmp/9168.0

and in use results in the following error:
Illegal filename. Character </> not allowed.
<E:\GRASSdata/newLocation/PERMANENT/.tmp/7512.0> is an illegal file name

In what context does the error occur?

btw, probably we could introduce in pythonlib new function for
generating temp map names?

tempmap() -> tmp_argv[0]_pid_num, eg. `temp_r_in_wms_7512.0` (`dot` is
not allowed for vector maps).

Martin

--
Martin Landa <landa.martin gmail.com> * http://geo.fsv.cvut.cz/~landa

Richard Edmonds wrote:

> It's possible that it's being used where it isn't appropriate
> (i.e. where an absolute path is appropriate), but it's also possible
> that you're passing an absolute path where a simple filename (or even
> a map name) is actually required.

Ah that makes sense,
I was trying to use it to generate, then remove a temporary layer for
use in the v.to.rast command. Apologies for not posting the command
which generated the error.

I have changed my bash script with the following which now generates
only a name rather than a path and at least now the v.to.rast command runs.

temp1="$$1"

The "usual" convention for temporary map names is to combine the name
of the script, "tmp", and the current PID, e.g.:

  temprast="myscript.tmp.$$"

Use of the PID should ensure uniqueness even amongst multiple
concurrent runs of the script in a single mapset (which shouldn't
happen anyhow), and anyone using ".tmp." in a "real" map name is
asking for trouble.

v.to.rast --overwrite input=$GIS_OPT_INPUT output=$temp1 use=attr
column=elevation

However I still get that error when I try to remove the layer/file. I
had inferred from the g.remove man page that:

g.remove rast=$temp1

would remove the raster layer $temp1 but instead i get the error:
Removing raster <39281>
raster: couldn't be removed

"$$" evaluates to the PID of the shell running the script. "$$1" will
be that PID with a "1" appended to it. So the above error indicates
that the PID was 3928.

There doesn't appear to be anything wrong with your commands; whatever
value is chosen for temp1, I would expect "g.remove rast=$temp1" to
remove the map created by "v.to.rast ... output=$temp1", provided that
it hasn't changed in the meantime.

--
Glynn Clements <glynn@gclements.plus.com>

On 16/03/2012 6:14 p.m., Glynn Clements wrote:

The "usual" convention for temporary map names is to combine the name
of the script, "tmp", and the current PID, e.g.:

  temprast="myscript.tmp.$$"

OK, now using that convention

There doesn't appear to be anything wrong with your commands; whatever
value is chosen for temp1, I would expect "g.remove rast=$temp1" to
remove the map created by "v.to.rast ... output=$temp1", provided that
it hasn't changed in the meantime.

Ok this is very strange, my script is very small 30 odd lines, using the correct convention I still get an error. Here's a script with ONLY the offending commands as follows with the subsequent error:

#!/bin/sh

# g.parser demo script for shell programing

#%module
#% description: g.parser test script
#%end
#%option
#% key: input_vector
#% type: string
#% gisprompt: old,vector,vector
#% description: shoreline vector file
#% required : yes
#%end
if [ -z "$GISBASE" ] ; then
     echo "You must be in GRASS GIS to run this program." 1>&2
     exit 1
fi
if [ "$1" != "@ARGS_PARSED@" ] ; then
     exec g.parser "$0" "$@"
fi

if [ -n "$GIS_OPT_OPTION1" ] ; then
     echo "Value of GIS_OPT_OPTION1: '$GIS_OPT_OPTION1'"
fi
TMPLAYER1="v.myscript.tmp.$$"
v.to.rast --overwrite input=$GIS_OPT_INPUT_VECTOR output=$TMPLAYER1 use=attr column=elevation
echo $TMPLAYER1
g.remove rast=$TMPLAYER1
# DONE

v.myscript.tmp.1564
Loading data...
Reading features...
Writing raster map...
Converted areas: 0 of 0
Converted points/lines: 4 of 4
v.to.rast complete.
Removing raster <v.myscript.tmp.1564>
raster: couldn't be removed
(Tue Mar 20 08:41:16 2012) Command finished (0 sec)

If my script appears correct and I still cant correctly remove the temp layer I've created does this mean I've got a setting deep in GRASS set incorrectly, or is it related to the fact I'm running winGRASS? Perhaps something to do with the data? Permissions problems (my go-to bug in LINUX) seem unlikely if GRASS made the file to begin with. I'm stumped, nothing I've read so far seems to relate. Perhaps this is a simple enough script for someone to copy and paste into winGRASS/GRASS and confirm?

Richard Edmonds wrote:

Ok this is very strange, my script is very small 30 odd lines, using the
correct convention I still get an error. Here's a script with ONLY the
offending commands as follows with the subsequent error:

v.to.rast --overwrite input=$GIS_OPT_INPUT_VECTOR output=$TMPLAYER1 use=attr column=elevation

g.remove rast=$TMPLAYER1

Removing raster <v.myscript.tmp.1564>
raster: couldn't be removed

is it related to the fact I'm running winGRASS?

Probably. There seems to be a problem with the database drivers on
WinGRASS:

  http://trac.osgeo.org/grass/ticket/1276
  http://trac.osgeo.org/grass/ticket/1579

--
Glynn Clements <glynn@gclements.plus.com>