- 积分
 - 4193
 
	- 贡献
 -  
 
	- 精华
 
	- 在线时间
 -  小时
 
	- 注册时间
 - 2013-1-16
 
	- 最后登录
 - 1970-1-1
 
 
 
 
 
 
 | 
	
 
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册 
 
 
 
x
 
 本帖最后由 infocert 于 2013-3-2 14:41 编辑  
 
有个nc文件,头格式如下:- netcdf VEGET {
 
 - dimensions:
 
 -         lon = 64 ;
 
 -         lat = 32 ;
 
 -         time = UNLIMITED ; // (1992 currently)
 
 - variables:
 
 -         float lon(lon) ;
 
 -                 lon:long_name = "longitude coordinate" ;
 
 -                 lon:standard_name = "longitude" ;
 
 -                 lon:units = "degrees_east" ;
 
 -                 lon:axis = "X" ;
 
 -         float lat(lat) ;
 
 -                 lat:long_name = "latitude coordinate" ;
 
 -                 lat:standard_name = "latitude" ;
 
 -                 lat:units = "degrees_north" ;
 
 -                 lat:axis = "Y" ;
 
 -         float time(time) ;
 
 -                 time:long_name = "time" ;
 
 -                 time:standard_name = "time" ;
 
 -                 time:units = "years since 0001-01-01 00:00:00" ;
 
 -                 time:time_origin = "01-JAN-0001 00:00:00" ;
 
 -         float vegfrac(time, lat, lon) ;
 
 -                 vegfrac:long_name = "fraction de culture par maille" ;
 
 -                 vegfrac:standard_name = "fraction_culture" ;
 
 -                 vegfrac:units = "%" ;
 
 -                 vegfrac:missing_value = -99.99f ;
 
 - }
 
 
  复制代码 
我读取这个nc所用的fortran如下[已经修改为正确代码]:- PROGRAM crop_grille_ecbilt
 
  
-   USE NETCDF
 
 -   implicit none
 
  
-   !variable pour l'ouverture ecriture du netcdf
 
 -   character(256) :: filenameIn, filenameOut
 
 -   integer :: intputID, unlimDimID, varID, ID, nbTime, iyr
 
  
-   !variable pour le calcul des moyennes, etc...
 
 -   real*4, dimension(:), allocatable :: lon, lat, time, ValueVar
 
 -   real*8, dimension(:,:,:), allocatable :: vegfrac
 
  
-   !variables pour le changement de grille
 
 -   !real*8 farea(64,32,1)
 
 -   integer nlat,nlon
 
  
-   integer :: outputID
 
 -   integer :: outdimid, outvarid
 
  
-   if(iargc().eq.2) then
 
 -     call getarg(1,filenameIn)
 
 -     call getarg(2,filenameOut)
 
 -   else
 
 -     write(*,'(A)') "Usage: ./ConvertVegetYo [input.nc] [output.nc]"
 
 -     stop
 
 -   endif
 
  
-   write(*,*) "Open ", trim(filenameIn)
 
 -   call check(nf90_open(filenameIn, nf90_nowrite, intputID))
 
 -   call check(nf90_inquire(intputID, unlimitedDimId = unlimDimID))
 
  
-   write(*,*) "Read longitude"
 
 -   call check(nf90_inq_varid(intputID, "lon", ID))
 
 -   allocate(lon(64))
 
 -   call check(nf90_get_var(intputID, ID, lon, (/ 1 /), (/ 64 /)))
 
  
-   write(*,*) "Read latitude"
 
 -   call check(nf90_inq_varid(intputID, "lat", ID))
 
 -   allocate(lat(32))
 
 -   call check(nf90_get_var(intputID, ID, lat, (/ 1 /), (/ 32 /)))
 
  
-   call check(nf90_inq_varid(intputID, "time", ID))
 
 -   allocate(time(1992))
 
 -   call check(nf90_get_var(intputID, ID, time, (/ 1 /), (/ 1992 /)))
 
  
