登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 151170080 于 2019-9-12 09:59 编辑
最近,有非大气专业的同学用到气象数据,应其要求非要将nc文件中的变量提转换为二维txt文件才能处理,我写了个脚本实现,分享一下!
首先贴一下这个nc文件的变量信息:short sp ( time, latitude, longitude ) scale_factor : 0.6499881858185952 add_offset : 79875.36836528209 _FillValue : -32767 missing_value : -32767 units : Pa long_name : Surface pressure standard_name : surface_air_pressure 将这个变量,保留经纬度维度,按时间提取。
贴一下主体程序:
begin
fpath = "/Users/zjkang/ncl/ecmwf/"+fn_head+months(t)+"/"+fn_head+months(t)+".nc"
f = addfile(fpath,"r")
; Translate gregorian time into UT time
time = f->time
ut_time = cd_calendar(time,3)
;get the value for nc files
sp = f->sp
nt = dimsizes(ut_time)
; Translate a nc value into the normal value when the value@add_offset and value@scale_factor exist
add_offset = sp@add_offset
scale_factor = sp@scale_factor
;Take the time as the output-file name and
do i=0,nt-1
sp_normal = sp(i,:,:)*scale_factor + add_offset
opt = True
opt@fout = "sp_"+ut_time(i)+".txt"
write_matrix(sp_normal,"41f10.2",opt)
delete([/sp_normal,opt/])
end do
delete([/fpath,f,time,ut_time,nt,sp,add_offset,scale_factor/])
end
这个程序主要用到了write_matrix这个函数,这个函数不仅可以将格式化的二维数组进行标准输出(opt=False,输出到屏幕上),还可以输出到文件中,它与write_table这个函数,各有各的特点。可以自行比较。
|