file-iograds

How to Read/Store many files using GrADS?


I would like to know if anyone can help me to find the issue about this code in GrADS language.

I'd like to read many files and compute and store them, but when I perform that in GraDS, I have the same answer for all imput file.

Could anyone help me in this problem, please?

'reinit'

****Modify here******
var = v10

ano = 1980

while(ano <= 2013)

    *Desire point
    local = IAG
    **lat = -22.3
    **lon = 314.62

    latInitialP = -34.58
    lonInitialP = 305.43
    *******************************************************
    'sdfopen C:\Pesquisa_Michelle\dados_amandenha\V10m_'ano'.nc' 

    *Storing out
    'set gxout print'
    'set prnopts %10.2f 1 1'

    in = 1

    test = 1460
    if ( math_fmod(ano,4) = 0 )
      test = 1464
    endif

    while(in <= test)
        'set t 'in
        in2 = in + 3

        say in
        say in2 

        'define m = ave('var',t='in',t='in2')'
        'set lat 'latInitialP''
        'set lon 'lonInitialP''
        'd m'

        valor = sublin(result,2)
        lixo = write('C:\Pesquisa_Michelle\dados_amandenha\'var'_'ano'.txt',valor)
        in = in + 4
    endwhile
    lixo = close(''var'_'ano'.txt')
    ano = ano + 1
endwhile

update

I don't get an error (message) about that. That works fine, the problem is the result files. All result files have the first result, like a file replication. When I perform that not using a loop, but changing each value for "ano" by myself, I got different result.

The problem is, in my point of view, the command:

'sdfopen C:\Pesquisa_Michelle\dados_amandenha\V10m_'ano'.nc'

This command aren't replace the files for each "ano".


Solution

  • The problem is that you are never closing your input datafile or resetting grads during your main loop. Consider this example where I open a file:

    % grads -l
    ga-> open data/semicircle-qv14_s.ctl 
    Scanning description file:  data/semicircle-qv14_s.ctl
    Data file data/semicircle-qv14_00%y4_s.dat is open as file 1
    LON set to -79.5 79.5 
    LAT set to -79.5 79.5 
    LEV set to 0.05 0.05 
    Time values set: 0:7:3:0 0:7:3:0 
    E set to 1 1 
    

    Notice that it says open as file 1. Now I'll narrow down my selection to a single grid point and display the pressure:

    ga-> set lat 0
    LAT set to 0.125 0.125 
    ga-> set lon 0
    LON set to 0.125 0.125 
    ga-> set lev 5
    LEV set to 4.92368 4.92368 
    ga-> d prs
    Result value = 55198.4 
    

    The pressure is 551 hPa. Looks good. Now lets open another data file without closing this one:

    ga-> open data-fine/semicircle-qv14_s.ctl 
    Scanning description file:  data-fine/semicircle-qv14_s.ctl
    Data file data-fine/semicircle-qv14_00%y4_s.dat is open as file 2
    

    Notice this says open as file 2. Lets display pressure from this file:

    ga-> d prs                                
    Result value = 55198.4 
    

    Wait, this is the same value? Yes, because we are still displaying from file 1, here prs is the same as writing prs.1. If we want pressure from the second file, we need to use prs.2:

    ga-> d prs.2
    Result value = -9.99e+08 
    

    Different result (and this on is missing because there is no data at this time step in the second file.

    There are two ways to fix this.

    1. Where you are incrementing ano in your loop, add the command close 1. This will close the input data file so when the next one is loaded the display commands will properly display its content.

    2. At the top of your loop, before sdfopen add reinit, which will reset lots of things, including closing all open files. You set your output up in each loop iteration so this shouldn't be a problem for you.