2015年3月11日水曜日

Hyetgraph TRMM 3B42 with R

RにてTRMMの3B42ファイルから、特定の地点のハイエトグラフを作成しました。
http://disc.sci.gsfc.nasa.gov/giovanniからダウンロードできる3B42はHDF4なので、h4toh5convertでHDF5に変換します。
> for %i in (*.hdf) do h4toh5convert %i
以降Rで作業します。
HDFファイルを扱うために、rhdf5ライブラリを利用します。rhdf5パッケージについては、以下ドキュメントがあります。
http://www.bioconductor.org/packages/release/bioc/vignettes/rhdf5/inst/doc/rhdf5.pdf

> library(rhdf5)

hdfファイルの構造を確認します。
> h5ls("3B42_daily.2013.08.27.7.G3.h5")

TRMM 3B42のHDFファイルには、西経180°南緯50°から、西経180°北緯50°へ、続いて西経179.75°南緯50°から北へ、といった順で、経緯0.25°ずつデータが格納されています。そのため、取得したい地点の緯度経度から、データの位置を求めます。

> origin.lon <- -180
> origin.lat <- -50
> resolution.lon <- 0.25
> resolution.lat <- 0.25

> in.data.dir             <- "in/3B42_daily/"
> in.data.name.common     <- "3B42_daily."
> in.data.name.common2    <- ".7.G3.h5"
> out.data.dir            <- "out/3B42_hyet/"
> in.data.name.hdf        <- "precipitation"

ハイエトグラフを作成したい地点の緯度経度のリストをテキストで準備しておきます。
> in.coodination.filename <- "in/3B42_daily/hyet_coodination.csv"

対象となる期間を指定します。
> target.period.year.begin <- 2012
> target.period.year.end   <- 2014
> target.period.begin.date <- "06-01"
> target.period.end.date   <- "09-30"

サイクルの日数の計算
> dulation <- as.numeric(as.Date(paste(target.period.year.begin,"-",target.period.end.date, sep="")) - as.Date(paste(target.period.year.begin,"-",target.period.begin.date, sep="")) + 1 )
> target.period.years <- target.period.year.end - target.period.year.begin + 1

期間の日付を作成
> target.date <- matrix(data = , nrow = dulation, ncol = target.period.years)
> for(y in 1:target.period.years){
>   day.begin <-  as.Date(paste(target.period.year.begin + y -1,"-",target.period.begin.date, sep=""))
>   day.end   <-  as.Date(paste(target.period.year.begin + y -1,"-",target.period.end.date, sep=""))
>   target.date[,y] <- day.begin:day.end
>   }

期間のファイル名を作成
>target.filenames <- matrix(paste(in.data.dir, in.data.name.common, format(as.Date(target.date, origin="1970/1/1"), "%Y.%m.%d"),
>                                 in.data.name.common2, sep=""),  nrow = dulation, ncol = target.period.years)

緯度経度のリストを読み込み
> location.table <- read.table(in.coodination.filename, header = TRUE, sep = ",")

データの位置の設定
> tada.matrix.row <- (location.table$Lon - origin.lon) %/% resolution.lon + 1
> tada.matrix.col <- (location.table$Lat - origin.lat) %/% resolution.lat + 1

ファイルの読み込み
> precipitation.table <- data.frame(location.table)
> for (y in 1:target.period.years){
>   for (d in 1:dulation){
>     precipitation.day <- h5read(target.filenames[d,y], name = in.data.name.hdf)[matrix(c(tada.matrix.row,tada.matrix.col),ncol = 2)]
>     precipitation.table  <- cbind(precipitation.table, precipitation.day)
>   }
> }
> for (y in 1:target.period.years){
>   precipitation.table <- cbind(precipitation.table, apply(precipitation.table[ncol(location.table)+dulation * (y - 1) + (1:dulation)],MARGIN=1,sum))
> }
> colnames(precipitation.table) <- c(colnames(location.table),format(as.Date(target.date, origin="1970/1/1"), "%Y-%m-%d"), target.period.year.begin:target.period.year.end)

グラフの描写及び書き出し
> for (p in 1:nrow(location.table)){
>   win.graph(width = 700, height = 500)
>   for (y in 1:target.period.years){
>     plot(as.Date(target.date[,y], origin="1970/1/1"), precipitation.table[ncol(location.table)+dulation * (y - 1) + (1:dulation)][p,],
>          type="l", lwd=2, ylim=c(0,100), xlab="Date", ylab="Precipitation (mm)", main=paste("Precipitation", location.table$SN[p], location.table$Name[p]),
>          col=rainbow(target.period.years)[y])
>     par(new="TRUE")
>   }
>   legend("topleft", legend=paste((target.period.year.begin:target.period.year.end),"Total",
>                                  sprintf("%5.1f",precipitation.table[p,as.character(target.period.year.begin:target.period.year.end)]), "mm"),
>          col=rainbow(target.period.years), lty=1, lwd=2, par(mar = c(1, 1, 1, 1)))
>   dev.copy(png, file=paste(out.data.dir,"plot_", location.table$SN[p], "_", location.table$Name[p], ".png", sep=""),width=700, height=500)
>   dev.off()
>   graphics.off()
> }

データの書き出し
> write.table(x = precipitation.table, paste(out.data.dir,"Precipitation.csv", sep=""), quote=FALSE,row.names=FALSE, sep="\t")

2015年3月4日水曜日

Excelのマクロによるスクロール

エクセルの同じブックにある全てのワークシートにおいて、特定のセルをアクティブにして、かつスクロール位置を指定するためのマクロ。標準モジュールに以下を貼り付けて実行する。 (EXCEL 2007)
Sub allsheetscroll()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        Application.Goto Reference:=ws.Range("A1"), Scroll:=True
        ActiveWindow.ScrollRow = 1
        ActiveWindow.ScrollColumn = 1
    Next
End Sub