2015年2月17日火曜日

[Linux] NcFTP

ファイルを再帰的にダウンロードするためのFTPクライアントを探したところ、NcFTPが見つかったのでメモ

・インストール
忘れた。yumで無かったら↓から落としてコンパイル。
NcFTP Client - http://www.ncftp.com/download/

・使い方
man ncftp か ncftpを実行してhelp

・接続方法
ncftp [OPTIONS] [host]
OPTIONOS
-u username
-p password
...

・Bookmark
よくアクセスする接続をbookmarkとして保存できる。
# 現在の接続を保存
ncftp > bookmark

# 一覧を呼び出し
ncftp > bookmarks

・再帰的にダウンロード
ncftp > get -R [target]    <- *が使える

2015年2月16日月曜日

[kdb] File Compression

ファイル圧縮に関するあれこれ

□ kxwiki - Cookbook/FileCompression
http://code.kx.com/wiki/Cookbook/FileCompression


□ contrib
Simon / compress - utilities for compressing kdb+ databases
Simon作成のcontribが便利そうなので、これを使うことにする。

・cutil.qの使い方(一部)
下部のsample sessionに記載の通り、infoテーブルを回しながら、
cinfo -> cuse -> cwrire -> cmv
と実行すればよい。cwriteでtdに指定したディレクトリに圧縮し、cmvで元のファイルを上書きしている。
圧縮済みのディレクトリを対象にしても再度圧縮することはなさそう。
cuse には何種類かバージョンが作成されている。gzipで圧縮する場合はcusegz、 kdb+ipcで圧縮する場合はcusekxを選べばよい。なお、32bit版kdb+でgzip圧縮を使う場合は、32bit版zlibが必要なので注意。(インストール方法のページ参照)
複数の日付・テーブル・カラムを対象にする場合のやり方は、build a bulk request として最下部に記載されている。カラムとかを可変にしたかったので、以下の関数を作成。
//
// 指定ディレクトリ以下をすべて圧縮します。
//
compressAll: {[fromdir;tempdir]
    // prepare
    partitionList: key fromdir:hsym fromdir;
    partitionList: partitionList where partitionList <> `sym;
    tableList: key ` sv fromdir,partitionList[0];
    tempdir: hsym tempdir;
    
    // execute
    {[d;td;partition_table]
        p: partition_table[0];
        t: partition_table[1];
        info:cinfo[d;td;p;t];
        /info:cusekx info;
        info:cusegz info;
        info:cwrite info;
        cmv info:cvalidate info;
    }[fromdir;tempdir;] each cross[partitionList;tableList]
  };

// example
/ compressAll[`:/data/kdb/xxx;`:/data/kdb/work]


・圧縮状態の確認
圧縮状態の一括で確認する方法が見つからなかったので、関数を作成してみた。
グローバルオブジェクトを作成してしまうのは、まあそんなもので。
//
// 指定ディレクトリ以下のすべての圧縮状態を取得します。
//
showCompressionInfo: {[dir]
    CompressStatus:: ([]date:`$();table:`$();col:`$();size:`long$();compressedLength:`long$();uncompressedLength:`long$();algorithm:`int$();logicalBlockSize:`int$();zipLevel:`int$());

    partitionList: key dir:hsym dir;
    partitionList: partitionList where partitionList <> `sym;
    tableList: key ` sv dir,partitionList[0];
    
    {[dir;partition_table]
        partition: partition_table[0];
        table: partition_table[1];
        // column list
        colList: key ` sv dir,partition,table;
        // remove .d
        colList: colList where colList <> `.d;
        // get compresstion statistics
        CompressStatus,:
        {[dir;partition;table;col]
            file: ` sv dir,partition,table,col;
            size: hcount file;
            res: `date`table`col`size`compressedLength`uncompressedLength`algorithm`logicalBlockSize`zipLevel!(partition;table;col;size;0Nj;0Nj;0N;0N;0N);
            res,: -21! file;
            select date, table, col, size, `long$compressedLength, `long$uncompressedLength, `int$algorithm, `int$logicalBlockSize, `int$zipLevel from res
        }[dir;partition;table;] each colList;
    }[dir;] each cross[partitionList;tableList];
    
    // return compression status
    :CompressStatus
  };

// example
/ showCompressionInfo[`:/data/kdb/xxx]

2015年2月2日月曜日

[kdb] Q Math Library (qml) (1) インストール

Q Math Library の使い方メモ
・配布元
http://althenia.net/qml


□インストール
以下は32bitのフリー版kdb向けの説明

(1) パッケージのDL
(2) 32bitコンパイラーのインストール
※先に(3)を実行してエラーになったら必要なものをインストールする感じで

# gcc 
yum install libgcc.i686
# gfortran
yum install libgfortran.i686

# LAPACK(BLAS) ※無くてもよかったっぽい
yum install lapack

ここは結構はまりました。qmlのconfigureではちゃんと動くコンパイラーをインストールしてくれとしか表示されないので。 gccとgfortranの32bitコンパイラーがインストールできているか確認するには、それぞれ簡単なプログラムをコンパイルしてみることをお勧めします。
# gcc
gcc -m64 -o ctest64 ctest.c
gcc -m32 -o ctest32 ctest.c

# gfortran
gfortran -m32 test.f90 -o test32
gfortran test.f90 -o test64

(3) インストール
configureファイルを見るとわかりますが、各種オプションが設定できます。
QHOMEが設定されている場合は、QHOMEのkdbのバージョンを見てコンパイルするビット数を自動で、QHOMEが無い場合はOSのバージョンを見るようです。
フリー版のkdbを使っている場合は32bitで、OSは通常64bitなので、必要があればオプションで32bitになるように設定してください。

# BLASとLAPACKのあたりは適宜
# ./configure --with-blas=/usr/lib64/libblas.so.3.4.2 --with-lapack=/usr/lib64/liblapack.so.3.4.2
./configure --build-blas
make
make test
make install

ちなみに、make install ではqml.qとqml.soがコピーされます。例↓
$ make install
・・・
cp qml.so '/opt/kdb/q'/l32/
cp qml.q  '/opt/kdb/q'/
・・・
(4) 実行
q) \l qml.q
q) .qml
     | ::
dll  | `qml
...
q).qml.nicdf .25 .5 .975                  / normal distribution quantiles
-0.6744898 0 1.959964



・おまけ
Cとfortranのサンプルソース
### CでHello world

#include 
int main(void){
    printf("Hello, World!\n");
    return 0;
}

### FortranでHello world
program main
    print *, 'Hello World!'
end



※最後の行はなぞ。なぜか表示される。。。