Gostaria de ajudar?

quarta-feira, 30 de outubro de 2013

Salvando arquivo texto no GrADS

A pedidos de Camila Carpenedo, segue uma dica de como salvar uma ou mais variável em um arquivo texto (.txt, ascii e etc) no GrADS.

A solução para isso é utilizar o comando 'set gxout print' e 'set prnopts ' format ' ' numl ' ' numb ' ' u ' '. No entanto, para que não tenhamos grandes dificuldades irei disponibilizar para vocês o script/função desenvolvida no OpenGrADS Cookbooks

A função é a seguinte:

function fprintf(args)
* Command line argumets
* ---------------------
  expr = subwrd(args,1)
  file = subwrd(args,2)
  format = subwrd(args,3)
  numl = subwrd(args,4)
  numb = subwrd(args,5)
  u = subwrd(args,6)
  if ( file='' )
    say ''
    say 'NAME'
    say '     fprintf - print GrADS variables to a text file'
    say ''
    say 'SYNOPSIS'
    say '     fprintf  expr  txtFile  [format numl numb [u]]'
    say ''
    say 'DESCRIPTION'
    say '     Evaluates the contents of the GrADS expression *expr* writing'
    say '     its values to a formatted text file *txtFile*. On output, the'
    say '     number of values and the undef values are returned; a negative'
    say '     number of values signals an error condition.'
    say ''
    say '     Unlike the output of *set gxout print*, the resulting ASCII'
    say '     file has only the data values and *no header*.'
    say ''
    say 'OPTIONS'
    say '     The optional parameters are the same as the ones required by'
    say '     the GrADS command *set prnopts*, namely'
    say '     format   a C language template for formatting ASCII output.'
    say '              Default is %g.'
    say '     numl     number of values to print per record. Default is 8.'
    say '     numb     number of blanks to insert between values. Default is 1.'
    say '     u        print "Undef" instead of the numerical value for'
    say '              missing data.'
    say 'BUGS'
    say '     The GrADS expression cannot have spaces in it.'
    say ''    
    say 'COPYRIGHT'
    say '     This script has been placed in the public domain'
    say ''
    return
  endif
* Set the display environment and produce buffered ASCII output
* -------------------------------------------------------------
  'set gxout print'
  if ( format != '' )
    'set prnopts ' format ' ' numl ' ' numb ' ' u ' '
  endif
  'display ' expr
   if ( rc!=0 ); return -1; endif
*  Get rid of header line: Printing Grid -- 3358 Values -- Undef = 1e+20
*  but record number of values and undef values for later reference
*  ---------------------------------------------------------------------
   buffer = result
   i = 1; line = sublin(buffer,i)
   n = subwrd(line,4)
   undef = subwrd(line,9)
* Now write the data values to text file: first line...
* -----------------------------------------------------
  i = 2; line = sublin(buffer,i)
  if ( write_(file,line) > 0 ); return -2; endif
* Append subsequent lines
* -----------------------
  i = i + 1; line = sublin(buffer,i)
  while ( line != '' )
    if ( write_(file,line,append) != 0 ); return -3; endif 
    i = i + 1; line = sublin(buffer,i)
  endwhile
  if ( close(file) != 0 ); return -4; endif
* All done
* --------
  say 'wrote ' n ' values to file "' file '"'
  return n ' ' undef ' ' 
function write_(file,line)
   rc = write(file,line)
   return subwrd(rc,1)

Para utilizar a função é simples:

Você deve criar um arquivo .gs contendo a função descrita acima chamada de fprintf.gs. Você pode salvar a função no diretório script do GrADS e desta forma você poderá "chamar" a função da mesma forma que você chamas as funções de barra (cbar, cbarn e etc) somente pelo nome.

Após criar a variável que pode ser uma média, anomalia, serie temporal e etc, basta executar o seguinte comando:

ga -> fprintf variável arquivo.txt

No Cookbooks existe umas outras funcionalidade interessantes que vocês podem aproveitar.

Boa Sorte e Bom Trabalho a Todos!!

Nenhum comentário :

Postar um comentário