- 积分
- 376
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2023-5-23
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在处理数据时经常会碰到文件夹嵌套文件夹的情况,如一个大文件夹包含a个高度文件夹,每个高度文件夹包含b个变量文件夹,每个变量文件夹包含c个日期文件,而日期文件中才包含真正的数据,共计a*b*c个文件,要对这些文件读取写出完整的命令太麻烦,所以调用系统命令读取文件夹名称和文件名。故分享实例代码如下,本代码读取嵌套文件数据并转换成grd格式,无法涵盖所有情况,仅供参考:
- program read_files
- USE DFLIB
- implicit none
- character*100 folder_path,output,ii, file_name
- character*20 varstr,hstr(4),tstr
- integer varnum,hnum(4),tnum,maxhnum,locmax(1)
- integer vari,hi,ti
- integer i,numfiles,filenum,ios
- logical(4) res
-
- open(100,file='D:\grads\dataofzhenduan.grd',form='binary')
- output='none.txt'
- call outnone(output)!!
-
- folder_path = 'D:\grads\zhenduanshuju\data'
- i=1
- write(ii,'(i2)'),i
- output='dir'//trim(adjustl(ii))//'.txt'
- call ListFile(folder_path,outPut)
- varnum=numfiles(output)
- open(31,file=''//trim(output)//'')
-
- do i=1,varnum
- read(31,*),varstr
- folder_path = 'D:\grads\zhenduanshuju\data\'//trim(varstr)
- write(ii,'(i2)'),10+i
- output='dir'//trim(adjustl(ii))//'.txt'
- call ListFile_n(folder_path,outPut)
- hnum(i)=numfiles(output)
- open(i,file=''//trim(output)//'')
- enddo
-
- rewind(31)
- maxhnum=maxval(hnum)
- locmax=maxloc(hnum)
- i=locmax(1)!!
- close(i)!!
- !print*, locmax
- write(ii,'(i2)'),10+locmax
- output='dir'//trim(adjustl(ii))//'.txt'
- open(32,file=''//trim(output)//'')
-
-
-
- folder_path = 'D:\grads\zhenduanshuju\data\air\100'
- i=3
- write(ii,'(i2)'),i
- output='dir'//trim(adjustl(ii))//'.txt'
- call ListFile(folder_path,outPut)
- tnum=numfiles(output)
- open(33,file=''//trim(output)//'')
- !print*,varnum,hnum,tnum,maxhnum
-
- do ti=1,tnum
- read(33,*),tstr
- !print*,tstr
- do vari=1,varnum-1
- read(31,*),varstr
- do hi=1,maxhnum
- read(32,*),hstr(4)!!
- read(vari,*,iostat=ios),hstr(vari)
- if(hstr(4)/=hstr(vari).OR.ios /= 0)then
- BACKSPACE(vari)
- endif
- if(hstr(4)/=hstr(vari))then
- file_name='none.txt'
- call writefile(file_name)
- print*,file_name
- cycle
- endif
- file_name='D:\grads\zhenduanshuju\data\'//trim(varstr)//'\'//trim(hstr(4))//'\'//trim(tstr)
- call writefile(file_name)
- print*,file_name
- enddo
- rewind(32)
- enddo
- file_name='uv'
- call writefileuv(file_name,tstr)
- rewind(31)
- rewind(1)
- rewind(2)
- rewind(3)
- enddo
- do i=1,varnum
- close(i)
- enddo
- close(31)
- close(32)
- close(33)
- close(100)
- pause
- end program read_files
- subroutine ListFile(fPath,outPut)
- character*(*),InTent(In):: fPath,outPut
- character*100 CMD
- LOGICAL(4) res
- CMD="dir /on/b "//trim(fPath)//" >"//trim(outPut)
- res=SYSTEMQQ(CMD)
- end subroutine
-
- subroutine ListFile_n(fPath,outPut)
- character*(*),InTent(In):: fPath,outPut
- character*100 CMD
- LOGICAL(4) res
- CMD="dir /o-n/b "//trim(fPath)//" >"//trim(outPut)
- res=SYSTEMQQ(CMD)
- end subroutine
-
- ! Get the number of files in the folder
- function numfiles(folder)
- integer numfiles,status
- character*100 folder
- numfiles=0
- open(unit=10, file=''//trim(folder)//'', status='old')
- do i = 1, 1000
- read(10, '(A)', iostat=status) file_name
- if (status /= 0) exit
- numfiles = numfiles + 1
- end do
- close(10)
- end function
-
- subroutine writefile(filename)
- real filedata(71,101)
- integer::i=1,aa
- character*100 filename
- open(90,file=''//trim(filename)//'')
- !if(aa==4)then
- ! read(90,*)
- ! read(90,*)
- ! read(90,*)
- !do i=1,71
- ! read(90,*),(filedata(i,j),j=1,101)
- ! write(100),(filedata(i,j),j=1,101)
- !enddo
- !i=1
- !do i=1,71
- ! read(90,*),(filedata(i,j),j=1,101)
- ! write(100),(filedata(i,j),j=1,101)
- !enddo
- !else
- read(90,*)
- read(90,*)
- read(90,*)
- read(90,*)
- do i=1,71
- read(90,*),(filedata(i,j),j=1,101)
- write(100),(filedata(i,j),j=1,101)
- enddo
- !endif
- close(90)
- end
-
- subroutine outnone(outPut)
- real::none(71,101)=9999.990
- integer i
- character*100 output
- open(unit=11, file=''//trim(output)//'')
- write(11,*),9999.990
- write(11,*),9999.990
- write(11,*),9999.990
- write(11,*),9999.990
- do i=1,71
- write(11,*),(none(i,j),j=1,101)
- enddo
- close(11)
- end subroutine
-
- subroutine writefileuv(varstr,tstr)
- real filedata(142,101,12)
- integer::i,j,k
- character*100 varstr,filefile
- character*20 tstr,hstr
- do k=1,12
- read(32,*),hstr
- filefile='D:\grads\zhenduanshuju\data\'//trim(varstr)//'\'//trim(hstr)//'\'//trim(tstr)
- open(90,file=''//trim(filefile)//'')
- read(90,*)
- read(90,*)
- read(90,*)
- do i=1,142
- read(90,*),(filedata(i,j,k),j=1,101)
- enddo
- enddo
- rewind(32)
- do k=1,12
- do i=1,71
- write(100),(filedata(i,j,k),j=1,101)
- enddo
- print*,k
- enddo
- do k=1,12
- do i=72,142
- write(100),(filedata(i,j,k),j=1,101)
- enddo
- print*,k
- enddo
- close(90)
- end subroutine
复制代码 以下有逐行注释的代码,本代码泛用性不强,其实可以通过宏定义提高泛用性,等以后有时间了再来修改吧,如果想要了解思路,可以下载代码文件
Source1.f90
(7.15 KB, 下载次数: 0, 售价: 1 贡献)
|
|