[GRASS-user] Running Scripts in GRASS

Hi Everyone,

I am a beginner user of GRASS (I normally use ArcGIS) running on Mac OS X Grass 6.4 ORC5.

I am trying to process MODIS chlor-a images according to the MODIS Wiki page. There are many scripts on the wiki page that I would like to use; however, I do not know how to:

  1. save the script file, i.e. what file extension to I use?
  2. where to store the script, i.e. Do I store it in the mapset location?
  3. how to call the script, i.e. within the terminal shell (bash).

Any help would be greatly appreciated.

Andrew

Hi Andrew,

I think you can search the grass list archive, because it is a very common ask :slight_smile:
Anyway, save your script under $GISBASE/scripts
(may be c:\GRASS\scripts"
You not need to set any special extention.

Let us know if you can do it.

bests

milton

2009/8/18 Andrew Lewin <andrew.lewin@sympatico.ca>

Hi Everyone,

I am a beginner user of GRASS (I normally use ArcGIS) running on Mac OS X Grass 6.4 ORC5.

I am trying to process MODIS chlor-a images according to the MODIS Wiki page. There are many scripts on the wiki page that I would like to use; however, I do not know how to:

  1. save the script file, i.e. what file extension to I use?
  2. where to store the script, i.e. Do I store it in the mapset location?
  3. how to call the script, i.e. within the terminal shell (bash).

Any help would be greatly appreciated.

Andrew


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

On Aug 18, 2009, at 3:18 PM, Andrew Lewin wrote:

Hi Everyone,

I am a beginner user of GRASS (I normally use ArcGIS) running on Mac OS X Grass 6.4 ORC5.

I am trying to process MODIS chlor-a images according to the MODIS Wiki page. There are many scripts on the wiki page that I would like to use; however, I do not know how to:
1) save the script file, i.e. what file extension to I use?

use whatever filename the script has (or is specified). No extension needed, but 'sh' is the standard shell script extension. But it does need to be executable, which won't be true if you download the raw script text or copy-n-paste. It should be set if the script is compressed.

In a Terminal, type this (drag-n-drop the script from a Finder window to the Terminal to paste the path to the script):

chmod +x /path/to/script

2) where to store the script, i.e. Do I store it in the mapset location?

You can put it whereever you like, but in the GISBASE is probably not a good idea. There is a GRASS environment setting to specify custom paths where to find GRASS addon executables: GRASS_ADDON_PATH.

Even easier, on OSX (for the application build), put it in:

~/Library/GRASS/6.4/Modules/bin

This is automatically added to GRASS_ADDON_PATH on startup. It will be created the first time you run GRASS.

3) how to call the script, i.e. within the terminal shell (bash).

type the name of the script.

Any help would be greatly appreciated.

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

-----
William Kyngesburye <kyngchaos*at*kyngchaos*dot*com>
http://www.kyngchaos.com/

"Oh, look, I seem to have fallen down a deep, dark hole. Now what does that remind me of? Ah, yes - life."

- Marvin

Thanks for the help, but I am not quite clear on the part you say the script has to be an executable. I copied and pasted the script from the wiki page and stored it in ~/Library/GRASS/6.4/Modules/bin; called the script by typing it (called chlatransl) to the Terminal shell. It said "chlatranl not found". How do I compile the script to make it an executable. Right now it is stored in a text file.

I apologize for being naive.

Andrew

Andrew Lewin, M.Sc., Adv. Dipl. GIS
Email: andrew.lewin@sympatico.ca
Tel: (905) 639-4646
Mobile: (905) 630-8441
Skype: andrew.lewin99

On 18-Aug-09, at 4:54 PM, William Kyngesburye wrote:

On Aug 18, 2009, at 3:18 PM, Andrew Lewin wrote:

Hi Everyone,

I am a beginner user of GRASS (I normally use ArcGIS) running on Mac OS X Grass 6.4 ORC5.

I am trying to process MODIS chlor-a images according to the MODIS Wiki page. There are many scripts on the wiki page that I would like to use; however, I do not know how to:
1) save the script file, i.e. what file extension to I use?

use whatever filename the script has (or is specified). No extension needed, but 'sh' is the standard shell script extension. But it does need to be executable, which won't be true if you download the raw script text or copy-n-paste. It should be set if the script is compressed.

In a Terminal, type this (drag-n-drop the script from a Finder window to the Terminal to paste the path to the script):

chmod +x /path/to/script

2) where to store the script, i.e. Do I store it in the mapset location?

You can put it whereever you like, but in the GISBASE is probably not a good idea. There is a GRASS environment setting to specify custom paths where to find GRASS addon executables: GRASS_ADDON_PATH.

Even easier, on OSX (for the application build), put it in:

~/Library/GRASS/6.4/Modules/bin

