从MODIS官网下载的modis数据文件名中的日期是以该年第多少天命名,而不是几月几日那种形式,如下图所示:
就稍微研究了以下,参考一些网上的资料,将天数转换为当年某月某日,直接上代码:
file = 'F:\02_data\RSdata\modis\oridata\MOD11A1\MOD11A1.A2021152.h26v05.061.2021153091150.hdf'
basename = file_basename(file)
data_date = (strsplit(basename,'.',/extract))[1]
;获取文件名中年和天数信息
year = strmid(data_date,1,4)
day = strmid(data_date,5,7)
print,year,day
month_day = day_to_date(year,day) ;调用天数转换当年某月某日函数进行转换
print,month_day
接下来贴上日期转换函数,主要思路就是先判断该年份为闰年还是平年,根据该结果构建当年每月1号在当年的天数列表,然后根据该列表和需要进行计算的天数进行差值,去索引+1即可得到几月几日。
function day_to_month,uintday,months
compile_opt idl2
uintyear = uint(year)
uintday = uint(day)
;判断是否为闰年,闰年:/400,/4取余都为0;/4取余为0且/100取余不为0.其余都为平年。
if uintyear mod 400 eq 0 and uintyear mod 4 eq 0 then begin
months=[1,32,61,92,122,153,183,214,245,275,306,336]
month = day_to_month(uintday,months)
endif else if uintyear mod 4 eq 0 and uintyear mod 100 ne 0 then begin
months=[1,32,61,92,122,153,183,214,245,275,306,336]
month = day_to_month(uintday,months)
endif else begin
months=[1,32,60,91,121,152,182,213,244,274,305,335]
month = day_to_month(uintday,months)
endelse
return,month
end
;该函数将天数转换为该天所在月份
function day_to_month,uintday,months
compile_opt idl2
;先做差值,然后获取 大于0且最小差值的索引,索引+1就为该天数所在月份。
diff = uintday-months
b = where(diff ge 0)
c = diff eq min(diff[b])
index = where(c eq 1)
uintday = diff[index] + 1
index = index+1
;将1至9月份和1至9日由一位数变为两位数,如9——》09
if index le 9 then begin
;去除所有空格
temp = strcompress(string(index),/remove_all)
index = '0' + temp
endif else begin
index = strcompress(string(index),/remove_all)
endelse
if uintday le 9 then begin
temp = strcompress(string(uintday),/remove_all)
strday = '0' + temp
endif else begin
strday = strcompress(string(uintday),/remove_all)
endelse
;构建一个数组,返回一个数组
str_arr = strarr(2)
str_arr[0] = index
str_arr[1] = strday
return,str_arr
end
结果如下:
;month: 06 01
file = 'F:\02_data\RSdata\modis\oridata\MOD11A1\MOD11A1.A2020141.h26v05.061.2021153091150.hdf'
;结果如下
;month:05 20