-   call check(nf90_inq_varid(intputID, "vegfrac", varID))
 
 -   allocate(vegfrac(64,32,1992))
 
 -   call check(nf90_get_var(intputID, varID, vegfrac, (/ 1, 1, 1 /), (/ 64,32,1992 /)))
 
 -   
 
 -   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 -   !!  Create netcdf START
 
 -   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 -   !write target file
 
 -   write(*,*) "Create ", trim(filenameOut)
 
 -   call check(nf90_create(filenameOut, nf90_clobber, outputID))
 
 -   
 
 -   !define dimension
 
 -   call check(nf90_def_dim(outputID, "lon", 64, outdimid))
 
 -   call check(nf90_def_var(outputID, "lon", NF90_FLOAT, (/ 1 /), outvarid))
 
 -   call check(nf90_put_att(outputID, outvarid, "long_name", "longitude coordinate"))
 
 -   call check(nf90_put_att(outputID, outvarid, "standard_name", "longitude"))
 
 -   call check(nf90_put_att(outputID, outvarid, "units", "degrees_east"))
 
 -   call check(nf90_put_att(outputID, outvarid, "axis", "X"))
 
  
-   call check(nf90_def_dim(outputID, "lat", 32, outdimid))
 
 -   call check(nf90_def_var(outputID, "lat", NF90_FLOAT, (/ 2 /), outvarid))
 
 -   call check(nf90_put_att(outputID, outvarid, "long_name", "latitude coordinate"))
 
 -   call check(nf90_put_att(outputID, outvarid, "standard_name", "latitude"))
 
 -   call check(nf90_put_att(outputID, outvarid, "units", "degrees_north"))
 
 -   call check(nf90_put_att(outputID, outvarid, "axis", "Y"))
 
 -   
 
 -   !NF90_UNLIMITED
 
 -   call check(nf90_def_dim(outputID, "time", NF90_UNLIMITED, outdimid))
 
 -   call check(nf90_def_var(outputID, "time", NF90_FLOAT, (/ 3 /), outvarid))
 
 -   call check(nf90_put_att(outputID, outvarid, "long_name", "time"))
 
 -   call check(nf90_put_att(outputID, outvarid, "standard_name", "time"))
 
 -   call check(nf90_put_att(outputID, outvarid, "units", "years since 0001-01-01 00:00:00"))
 
 -   call check(nf90_put_att(outputID, outvarid, "time_origin", "01-JAN-0001 00:00:00"))
 
  
-   !define variable
 
 -   call check(nf90_def_var(outputID, "vegfrac", NF90_FLOAT, (/ 1, 2, 3 /), outvarid))
 
 -   call check(nf90_put_att(outputID, outvarid, "long_name", "fraction de culture par maille"))
 
 -   call check(nf90_put_att(outputID, outvarid, "standard_name", "fraction_culture"))
 
 -   call check(nf90_put_att(outputID, outvarid, "units", "%"))
 
 -   call check(nf90_put_att(outputID, outvarid, "missing_value", real(-99.99)))
 
  
-   !finish the configuration of the output file
 
 -   call check(nf90_enddef(outputID))
 
 -   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 -   !! Create END
 
 -   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
  
-   do iyr=1,1992
 
 -     do nlat=1,32
 
 -       do nlon=1,64
 
 -         !farea(nlon,nlat,iyr)=vegfrac(nlon,nlat,1)
 
 -         write(*,*) nlon,",",nlat,",",iyr," => ", farea(nlon,nlat,iyr)
 
 -       end do
 
 -     end do
 
 -   end do
 
 -   
 
 -   call check(nf90_close(intputID))
 
  
- END PROGRAM crop_grille_ecbilt
 
  
 
- SUBROUTINE check(status)
 
 -   USE NETCDF
 
 -   IMPLICIT NONE
 
  
-   INTEGER, INTENT (IN) :: status
 
 -   if(status /= nf90_noerr) then
 
 -     write(*,*)"Error  : ", trim(nf90_strerror(status))
 
 -     stop
 
 -   end if
 
 - END SUBROUTINE check
 
 
  复制代码 
 
我想问,这段代码到底对不对?编译成功,运行时,只打印出第一年的数据,第二年的数为空,然后程序就出错 
出错信息为:
 - forrtl: severe (174): SIGSEGV, segmentation fault occurred
 
 - Image              PC                Routine            Line        Source
 
 - a.out              0000000000401740  Unknown               Unknown  Unknown
 
 - a.out              0000000000400E1C  Unknown               Unknown  Unknown
 
 - libc.so.6          00002B1417944CDD  Unknown               Unknown  Unknown
 
 - a.out              0000000000400D19  Unknown               Unknown  Unknown
 
 
  复制代码 请论坛中的前辈帮忙解解,多谢了!!! 
 
改变读数据方式后成功,读vegfrac时不在循环里读,直接一次性读出来 
 
 
 
 |   
 
 
 
 |