[GRASS-dev] Using the pygrass "Mapset.glist" method and a dictionary

Hi list.

In a Python script, I use the following pygrass method to retrieve a list of
raster maps:

  from grass.pygrass.gis import Mapset
  ..
  landsat_imagery = dict()
  ..
  landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
'B[123457]')

However, whenever the result is empty, i.e. there are no B1, B2 and so on
named raster maps, running the script fails with the error:

  landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
'B[123457]')
  TypeError: 'str' object is not callable

Running the instructions step-wise, from within ipython, doesn't appear to be
problematic. What am I missing in this case?

Thank you for any hints, Nikos

Ciao Nikos,

On Thursday 23 May 2013 22:50:10 Nikos Alexandris wrote:

Hi list.

In a Python script, I use the following pygrass method to retrieve a list of
raster maps:

  from grass.pygrass.gis import Mapset
  ..
  landsat_imagery = dict()
  ..
  landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
'B[123457]')

However, whenever the result is empty, i.e. there are no B1, B2 and so on
named raster maps, running the script fails with the error:

  landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
'B[123457]')
  TypeError: 'str' object is not callable

Running the instructions step-wise, from within ipython, doesn't appear to
be problematic. What am I missing in this case?

Mmh...

I've tried your code... and it is working on my machine. In both: python shell
and from file...

Python shell:

from grass.pygrass.gis import Mapset
Mapset().glist('rast', pattern = 'B[123457]')

['B4', 'B5', 'B1', 'B3', 'B2', 'B7']

From file:

cat nik.py

from grass.pygrass.gis import Mapset

landsat_imagery = dict()
landsat_imagery['Spectral Bands'] = Mapset().glist('rast',
                                                   pattern='B[123457]')

print landsat_imagery

%run nik.py

{'Spectral Bands': ['B4', 'B5', 'B1', 'B3', 'B2', 'B7']}

Therefore I think that the problem is not here...
can you send me or share/publish the whole code somewhere (gist.github, dpaste
or other).

Cheers

Pietro

On Friday 24 of May 2013 00:23:28 Pietro Zambelli wrote:

Ciao Nikos,

Ciao Pietro,

..

> However, whenever the result is empty, i.e. there are no B1, B2 and so on
> named raster maps, running the script fails with the error:
> landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
>
> 'B[123457]')
>
> TypeError: 'str' object is not callable
>
> Running the instructions step-wise, from within ipython, doesn't appear to
> be problematic. What am I missing in this case?

Mmh...

I've tried your code... and it is working on my machine. In both: python
shell and from file...

Python shell:
>>> from grass.pygrass.gis import Mapset
>>> Mapset().glist('rast', pattern = 'B[123457]')

['B4', 'B5', 'B1', 'B3', 'B2', 'B7']

This should be empty! From the above result it means that the requested bands
pre-exist. Consider a Mapswet with bands named like B10, B20, B30, B40, B50,
B70, B80.

(Sure, we can erase that ending "0" while importing, for example, Landsat
bands in GRASS, but... who knows, maybe someone needs to respect the original
naming pattern of old Landsat scenes.)

From file:
>>> cat nik.py

from grass.pygrass.gis import Mapset

landsat_imagery = dict()
landsat_imagery['Spectral Bands'] = Mapset().glist('rast',
                                                   pattern='B[123457]')

print landsat_imagery

>>> %run nik.py

{'Spectral Bands': ['B4', 'B5', 'B1', 'B3', 'B2', 'B7']}

Here too, it should be empty.

Therefore I think that the problem is not here...
can you send me or share/publish the whole code somewhere (gist.github,
dpaste or other).

Grazie, Nikos

[all deleted]

@Pietro: I think I have messed up in trying to integrate a dictionary (as you
correctly recommended last week, off-list communication) in some functions.

Using simple lists is for me easier. However, I recognise the advantages in
using a dictionary and want to incorporate it.

Sensing you of list the latest code...

Thank you, Nikos

On 23/05/13 21:50, Nikos Alexandris wrote:

Hi list.

In a Python script, I use the following pygrass method to retrieve a list of
raster maps:

  from grass.pygrass.gis import Mapset
  ..
  landsat_imagery = dict()
  ..
  landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
'B[123457]')

However, whenever the result is empty, i.e. there are no B1, B2 and so on
named raster maps, running the script fails with the error:

  landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
'B[123457]')
  TypeError: 'str' object is not callable

Running the instructions step-wise, from within ipython, doesn't appear to be
problematic. What am I missing in this case?

I can't reproduce this:

>>> landsat_imagery = dict()
>>> landsat_imagery
{}
>>> Mapset().glist('rast', pattern = 'B[123457]')

>>> landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern = 'B[123457]')
>>> landsat_imagery
{'Spectral Bands': }

So even with an empty result of the glist call, I don't get an error message.

Moritz

Nikos Alexandris wrote:

> In a Python script, I use the following pygrass method to retrieve a list
> of>
> raster maps:
> from grass.pygrass.gis import Mapset
> ..
> landsat_imagery = dict()
> ..
> landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
>
> 'B[123457]')
>
> However, whenever the result is empty, i.e. there are no B1, B2 and so on
>
> named raster maps, running the script fails with the error:
> landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern =
>
> 'B[123457]')
>
> TypeError: 'str' object is not callable
>
> Running the instructions step-wise, from within ipython, doesn't appear to
> be problematic. What am I missing in this case?

Moritz Lennert wrote:

I can't reproduce this:
>>> landsat_imagery = dict()
>>> landsat_imagery

{}

>>> Mapset().glist('rast', pattern = 'B[123457]')

>>> landsat_imagery['Spectral Bands'] = Mapset().glist('rast', pattern

= 'B[123457]')

>>> landsat_imagery

{'Spectral Bands': }

So even with an empty result of the glist call, I don't get an error
message.

Right! I committed a classical beginner's mistake, that is right after
importing

  from grass.pygrass.gis import Mapset

I used "Mapset", (outside of the function in which I used the above mentioned
code) as

  gisenv = grass.gisenv()
  Mapset = gisenv['MAPSET']

This naturally lead to the reported error. I found the question
<http://stackoverflow.com/q/10775541/1172302&gt; to be helpful.

Thanks, Nikos