This is automatically added to GRASS_ADDON_PATH on startup. It will be created the first time you run GRASS.

3) how to call the script, i.e. within the terminal shell (bash).

type the name of the script.

Any help would be greatly appreciated.

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

-----
William Kyngesburye <kyngchaos*at*kyngchaos*dot*com>
http://www.kyngchaos.com/

"Oh, look, I seem to have fallen down a deep, dark hole. Now what does that remind me of? Ah, yes - life."

- Marvin

On Wed, Aug 19, 2009 at 2:16 AM, Andrew Lewin<andrew.lewin@sympatico.ca> wrote:

Thanks for the help, but I am not quite clear on the part you say the script
has to be an executable.

(indeed, it is not a requirement, see below)

I copied and pasted the script from the wiki page
and stored it in ~/Library/GRASS/6.4/Modules/bin; called the script by
typing it (called chlatransl) to the Terminal shell.

That's fine.

It said "chlatranl not found".

.. because typically the current directory is not in the path.
If the executable flag is on (set with chmod or a file manager)
then run it like this:

a) in the directory where the script is with
   ./chlatransl

or
b) with full path from anywhere
   /path/to/chlatransl

How do I compile the script to
make it an executable. Right now it is stored in a text file.

No need to compile (essentially, you cannot :slight_smile:
If you want to even avoid the chmod/filemanager stuff, then
give it as parameter to "sh" which is the shell interpreter:

a) in the directory where the script is with
    sh chlatransl

or
b) with full path from anywhere
    sh /path/to/chlatransl

Perhaps we should cook up a Wiki page describing this briefly?

Hope this helps,
Markus

On Aug 18, 2009, at 7:16 PM, Andrew Lewin wrote:

Thanks for the help, but I am not quite clear on the part you say the script has to be an executable. I copied and pasted the script from the wiki page and stored it in ~/Library/GRASS/6.4/Modules/bin; called the script by typing it (called chlatransl) to the Terminal shell. It said "chlatranl not found". How do I compile the script to make it an executable. Right now it is stored in a text file.

So, the script is: ~/Library/GRASS/6.4/Modules/bin/chlatransl

Take that chmod command I mentioned and type in the Terminal:

chmod +x ~/Library/GRASS/6.4/Modules/bin/chlatransl

I apologize for being naive.

Andrew

Andrew Lewin, M.Sc., Adv. Dipl. GIS
Email: andrew.lewin@sympatico.ca
Tel: (905) 639-4646
Mobile: (905) 630-8441
Skype: andrew.lewin99

On 18-Aug-09, at 4:54 PM, William Kyngesburye wrote:

On Aug 18, 2009, at 3:18 PM, Andrew Lewin wrote:

Hi Everyone,

I am a beginner user of GRASS (I normally use ArcGIS) running on Mac OS X Grass 6.4 ORC5.

I am trying to process MODIS chlor-a images according to the MODIS Wiki page. There are many scripts on the wiki page that I would like to use; however, I do not know how to:
1) save the script file, i.e. what file extension to I use?

use whatever filename the script has (or is specified). No extension needed, but 'sh' is the standard shell script extension. But it does need to be executable, which won't be true if you download the raw script text or copy-n-paste. It should be set if the script is compressed.

In a Terminal, type this (drag-n-drop the script from a Finder window to the Terminal to paste the path to the script):

chmod +x /path/to/script

-----
William Kyngesburye <kyngchaos*at*kyngchaos*dot*com>
http://www.kyngchaos.com/

"Time is an illusion - lunchtime doubly so."

- Ford Prefect

Markus wrote:

Perhaps we should cook up a Wiki page describing this briefly?

shrug. or as it's an external task, delegate the matter and add a link to some good document on the web about unix shell scripting. I'm sure there are many out there already. (but
whatever)

Andrew, another thing to realize is that these are just terminal prompt commands strung together via cut and paste into a file.

Also the wiki instructions were not written with a script in mind, so you will probably have to adjust a few filenames etc by hand.

I just updated the simple XY method example to use the new
'r.in.gdal -l' directly into a lat/lon location, but I think
the gdal_translate method is better.

typically shell scripts are given a shebang line which looks
like:

#!/bin/sh

this tells the OS that when you run the program it should use
the shell program (found in /bin/sh) to run it.

Hamish

Thanks to all of you who helped solve my problem.

I was able to run the script from the folder where the files I needed to process were stored.

I still have to tweak the script as the program says that it doesn’t recognize “". The script uses the "” symbol in the following manner:

for file in A*_MO_CHLO_4 ; do
  echo "map: $file"

  gdal_translate -a_srs "+init=epsg:4326" -a_nodata 65535 \
    -a_ullr -180 90 180 -90 -co "COMPRESS=PACKBITS" \
    $file ${file}_prep.tif

  r.in.gdal in=${file}_prep.tif out=$file
