GRASS'c Python core function to use as g.mlist

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

GRASS'c Python core function to use as g.mlist

Jenny Turner
Greetings

I'm doing a GRASS Python Script where I need to list a set of raster maps but I'm not using its exact name but a basic regular expression (r flag in g.mlist function).
So I want to find a list of rasters with output on its name:
p=grass.mlist_grouped ('rast', pattern='output', mapset='Mapping') 
But I get null entries because no raster map is named output only output01 output02 output03 ...
I want to list all raster maps with output on itsd name. How can I do this using grass.mlist_grouped?

Thanks
Jenny

_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: GRASS'c Python core function to use as g.mlist

Ricardo Filipe Soares Garcia da
Hi Jenny

Long story short: use your command like this (note the asterisk on the
'pattern' variable):

# python code
p=grass.mlist_grouped ('rast', pattern='output*', mapset='Mapping')


I am using the Ipython shell to script grass. It has a cool feature,
that if you type the name of a function followed by two question
marks, you can see the source code of the function (as long as it is
coded in python itself).
So, in doing:

grass.mlist_grouped??

It is possible to see that the mlist_grouped function is internally
calling the 'g.mlist' command with only the 'm' flag.

Reading the documentation for the g.mlist command [1], I realize that,
without using the -r or -e flags, this command accepts wildcards[2] as
the 'pattern' string. The asterisk (*) is the wildcard character for
substituting for any zero or more characters. So, in order to get a
string that starts with 'output' and after that can have anything, you
just type 'output*'


[1] - http://grass.fbk.eu/grass64/manuals/html64_user/g.mlist.html
[2] - http://en.wikipedia.org/wiki/Wildcard_character#Computing


On Fri, Mar 11, 2011 at 12:10 PM, Jenny Turner
<[hidden email]> wrote:

> Greetings
> I'm doing a GRASS Python Script where I need to list a set of raster maps
> but I'm not using its exact name but a basic regular expression (r flag in
> g.mlist function).
> So I want to find a list of rasters with output on its name:
> p=grass.mlist_grouped ('rast', pattern='output', mapset='Mapping')
> But I get null entries because no raster map is named output only output01
> output02 output03 ...
> I want to list all raster maps with output on itsd name. How can I do this
> using grass.mlist_grouped?
> Thanks
> Jenny
> _______________________________________________
> grass-user mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/grass-user
>
>



--
___________________________ ___ __
Ricardo Garcia Silva
_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: GRASS'c Python core function to use as g.mlist

Jenny Turner
Hello Ricardo
Thanks for your feedback but the problem is that mapset cannot be used as input.
Because I got this error:
p=grass.mlist_grouped ('rast', pattern=pattern,
mapset='PERMANENT')
TypeError: mlist_grouped() got an unexpected keyword
argument 'mapset'.
If I avoid using mapset I get a dictionary variable in which, each entry is the list of rasters for each MAPSET.

Am I doing something wrong?
Thanks
Jenny

On Fri, Mar 11, 2011 at 2:29 PM, Ricardo Filipe Soares Garcia da <[hidden email]> wrote:
Hi Jenny

Long story short: use your command like this (note the asterisk on the
'pattern' variable):

# python code
p=grass.mlist_grouped ('rast', pattern='output*', mapset='Mapping')


I am using the Ipython shell to script grass. It has a cool feature,
that if you type the name of a function followed by two question
marks, you can see the source code of the function (as long as it is
coded in python itself).
So, in doing:

grass.mlist_grouped??

It is possible to see that the mlist_grouped function is internally
calling the 'g.mlist' command with only the 'm' flag.

Reading the documentation for the g.mlist command [1], I realize that,
without using the -r or -e flags, this command accepts wildcards[2] as
the 'pattern' string. The asterisk (*) is the wildcard character for
substituting for any zero or more characters. So, in order to get a
string that starts with 'output' and after that can have anything, you
just type 'output*'


[1] - http://grass.fbk.eu/grass64/manuals/html64_user/g.mlist.html
[2] - http://en.wikipedia.org/wiki/Wildcard_character#Computing


On Fri, Mar 11, 2011 at 12:10 PM, Jenny Turner
<[hidden email]> wrote:
> Greetings
> I'm doing a GRASS Python Script where I need to list a set of raster maps
> but I'm not using its exact name but a basic regular expression (r flag in
> g.mlist function).
> So I want to find a list of rasters with output on its name:
> p=grass.mlist_grouped ('rast', pattern='output', mapset='Mapping')
> But I get null entries because no raster map is named output only output01
> output02 output03 ...
> I want to list all raster maps with output on itsd name. How can I do this
> using grass.mlist_grouped?
> Thanks
> Jenny
> _______________________________________________
> grass-user mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/grass-user
>
>



