Python: Conversion expression date (day of year & year month day)

We used the date format is "date" type, that is year-month-day, for example, today is September 14, 2019, 2019-09-04.

However, in some places, such as remote sensing images downloaded inside named, for ease of presentation data, often doy (day of year) type, as shown in the cloud geospatial data download landsat8:

 

'2019133' represents the first 133 days of 2019, is unlikely to be a normal person say this is a few months a few numbers.

Especially after the bulk download of remote sensing images, not so much patience to count this stuff. Therefore, the establishment of the two functions to convert.

Benpian use of technical content is not high, but practical.

def date2doy(year,month,day):
            month_leapyear=[31,29,31,30,31,30,31,31,30,31,30,31]
            month_notleap= [31,28,31,30,31,30,31,31,30,31,30,31]
            doy=0

            if month==1:
                  pass
            elif year%4==0 and (year%100!=0 or year%400==0):
                  for i in range(month-1):
                          doy+=month_leapyear[i]
            else:
                  for i in range(month-1):
                          doy+=month_notleap[i]
            doy+=day
            return doy

def doy2date(year,doy):    
       month_leapyear=[31,29,31,30,31,30,31,31,30,31,30,31]
       month_notleap= [31,28,31,30,31,30,31,31,30,31,30,31]

       if year%4==0 and (year%100!=0 or year%400==0):
          for i in range(0,12):
             if doy>month_leapyear[i]:
                 doy-=month_leapyear[i]
                 continue
             if doy<=month_leapyear[i]:
                 month=i+1
                 day=doy
                 break
       else:
          for i in range(0,12):
             if doy>month_notleap[i]:
                 doy-=month_notleap[i]
                 continue
             if doy<=month_notleap[i]:
                 month=i+1
                 day=doy
                 break
       return month,day

Guess you like

Origin www.cnblogs.com/maoerbao/p/11518831.html