搜索此博客

2018年7月6日星期五

【转载】将数据归一化到任意区间范围的方法

特殊情况下,将数据的范围归一化到[0,1]范围,
归一化公式为:

y_nor  = ( y  - min) / ( max - min )

一般情况下,即将数据的范围归一化到[ a, b ]范围,
此时的规范化公式为:

y_nor  = a + ( b  - min) / ( max - min ) * ( b - a )


Reference:
https://blog.csdn.net/guyuealian/article/details/78845031


2018年7月2日星期一

Download tar compressed files from FTP using Python

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 01 13:00:26 2017

@author: Baikal
"""
from ftplib import FTP
import os
import urllib2

def Download_CIS( Root_dir, Sub_dir, Position, Year, CIS_Root_DIR, CIS_SUB_DIR ):

    CIS_DIR = os.path.join( CIS_Root_DIR, Year, CIS_SUB_DIR )
    os.chdir( CIS_DIR )
    Web_Sub_dir = Sub_dir + '/' + Position + '/' + Year 
    
    ftp = FTP( Root_dir )
    #Log in anonymous
    ftp.login()
    # Change the directory.
    ftp.cwd( Web_Sub_dir ) 
    
    lists = ftp.nlst() 
    
    for list in lists:
        if list.endswith( '.tar' ):
            Website_file_dir = 'ftp://' + Root_dir + '/' + Web_Sub_dir + '/' + list
            CIS_data = urllib2.urlopen( Website_file_dir ).read(  )
            CIS_File = os.path.join( CIS_DIR, list )
            with open( CIS_File, 'wb' ) as Data:
                Data.write( CIS_data )
                print list + 'has written down'


##                
Root_dir = 'sidads.colorado.edu'
Sub_dir = 'pub/DATASETS/NOAA/G02171'
Position = 'Western_Arctic'
Year = '2018'


CIS_Root_DIR = r'G:\CIS_Sea_Ice_Chart_3573'
if Position == 'Western_Arctic':
    CIS_SUB_DIR = 'WA'
else:
    CIS_SUB_DIR = 'EA'
    
Download_CIS( Root_dir, Sub_dir, Position, Year, CIS_Root_DIR, CIS_SUB_DIR )


Reference:
https://stackoverflow.com/questions/13534807/python-ftplib-connection-error-gaierror


2018年6月27日星期三

Zip files using Python(基于Python压缩文件夹)

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 27 2018

@author: Baikal
"""
#Purpose: To zip unzip folders of Sentinel-1 files and rename zipfile(Mainly to remove this '.SAFE' four characters) 
#         to adapt the python processing.

#利用的是对目录进行深度优先遍历,首先把第一级目录中的文件进行遍历,如果是文件,
#则把它连同当前路径一起加入result(list),如果是子目录,则在整个目录上继续DFS。
#直到所有的文件都被加入。


import os
import time
import zipfile

# 迭代判断是否到达根目录 def dfs_get_zip_file(  S1_dir, result ):
    
    id = 0
    files = os.listdir( S1_dir )
    for file in files:
        if os.path.isdir( S1_dir + '/' + file ):
            dfs_get_zip_file( S1_dir + '/'+ file, result )
        else:   
            result.append( S1_dir + '/' + file  )

def zip_path(In_dir, Out_dir):

    time_start = time.time()
    FOLDERS = os.listdir( In_dir )
    for S1_FOLDER in FOLDERS:
        if len( S1_FOLDER ) == 72:
            Output_name = In_dir + S1_FOLDER[ :-4 ] + 'zip'
            if os.path.exists( Output_name ) == 0:
                f = zipfile.ZipFile( Output_name ,'w' ,zipfile.ZIP_DEFLATED )
                filelists = []      
                S1_dir = In_dir + S1_FOLDER
                dfs_get_zip_file( S1_dir, filelists )
                for file in filelists:
                    f.write(  file , arcname= S1_FOLDER + file[ 113: ] )
                f.close()
            print S1_FOLDER[ :-4 ] + 'have down'
    
    time_end = time.time()
    ZIP_Time = time_end - time_start
    print 'Classification Time:'
    print str( "%.*f" % (3, ZIP_Time / 60 ) ) + 'min' + str( "%.*f" % (3, ZIP_Time % 60 ) ) + 's'
    print "Well Down"
    
    
##Main
In_dir = r'G:\201606Sen\scihub.copernicus.eu\201611/'
Out_dir = In_dir
zip_path(In_dir, Out_dir)


Reference:
https://blog.csdn.net/wangtua/article/details/68943231

2018年6月6日星期三

