Recipe: Fortran program as OPeNDAP client: Difference between revisions
Line 119: | Line 119: | ||
The code to read data from the OPeNDAP server is a minor modification (3 lines) of the above program and is reproduced below | The code to read data from the OPeNDAP server is a minor modification (3 lines) of the above program and is reproduced below | ||
<syntaxhighlight lang="fortran" line> | |||
program g5nr_reader_dap | |||
use netcdf ! for reading the NR files | |||
implicit none | |||
! File name | |||
! --------- | |||
character(len=256) :: T_file | |||
! Global, 4D array: (lon,lat,lev,time) | |||
! ------------------------------------ | |||
real, allocatable :: T(:,:,:,:) | |||
! Miscellaneous | |||
! ------------- | |||
integer :: ierr | |||
integer :: im, jm, lm | |||
integer :: ncid, varid | |||
integer :: start(4), count(4) | |||
! file name and dimensions | |||
! ------------------------ | |||
im = 720 | |||
jm = 361 | |||
lm = 72 | |||
T_file = "http://opendap.nccs.nasa.gov:9090/dods/OSSE/GEOS-5.12/BETA9/0.5000_deg/inst/inst01hr_3d_T_Cv" | |||
! Allocate the Global 4-D array with only 1 time level | |||
! ---------------------------------------------------- | |||
allocate(T(im,jm,lm,1)) | |||
! Hypercube for reading one 1 array | |||
! --------------------------------- | |||
start = (/ 1, 1, 1, 11772 /) ! time level 11748 corresponds to 2006/09/18, 9z | |||
count = (/ im, jm, lm, 1 /) ! 1 time level, 3D (lon,lat,lev) array | |||
! Read the data file | |||
! ------------------ | |||
write(*,*)'Reading T' | |||
call check(nf90_open(T_file,NF90_NOWRITE,ncid), "opening T file") | |||
call check(nf90_inq_varid(ncid,"t",varid), "getting T varid") | |||
call check(nf90_get_var(ncid,varid,T,start=start,count=count), "reading T") | |||
call check(nf90_close(ncid), "closing T file") | |||
! Print min/max of arrays | |||
! ----------------------- | |||
write(*,*)'T: ', maxval(T),minval(T) | |||
deallocate(T) | |||
! All done | |||
! -------- | |||
contains | |||
subroutine check(status, loc) | |||
integer, intent(in) :: status | |||
character(len=*), intent(in) :: loc | |||
if(status /= NF90_NOERR) then | |||
write (*,*) "Error at ", loc | |||
write (*,*) NF90_STRERROR(status) | |||
end if | |||
end subroutine check | |||
end program g5nr_reader_dap | |||
</syntaxhighlight> | |||
==== Compile and run ==== | ==== Compile and run ==== |