[gradsusr] Utilizing data from text file to make calculation in GrADS

Phil Klotzbach philk at atmos.colostate.edu
Thu Feb 21 15:43:20 EST 2013


OK, I've done some more playing around with the code, and it now works.  
I used subwrd instead of substr, and had it read the 2nd, 3rd and 4th 
words from the string (to leave out the return code).  If anyone is 
interested, here is the working code.  Thanks so much for your help!

Phil

input = 'c:\test.txt'
'open c:\500mbtemp.ctl'
looptime = 1
while (looptime < 3)
rec = read(input)
pointlon = subwrd(rec, 2)
pointlat = subwrd(rec, 3)
pointtime = subwrd(rec, 4)
'set lat 'pointlat
'set lon 'pointlon
'set t 'pointtime
'd temp'
answer=result
res=write("c:\500mbtemp.txt",answer)
looptime=looptime+1
endwhile
'close 1'

----------------------------------------------------
Phil Klotzbach, Ph.D.
Research Scientist
Department of Atmospheric Science
Colorado State University
Email: philk at atmos.colostate.edu
----------------------------------------------------

On 2/22/2013 7:20 AM, Phil Klotzbach wrote:
> Chuck and Ricardo,
>
> Thanks for the rapid responses.  I used the "say" command in GrADS to
> confirm that pointlon, pointlat and pointtime variables are all reading
> what I want them to, so I don't think the substr vs. sublin function is
> an issue.  Thanks for the suggestion about clearing up the extra single
> quotes.  Unfortunately, modifying the code did not change the result.
> It seems like GrADS is still treating those variables as if they are
> text strings instead of numbers.
>
> Phil
>
> ----------------------------------------------------
> Phil Klotzbach, Ph.D.
> Research Scientist
> Department of Atmospheric Science
> Colorado State University
> Email: philk at atmos.colostate.edu
> ----------------------------------------------------
>
> On 2/22/2013 6:49 AM, Charles Seman - NOAA Federal wrote:
>> Phil,
>>
>>    From http://grads.iges.org/grads/gadoc/script.html#intrinsic
>> ---
>> read (filename)
>> This functions reads individual records from file filename. Repeated
>> calls must be made to read consecutive records. The result is a string
>> containing two lines: the first line is the return code, the 2nd line is
>> the record read from the file. The record may be a maximum of 80
>> characters. Use the sublin function to separate the result. Return codes
>> are:
>>
>>        0 - ok
>>        1 - open error
>>        2 - end of file
>>        8 - file open for write
>>        9 - I/O error
>>
>> Files are opened when the first call to read is made for a particular
>> file name. Files are closed when the execution of the script file
>> terminates (note that files remain open between function calls, etc).
>> ---
>>
>> Try something like this in your script:
>> ...
>> res = read(input)
>> rc = sublin(res,1)
>> if ( rc != 0 )
>>      say 'return code is: 'rc
>>      exit
>> endif
>> rec = sublin(res,2)
>> ...
>>
>> also, check on "looptime" vs "time" for loop counter...
>>
>> Hope this helps,
>> Chuck
>>
>>
>> On 02/21/2013 01:03 PM, Phil Klotzbach wrote:
>>> Hi everyone,
>>>
>>> I am currently running GrADS 2.0.1 on my Windows 7 laptop.  What I have
>>> is a simple text file with three columns, a longitude, a latitude, and a
>>> time (since 1979 at 0Z).  When I attempt to run the code in GrADS, I get
>>> the following error message:
>>>
>>> Set error: Missing or invalid arguments for LON option
>>> Non-numeric args to numeric operations
>>> Error occurred on line 18
>>> In file /cygdrive/c/OPENGR~1/Contents/Resources/Scripts/readfile.gs
>>>
>>> The strange thing is, I can check and see that latitude has been set
>>> correctly by GrADS.  It just has problems with longitude and time. I
>>> used the three say commands to verify that it has read in the variables
>>> correctly (which it has).
>>>
>>> The following is my code as currently written:
>>>
>>> input = 'c:\test.txt'
>>> 'open c:\500mbtemp.ctl'
>>> looptime = 1
>>> while (looptime < 3)
>>> rec = read(input)
>>> pointlon = substr(rec, 2, 7)
>>> pointlat = substr(rec, 10, 7)
>>> pointtime = substr(rec, 17, 2)
>>> say pointlon
>>> say pointlat
>>> say pointtime
>>> 'set lat 'pointlat''
>>> 'set lon 'pointlon''
>>> 'set t 'pointtime''
>>> 'd temp'
>>> answer=result
>>> res=write("c:\500mbtemp.txt",answer)
>>> time=time+1
>>> endwhile
>>>
>>> Here are the contents of c:\test.txt
>>>
>>> 150.00 -16.75 5
>>> 149.25 -22.50 7
>>>
>>> Lastly c:\500mbtemp.ctl is an ERA-Interim 500 mb temperature file. I
>>> have verified manually that the file opened correctly and is working
>>> properly.   I know the GrADS treats all variables read from text files
>>> as strings, and I'm guessing that this is what is causing the problem
>>> here.  Any insights into how this problem can be solved are much
>>> appreciated!  Thanks!
>>>
>>> Phil
>>>
> _______________________________________________
> gradsusr mailing list
> gradsusr at gradsusr.org
> http://gradsusr.org/mailman/listinfo/gradsusr




More information about the gradsusr mailing list