[GRASS-dev] GRASS Scripts access to channel headers info

Greetings all

I'm doing a few scripts that requires accessing LANDSAT/or other satellite images channel header information (e.g. Gain/Bias). Is it possible to acess that information through a Script file? I mean, having a script that processes what I want and in the meanwhile uses some channel header values?
Thanks
Best regards,
Antonio

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4834 (20100204) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

2010/2/4 António Rocha <antonio.rocha@deimos.com.pt>:

Greetings all

I'm doing a few scripts that requires accessing LANDSAT/or other satellite
images channel header information (e.g. Gain/Bias). Is it possible to acess
that information through a Script file? I mean, having a script that
processes what I want and in the meanwhile uses some channel header values?

See
http://www.grassbook.org/examples_menu3rd.php
-> CHAPTER 8: Scripts to bulk import LANDSAT-TM5/LANDSAT-TM7 scenes
    from GLCF Maryland into GRASS
    * glcf_landsat7_for_NC_SPM_WAKE_process.sh (reproject, spatial
subset with GDAL)
    * glcf_landsat5_for_NC_SPM_WAKE_import.sh (import into GRASS)
    * glcf_landsat7_2000_for_NC_SPM_WAKE_import.sh (import into GRASS)
    * glcf_landsat7_2002_for_NC_SPM_WAKE_import.sh (import into GRASS)

You can then use r.info to retrieve the metadata and use them for processing.

Hope this helps,
Markus

Thank you Markus. I suppose, for now, it's enough to give me a clue about managing LANDSAT images
By the way, is there any specific command to retrieve a single information from metadata? I mean, r.info gives me a lot of information in a string.
Best regards
Antonio

Markus Neteler wrote:

2010/2/4 António Rocha <antonio.rocha@deimos.com.pt>:

Greetings all

I'm doing a few scripts that requires accessing LANDSAT/or other satellite
images channel header information (e.g. Gain/Bias). Is it possible to acess
that information through a Script file? I mean, having a script that
processes what I want and in the meanwhile uses some channel header values?
    
See
http://www.grassbook.org/examples_menu3rd.php
-> CHAPTER 8: Scripts to bulk import LANDSAT-TM5/LANDSAT-TM7 scenes
    from GLCF Maryland into GRASS
    * glcf_landsat7_for_NC_SPM_WAKE_process.sh (reproject, spatial
subset with GDAL)
    * glcf_landsat5_for_NC_SPM_WAKE_import.sh (import into GRASS)
    * glcf_landsat7_2000_for_NC_SPM_WAKE_import.sh (import into GRASS)
    * glcf_landsat7_2002_for_NC_SPM_WAKE_import.sh (import into GRASS)

You can then use r.info to retrieve the metadata and use them for processing.

Hope this helps,
Markus

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4847 (20100208) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Hi, maybe the grass-python bindings can help you ?

from grass, starting python :

In [1]: import grass.script as grass

In [2]: dict_region_info = dict(x.split('=', 1) for x in grass.read_command('r.info', flags = 'rstgudmp', map = 'elevation.10m').strip().replace('"','').replace('(','').replace(')','').split('\n') if '=' in x)

In [3]: dict_region_info
Out[3]:
{'datatype': 'DCELL',
'east': '609000',
'ewres': '10',
'max': '1846.743408',
'min': '1061.064087',
'north': '4928000',
'nsres': '10',
'south': '4914020',
'timestamp': 'none',
'title': ' elevation.10m',
'units': '(none)',
'vertical_datum': 'none',
'west': '590010'}

In [4]: dict_region_info['east']
Out[4]: '609000'

the code maybe needs little adjustmens, but it can give you an idea.

Il giorno 08/feb/2010, alle ore 11.36, António Rocha ha scritto:

Thank you Markus. I suppose, for now, it's enough to give me a clue about managing LANDSAT images
By the way, is there any specific command to retrieve a single information from metadata? I mean, r.info gives me a lot of information in a string.

maybe the grass-python bindings can help you ?

from grass, start python :

In [1]: import grass.script as grass

In [2]: dict_region_info = dict(x.split('=', 1) for x in grass.read_command('r.info', flags = 'rstgudmp', map = 'elevation.10m').strip().replace('"','').replace('(','').replace(')','').split('\n') if '=' in x)

In [3]: dict_region_info
Out[3]:
{'datatype': 'DCELL',
'east': '609000',
'ewres': '10',
'max': '1846.743408',
'min': '1061.064087',
'north': '4928000',
'nsres': '10',
'south': '4914020',
'timestamp': 'none',
'title': ' elevation.10m',
'units': '(none)',
'vertical_datum': 'none',
'west': '590010'}

In [4]: dict_region_info['east']
Out[4]: '609000'

the code maybe needs little adjustmens, but it can give you an idea.

Best regards
Antonio

Markus Neteler wrote:

2010/2/4 António Rocha <antonio.rocha@deimos.com.pt>:

Greetings all

I'm doing a few scripts that requires accessing LANDSAT/or other satellite
images channel header information (e.g. Gain/Bias). Is it possible to acess
that information through a Script file? I mean, having a script that
processes what I want and in the meanwhile uses some channel header values?
   
See
http://www.grassbook.org/examples_menu3rd.php
-> CHAPTER 8: Scripts to bulk import LANDSAT-TM5/LANDSAT-TM7 scenes
   from GLCF Maryland into GRASS
   * glcf_landsat7_for_NC_SPM_WAKE_process.sh (reproject, spatial
subset with GDAL)
   * glcf_landsat5_for_NC_SPM_WAKE_import.sh (import into GRASS)
   * glcf_landsat7_2000_for_NC_SPM_WAKE_import.sh (import into GRASS)
   * glcf_landsat7_2002_for_NC_SPM_WAKE_import.sh (import into GRASS)

You can then use r.info to retrieve the metadata and use them for processing.

Hope this helps,
Markus

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4847 (20100208) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

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

Massimo Di Stefano wrote:

Hi, maybe the grass-python bindings can help you ?

from grass, starting python :

In [1]: import grass.script as grass

In [2]: dict_region_info = dict(x.split('=', 1) for x in
grass.read_command('r.info', flags = 'rstgudmp', map =
'elevation.10m').strip().replace('"','').replace('(','').replace(')','').split('\n')
if '=' in x)

raster_info() already does this; it also converts the bounds and range
to numbers:

grass.raster_info('elevation.dem')

{'north': 4928000.0,
'timestamp': '"none"',
'min': 1066.0,
'datatype': 'CELL',
'max': 1840.0,
'ewres': 30.0,
'vertical_datum': '(none)',
'west': 590010.0,
'units': '(none)',
'title': 'DEM (7.5 minute) (elevation)',
'east': 609000.0,
'nsres': 30.0,
'south': 4914020.0}

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

Hey All

Both suggestions worked. Thanks
By the way, I'm having some difficulties in using mapcalc in a python scripts.
I'm doing this
grass.mapcalc(options['output']=options['raster1']+options['raster2'])

And I get:
  File "/usr/local/grass-6.4.0svn/scripts/python.teste",
line 61
    grass.mapcalc(options['output']=options['raster1']+options['raster2'])
SyntaxError: keyword can't be an expression

How can I use an option as an input in mapcalc funtion, called from a Python script?
Thanks
Antonio
Glynn Clements wrote:

Massimo Di Stefano wrote:

Hi, maybe the grass-python bindings can help you ?

from grass, starting python :

In [1]: import grass.script as grass

In [2]: dict_region_info = dict(x.split('=', 1) for x in
grass.read_command('r.info', flags = 'rstgudmp', map =
'elevation.10m').strip().replace('"','').replace('(','').replace(')','').split('\n')
if '=' in x)
    
raster_info() already does this; it also converts the bounds and range
to numbers:

grass.raster_info('elevation.dem')
    

{'north': 4928000.0,
'timestamp': '"none"',
'min': 1066.0,
'datatype': 'CELL',
'max': 1840.0,
'ewres': 30.0,
'vertical_datum': '(none)',
'west': 590010.0,
'units': '(none)',
'title': 'DEM (7.5 minute) (elevation)',
'east': 609000.0,
'nsres': 30.0,
'south': 4914020.0}

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4854 (20100210) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

2010/2/10 António Rocha <antonio.rocha@deimos.com.pt>:

Hey All

Both suggestions worked. Thanks
By the way, I'm having some difficulties in using mapcalc in a python
scripts.
I'm doing this
grass.mapcalc(options['output']=options['raster1']+options['raster2'])

And I get:
File "/usr/local/grass-6.4.0svn/scripts/python.teste",
line 61
grass.mapcalc(options['output']=options['raster1']+options['raster2'])
SyntaxError: keyword can't be an expression

How can I use an option as an input in mapcalc funtion, called from a Python
script?

In grass7/scripts/r.plane/r.plane.py I see

    grass.mapcalc("$name = $type($round(x() * $kx + y() * $ky + $kz))",
                  name = name, type = type, round = round, kx = kx, ky
= ky, kz = kz)

Perhaps a way how to do it.

Markus

António Rocha wrote:

By the way, I'm having some difficulties in using mapcalc in a python
scripts.
I'm doing this
grass.mapcalc(options['output']=options['raster1']+options['raster2'])

This isn't valid Python syntax.

grass.mapcalc() accepts a template string followed by keyword
arguments for the substitutions, e.g.:

  grass.mapcalc("${out} = ${rast1} + ${rast2}",
                out = options['output'],
                rast1 = options['raster1'],
                rast2 = options['raster2'])

