Thursday, May 16, 2013

Some tips on running GrADS from the Windows Command Line and .bat scripts

If you are using GrADS through a linux operating system, chances are you are familiar with running GrADS from the command line, so this post is mostly aimed at the Windows users.  This post will go through the basic instructions for running GrADS from your command line along with arguments and options.  So lets first begin with a question.

Why would you want to run GrADS from the command line if your mouse is perfectly capable of opening GrADS via the "double click?"

The best answer that I can give you is that running GrADS from the command line vs. double clicking gives you the option to include arguments.  Furthermore, the command syntax used is the same used in .bat scripts.

Now, there are a number of different options available that you can include on the command line when you open the program.  The most important is the -help option.  So if you type:

    'grads -help'

Into the command line, you will get a whole list of options you can include when you run the program.

 The options I have found to have been the most useful are these:

  • -l/p: Choosing to open GrADS in landscape or portrait mode
  • -b: running GrADS as a batch process (program is hidden from view)
  • -c: Allows you to execute a command (likely a script)
  • -x: Exits GrADS automatically after executing your command


There are other options that you can see with the -help command, but these are the ones I have found most useful.

Example: grads -lcx 'run script.gs'

This example would open GrADS in landscape mode and run the file "script.gs" and then exit after it was done.

Now, you can actually carry this command line method over to a Windows batch (.bat) script.  Basically, a batch script is similar to a GrADS script, in that it is a file that can execute a bunch of Windows commands at once.  Now, you can include a command to run GrADS with options within your batch script.

Now, this can be very useful as this way you can actually pass arguments from your windows script into your GrADS script.  For example, if you wanted to set your lat/lon boundaries in your windows script, or your display color, you can do that.  All you need to do, is set these variables inside your batch script, and call GrADS with the arguments attached.

Batch Script:
      set display="white"
      set minlat="20"
      set maxlat="50"
      set minlon="-125"
      set maxlon="-65"

     grads -lc 'run script.gs '%display%' '%minlat%' '%maxlat%' '%minlon%' '%maxlon%

GrADS Script:     function script(args)
     display=subwrd(args,1)
     minlat=subwrd(args,2)
     maxlat=subwrd(args,3)
     minlon=subwrd(args,4)
     maxlon=subwrd(args,5)

Now, what you see in the GrADS script, that may be new to you, is the function declaration at the top.  This is required if you want your script to read in the arguments that you laid out in the batch script.  All this line does, is essentially tell GrADS that you are looking for arguments.  You also see in the batch script you have spaces between each argument denoted by the ' '.  This allows you to separate your arguments in GrADS using the subwrd() function.

But the ability to pass arguments from the windows script into the GrADS script can be very helpful, especially if you are running scripts that need variable input (e.g., date or time).  As this method could allow you to run GrADS without having to constantly change the script around.

In any case, I just wanted to provide a few tips on running GrADS from the command line in Windows.  I didn't go too in depth into windows scripting (I leave that to brighter minds), but hopefully you have an idea as to how to run GrADS in a batch script as well as how to pass arguments from one to the other.



9 comments:

  1. How the can I run a narropen script given that my data is A.nc and is in this path c:/Grads/Hana/?

    ReplyDelete
    Replies
    1. grads -lc 'run narropen.gs'

      then in narropen.gs

      'sdfopen c:/Grads/Hana/A.nc'

      that should open up your file, once you've done that you are on your own

      Delete
  2. I have downloaded some data but have no idea how to open it in grads. the file name is gpcp_1dd_v1.2_p1d.199611 which i unzipped with 7-zip and saved in a folder inside my execution/setup folder

    ReplyDelete
  3. i am always getting the error response 'gradsdf: Couldnt ingest SDF metadata'

    ReplyDelete
  4. i am always getting the error response 'gradsdf: Couldnt ingest SDF metadata'

    ReplyDelete
  5. I have downloaded some data but have no idea how to open it in grads. the file name is gpcp_1dd_v1.2_p1d.199611 which i unzipped with 7-zip and saved in a folder inside my execution/setup folder

    ReplyDelete
  6. how to run script in open grad recently i start working on open grad i have to run my own script because i want to find out the precipitation rate but everytime i face errors and my script is not working properly kindly help me or suggest me any portal so i will be able to learn about it.reply soon.

    ReplyDelete
  7. I'd be happy to help if I can, but i would need to know what your errors are, and look at your script.

    ReplyDelete