简体   繁体   English

如何将数据从GrADs导出到.csv文件或从NetCDF导出到.csv?

[英]How can I export data from GrADs to a .csv file or from NetCDF to .csv?

I'm having real difficulty with exporting data from GrADS to a .csv file although it should be really easy. 我确实很难将数据从GrADS导出到.csv文件,尽管这确实很容易。 The file in question is from the APHRODITE project relating to rainfall over Asia. 有问题的文件来自与亚洲降雨有关的APHRODITE项目。 Basically I can read this file into GrADS using: 基本上,我可以使用以下命令将此文件读入GrADS:

open d:/aphro/aphro.ctl

and it tells me that: 它告诉我:

Data file d:/aphro/APHRO_MA_025deg_V1101R2.%y4 is open as file 1
Lon set to 60.125 149.875
Lat set to -14.875 54.875
Lev set to 1 1
Time values set: 1961:1:1:0 1961:1:1:0
E set to  1 1

If I execute: 如果我执行:

q ctlinfo

it also tells me that I have three variables: 它还告诉我,我有三个变量:

precip 1 0 daily precipitation analysis
rstn 1 0  ratio of 0.05 degree grids with station
flag 1 0 ratio of 0.05 degree grids with snow

Okay, now all I want to do is produce a list in a .csv file (or .txt) file with the following information: 好的,现在我要做的就是在.csv文件(或.txt)文件中生成一个包含以下信息的列表:

Precipitation   Lon    Lat   Time(date)

It sounds really easy but I just can't do it. 听起来真的很容易,但我做不到。 One method is to use: 一种方法是使用:

fprintf precip d:/output.csv %g 1

This gives me an .csv file with the entire data for that day in one long column (which is what I want). 这给了我一个.csv文件,该文件将当天的全部数据放在一个长列中(这是我想要的)。 I can also do the same for lon and lat in different files and combine them. 我也可以在不同的文件中对lon和lat进行相同的处理,并将它们组合在一起。 The problem is that this takes for ages for the output file - it is much faster if you don't mind lots of columns but this becomes a pain to manage. 问题在于,这需要花很长时间才能输出文件-如果您不介意过多的列,则速度会更快,但是这变得难以管理。 Basically, this method is too slow. 基本上,这种方法太慢了。

Another method is to export the data as a NetCDF file by: 另一种方法是通过以下方式将数据导出为NetCDF文件:

Set sdfwrite -4d d:/output.nc
define var = precip
sdfwrite precip

This then very quickly writes a file called output.nc which contains all the data I need. 然后,这很快就会写入一个名为output.nc的文件,其中包含我需要的所有数据。 Using RI can then read all the variables individually eg 然后,使用RI可以分别读取所有变量,例如

f <- open.ncdf("D:/aphro/test.nc")
A <- get.var.ncdf(nc=f,varid="time")
B <- get.var.ncdf(nc=f,varid="rain")
D <- get.var.ncdf(nc=f,varid="lon")
E <- get.var.ncdf(nc=f,varid="lat")

But what I want is to make an output file where each row tells me the time, rain amount, lon and lat. 但是我想要的是制作一个输出文件,每一行告诉我时间,雨量,经度和纬度。 I tried rbind but it doesn't associate the correct time(date) with the right rain amount, and similarly messes up the lon and lat as there are hundreds of thousand of rain data but only a few dates and only 360 lon points and 280 lat points (ie the rain data is a grid of data for each day over several days). 我尝试了rbind,但是它没有将正确的时间(日期)与正确的降雨量相关联,并且类似地弄乱了lon和lat,因为有成千上万的降雨数据,但是只有几个日期,只有360 lon点和280纬度点(即降雨数据是几天内每天的数据网格)。 I'm sure this should be easy but how to do it? 我敢肯定这应该很容易,但是怎么做呢?

Please help 请帮忙

Tony 托尼

Up to my knowledge, you can change the GrAD file to NetCDF file by using climate data operator and R together. 据我所知,可以一起使用气候数据运算符和R将GrAD文件更改为NetCDF文件。 Details can be found here . 详细信息可以在这里找到。 Further a NetCDF file can be converted in to a .csv file. 此外,可以将NetCDF文件转换为.csv文件。 For this I am providing a dummy code. 为此,我提供了一个伪代码。

library(ncdf)
nc <- open.ncdf("foo.nc")             #open ncdf file and read variables
lon <- get.var.ncdf(nc, "lon")         # Lon lat and Time
lat <- get.var.ncdf(nc, "lat")
time <- get.var.ncdf(nc, "time")
dname <- "t"                         # name of variable which can be found by using print(nc)
nlon <- dim(lon)
nlat<- dim(lat)
nt<- dim(time)
lonlat <- expand.grid(lon, lat)    # make grid of given longitude and latitude 
mintemp.array <- get.var.ncdf(nc, dname)
dlname <- att.get.ncdf(nc, dname, "long_name")
dunits <- att.get.ncdf(nc, dname, "units")
fillvalue <- att.get.ncdf(nc, dname, "_FillValue")
mintemp.vec.long <- as.vector(mintemp.array)
mintemp.mat <- matrix(mintemp.vec.long, nrow = nlon * nlat, ncol = nt)
mintemp.df <- data.frame(cbind(lonlat, mintemp.mat))
options(width = 110)
write.csv(mintemp.df, "mintemp_my.csv")

I hope, it explains your question. 希望它能解释您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM