[gradsusr] Fortran to Read in GrADS Binary File
Charles Seman - NOAA Federal
charles.seman at noaa.gov
Tue Jul 5 11:44:04 EDT 2016
Lydia,
Try this:
do t=1,days
read(17,REC=irec) tmax
read(17,REC=irec+1) tmin
read(17,REC=irec+2) prcp
read(17,REC=irec+3) srad
write(18,REC=irec) tmin
write(18,REC=irec+1) tmin
write(18,REC=irec+2) prcp
write(18,REC=irec+3) srad
irec=irec+4
enddo
Here is some code from a Fortran 90 subroutine I use that works OK:
!
! 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
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)
If there still is a problem, sometimes the record length is not "4".
From a stackoverflow post, try using the "inquire" function:
http://stackoverflow.com/questions/9013616/fortran-i-o-specifying-large-record-sizes
Hope this helps,
Chuck
On 07/05/2016 09:57 AM, Lydia Rill wrote:
> Hi all,
>
> I am a new grads user and I have never worked with binary files before.
> I have a large binary file I created in grads (code shown below). I want
> to split up the binary file by latitude and longitude degree into
> smaller binary files. While this is possible in GrADS, it is too slow
> for my needs, so I am trying to do it in Fortran 90.
>
> However, I am having problems reading the GrADS binary file in Fortran.
> It gives me the missing value for each of my variables at every time
> step and every location.
>
> The binary file contains NLDAS 2 forcing data, with 464 x steps, 224 y
> steps, and 13688 daily time steps for 4 variables (only 1 Z level). It
> is a large file at 22GB, and I can successfully display the variables in
> GrADS from this binary file. This binary file was created from data in
> Grib files.
>
> I have tried many various ways of reading in the binary data, using x
> and y loops, or time loops, or looping through the 4 variables, but
> nothing has been successful.
>
> I am working on a Mac (El Capitan).
>
> Here is the GrADS script I used to create the binary file:
>
> functionmain(args)
>
> counthr=subwrd(args,1)
>
> 'reinit'
>
> 'open /Volumes/WebData/NLDAS2/ProcessingFiles/24hours.ctl'
>
> 'set undef 9.999e20'
>
> 'set gxout fwrite'
>
> 'set fwrite -ap /Volumes/WebData/NLDAS2/DailyNLDAS/daily.bin'
>
>
> 'set t 1'
>
> t1=1+4
>
> t1p=1+5
>
> while(t1<=counthr+5-23)
>
> t2=t1+23
>
> t2p=t1p+23
>
> 'd (max(TMP2m,t='t1',t='t2')-273.15)'
>
> 'd (min(TMP2m,t='t1',t='t2')-273.15)'
>
> 'd sum(APCPsfc,t='t1p',t='t2p')'
>
> 'd (ave(DSWRFsfc,t='t1',t='t2')*24*3600/1000000)'
>
> t1=t1+24
>
> t1p=t1p+24
>
> endwhile
>
> 'quit'
>
>
> Here is the Fortran script to read the binary file
>
> ProgramReadbin
>
> implicitnone
>
> integer irec,i,j,t,days
>
> real tmin(464,224),tmax(464,224),prcp(464,224),srad(464,224)
>
> OPEN(17,file='/Volumes/WebData/NLDAS2/DailyNLDAS/daily.bin',&
>
> &STATUS='Old',FORM='UNFORMATTED',ACCESS='DIRECT',RECL=464*224*4)
>
>
> OPEN(18,file='/Volumes/WebData/NLDAS2/DailyNLDAS/testdaily.bin',&
>
> &STATUS='UNKNOWN',FORM='UNFORMATTED',ACCESS='DIRECT',RECL=464*224*4)
>
> days=10
>
> irec=1
>
> do t=1,days
>
> read(17,REC=irec) ((tmax(i,j),i=1,8),j=1,8)
>
> read(17,REC=irec+1) ((tmin(i,j),i=1,8),j=1,8)
>
> read(17,REC=irec+2) ((prcp(i,j),i=1,8),j=1,8)
>
> read(17,REC=irec+3) ((srad(i,j),i=1,8),j=1,8)
>
> write(18,REC=irec) ((tmin(i,j),i=1,8),j=1,8)
>
> write(18,REC=irec+1) ((tmin(i,j),i=1,8),j=1,8)
>
> write(18,REC=irec+2) ((prcp(i,j),i=1,8),j=1,8)
>
> write(18,REC=irec+3) ((srad(i,j),i=1,8),j=1,8)
>
> irec=irec+4
>
> enddo
>
> end program
>
>
> The corresponding control file is:
>
> set ^testdaily.bin
>
> undef 9.999E+20
>
> title Daily NLDAS2 Data for00Z through 23Z
>
> xdef 8linear -124.93750.125
>
> ydef 8linear 25.06250.125
>
> tdef 10linear 00Z02Jan1979 1dy
>
> zdef 1linear 11
>
> vars 4
>
> TMAX 012,105,2**maximum temperature at 2m above surface [C]
>
> TMIN 011,105,2**minimum temperature at 2m above surface [C]
>
> PRCP 061,1,0**total precipitation backward accumulated [kg/m^2ormm]
>
> SRAD 0204,1,0**total surface downward shortwave radiation flux [mJ/m^2]
>
> ENDVARS
>
>
>
> Thanks,
>
> Lydia
>
> --
> Lydia D. Rill
> M.S., Michigan State University 2016
> B.S., Valparaiso University 2014
> (224) 406-5130
> Lydia.D.Rill at gmail.com <mailto:Lydia.D.Rill at gmail.com>
>
>
> _______________________________________________
> gradsusr mailing list
> gradsusr at gradsusr.org
> http://gradsusr.org/mailman/listinfo/gradsusr
>
--
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 reflect any
official or unofficial position of the United States Federal Government,
the United States Department of Commerce, or NOAA."
More information about the gradsusr
mailing list