--
___________________________ ___ __
Ricardo Garcia Silva


_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: GRASS'c Python core function to use as g.mlist

Ricardo Filipe Soares Garcia da
Hi Jenny

> Because I got this error:
> p=grass.mlist_grouped ('rast', pattern=pattern,
> mapset='PERMANENT')

This command runs fine on my system (by replacing 'pattern' with a
valid pattern string'). Maybe it is a version problem? I'm running
GRASS 6.4.0 on Ubuntu 10.10, coming from the ubuntugis-unstable
repository.

> If I avoid using mapset I get a dictionary variable in which, each entry is
> the list of rasters for each MAPSET.

Maybe you can solve your problem by discarding the rest of the output.
If you only want the rasters present in the 'PERMANENT' mapset and
since mlist_grouped returns a dictionary with mapset names as keys,
you can keep just the value associated with the 'PERMANENT' key. So,
you can just do:

# returns a list with rasters present in the 'PERMANENT' mapset
p = grass.mlist_grouped('rast', pattern=pattern)['PERMANENT']



On Mon, Mar 14, 2011 at 3:00 PM, Jenny Turner <[hidden email]> wrote:

> Hello Ricardo
> Thanks for your feedback but the problem is that mapset cannot be used as
> input.
> Because I got this error:
> p=grass.mlist_grouped ('rast', pattern=pattern,
> mapset='PERMANENT')
> TypeError: mlist_grouped() got an unexpected keyword
> argument 'mapset'.
> If I avoid using mapset I get a dictionary variable in which, each entry is
> the list of rasters for each MAPSET.
> Am I doing something wrong?
> Thanks
> Jenny
> On Fri, Mar 11, 2011 at 2:29 PM, Ricardo Filipe Soares Garcia da
> <[hidden email]> wrote:
>>
>> Hi Jenny
>>
>> Long story short: use your command like this (note the asterisk on the
>> 'pattern' variable):
>>
>> # python code
>> p=grass.mlist_grouped ('rast', pattern='output*', mapset='Mapping')
>>
>>
>> I am using the Ipython shell to script grass. It has a cool feature,
>> that if you type the name of a function followed by two question
>> marks, you can see the source code of the function (as long as it is
>> coded in python itself).
>> So, in doing:
>>
>> grass.mlist_grouped??
>>
>> It is possible to see that the mlist_grouped function is internally
>> calling the 'g.mlist' command with only the 'm' flag.
>>
>> Reading the documentation for the g.mlist command [1], I realize that,
>> without using the -r or -e flags, this command accepts wildcards[2] as
>> the 'pattern' string. The asterisk (*) is the wildcard character for
>> substituting for any zero or more characters. So, in order to get a
>> string that starts with 'output' and after that can have anything, you
>> just type 'output*'
>>
>>
>> [1] - http://grass.fbk.eu/grass64/manuals/html64_user/g.mlist.html
>> [2] - http://en.wikipedia.org/wiki/Wildcard_character#Computing
>>
>>
>> On Fri, Mar 11, 2011 at 12:10 PM, Jenny Turner
>> <[hidden email]> wrote:
>> > Greetings
>> > I'm doing a GRASS Python Script where I need to list a set of raster
>> > maps
>> > but I'm not using its exact name but a basic regular expression (r flag
>> > in
>> > g.mlist function).
>> > So I want to find a list of rasters with output on its name:
>> > p=grass.mlist_grouped ('rast', pattern='output', mapset='Mapping')
>> > But I get null entries because no raster map is named output only
>> > output01
>> > output02 output03 ...
>> > I want to list all raster maps with output on itsd name. How can I do
>> > this
>> > using grass.mlist_grouped?
>> > Thanks
>> > Jenny
>> > _______________________________________________
>> > grass-user mailing list
>> > [hidden email]
>> > http://lists.osgeo.org/mailman/listinfo/grass-user
>> >
>> >
>>
>>
>>
>> --
>> ___________________________ ___ __
>> Ricardo Garcia Silva
>
>



--
___________________________ ___ __
Ricardo Garcia Silva
_______________________________________________
grass-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-user