Although, I normally copy all of the options into separate variables
at the beginning of main(), i.e.:

  def main():
      output = options['output']
      raster1 = options['raster1']
      raster2 = options['raster2']

  ...

      grass.mapcalc("${out} = ${rast1} + ${rast2}",
                    out = output,
                    rast1 = raster1,
                    rast2 = raster2)

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

Hey

Regarding a previous message, from Glynn, about how to use mapcalc in a Python Script in GRASS6.4. I have the following script (module hearer is also there):
import os
import sys
import grass.script as grass

def main():
    output = options['output']
    raster1 = options['raster1']
    raster2 = options['raster2'] grass.mapcalc("${out} = ${rast1} + ${rast2}", out = output, rast1 = raster1, rast2 = raster2)
    return 0

if __name__ == "__main__":
    options, flags = grass

When I enter:
python.teste raster1=aspect@PERMANENT raster2=aspect@PERMANENT output=Zteste8

GRASS freezes, I mean. It's still processing without stopping or exiting or even any error. Any idea of what might be wrong?
Thanks

Best Regards
Antonio

António Rocha wrote:

By the way, I'm having some difficulties in using mapcalc in a python scripts.
I'm doing this
grass.mapcalc(options['output']=options['raster1']+options['raster2'])
    
This isn't valid Python syntax.

grass.mapcalc() accepts a template string followed by keyword
arguments for the substitutions, e.g.:

  grass.mapcalc("${out} = ${rast1} + ${rast2}",
                out = options['output'],
                rast1 = options['raster1'],
                rast2 = options['raster2'])

Although, I normally copy all of the options into separate variables
at the beginning of main(), i.e.:

  def main():
      output = options['output']
      raster1 = options['raster1']
      raster2 = options['raster2']

  ...

      grass.mapcalc("${out} = ${rast1} + ${rast2}",
                    out = output,
                    rast1 = raster1,
                    rast2 = raster2)

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4860 (20100212) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

António Rocha wrote:

    options, flags = grass

I presume that the rest got lost here?

When I enter:
python.teste raster1=aspect@PERMANENT raster2=aspect@PERMANENT
output=Zteste8

GRASS freezes, I mean. It's still processing without stopping or exiting
or even any error. Any idea of what might be wrong?

No. If you post the complete script as an attachment, I can test it.

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

YOu right
Here it goes

Thank you

Glynn Clements wrote:

António Rocha wrote:

    options, flags = grass
    
I presume that the rest got lost here?

When I enter:
python.teste raster1=aspect@PERMANENT raster2=aspect@PERMANENT output=Zteste8

GRASS freezes, I mean. It's still processing without stopping or exiting or even any error. Any idea of what might be wrong?
    
No. If you post the complete script as an attachment, I can test it.

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4860 (20100212) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

(attachments)

python.teste (1.38 KB)

António Rocha wrote:

>> GRASS freezes, I mean. It's still processing without stopping or exiting
>> or even any error. Any idea of what might be wrong?
>>
>
> No. If you post the complete script as an attachment, I can test it.

Here it goes

It works fine for me in 6.4, 6.5 and 7.0.

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

Hello Glynn

That is strange and unexpected. Do you just copy the python file to the
Scripts folder or do you use Makefiles?
What can I do debug some sort of problem because it really freezes my
GRASS and it doesn't do anything?

Thanks
Antonio

António Rocha wrote:

>> GRASS freezes, I mean. It's still processing without stopping or
exiting
>> or even any error. Any idea of what might be wrong?
>>
>
> No. If you post the complete script as an attachment, I can test it.

Here it goes

It works fine for me in 6.4, 6.5 and 7.0.

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

antonio wrote:

GRASS freezes, I mean. It's still processing without stopping or
exiting or even any error. Any idea of what might be wrong?

is the CPU is at 100%?
if so, it is probably working just on a very big problem.

what does the computational region look like?
g.region -p

more than 40000 x 40000 rows,cols?

Hamish

antonio.rocha@deimos.com.pt wrote:

That is strange and unexpected. Do you just copy the python file to the
Scripts folder or do you use Makefiles?

I just saved the attachment, added execute pemission, and ran it.

What can I do debug some sort of problem because it really freezes my
GRASS and it doesn't do anything?

As Hamish suggests, are you sure that you aren't trying to run it with
an excessively large region?

Is r.mapcalc showing up in the output from "ps"?

Do you see files being written to the temporary directory:

  <database>/<location>/<mapset>/.tmp/<hostname>/
?

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

Greetings all

Seems that the problem might be Compational region...
Now it's working.

THanks

Best regards
Antonio

Hamish wrote:

antonio wrote:
  

GRASS freezes, I mean. It's still processing without stopping or
exiting or even any error. Any idea of what might be wrong?
    
is the CPU is at 100%?
if so, it is probably working just on a very big problem.

what does the computational region look like?
g.region -p

more than 40000 x 40000 rows,cols?

Hamish

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4872 (20100216) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4872 (20100216) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com