fortran-时间的格式化输出

1 声明部分

    implicit none
    real :: read_null           
    # 用于读取时跳过输入文件中不需要的列
    integer :: leap_year        
    # 用于判断是否为闰年。 0是平年;1是闰年
    integer,allocatable :: year(:), month(:), day(:), hour(:)   
    #  时间间隔为1小时debug

2 输出时间格式生成

 # 输出的时间格式生成,起始时段为净雨量文件中的起始时段

    allocate(year(size(Q)),month(size(Q)),day(size(Q)),hour(size(Q)))  ! 数组长度等于径流的长度
    ! 读取初始时间
    filename = trim(Address)//'\'//'data_rs.txt'
    open(10,file=filename,access='sequential',status='old')
        read(10,*) year(1),month(1),day(1),hour(1)
    close(10)

    do t=2,size(Q)
        leap_year = 0
        ! 一般情况下,“小时”+1,而“日,月,年”不变,把它们放在后边处理
        hour(t) = hour(t-1)+1
        day(t) = day(t-1)
        month(t) = month(t-1)
        year(t) = year(t-1)
        !判断是否为闰年,确定二月的天数
        if(mod(year(t),100)==0) then
            if(mod(year(t),400)==0) then
                leap_year = 1
            end if 
        else
            if(mod(year(t),4)==0) then
                leap_year = 1
            end if
        end if
        ! 24时统一记为第二天0if(hour(t) == 24) then
            hour(t)=0
            day(t) = day(t)+1
        end if
        !超出一个月的最大天数,则计入下一个月
        select case(month(t))
        case(1,3,5,7,8,10,12)
            if (day(t)==32)then
                day(t) = 1
                month(t)= month(t)+1
            end if 
        case(4,6,9,11)
            if(day(i)==31)then
                day(t) = 1
                month(t)= month(t)+1 
            end if 
        case(2)
            if(leap_year==1 .AND. day(t)==30)then
                day(t) = 1
                month(t)= month(t)+1 
            elseif(leap_year==0 .AND. day(t)==29)then
                day(t) = 1
                month(t)= month(t)+1 
            end if
        end select
    end do

3 输出

     filename=trim(Address)//'\'//'output_path'
     open(10,file=filename)
     !write(10,"(5A6)") "year" ,"month", "day", "hour" ,"Q"
     do i=1,size(Q)
      write(10,"(4I5,F10.3)")year(i),month(i),day(i),hour(i),Q(i)
     end do
     close(10)

猜你喜欢

转载自blog.csdn.net/xiaofeixiazyh/article/details/78700394
今日推荐