Thank you Dave & Pierce,
Sorry about that confusion, Yes it
is possible for allocating an array with 2 dimenstion as parameters
and last dimesion as variable. What I as facing was some error with
the library. I have a question regarding your modules "getnc_dims.f90
netcdf_check.f90". After seeing that they are very useful modules I
have added them to my include path ie under directory along with
netcdf.mod as below
sjo@sjo-desktop:~/hdd4/NRL_file_gen$ ll /usr/local/include/*.mod
-rw-r--r-- 1 sjo sjo 4272 2010-06-14 10:40
/usr/local/include/getnc__dims.mod
-rw-r--r-- 1 sjo sjo 562 2010-06-14 10:40
/usr/local/include/netcdf__check.mod
-rw-r--r-- 1 root root 277339 2010-06-03 18:57 /usr/local/include/netcdf.mod
-rw-r--r-- 1 root root 1510 2010-06-03 18:57 /usr/local/include/typesizes.mod
but when I complie by
ifort -extend-source -O3 -o ncep2ascii.x -I/usr/local/include
ncep2ascii.f90 getnc_dims.f90 -L/usr/local/lib/ -lnetcdf
it gives error as below
sjo@sjo-desktop:~/hdd4/NRL_file_gen$ ifort -extend-source -O3
-I/usr/local/include ncep2ascii.f90 getnc_dims.f90 -L/usr/local/lib/
-lnetcdf/tmp/ifortEEeYgg.o: In function `MAIN__':
ncep2ascii.f90:(.text+0x30c): undefined reference to `netcdf__check_mp_check_'
ncep2ascii.f90:(.text+0x55e): undefined reference to `netcdf__check_mp_check_'
ncep2ascii.f90:(.text+0x595): undefined reference to `netcdf__check_mp_check_'
ncep2ascii.f90:(.text+0x5cc): undefined reference to `netcdf__check_mp_check_'
ncep2ascii.f90:(.text+0x667): undefined reference to `netcdf__check_mp_check_'
/tmp/ifortEEeYgg.o:ncep2ascii.f90:(.text+0x6f8): more undefined
references to `netcdf__check_mp_check_' follow
but if i compile by
ifort -extend-source -O3 -o ncep2ascii.x -I/usr/local/include
ncep2ascii.f90 getnc_dims.f90 netcdf_check.f90 -L/usr/local/lib/
-lnetcdf
it works with out problem
So are these modules not supposed to be used similar way as netcdf.mod
? which can be used in any code by including its path during
compilation? or what change I need to make them to use them like
netcdf.mod?
I have attached here the code where I am trying to use these modules.
please advice.
with best regards,
Sudheer
On Fri, Jun 11, 2010 at 10:06 AM, Sudheer Joseph <sjo@xxxxxxxxxxxxx> wrote:
> Thank you Dave,
> The subroutine you mentioned is very
> useful. But my question was slightly different. suppose if you get the
> dimension size before reading the 3 dimensional variable. we cannot
> use that information to read the netcdf 3rd dimension at a stretch.
> I mean
> if I use the
>
> NTIM= nf_get_dim(time) ( just for illustration sake )
> and use this for allocating the dimension for 3D variable it will not
> accept as it is not defined as a parameter.
>
> allocate(pres_in(NLONS, NLATS, NTIM))
> count = (/ NLONS, NLATS,NTIM/)
> start = (/ 1, 1, 1/)
>
> One possibility came to my mind yesterday night was to read the data
> as xy slabs and use a do loop to read the time dimension. It appears
> there is no way to read the 3D variable at one call if we are not
> defining the dimension as parameter.
> with best regards,
> Sudheer
>
> On Fri, Jun 11, 2010 at 3:42 AM, Dave Allured <dave.allured@xxxxxxxx> wrote:
>> Sudheer,
>>
>> If the names of the dimensions are always the same, then use
>> nf90_inq_dimid followed by nf90_inquire_dimension to get the length
>> of each dimension. See the example on this page:
>>
>> http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90/NF90_005fINQUIRE_005fDIMENSION.html
>>
>> If all the dimensions have coordinate variables, then the following
>> subroutine will get several dimension lengths in one call:
>>
>> http://www.esrl.noaa.gov/psd/people/dave.allured/data/f90/lib/getnc_dims.f90
>>
>> If the dimension names change between Netcdf files, then the problem
>> is more complicated. We can address that if neither of the above
>> works for you.
>>
>> --Dave A.
>> NOAA/PSD/CIRES
>>
>> On 6/10/2010 7:55 AM, Sudheer Joseph wrote:
>>> Dear Users,
>>> I have been coding a program to read multiple
>>> netcdf files having different time dimensions. I want to know if there
>>> is a way to read netcdf varibale with out defining the dimension as
>>> parameter. with in fortran 90 program.
>>> normally we need to define like below
>>> ! timesteps of data.
>>> integer, parameter :: NDIMS = 3, NRECS =1
>>> integer, parameter :: NTIM=1464, NLATS = 94, NLONS = 192
>>> allocate(pres_in(NLONS, NLATS, NTIM))count = (/ NLONS, NLATS,NTIM/)
>>> start = (/ 1, 1, 1/)
>>>
>>> ! Read the surface pressure and temperature data from the file, one
>>> ! record at a time.
>>> call check( nf90_get_var(ncid, pres_varid, pres_in, start =
>>> start,count = count) )
>>>
>>> In my case ntim may be different for different files. I wanted to use
>>> the reading part as a subroutine where ntim is provided as a variable
>>> and passed to subroutine
>>> but if I declare ntim as varible then netcdf gives an error.
>>> If any one have a solution please help. I am not looking for solutions
>>> like replacing the parameter statement using shell script and re
>>> compiling it etc.
>>>
>>
program ncep2ascii
implicit none
INTEGER ntim, nlat, nlon,tlen,sln
CHARACTER*80 outfile,vname,infile
infile='air.2m.gauss.2004.nc'
vname="air"
call ncep_var_read(infile,vname)
contains
subroutine ncep_var_read(infile,vname)
use netcdf
use getnc__dims
use netcdf__check
implicit none
! This is the name of the data file we will read.
character (len = 80) :: FILE_NAME
character (len = 80) :: outfile,vname,infile
integer :: ncid,l,n,m,ss,tlen,nx,ny,nt,nl,ntim
! timesteps of data.
integer, parameter :: NDIMS=3
integer, parameter :: nlat = 94, nlon = 192
character (len = *), parameter :: LAT_NAME = "lat"
character (len = *), parameter :: LON_NAME = "lon"
character (len = *), parameter ::TIME_NAME = "time"
! The start and count arrays will tell the netCDF library where to
! read our data.
integer :: start(NDIMS), count(NDIMS),start_1(1),count_1(1)
! In addition to the latitude and longitude dimensions, we will also
! create latitude and longitude variables which will hold the actual
! latitudes and longitudes. Since they hold data about the
! coordinate system, the netCDF term for these is: "coordinate
! variables."
real :: lat(nlat), lon(nlon)
integer :: lon_varid, lat_varid,time_varid
real :: field,add_offset,scale_factor
! We will read surface temperature and sure fields. In netCDF
! terminology these are called "variables."
character (len = 80) :: var_NAME
integer :: var_varid
! Program variables to hold the data we will read in. We will only
! need enough space to hold one timestep of data; one record.
! Allocate memory for data.
real, dimension(:,:,:), allocatable :: var_in,uflx
real, dimension(:), allocatable :: time
! Use these to calculate the values we expect to find.
! Loop indices
! Allocate memory
FILE_NAME=infile
var_NAME='air'
call get_ofl(file_name,outfile)
call check( nf90_open(FILE_NAME, nf90_nowrite, ncid) )
call getnc_dims(ncid, nx, ny, nt, nl)
ntim=nt
allocate(var_in(NLON,NLAT,ntim))
allocate(time(ntim))
! Open the file.
! Get the varids of the latitude and longitude coordinate variables.
call check( nf90_inq_varid(ncid, LAT_NAME, lat_varid) )
call check( nf90_inq_varid(ncid, LON_NAME, lon_varid) )
call check( nf90_inq_varid(ncid, TIME_NAME, time_varid) )
! Read the latitude and longitude data.
call check( nf90_get_var(ncid, lat_varid, lat) )
call check( nf90_get_var(ncid, lon_varid, lon) )
call check( nf90_get_var(ncid, time_varid, time) )
! Check to make sure we got what we expected.
! Get the varids of the sure and temperature netCDF variables.
call check( nf90_inq_varid(ncid, var_NAME,var_varid) )
! Read 1 record of NLONS*NLATS*NLVLS values, starting at the beginning
! of the record (the (1, 1, 1, rec) element in the netCDF file).
count = (/ nlon, nlat,ntim/)
start = (/1, 1,1/)
call check( nf90_get_var(ncid, var_varid, var_in, start = start,count =
count) )
! Read the surface sure and temperature data from the file, one
! timestep at a time.
open(unit=21,file=outfile,status = 'new')
call check(NF90_GET_ATT(ncid,var_varid,'scale_factor',scale_factor))
call check(NF90_GET_ATT(ncid,var_varid,'add_offset',add_offset))
do l=1,ntim
do m=1,nlat
do n=nlon,1,-1
field=var_in(n,m,l)*scale_factor+add_offset
write(21,115) field,lat(m),lon(n),time(l)
enddo
enddo
! write(*,*) "south_west",var_in(1,1,l)*scale_factor+add_offset
! write(*,*) "south_east",var_in(nlon,1,l)*scale_factor+add_offset
! write(*,*) "north_east",var_in(nlon,nlat,l)*scale_factor+add_offset
! write(*,*) "north_west",var_in(1,nlat,l)*scale_factor+add_offset
write(*,*)"Writing...", l,"^th record"
enddo
115 format (E13.6,2F12.3,F14.2)
call check( nf90_close(ncid) )
! If we got this far, everything worked as expected. Yipee!
write(*,*) "scale_factor add_offset"
write(*,*) scale_factor,add_offset
print *,"*** SUCCESS fully writen ",outfile, "!"
end subroutine ncep_var_read
subroutine get_ofl(infile,outfile)
integer sln,tlen
character*150 cmd
character*80 infile,outfile
logical back
sln=index(infile,'.nc', back)
outfile=infile(1:sln)//"xya"
return
end subroutine get_ofl
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end program ncep2ascii