!按格式读取文件,将csv文件按字符串形式分列读取,对时间格式进行处理 !csv文件首列为时间,后续为数值,参考智慧大坝环境文件 !时间格式支持年月日时分和年月日,年月日以/隔开,时分以:隔开 !若数值有多列,字符串长度适度增加 subroutine FileTrans(M,FileLen,FileName,N,OutLine) implicit none common /tips/err integer :: err integer :: M !文件列数 integer :: FileLen character(len=FileLen) :: FileName real*8 :: OutLine(1000,M) !时间数值(精确到小时)超过8位 character(len=200) :: cha character(len=200) :: cdate,cnum integer :: date(1000,5) real*8 :: num(1000,M-1) integer :: i,stat,j integer :: datenum !日期格式数量 integer :: N !数列长度 N = 0 datenum = 0 date = 0 num = 0 OutLine = 0 open(100,file=FileName,status='old') read(100,'(a)')cha !第一行为表头 do i=1,40000 read(100,'(a)',iostat=stat)cha if(cha(len_trim(cha):len_trim(cha))==',') then err = 1 !数据缺失(末行) goto 100 end if if(stat/=0) exit N = N+1 if(index(cha(1:len_trim(cha)),':')>0) then datenum = 5 else datenum = 3 end if if(index(cha,',,')>0.or.index(cha,'/')==0) then err = 1 !数据缺失(首行或中间行) goto 100 end if do j=1,len_trim(cha) if(index('-/:',cha(j:j))>0) cha(j:j)=' ' if(cha(j:j)==',') then cdate = cha(1:j-1) cnum = cha(j+1:) exit end if end do read(cdate,*)date(i,1:datenum) read(cnum,*)num(i,:) end do close(100) !当计算月份时,需要获取初始时间,存放在序列最后一行,所以此时文件数据不能超过999行 date(N+1,1) = date(1,1) date(N+1,2:3) = 1 OutLine(:,2:M) = num(:,1:M-1) call Date_to_num(date,N,OutLine(:,1)) 100 end subroutine