Fortran Coder

查看: 10198|回复: 9
打印 上一主题 下一主题

[数学库] linux下使用netcdf代码的错误

[复制链接]

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
跳转到指定楼层
楼主
发表于 2017-4-16 20:46:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
调用子程序完成文件的输入和输出,程序如下(应用求助过的程序-linux下):
[Fortran] 纯文本查看 复制代码
program link

      implicit none
      external wheat_area,allockey_lei,integrated_wheat,link_area_calendar

      integer k,i,j
      integer,parameter::nCol
      character(90) str(nCol)
      character(90) str1(nCol1)
      integer,dimension(3166782)::ALLOC_KEY1
      integer,dimension(792304)::alloc_key,m_1,n_1
      real,dimension(3166782)::X,Y
      real,dimension(792304)::lon_1,lat_1,whea,plant_1,harvest_1
      character*15,dimension(3166782)::Name
      character*15,dimension(792304)::name_1
      real,dimension(792304)::lon_1,lat_1,
      character*15,dimension(792304)::name_1
      integer,parameter::latitude=360,longitude=720
      real lon_2(longitude)
      real lat_2(latitude)
      real plant_2(longtitude,latitude)
      real harvest_2(longtitude,latitude)

      call wheat_area(str,nCol)
      call allockey_lei(str1,nCol1)
      call integrated(ALLOC_KEY1,alloc_key,X,Y,lon_1,lat_1,whea,Name,name_1)
      call linkall(alloc_key,m_1,n_1,lon_1,lon_2,lat_1,lat_2,whea,plant_1,plant_2,harvest_1,harvest_2,name_1)

      end program

      !number 1,extract area
      subroutine wheat_area(nCol,str)
         implicit none

         integer k,i
         integer,parameter::nCol=113     !Define a constant nCol equal to 113
         character(90) str(nCol)
         character(9),parameter:: strHead(6)=['alloc_key','whea     ','whea_h   ','whea_l   ', 'whea_i   ','whea_s   ']
         logical (kind=4) L(nCol)
         open(10,file="/home/iga_qtong/fortran_xcao/gae_c/spam_p.csv")
         open(11,file="/home/iga_qtong/fortran_xcao/gae_c/whout1.csv")
         read(10,*) str  !read the head of table
         L(i)= .false.       !determin the column to read
         do i=1,nCol
            str(i)=AdjustL(str(i))
            if(any(Trim(str(i))==strHead))then    !if the string equal to the strHead
            L(i)= .true.
            write(11,"(a,',')",advance='no') Trim(str(i))
            end if
         end do
         write(11,*)
         do
           read(10,*,iostat=k) str
           if(k/=0) exit
           do i=1,nCol
           if(.NOT.L(i)) cycle
           write(11,'(a,",")',advance='no') Trim(str(i))
           end do
         write(11,*)
         end do

      end subroutine wheat_area

      !number 2,extract longitude and latitude
      subroutine allockey_lei(nCol1,str1)
         implicit none

         integer k,i
         integer,parameter::nCol1=6     !Define a constant nCol equal to 6
         character(90) str1(nCol1)
         character(9),parameter::strHead(4)=['X        ','Y        ','Name     ','ALLOC_KEY']
         logical (kind=4) L(nCol1)

         open(12,file="/home/iga_qtong/fortran_xcao/gae_c/allockey.csv")
         open(13,file="/home/iga_qtong/fortran_xcao/gae_c/al_new1.csv")
         read(12,*) str1  !read the head of table
         L= .false.       !determin the column to read
         do i=1,nCol1
            str1(i)=AdjustL(str1(i))
         if(any(Trim(str1(i))==strHead))then        !if the string equal to the strHead
            L(i)= .true.
            write(12,"(a,',')",advance='no') Trim(str1(i))
            end if
         end do

         do
            read(10,*,iostat=k) str1
            if(k/=0) exit
            do i=1,nCol1
            if(.NOT.L(i)) cycle
            write(13,'(a,",")',advance='no') Trim(str1(i))
            end do

         end do

      end subroutine allockey_lei

      !number 3
      subroutine integrated(ALLOC_KEY1,alloc_key,X,Y,lon_1,lat_1,whea,Name,name_1)
         implicit none
         integer i,j
         integer,dimension(3166782)::ALLOC_KEY1   !extract variable from al_new.csv
         integer,dimension(792304)::alloc_key     !extract variable from whout.csv
         real,dimension(3166782)::X,Y             !X and Y got from al_new.csv
         real,dimension(792304)::whea             !wheat area in whout.csv
         real,dimension(792304)::lon_1,lat_1      !selected longtitude and latitude to the new file
         character*15,dimension(3166782)::Name    !country name
         character*15,dimension(792304)::name_1   !selected country name
         character c                              !c for null read
         open(14,file="/home/iga_qtong/fortran_xcao/gae_c/al_new1.csv")
         open(15,file="/home/iga_qtong/fortran_xcao/gae_c/whout1.csv")
         open(16,file="/home/iga_qtong/fortran_xcao/gae_c/integrated_wheat1.csv")
         !read al_new.csv
         read(14,*) c
         do i=1,3166782
            read(14,*) X(i),Y(i),Name(i),ALLOC_KEY1(i)
         end do
         write(*,*) X(899920),Y(899920),ALLOC_KEY(899920)
         !read whout.csv
         read(15,*)c
         do j=1,792304
            read(15,*) alloc_key(j),whea(j)
         end do
         write(*,*) 139000,alloc_key(139000),whea(139000)
         !tegrated wheat_area and allockey
         do j=1,792304
            do i=1,3166782
             if(alloc_key(j)==ALLOC_KEY1(i)) then
               name_1(j)=Name(i)
               lon_1(j)=X(i)
               lat_1(j)=Y(i)
              write(16,10) alloc_key(j),',',name_1(j),',',lon_1(j),',',lat_1(j),',',whea(j)
           10 format(I8,A15,F10.3,F10.3,F10.2)
              end if
             end do
         end do

         close(14)
         close(15)
         close(16)

      end subroutine integrated


      !number 4, to link calendar and area by lon and lat
      subroutine linkall(alloc_key,m_1,n_1,lon_1,lon_2,lat_1,lat_2,whea,plant_1,
         &plant_2,harvest_1,harvest_2,name_1)
         implicit none
         integer i,j

         integer,dimension(792304)::alloc_key,m_1,n_1
         real,dimension(792304)::lon_1,lat_1,whea,plant_1,harvest_1
         character*15,dimension(792304)::name_1
         integer,parameter::latitude=360,longitude=720   !calendar
         integer ierr,ncid,varid,len_file
         real lon_2(longitude)
         real lat_2(latitude)
         real plant_2(longtitude,latitude)
         real harvest_2(longtitude,latitude)
         character*299 calendar_file

         include 'netcdf.inc'

         calendar_file="/home/iga_qtong/fortran_xcao/gae_c/Wheat.nc"
         plant_1=0.1
         harvest_1=0.1
         m_1=0
         n_1=0

         open(17,file="/home/iga_qtong/fortran_xcao/gae_c/integrated_wheat1.csv")
         open(18,file="/home/iga_qtong/fortran_xcao/gae_c/link_area_calendar1.csv")

         !open the integrated_wheat1.csv and extract information
         do i=1,792304
            read(17,*) alloc_key(i),name_1(i),lon_1(i),lat_1(i),whea(i)
         end do
         write(*,*) 1,alloc_key(1),name_1(1),lon_1(1),lat_1(1),whea(1)
         write(*,*) i-1,alloc_key(i-1),name_1(i-1),lon_1(i-1),lat_1(i-1),whea(i-1)

         !open the Wheat.nc and extract information
         len_file=len_trim(calendar_file)
         ierr=nf_open(trim(calendar_file),nf_nowrite, ncid)

         ierr=nf_inq_varid(ncid, 'plant', varid)     !get "plant"
         ierr=nf_get_var_real(ncid, varid, plant_2)
         write(*,*) "the status of plant is",ierr
         write(*,*) plant_2(50,70)


         ierr=nf_inq_varid(ncid, 'harvest', varid)   !get "harvest"
         ierr=nf_get_var_real(ncid, varid, harvest)
         write(*,*) "the status of harvest is",ierr
         write(*,*) harvest_2(50,70)


         ierr=nf_inq_varid(ncid, 'longitude', varid)   !get "longitude"
         ierr=nf_get_var_real(ncid, varid, lon_2)
         write(*,*) "the status of longitude is",ierr
         write(*,*) lon_2(50)


         ierr=nf_inq_varid(ncid, 'latitude', varid)   !get "latitude"
         ierr=nf_get_var_real(ncid, varid, lat_2)
         write(*,*) "the status of longitude is",ierr
         write(*,*) lat_2(50)

         ! match lon and lat by a loop program
         do i=1,793204             !longitude
            do j=1,720
              if(abs(lon_1(i)-lon_2(j))-mival(abs(lon_1(i)-lon_2(j))==0) then
                m_1(i)=j
                write(*,*) i,m_1(i)
              end if
            end do
         end do

         do i=1,793204              !latitude
            do j=1,360
              if(abs(lat_1(i)-lat_2(j))-mival(abs(lat_1(i)-lat_2(j))==0) then
                n_1(i)=j
                write(*,*) i,n_1(i)
              end if
            end do
         end do

         do i=1,792304              !assign plant_2 to plant_1
           plant_1(i)=plant_2(m_1(i),n_1(i))
           write(*,*) plant_1(i)

         do i=1,792304              !assign harvest_2 to harvest_1
           harvest_1(i)=harvest_2(m_1(i),n_1(i))
           write(*,*) harvest_1(i)
          end do

          ! Last, write varibales into a new file
          do i=1,793204
          write(18,100) alloc_key(i),',',name_1(i),',',lon_1(i),',',lat_1(i),',',whea(i),',',
          &plant_1(i),',',harvest_1(i),',',m_1(i),',',n_1(i)
        100 format(I8,A15,F10.3,F10.3,F10.2,F10.2,F7.1,I5,I5)
           write(*,*) 1,alloc_key(1),name_1(1),lon_1(1),lat_1(1),whea(1)
           write(*,*) i,alloc_key(i),name_1(i),lon_1(i),lat_1(i),whea(i)

           close(17)
           close(18)
        subroutine linkall


编译出现了问题,请问:
1、各个子程序之间是否可以使用同样的变量
2、一旦其中有读取nc文件,是否编译要按Nc文件的翻译
3、请问是否能帮忙指出程序里的错误。

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 2017-4-16 21:06:59 | 只看该作者
1.可以
2.没有“按NC文件的翻译”这种说法
3.你遇到了什么错误?

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
板凳
 楼主| 发表于 2017-4-16 22:00:44 | 只看该作者
fcode 发表于 2017-4-16 21:06
1.可以
2.没有“按NC文件的翻译”这种说法
3.你遇到了什么错误?

错误如下:
link.f90(16): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: %FILL <IDENTIFIER>
      real,dimension(792304)::lon_1,lat_1,
------------------------------------------^
link.f90(36): error #6406: Conflicting attributes or multiple declaration of name.   [NCOL]
         integer,parameter::nCol=113     !Define a constant nCol equal to 113
----------------------------^
link.f90(70): error #6406: Conflicting attributes or multiple declaration of name.   [NCOL1]
         integer,parameter::nCol1=6     !Define a constant nCol equal to 6
----------------------------^
link.f90(147): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: * <IDENTIFIER>
      subroutine linkall(alloc_key,m_1,n_1,lon_1,lon_2,lat_1,lat_2,
-------------------------------------------------------------------^
link.f90(148): error #5082: Syntax error, found '&' when expecting one of: <LABEL> <END-OF-STATEMENT> ; BLOCK PROGRAM BLOCKDATA MODULE INTEGER REAL COMPLEX ...
         &whea,plant_1,plant_2,harvest_1,harvest_2,name_1
----------^
link.f90(211): error #5082: Syntax error, found IDENTIFIER 'THEN' when expecting one of: ( * ) :: , <END-OF-STATEMENT> ; . % (/ + - [ : ] /) . ' ** / ...
              if(abs(lon_1(i)-lon_2(j))-mival(abs(lon_1(i)-lon_2(j))==0) then
-------------------------------------------------------------------------^
link.f90(220): error #5082: Syntax error, found IDENTIFIER 'THEN' when expecting one of: ( * ) :: , <END-OF-STATEMENT> ; . % (/ + - [ : ] /) . ' ** / ...
              if(abs(lat_1(i)-lat_2(j))-mival(abs(lat_1(i)-lat_2(j))==0) then
-------------------------------------------------------------------------^
link.f90(238): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> <CHARACTER_CONSTANT> <INTEGER_CONSTANT> ...
            write(18,100) alloc_key(i),',',name_1(i),',',lon_1(i),',',lat_1(i),',',whea(i),',',
-----------------------------------------------------------------------------------------------^
link.f90(239): error #5082: Syntax error, found '&' when expecting one of: <LABEL> <END-OF-STATEMENT> ; BLOCK PROGRAM BLOCKDATA MODULE INTEGER REAL COMPLEX ...
            &plant_1(i),',',harvest_1(i),',',m_1(i),',',n_1(i)
-------------^
link.f90(246): error #5082: Syntax error, found END-OF-FILE when expecting one of: <LABEL> <END-OF-STATEMENT> ; BLOCK PROGRAM BLOCKDATA MODULE INTEGER REAL COMPLEX ...
        subroutine linkall
--------------------------^
compilation aborted for link.f90 (code 1)

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

地板
发表于 2017-4-17 08:21:07 | 只看该作者
第7行
integer,parameter::nCol
常数定义时必须赋值,比如
integer,parameter::nCol = 113
请根据自己的情况赋合理的值

第9行
nCol1 在主程序没有定义,没有赋值。可能应该是 nCol

第16行
real,dimension(792304)::lon_1,lat_1,
逗号后面没有内容,没有结束。
并且与第13行重复定义了 Lon_1 和 lat_1,删除此行

第17行
与第15行重复

第21行,22行
longtitude 拼写错误,应为 longitude

第25行
nCol1 没有定义,可能应该是 nCol

第32行
nCol和str位置颠倒了(改实参的顺序也可以)

第36行
integer,parameter::nCol=113     !Define a constant nCol equal to 113
nCol 与虚参重复。应改为 integer :: nCol

第65行
同32行

第69行
同上

第118行
write(*,*) X(899920),Y(899920),ALLOC_KEY(899920)
此处899920超出ALLOC_KEY定义的 796304

第146行
续行格式不对

第158行
longtitude 拼写错误

第191行
harvest 未定义,是否应该为 harvest_1 或 harvest_2

第210行
if(abs(lon_1(i)-lon_2(j))-mival(abs(lon_1(i)-lon_2(j))==0) then
此句括号不匹配,看不出来你的意图,也不知道应该怎么修改

第219行
同上

第 229行
没写 end do

第234行
同上

第238行
续行格式错误

第242行
没写 enddo

第 245 行
应该为 end subroutine linkall

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
5#
 楼主| 发表于 2017-4-17 14:28:59 | 只看该作者
fcode 发表于 2017-4-17 08:21
第7行
integer,parameter::nCol
常数定义时必须赋值,比如

运行时出现了以下问题:
forrtl: severe (24): end-of-file during read, unit 14, file /home/iga_qtong/fortran_xcao/gae_c/al_new1.csv
请问:
1、将第二个子程序单独运行的时候可以,但是在这里又说文件过长是为什么?如何改正?
2、该程序第三个子程序需要用到第一和第二个子程序的输出文件,可以直接Open吗?还是可以在子程序里call sub2?

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

6#
发表于 2017-4-17 15:41:49 | 只看该作者
0. 出现 end-of-file during read 意思是,读取文件过程中,遇到了文件的尾部。(可能是文件不够长/行数太少,也可能是代码要求的行数太多)
1. 子程序不能单独运行。而且这里的错误并不是文件过长。(也不会有文件过长这个错误提示)
2. 可以直接 open,但前提是,之前的代码(不管是主程序还是子程序),已经 close 了这个文件。同一个文件,不能被程序打开多次(已经关闭的除外)。
   也可以直接 call sub2() 都是可以的。
  文件和子程序,并没有相互约束。只要符合文件的用法,符合子程序的用法。都是允许的。

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

7#
发表于 2017-4-17 16:21:55 | 只看该作者
NetCDF不是有现成的Fortran代码和算例吗?
NetCDF Downloads
http://www.unidata.ucar.edu/downloads/netcdf/index.jsp

lz贴的代码只有csv文件,标题却说“linux下使用netcdf代码的错误”???
时间紧,任务急,无C和C++基础,真心建议果断投奔MATLAB怀抱

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

8#
发表于 2017-4-17 16:59:50 | 只看该作者
它代码里有 nc 文件呀
/home/iga_qtong/fortran_xcao/gae_c/Wheat.nc

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

9#
发表于 2017-4-17 18:24:14 | 只看该作者
fcode 发表于 2017-4-17 16:59
它代码里有 nc 文件呀
/home/iga_qtong/fortran_xcao/gae_c/Wheat.nc

好吧,还是你能够平心静气的看完全部代码并耐心解释出错原因
本站的视频教程以及收集的书籍、代码范例不敢说世界一流水平,好歹也是亚洲一流
好多求助帖子的内容实在。。。速胜论要不得,伸手党更要不得,视频教程看三遍,彭国伦的书看三遍,再把范例代码敲1遍,编译调试1遍,好多问题压根就不是问题


1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

10#
发表于 2017-4-17 19:07:45 | 只看该作者
谢谢夸奖。
伸手党在这里也是不受欢迎的。至于速胜论,我想帮也帮不上。

这个楼主还是挺好学的,自己尝试去写了。我也是有强迫症,刚开始没想到错误这么多,就解释了几个。后来越看越多~~~
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-27 05:21

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表