Opengrads Cookbook contributions Re: Fortran code to convert XYZ data to grads format ...?

Mike Bosilovich mike.bosilovich at GMAIL.COM
Fri May 8 15:09:01 EDT 2009


Hi Chuck,

I skimmed over you're code and files. It is really well organized and
well done. Have you considered contributing this to the Opengrads
cookbooks? While Arlindo points out, there is a similar entry already,
but the reality is that different people have different levels of
experience and or needs, so even similar recipes that look at a
problem from different perspectives can be very useful.

There are some other advantages to posting there. As Arlindo did, only
a link to the site is needed in the gradsusr reply. Others may be
searching for something like this, and they will find it more easily.
Also, others may improve upon it and have a reference to share from.

I've only posted a few entries on the cookbook pages. But I have heard
from several people who have taken my scripts and applied them. Since
I've had them lying around for years before that, it's nice to know
that they just aren't collecting dust between my need for them.

Just a suggestion!

Mike

On Thu, May 7, 2009 at 3:57 PM, Charles Seman <Charles.Seman at noaa.gov> wrote:
> Mald,
>
> Please find attached a Fortran 90 program, subroutines and output files
> which I hope may help...
>
> write_grads.f90 is the main program, and there is a parameter you can
> adjust for your particular computer system via data_options (currently
> set for "little_endian" which allows one to use the GrADS ctl file on
> either a "little_endian" or "big_endian" computer;  without this option
> in the GrADS ctl file, GrADS can only view the data correctly on a
> computer system with "endianness" the same as that on which the data
> were generated: e.g. http://en.wikipedia.org/wiki/Endianness ...to check
> your computer on which you intend to generate the data, you can start-up
> GrADS, and the "config" info will tell you how GrADS was configured for
> your computer)...
>
> To compile and run, if your system has "gfortran" you can compile using:
>  gfortran write_grads.f90 write_grads_ieee.f90 write_grads_ctl.f90
> then to run:
>  a.out
>
> For comparison, you can check your local results against those generated
> here using attached grads_example.ctl and grads_example.ieee files.
>
> Please let me know if you have any questions or problems with the codes.
>
> Thanks,
> Chuck
>
> Mald MM5 wrote:
>>
>> Hi users,
>>
>> Am sure someone must have a fortran code to convert XYZ data (ASCII
>> data) into grads format. Since I am not very good at fortran, is it
>> possible to share such a code?
>>
>> Anyone ?
>>
>> Thanks
>>
>
> --
>
> Please note that Charles.Seman at noaa.gov should be considered my NOAA
> email address, not cjs at gfdl.noaa.gov.
>
> ********************************************************************
> Charles Seman                                Charles.Seman at noaa.gov
> U.S. Department of Commerce / NOAA / OAR
> Geophysical Fluid Dynamics Laboratory         voice: (609) 452-6547
> 201 Forrestal Road                              fax: (609) 987-5063
> Princeton, NJ  08540-6649            http://www.gfdl.noaa.gov/~cjs/
> ********************************************************************
>
> "The contents of this message are mine personally and do not necessarily
> reflect any position of the Government or NOAA."
>
>
> program write_grads
>
> implicit none
>
> !-----------------------------------------------------------------------
> !  Diagnostic data dimensions
> !-----------------------------------------------------------------------
> !  (x,y,z,vars,t) grid dimensions
> !     nx, ny = number of (x,y) grid points, respectively
> !     nz     = number of vertical grid levels
> !     nv     = number of variables
> !     nt     = number of time-levels
> !-----------------------------------------------------------------------
> integer, parameter :: nx=10, ny=10, nz=10, nv=3, nt=3
>
> !-----------------------------------------------------------------------
> !  Diagnostic Data
> !-----------------------------------------------------------------------
> real, dimension(nx,ny,nz,nv,nt) :: dd
>
> !-----------------------------------------------------------------------
> !  Fortran unit number for writing out the GrADS data
> !-----------------------------------------------------------------------
> integer :: unit=10
>
> !-----------------------------------------------------------------------
> !  Output filename for constructing GrADS output files
> !-----------------------------------------------------------------------
> character(len=72) :: filename = 'grads_example'
>
> !-----------------------------------------------------------------------
> !  Output data characteristics
> !-----------------------------------------------------------------------
> character(len=30) :: data_options = 'little_endian'
>
> !-----------------------------------------------------------------------
> !  GrADS undef value
> !-----------------------------------------------------------------------
> real :: undef=-999.
>
> !-----------------------------------------------------------------------
> !  Grid parameters...
> !-----------------------------------------------------------------------
> !
> ! (x,y,z) grid origin values
> !
> real :: x0=0.,y0=0.,z0=0.
> !
> ! (x,y,z) grid increments
> !
> real :: dx=10.,dy=10.,dz=1.
> !
> ! time origin, increment
> !
> character(len=30) :: t0 = '00:00z1Jan2004', dt = '1mn'
>
> !-----------------------------------------------------------------------
> !  GrADS variables names and units
> !-----------------------------------------------------------------------
> character(len=15), dimension(nv) :: var, var_units
>
> !-----------------------------------------------------------------------
> !  Local variables
> !-----------------------------------------------------------------------
> integer :: i,j,k,iv,n
> character(len=80) :: ctl_file, ieee_file
>
> !-----------------------------------------------------------------------
> !  Define GrADS ieee binary data and ctl files...
> !-----------------------------------------------------------------------
>      ctl_file = TRIM(filename) // '.ctl'
>      ieee_file = TRIM(filename) // '.ieee'
>
> !-----------------------------------------------------------------------
> !  Define and write the diagnostic output into GrADS ieee binary data
> file...
> !-----------------------------------------------------------------------
>
>      do n = 1,nt
>        do iv = 1,nv
>          do k = 1,nz
>            do j = 1,ny
>            do i = 1,nx
>              dd(i,j,k,iv,n) = REAL(i+j+k+iv+n)
>            enddo
>            enddo
>          enddo
>        enddo
>      enddo
>
>      call write_grads_ieee( unit, ieee_file, nx, ny, nz, nv, nt, dd )
>
>      var = (/ 'a', &
>               'b', &
>               'c' /)
>      var_units = (/ '(m)', &
>                     '(K)', &
>                     '(s)' /)
>
>      call write_grads_ctl( unit, ctl_file, &
>                            ieee_file, &
>                            data_options, &
>                            undef, &
>                            nx, ny, nz, nt, &
>                            x0, y0, z0, t0, &
>                            dx, dy, dz, dt, &
>                            nv, var, var_units)
>
> end program write_grads
>
> subroutine write_grads_ieee( iu, ieee_file, nx, ny, nz, nv, nt, dd )
>
> implicit none
>
> !-----------------------------------------------------------------------
> !  Fortran unit number for writing out the GrADS data
> !-----------------------------------------------------------------------
> integer, intent(in) :: iu
>
> !-----------------------------------------------------------------------
> !  Output filename for constructing GrADS output file
> !-----------------------------------------------------------------------
> character(len=80), intent(in) :: ieee_file
>
> !-----------------------------------------------------------------------
> !  Diagnostic data dimensions
> !-----------------------------------------------------------------------
> !  (x,y,z,vars,t) grid dimensions
> !     nx, ny = number of (x,y) grid points, respectively
> !     nz     = number of vertical grid levels
> !     nv     = number of variables
> !     nt     = number of time-levels
> !-----------------------------------------------------------------------
> integer, intent(in) :: nx, ny, nz, nv, nt
>
> !-----------------------------------------------------------------------
> !  Diagnostic Data
> !-----------------------------------------------------------------------
> real, dimension(nx,ny,nz,nv,nt), intent(in) :: dd
>
> !-----------------------------------------------------------------------
> !  Local variables
> !-----------------------------------------------------------------------
> !
> !  IEEE array for writing out the GrADS data
> !
> real(kind=4), dimension(nx,ny) :: grads_data
> !
> !  record number for writing out the GrADS data
> !
> integer :: irec
> integer :: k,iv,n
>
> !-----------------------------------------------------------------------
> !  Write the diagnostic output into GrADS ieee binary data file...
> !-----------------------------------------------------------------------
>
>      print *
>      print *,'Define output Unit ',iu,' for filename: ',TRIM(ieee_file)
>
>      close(iu)
>      open(iu,file=TRIM(ieee_file),form="unformatted",access="direct", &
>           recl=nx*ny*4,status="unknown")
>
>      print *,'Write GrADS ieee data to Unit: ',iu
>
>      irec = 1          ! initialize
>      do n = 1,nt
>        do iv = 1,nv
>          do k = 1,nz
>            grads_data(:,:) = dd(:,:,k,iv,n)
>            write(iu,rec=irec) grads_data
>            irec = irec + 1             ! increment for next write-out record
>          enddo
>        enddo
>      enddo
>      close(iu)
>
> end subroutine write_grads_ieee
>
> !-----------------------------------------------------------------------
> !  write_grads_ctl:  generates GrADS ctl file
> !-----------------------------------------------------------------------
> subroutine write_grads_ctl(iu,ctl_file, &
>                           ieee_file, &
>                           data_options, &
>                           undef, &
>                           nx,ny,nz,nt, &
>                           x0,y0,z0,t0, &
>                           dx,dy,dz,dt, &
>                           nv,var,var_units)
> implicit none
>
> !-----------------------------------------------------------------------
> !  Fortran unit number for writing out the GrADS ctl file
> !-----------------------------------------------------------------------
> integer, intent(in) :: iu
>
> !-----------------------------------------------------------------------
> !  Output filename for constructing GrADS output file
> !-----------------------------------------------------------------------
> character(len=80), intent(in) :: ctl_file, ieee_file
>
> !-----------------------------------------------------------------------
> !  Output data characteristics
> !-----------------------------------------------------------------------
> character(len=30), intent(in) :: data_options
>
> !-----------------------------------------------------------------------
> !  GrADS undef value
> !-----------------------------------------------------------------------
> real, intent(in) :: undef
>
> !-----------------------------------------------------------------------
> !  Grid parameters...
> !-----------------------------------------------------------------------
> integer, intent(in) :: nx,ny,nz,nt      ! (x,y,z,t) dimensions
> real,    intent(in) :: x0,y0,z0         ! (x,y,z) grid origin values
> real,    intent(in) :: dx,dy,dz         ! (x,y,z) grid increments
> character(len=30), intent(in) :: t0,dt  ! time origin, increment
>
> !-----------------------------------------------------------------------
> !  Number of GrADS variables, and their names and units
> !-----------------------------------------------------------------------
> integer, intent(in) :: nv
> character(len=15), dimension(nv), intent(in) :: var, var_units
>
> !-----------------------------------------------------------------------
> !  Local variables
> !-----------------------------------------------------------------------
>
> integer :: iv
>
> !-----------------------------------------------------------------------
>
>      print *
>      print *,'Writing GrADS ctl file for GrADS dataset: '
>      print *,' ',TRIM(ieee_file)
>
>      close(iu)
>      open(iu,file=TRIM(ctl_file),status="unknown")
>      rewind(iu)
>
>      write(iu,10) TRIM(ieee_file)
>      write(iu,11) TRIM(data_options)
>      write(iu,12) undef
>      write(iu,13) nx,x0,dx
>      write(iu,14) ny,y0,dy
>      write(iu,15) nz,z0,dz
>      write(iu,16) nt,TRIM(t0),TRIM(dt)
>      write(iu,17) nv
>      do iv=1,nv
>        write(iu,18) TRIM(var(iv)),nz,TRIM(var_units(iv))
>      enddo
>      write(iu,19)
>      close(iu)
>
>      print *,'Finished writing GrADS ctl file:'
>      print *,' ',TRIM(ctl_file)
>      print *
>
> 10    format('dset ^',a)
> 11    format('options ',a)
> 12    format('undef ',e12.5)
> 13    format('xdef ',i4,' linear ',e12.5,' ',e12.5)
> 14    format('ydef ',i4,' linear ',e12.5,' ',e12.5)
> 15    format('zdef ',i4,' linear ',e12.5,' ',e12.5)
> 16    format('tdef ',i5,' linear ',a,' ',a)
> 17    format('vars ',i3)
> 18    format(a,' ',i3,' 99 ',a)
> 19    format('endvars')
>
> end subroutine write_grads_ctl
>
> dset ^grads_example.ieee
> options little_endian
> undef -0.99900E+03
> xdef   10 linear  0.00000E+00  0.10000E+02
> ydef   10 linear  0.00000E+00  0.10000E+02
> zdef   10 linear  0.00000E+00  0.10000E+01
> tdef     3 linear 00:00z1Jan2004 1mn
> vars   3
> a  10 99 (m)
> b  10 99 (K)
> c  10 99 (s)
> endvars
>
>



More information about the gradsusr mailing list