如何使用R将包含多级建模数据的NetCDF文件(.nc)转换为.csv文件?

时间:2016-03-21 00:23:09

标签: r excel csv netcdf multi-level

我正在研究NetCDF文件,其中我的.nc文件中有多级模型数据。我的目标是将其转换为.csv或.txt文件,我可以通过Excel读取它。我正在关注本网站的说明http://geog.uoregon.edu/bartlein/courses/geog607/Rmd/netCDF_01.htm

它适用于单级数据模型,但我想将此代码用于2级或更高级别的数据,并且在完成一些R操作后我也得到了.csv文件但是无论我得到它还是没有显示实际结果,我的实现结果与实际结果之间存在一定的温度差异。我正在使用这个R代码进行多层建模。

>library(chron)
> library(RColorBrewer)
> library(lattice)
> library(ncdf4)

> getwd()
[1] "/Users/arnob_t78"
> workdir <- "/Users/arnob_t78/Angelika"
> setwd(workdir)

> ncname <- "60_59"
> ncfname <- paste(ncname, ".nc", sep = "")
> dname1 <- "t"
> ncin <- nc_open(ncfname)
> print(ncin)
File 60_59.nc (NC_FORMAT_64BIT):

 1 variables (excluding dimension variables):
    short t[longitude,latitude,level,time]   
        scale_factor: 0.00097129842383799
        add_offset: 262.636706123249
        _FillValue: -32767
        missing_value: -32767
        units: K
        long_name: Temperature
        standard_name: air_temperature

 4 dimensions:
    longitude  Size:97
        units: degrees_east
        long_name: longitude
    latitude  Size:55
        units: degrees_north
        long_name: latitude
    level  Size:2
        long_name: model_level_number
    time  Size:155   *** is unlimited ***
        units: hours since 1900-01-01 00:00:0.0
        long_name: time
        calendar: gregorian

2 global attributes:
    Conventions: CF-1.6
    history: 2016-03-16 17:53:56 GMT by grib_to_netcdf-1.14.5: grib_to_netcdf /data/data01/scratch/_mars-atls00-    95e2cf679cd58ee9b4db4dd119a05a8d-BfvdSl.grib -o /data/data01/scratch/_grib2netcdf-atls09-95e2cf679cd58ee9b4db4dd119a05a8d-M7sA4v.nc -utime

> lon <- ncvar_get(ncin, "longitude")
> nlon <- dim(lon)
> head(lon)
[1] -27.00 -26.25 -25.50 -24.75 -24.00 -23.25

> lat <- ncvar_get(ncin, "latitude")
> nlat <- dim(lat)
> head(lat)
[1] 73.50 72.75 72.00 71.25 70.50 69.75

> level <- ncvar_get(ncin, "level")
> nlevel <- dim(level)
> head(level)
[1] 59 60

> t <- ncvar_get(ncin, "time")
> tunits <- ncatt_get(ncin, "time", "units")
> nt <- dim(t)
> head(t)
[1] 973014 973038 973062 973086 973110 973134

> print(c(nlon, nlat, nlevel, nt))
[1]  97  55   2 155

> tmp.array1 <- ncvar_get(ncin, dname1)
> dlname1 <- ncatt_get(ncin, dname1, "long_name")
> dunits1 <- ncatt_get(ncin, dname1, "units")
> fillvalue <- ncatt_get(ncin, dname1, "_FillValue")
> dim(tmp.array1)
[1]  97  55   2 155

> title <- ncatt_get(ncin, 0, "title")
> institution <- ncatt_get(ncin, 0, "institution")
> datasource <- ncatt_get(ncin, 0, "source")
> references <- ncatt_get(ncin, 0, "references")
> history <- ncatt_get(ncin, 0, "history")
> conventions <- ncatt_get(ncin, 0, "Conventions")

> tmp.vec.long1 <- as.vector(tmp.array1)
> length(tmp.vec.long1)
[1] 1653850

> tmp.mat1 <- matrix(tmp.vec.long1, nrow = nlon * nlat, ncol = nlevel)
> dim(tmp.mat1)
[1] 5335    2

> head(na.omit(tmp.mat1)) 
     [,1]     [,2]
[1,] 254.3496 254.5176
[2,] 253.5764 253.7066
[3,] 252.9684 252.4604
[4,] 252.6692 250.9879
[5,] 252.7304 249.6252
[6,] 253.1054 248.7433

> tlonlat1 <- expand.grid(t,lon, lat)
> tmp.df02t <- data.frame(cbind(tlonlat1, tmp.mat1))
> names(tmp.df02t) <- c("t", "lon", "lat", "t59", "t60")
> head(na.omit(tmp.df02t, 20))
       t lon  lat      t59      t60
1 973014 -27 73.5 254.3496 254.5176
2 973038 -27 73.5 253.5764 253.7066
3 973062 -27 73.5 252.9684 252.4604
4 973086 -27 73.5 252.6692 250.9879
5 973110 -27 73.5 252.7304 249.6252
6 973134 -27 73.5 253.1054 248.7433

> csvfile <- "60_59_data.csv"
> write.table(na.omit(tmp.df02t), csvfile, row.names = FALSE, sep = ",")

问题是在完成操作R代码后我在t59和t60实现的值实际上并不是确切的结果,在-10到20开尔文的温度范围内有一个分数。你能告诉我我错过了什么或做错了吗?我希望你能理解这个场景。

以下是一些图像,显示了我在R中所做的以及样本结果。

i guess here i am missing something

0 个答案:

没有答案