我一直在使用 python 包xgrads来解析和读取带有后缀的描述符文件,.ctl
该文件描述了由 GrADS(网格分析和显示系统)提供的原始二进制 3D 数据集,GrADS 是一种广泛使用的软件,可轻松访问、操作和可视化地球科学数据。我一直在使用以下代码将二进制数据读入xarray.Dataset
.
from xgrads import open_CtlDataset
dset = open_CtlDataset('./ur2m_eta40km_2001011312.ctl')
# print all the info in ctl file
print(dset)
<xarray.Dataset>
Dimensions: (time: 553, lat: 36, lon: 30)
Coordinates:
* time (time) datetime64[ns] 2001-01-13T12:00:00 ... 2001-05-31T12:00:00
* lat (lat) float32 -21.2 -20.8 -20.4 -20.0 -19.6 ... -8.4 -8.0 -7.6 -7.2
* lon (lon) float32 -47.8 -47.4 -47.0 -46.6 ... -37.4 -37.0 -36.6 -36.2
Data variables:
ur2m (time, lat, lon) float32 dask.array<chunksize=(1, …
Run Code Online (Sandbox Code Playgroud) 我正在运行一个模型并将模型输出写入二进制文件(GrADS *gra 文件),例如:
integer,parameter :: nvar =3 ,& !number of variables to be written to file
nx=10,ny=10,& !number of girdboxes in lat & long
nt = 5
integer :: it, & ! loop counter
irec ! Record number
real :: var1(nx,ny), var2(nx,ny),var3(nx,ny)
OPEN(30,file='Outfile.gra',action='write',form='unformatted',access='direct',&
recl=4*nVar*nx*ny,status='replace')
!loop over timesteps
it = 1, nt
irec = irec + 1
WRITE(1,rec=irec) Var1(:,:),Var2(:,:),Var3(:,:)
enddo
Run Code Online (Sandbox Code Playgroud)
该文件可以在 GrADS 中读取,*ctl 文件如下所示
dset /mypath/Outfile.gra
title MyTitle
options little_endian
xdef 10 linear 1 1
ydef 10 linear 1 1
zdef …
Run Code Online (Sandbox Code Playgroud) 我在将数据从GrADS导出到.csv文件时遇到了真正的困难,尽管它应该非常简单.该文件来自与亚洲降雨有关的APHRODITE项目.基本上我可以使用以下方法将此文件读入GrADS:
open d:/aphro/aphro.ctl
Run Code Online (Sandbox Code Playgroud)
它告诉我:
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
Run Code Online (Sandbox Code Playgroud)
如果我执行:
q ctlinfo
Run Code Online (Sandbox Code Playgroud)
它还告诉我,我有三个变量:
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
Run Code Online (Sandbox Code Playgroud)
好的,现在我想做的就是在.csv文件(或.txt)文件中生成一个列表,其中包含以下信息:
Precipitation Lon Lat Time(date)
Run Code Online (Sandbox Code Playgroud)
这听起来很容易,但我无法做到.一种方法是使用:
fprintf precip d:/output.csv %g 1
Run Code Online (Sandbox Code Playgroud)
这给了我一个.csv文件,其中包含当天在一个长列中的整个数据(这就是我想要的).我也可以为lon和lat在不同的文件中做同样的事情并将它们组合起来.问题是输出文件需要很长时间 - …