搜索此博客

2018年5月21日星期一

Sentinel-1影像数据陆地掩模与dB值转换(IDL Programming)

;+
; :Author: Baikal
;-
; Request: .zip format source file should be stored in the same directory
; with the processed folder using the procSentinelRTC_recipe.py script.
;
; Purpose:
; Mask land areas and convert pixel values to dB.
; -Version: 1.0
; Time: Sep.24,2017
;
; First Modification:
; Purpose:
; To sovle abnormal display of pixel values in elevation file and HH&VV output img.
; Time : Sept.25,2017
;
; Second Modification:
; Purpose:
; To clip the image based on Mask_Sea file rather than elevation image.
; -Version: 2.0
; Time : Oct.20,2017
; Third Modification:
; Purpose:
; To add modification of header file.
; -Version: 2.0
; Time : Oct.21,2017


PRO Mask_Land_To_dB_V2

  COMPILE_OPT idl2

  ;给定文件路径
  directory = 'C:\Users\Baikal\Desktop\test\'
  ;查找zip文件
  file = file_search(directory,count = num,'*.zip')
  for id = 0,num-1 DO BEGIN
    ;获取文件名
    file_name = strmid(file[id],70,67,/Reverse_Offset)

    ;获取HH极化与HV极化影像路径
    Image_HH = directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\" + "Beta0_HH.img"
    Image_HV = directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\" + "Beta0_HV.img"

    ;获取HH极化与HV极化影像头文件路径
    HH_hdr = directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\" + "Beta0_HH.hdr"
    HV_hdr = directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\" + "Beta0_HV.hdr"

    ;获取HH极化影像的掩膜文件路径(掩膜文件中仅含有陆地区域)
    Image_HH_Mask = directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC(Mask_Sea).data\" + "Beta0_HH.img"

    ;查找影像中是否有已经存在的转为dB值的文件,若有,则删除
    ;    HH_file = file_search(directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\",'Aim_Beta0_HH.hdr')
    ;    IF HH_file THEN  FILE_DELETE,HH_file
    ;    HV_file = file_search(directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\",'Aim_Beta0_HV.hdr')
    ;    IF HV_file THEN  FILE_DELETE,HV_file
    ;    HH_HV_file = file_search(directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\",'Aim_Beta0_HH_HV.hdr')
    ;    IF HH_HV_file THEN  FILE_DELETE,HH_HV_file

    ;对HH、HV、HH/HV极化影像的三个波段进行波段合成
    header_path = directory + file_name + '\' + file_name + "_TNR_OB_CAL_TC.data\"
    FILE_COPY, HH_hdr, header_path + 'Aim_Beta0_HH.hdr'
    FILE_COPY, HV_hdr, header_path + 'Aim_Beta0_HV.hdr'
    FILE_COPY, HV_hdr, header_path + 'Aim_Beta0_HH_HV.hdr'
    Aim_HH_hdr    = header_path + 'Aim_Beta0_HH.hdr'
    Aim_HV_hdr    = header_path + 'Aim_Beta0_HV.hdr'
    Aim_HH_HV_hdr = header_path + 'Aim_Beta0_HH_HV.hdr'
    ;复制生成转为dB值文件的头文件
    Output_Directory = directory + file_name + '\Composition\'
    file_mkdir, Output_Directory
    FILE_COPY, HV_hdr, Output_Directory + 'Layer Stacking.hdr'
    LS_hdr = Output_Directory + 'Layer Stacking.hdr'

    ;将头文件中的内容进行修改
    OPENU,hdr_lun,Aim_HH_hdr,/get_lun
    line    = 'band names = { Beta0_HH }'
    replace = 'band names={Aim_Beta0_HH}'
    Skip_Lun,hdr_lun,10,/lines
    Point_lun,-hdr_lun,line
    printf,hdr_lun,replace
    CLOSE,hdr_lun & FREE_LUN, hdr_lun

    OPENU,hdr_lun,Aim_HV_hdr,/get_lun
    line    = 'band names = { Beta0_HV }'
    replace = 'band names={Aim_Beta0_HV}'
    Skip_Lun,hdr_lun,10,/lines 
    Point_lun,-hdr_lun,line
    printf,hdr_lun,replace
    CLOSE,hdr_lun & FREE_LUN, hdr_lun

    OPENU,hdr_lun,Aim_HH_HV_hdr,/get_lun
    line    = 'band names = { Beta0_HV }'
    replace = 'band names={Beta0_HH_HV}'
    Skip_Lun,hdr_lun,10,/lines
    Point_lun,-hdr_lun,line
    printf,hdr_lun,replace
    CLOSE,hdr_lun & FREE_LUN, hdr_lun

    OPENU,hdr_lun,LS_hdr,/get_lun
    line_Band  = 'bands = 1'
    replace_line_Band = 'bands = 3'
    Skip_Lun,hdr_lun,4,/lines
    Point_lun,-hdr_lun,line_Band
    printf,hdr_lun,replace_line_Band
    CLOSE,hdr_lun & FREE_LUN, hdr_lun
   
    OPENU,hdr_lun,LS_hdr,/get_lun
    line_Band_Name    = 'band names = { Beta0_HV }'
    replace_Band_Name = 'band names={HH,HV,HH_HV}'
    Skip_Lun,hdr_lun,10,/lines
    Point_lun,-hdr_lun,line_Band_Name
    printf,hdr_lun,replace_Band_Name
    CLOSE,hdr_lun & FREE_LUN, hdr_lun

    ;打开HH极化的头文件
    OPENR,hdr_lun,HH_hdr,/get_lun
    ;设立一个存储头文件行数值的变量
    line = ''
    WHILE (~EOF(hdr_lun)) DO BEGIN
      ;读取一行数据
      READF,hdr_lun,line
      tmp = strsplit(line[0],'=',/extract)
      ;利用空格( )获得影像的头文件参数信息
      header_keyword = Strsplit(tmp[0], ' ', /extract)
      ;按照头文件中变量名字获得相应的参数
      IF header_keyword[0] EQ 'samples' THEN xs = LONG(tmp[1])
      IF header_keyword[0] EQ 'lines' THEN ys = LONG(tmp[1])
      IF header_keyword[0] EQ 'data type' THEN type = LONG(tmp[1])
    ENDWHILE
    ;关闭并释放头文件指针
    CLOSE,hdr_lun & FREE_LUN, hdr_lun

    ;读取HH极化影像数据
    OPENR,HH_unit,Image_HH,/get_lun
    HH = MAKE_ARRAY(xs,ys,type = type)
    READU,HH_unit,HH
    ;!!!修改影像读取的存储方式
    BYTEORDER,HH,/FTOXDR

    ;读取HV极化影像数据
    OPENR,HV_unit,Image_HV,/get_lun
    HV = MAKE_ARRAY(xs,ys,type = type)
    READU,HV_unit,HV
    BYTEORDER,HV,/FTOXDR

    ;打开掩膜文件的头文件
    OPENR,HH_Mask_unit,Image_HH_Mask,/get_lun
    HH_Mask = MAKE_ARRAY(xs,ys,type = type)
    READU,HH_Mask_unit,HH_Mask
    BYTEORDER,HH_Mask,/FTOXDR

    ;生成HH/HV波段数据
    HH_HV = MAKE_ARRAY(xs,ys,type = type)
    ;修改影像的存储方式
    BYTEORDER,HH_HV,/FTOXDR

    for i = 0,xs-1 DO BEGIN
      for j = 0,ys-1 DO BEGIN
        ;如果掩膜文件为非零值或者HH极化数据为零值(背景值),则将输出的文件设置为255
        IF HH_Mask[i,j] NE 0 OR HH[i,j] EQ 0 THEN BEGIN
          HH[i,j]    =  255
          HV[i,j]    =  255
          HH_HV[i,j] =  255
        ENDIF ELSE BEGIN
          HH[i,j]    = 10*ALOG10(HH[i,j])
          HV[i,j]    = 10*ALOG10(HV[i,j])
          HH_HV[i,j] = HH[i,j]/HV[i,j]
        ENDELSE
      endfor
    endfor
    FREE_LUN, HH_unit
    FREE_LUN, HV_unit
    FREE_LUN, HH_Mask_unit

   
    ;!!!!!将文件的存储方式进行修改
    BYTEORDER,HH,/XDRTOF
    BYTEORDER,HV,/XDRTOF
    BYTEORDER,HH_HV,/XDRTOF

    ;检查程序是否存在数学错误
    IF check_math() THEN Print, check_math()
    Beta0_HH_Img = header_path + 'Aim_Beta0_HH.img'
    OPENW,Beta0_HH_lun,Beta0_HH_Img,/get_lun
    ;生成HH极化转为dB值文件的的头文件信息
    WriteU,Beta0_HH_lun,HH
    Close,Beta0_HH_lun
    Free_LUN,Beta0_HH_lun

    Beta0_HV_Img = header_path + 'Aim_Beta0_HV.img'
    OPENW,Beta0_HV_lun,Beta0_HV_Img,/get_lun
    WriteU,Beta0_HV_lun,HV
    Close,Beta0_HV_lun
    Free_LUN,Beta0_HV_lun

    Beta0_HH_HV_Img = header_path + 'Aim_Beta0_HH_HV.img'
    OPENW,Beta0_HH_HV_lun,Beta0_HH_HV_Img,/get_lun
    WriteU,Beta0_HH_HV_lun,HH_HV
    Close,Beta0_HH_HV_lun
    Free_LUN,Beta0_HH_HV_lun

    ;HH、HV、HH_HV影像3个波段进行波段合成
    Aim_Beta0_HH_HV = [[HH],[HV],[HH_HV]]
    Aim_Beta0_HH_HV = Reform(Aim_Beta0_HH_HV,[3,xs,ys])
    Output_File = Output_Directory + 'Layer Stacking.img'
    OPENW,lun,Output_File,/get_lun
    WriteU,lun,Aim_Beta0_HH_HV
    Close,lun
    Free_Lun,lun

    ;输出显示处理状态
    Print,'还剩',strtrim(num-id-1,1),'景影像待处理,已处理',strtrim(id+1,1),'景影像'
  ENDfor
  print,'处理完毕'
END

没有评论:

发表评论

LibSVM Chinese Brief Infroduction

Reference: [1]  https://blog.csdn.net/v_july_v/article/details/7624837 [2]  https://wenku.baidu.com/view/c402e983336c1eb91b375d37.html?fr...

  • Word (2)