done

Can anyone tell me the language the program is in? I am not familiar with it and it would be good to know more about it.

Thanks again for your help.

Andrew

Date: Tue, 18 Aug 2009 18:04:59 -0700
From: hamish_b@yahoo.com
Subject: Re: [GRASS-user] Running Scripts in GRASS
To: andrew.lewin@sympatico.ca; neteler@osgeo.org
CC: grass-user@lists.osgeo.org

Markus wrote:

Perhaps we should cook up a Wiki page describing this briefly?

shrug. or as it’s an external task, delegate the matter and add a link to some good document on the web about unix shell scripting. I’m sure there are many out there already. (but
whatever)

Andrew, another thing to realize is that these are just terminal prompt commands strung together via cut and paste into a file.

Also the wiki instructions were not written with a script in mind, so you will probably have to adjust a few filenames etc by hand.

I just updated the simple XY method example to use the new
‘r.in.gdal -l’ directly into a lat/lon location, but I think
the gdal_translate method is better.

typically shell scripts are given a shebang line which looks
like:

#!/bin/sh

this tells the OS that when you run the program it should use
the shell program (found in /bin/sh) to run it.

Hamish

I found the solution…again, I had to ensure that the program was reading from the correct path.

Andrew


From: andrew.lewin@sympatico.ca
To: hamish_b@yahoo.com; neteler@osgeo.org
Subject: RE: [GRASS-user] Running Scripts in GRASS
Date: Wed, 19 Aug 2009 12:47:51 +0000
CC: grass-user@lists.osgeo.org

Thanks to all of you who helped solve my problem.

I was able to run the script from the folder where the files I needed to process were stored.

I still have to tweak the script as the program says that it doesn’t recognize “". The script uses the "” symbol in the following manner:

for file in A*_MO_CHLO_4 ; do
  echo "map: $file"

  gdal_translate -a_srs "+init=epsg:4326" -a_nodata 65535 \
    -a_ullr -180 90 180 -90 -co "COMPRESS=PACKBITS" \
    $file ${file}_prep.tif

  r.in.gdal in=${file}_prep.tif out=$file
done

Can anyone tell me the language the program is in? I am not familiar with it and it would be good to know more about it.

Thanks again for your help.

Andrew

Date: Tue, 18 Aug 2009 18:04:59 -0700
From: hamish_b@yahoo.com
Subject: Re: [GRASS-user] Running Scripts in GRASS
To: andrew.lewin@sympatico.ca; neteler@osgeo.org
CC: grass-user@lists.osgeo.org

Markus wrote:

Perhaps we should cook up a Wiki page describing this briefly?

shrug. or as it’s an external task, delegate the matter and add a link to some good document on the web about unix shell scripting. I’m sure there are many out there already. (but
whatever)

Andrew, another thing to realize is that these are just terminal prompt commands strung together via cut and paste into a file.

Also the wiki instructions were not written with a script in mind, so you will probably have to adjust a few filenames etc by hand.

I just updated the simple XY method example to use the new
‘r.in.gdal -l’ directly into a lat/lon location, but I think
the gdal_translate method is better.

typically shell scripts are given a shebang line which looks
like:

#!/bin/sh

this tells the OS that when you run the program it should use
the shell program (found in /bin/sh) to run it.

Hamish

Andrew wrote:

for file in A*_MO_CHLO_4 ; do
  echo "map: $file"

  gdal_translate -a_srs "+init=epsg:4326" -a_nodata 65535 \
    -a_ullr -180 90 180 -90 -co "COMPRESS=PACKBITS" \
    $file ${file}_prep.tif

  r.in.gdal in=${file}_prep.tif out=$file
done

Can anyone tell me the language the program is
in? I am not familiar with it and it would be good to
know more about it.

The language is Bourne shell script; the terminal you get from
Applications/Utilities/Terminal is mostly likely running "Bash".

(try "echo $BASH_VERSION" from the terminal prompt to check; if your
user account was created by OSX 10.2 or earlier it is likely you are
using csh instead of bash there..)

Bourne shell scripting is the native language (if you can call it that)
of the Bash terminal. (Bash actually stands for Bourne Again SHell).

It lets you do fancy things at the terminal prompt like creating a loop
to run some command on all the data files in that directory, such as
the few lines above are doing.

The * is a wildcard; $file is the variable within the loop which sets
itself to each file name, runs the commands substituting as it goes for
each iteration of the for loop.

gdal_translate is a program for manipulating GeoTiffs etc which comes
with GDAL tools, r.in.gdal is the GRASS command for importing things
like GeoTiffs. echo just prints some text to the screen.

Hamish