Recipe: Fortran program as OPeNDAP client: Difference between revisions
Line 19: | Line 19: | ||
==== Code ==== | ==== Code ==== | ||
<syntaxhighlight lang="fortran" line> | |||
program g5nr_reader_dap | |||
use netcdf ! for reading the NR files | |||
implicit none | |||
! File name | |||
! --------- | |||
character(len=256) :: T_file | |||
! 4D array: (lon,lat,lev,time) | |||
! ------------------------------------ | |||
real, allocatable :: T(:,:,:,:) ! subset of global data | |||
! Miscellaneous | |||
! ------------- | |||
integer :: ierr | |||
integer :: im, jm, lm | |||
integer :: ncid, varid | |||
integer :: start(4), count(4) | |||
integer :: imin, imax, jmin, jmax | |||
real :: minlat, minlon, maxlat, maxlon | |||
! file name and (subset) dimensions | |||
! bounding box: | |||
! lons = -180:0.5:179.5 | |||
! lats = -90:0.5:90 | |||
! --------------------------------- | |||
T_file = "http://opendap.nccs.nasa.gov:9090/dods/OSSE/G5NR/Ganymed/7km/0.5000_deg/inst/inst01hr_3d_T_Cv" | |||
minlon = -130. | |||
maxlon = -65. | |||
minlat = 25. | |||
maxlat = 50. | |||
! compute array indices from the | |||
! bounding lat/lon values | |||
imin = nint((minlon + 180.)/0.5) | |||
imax = nint((maxlon + 180.)/0.5) | |||
jmin = nint((minlat + 90.)/0.5) | |||
jmax = nint((maxlat + 90.)/0.5) | |||
! compute array sizes | |||
im = imax-imin+1 | |||
jm = jmax-jmin+1 | |||
lm = 72 | |||
! Open file and get var id | |||
! ------------------------ | |||
call check(nf90_open(T_file,NF90_NOWRITE,ncid), "opening T file") | |||
call check(nf90_inq_varid(ncid,"t",varid), "getting T varid") | |||
! Read global temperature data | |||
! ---------------------------- | |||
allocate(T(im,jm,lm,1)) ! global 4D array with 1 time level | |||
start = [imin, jmin, 1, 11772] ! time level 11772 corresponds to 2006/09/18, 9z | |||
count = [im, jm, lm, 1] ! 1 time level, 3D (lon,lat,lev) array | |||
write(*,*) 'Reading T...' | |||
call check(nf90_get_var(ncid,varid,T,start=start,count=count), "reading T") | |||
write(*,*)'T: ', maxval(T),minval(T) | |||
! close file, release memory | |||
call check(nf90_close(ncid), "closing T file") | |||
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> | </syntaxhighlight> | ||