IDL Plot函数绘制散点图时设置透明度

目标:
如图1散点图中如果点数太多,簇与簇之间的混淆则难以辨识。因而,希望通过更改某一类簇的透明度来清晰表现不同类簇之间的混淆性与可分性。
图1 散点图

IDL中PLOT函数的关键字:

Syntax


graphic = PLOT(Y, [Format] [, Keywords=value] [, Properties=value])
graphic = PLOT(X, Y, [Format] [, Keywords=value] [, Properties=value])
graphic = PLOT(Equation, [Format] [, Keywords=value] [, Properties=value])

Keywords

Keywords are applied only during the initial creation of the graphic.
AXIS_STYLE=value
/BUFFER, /CURRENT, /DEVICE, DIMENSIONS=[width, height], LAYOUT=array, LOCATION=[x, y], MARGIN=scalar or [left, bottom, right, top], /NO_TOOLBAR, /NODATA, /OVERPLOT, /WIDGETS

Properties

Properties can be set as keywords to the function during creation, or retrieved or changed using the "." notation after creation.

参数有很多,与目标相关的参数为SYM_TRANSPARENCY,范围为0-100。其中,默认值为0,即不透明,100为完全透明。

SYM_TRANSPARENCY

An integer between 0 and 100 that specifies the percent transparency of the symbols. The default value is 0.

Reference:


2018年6月5日星期二

Word表格前面怎么添加一个空白行?

快捷键法:


将光标定位在表格的第一个单元格再用快捷键CTRL+SHIFT+ENTER可以快速的在表格前增加一个空白行。



Installing pip and LibSVM package and installing LibSVM in python and LibSVM Usage

1.
PIP安装步骤,详见参考[1]。

说明:
我的电脑此前已经安装有Anaconda2(32bit),因而,pip默认的安装地址并非帖子中所列的C:\Python27\Scripts,而是D:\Anaconda2\Lib\site-packages。
因而,对我来说,我需要导航至这个目录,进行pip安装结果的验证,

2.
 LibSVM的安装步骤请参考[2]:

Steps:
1. Navigate to http://www.lfd.uci.edu/~gohlke/pythonlibs/#libsvm
2. Download the .whl file of libsvm corresponding to your OS.
3. Open command prompt and navigate to that folder containing the downloaded .whl file.
4. Type the following command in command prompt-

pip install libsvm-3.22-cp27-cp27m-win32.whl
NOTE: Type name of your .whl file after pip install
 安装成功

3.
LibSVM在Python中的安装步骤,参考链接详见[3]。

4.
关于在Python中调用LibSVM的方法,请参考[4].

5.
关于程序中的预测函数svm_predict()中的标签疑惑问题(困扰我一天有余。。。),参考链接[5].


Reference:
[1]
https://dev.to/el_joft/installing-pip-on-windows
[2]
https://stackoverflow.com/questions/12877167/how-do-i-install-libsvm-for-python-under-windows-7/32358275#32358275
[3]
https://www.jianshu.com/p/c3573ddba793
[4]
http://www.cnblogs.com/Dzhouqi/p/3653823.html
[5]
http://www.cnblogs.com/xiaodi914/p/5493646.html





2018年6月4日星期一

Break Line Difference of PrintF and WriteU keyword of IDL『IDL PrintF(默认换行输出)与WriteU(默认不换行输出)打印输出差异』

目的:

利用IDL实现打印输出如下表:


实现:
   ;设置第一行纹理特征名称
    Corr_Name = [ ' ' ,'Mean ' ,'Variance ' ,'Homogeneity ' ,'Contrast ' ,'Dissimilarity ' ,'Entropy ' ,'Second_Moment ' ,'Correlation ' ]

    ;输出第一行纹理特征名
    PrintF , FYI_LUN , Corr_Name
    FOR ii = 0,7 DO BEGIN
      ;输出第一列纹理特征名

      WriteU , FYI_LUN , Corr_Name[ii + 1]

      ;输出各项纹理特征值
      PrintF , FYI_LUN , Correlation_FYI[* , ii]
    ENDFOR

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

2018年5月1日星期二

word公式编辑


1.       打开视图—标尺;
2.       “开始”选项卡下,“样式”栏选择“创建样式”,点击修改;

3.       选择格式,点击制表位;

4.       输入39.5,选择右对齐,前导符无,点击设置;
5.       再输入17.95,居中,前导符无,设置,确定;
6.       编辑一个公式,后面编号,选中我们的编号公式格式;
7.       在公式前键入一个Tab,编号前键入一个Tab,biu~自动就好了

实例:
                                           F=ma                                                        (1)

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)