[GRASS-user] retrieving a GRASS-python script return values

Greetings

I have built a GRASSPy Script to read a few text files (r.in.text.py)
At the end of the file I do a return of a list of integers with:
values=[1,2,4,5,6,43,32,553,2]

return values

My question is: I have another GRASS python script that and I want to call r.in.text.py to get these values.
What GRASS python functionality shall i use to retrieve these values? (I suppose grass.run_command is not a good idea).

THanks
Kat

katrin eggert wrote:

I have built a GRASSPy Script to read a few text files (r.in.text.py)
At the end of the file I do a return of a list of integers with:
values=[1,2,4,5,6,43,32,553,2]

return values

My question is: I have another GRASS python script that and I want to call
r.in.text.py to get these values.
What GRASS python functionality shall i use to retrieve these values? (I
suppose grass.run_command is not a good idea).

Have r.text.py write the data to stdout, then invoke r.text.py via
grass.read_command() or grass.parse_command().

For more complex data, you could add an option to r.text.py to write
the data in Python's "pickle" format.

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

Hello all,

I have undertaken a number of transformations of spectral data using the i.pca in both GRASS 6.4 and GRASS 7. Unfortunately when more than 6 bands are used r.info cannot display the eigen vectors and % importance
Output for r.info should look like this:

   PC1   1170.12 ( -0.63 -0.65 -0.43 ) [ 88.07% ]
   PC2    152.49 (  0.23  0.37 -0.90 ) [ 11.48% ]

But instead looks like this:

PC1 13770134.87 ( 0.1019, 0.5087, 0.1896, 0.5185, 0.4578, 0.4527, 0.107
6PC2 174134.96 ( 0.6530,-0.1228,-0.0673,-0.1493,-0.0026, 0.0203, 0.7289

with the full metadata being truncated.

As I know the values are being calculated as they display in the i.pca output upon completion,
so this appears to be a size issue with the display of r.info?
Is
 there any way to access the full metadata for the rasters without recalculating them?

Many thanks for reading!

Rebecca Bennett
Post-graduate Researcher
Bournemouth University

hello Glynn
I’m trying to parse a list of strings like this:
sys.stdout.write(output_v)
And I get this error

TypeError: argument 1 must be string or read-only character
buffer, not list
Any suggestion to do this?
Thanks

2011/2/10 Glynn Clements <glynn@gclements.plus.com>

katrin eggert wrote:

I have built a GRASSPy Script to read a few text files (r.in.text.py)
At the end of the file I do a return of a list of integers with:
values=[1,2,4,5,6,43,32,553,2]

return values

My question is: I have another GRASS python script that and I want to call
r.in.text.py to get these values.
What GRASS python functionality shall i use to retrieve these values? (I
suppose grass.run_command is not a good idea).

Have r.text.py write the data to stdout, then invoke r.text.py via
grass.read_command() or grass.parse_command().

For more complex data, you could add an option to r.text.py to write
the data in Python’s “pickle” format.


Glynn Clements <glynn@gclements.plus.com>

From the error message I take that output_v is a list but the write

command expects a string. So you need to convert your output_v to
string in order to use write.

One trick that I learned recently is how to concatenate values from a
list with using a separator.
1) convert the items in your list to strings: output_str = [str(i)
for i in output_v]
2) create a string using the join method: line =
'<separator>'.join(output_str) -- where <separator> is anything you
choose, comma, space, tab, |
3) write your string

There might be other ways to do this but I found this solution quite easy

Hope it helps
cheers
Daniel

On Wed, Feb 23, 2011 at 9:47 AM, katrin eggert
<katrineggert1980@gmail.com> wrote:

hello Glynn
I'm trying to parse a list of strings like this:
sys.stdout.write(output_v)
And I get this error
TypeError: argument 1 must be string or read-only character
buffer, not list
Any suggestion to do this?
Thanks
2011/2/10 Glynn Clements <glynn@gclements.plus.com>

katrin eggert wrote:

> I have built a GRASSPy Script to read a few text files (r.in.text.py)
> At the end of the file I do a return of a list of integers with:
> values=[1,2,4,5,6,43,32,553,2]
>
> return values
>
> My question is: I have another GRASS python script that and I want to
> call
> r.in.text.py to get these values.
> What GRASS python functionality shall i use to retrieve these values? (I
> suppose grass.run_command is not a good idea).

Have r.text.py write the data to stdout, then invoke r.text.py via
grass.read_command() or grass.parse_command().

For more complex data, you could add an option to r.text.py to write
the data in Python's "pickle" format.

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

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

katrin eggert wrote:

I'm trying to parse a list of strings like this:
       sys.stdout.write(output_v)
And I get this error
TypeError: argument 1 must be string or read-only character
buffer, not list

The simplest way to write Python objects is to use repr(), which will
convert them to a string in the format which Python uses for literals.
E.g.:

  > x=[1,2,3]
  > repr(x)
  '[1, 2, 3]'
  > sys.stdout.write(repr(x))
  [1, 2, 3]>

You can then parse the value with eval(). This will work for any
primitive type (anything which has a literal syntax (numbers, strings,
lists, tuples, dictionaries) as well as sets, but not for arbitrary
objects.

Alternatively, for lists you can use the .join and and .split methods
(along with e.g. str() and int() to convert the members to/from
strings), e.g.:

  > x=[1,2,3]
  > map(str,x)
  ['1', '2', '3']
  > y=','.join(map(str,x))
  > y
  '1,2,3'
  > y.split(',')
  ['1', '2', '3']
  > map(int,y.split(','))
  [1, 2, 3]

For passing more complex data between processes, look at the "pickle"
module from Python's standard library. This can handle arbitrary
objects, and handles references correctly (i.e. multiple references to
the same object won't get converted into multiple, distinct objects).

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