爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 366|回复: 2

使用NCL(6.6.2)生成netCDF4文件出现未定义变量

[复制链接]

新浪微博达人勋

发表于 2024-7-16 21:57:52 | 显示全部楼层 |阅读模式
NCL
系统平台: ubuntu 22.04
问题截图: -
问题概况: 使用NCL(6.6.2)生成netCDF4文件出现ncl开头的未定义变量,其值与已定义的变量存在重复。
我看过提问的智慧: 看过
自己思考时长(天): 8

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
由于EDGAR CO2排放的网格数据是二维的,需要加上时间维度才能被anthro_emis识别,所有我采用NCL来对nc文件进行处理,维度是加上了,但是最终处理出来的结果出现了很多ncl开头的未定义变量,希望能够将多余的变量去掉,在emissions维度那一栏显示(time.lat.lon)。生成ncl的脚本如下:

load "/usr/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "/usr/lib/ncarg/nclscripts/csm/contributed.ncl"
system("cd /home/lxy/Downloads/ANTHRO/data/edgar/")
input_file = "v8.0_FT2022_GHG_CO2_2022_TOTALS_emi.nc"
output_files = (/ "v8.0_FT2022_GHG_CO2_2022_TOTALS_emi_new.nc", \
                      "v8.0_FT2022_GHG_CO2_2023_TOTALS_emi_new.nc", \
                      "v8.0_FT2022_GHG_CO2_2024_TOTALS_emi_new.nc" /)
nc_in = addfile(input_file, "r")

lat = tofloat(nc_in->lat)
lon = tofloat(nc_in->lon)
emissions = nc_in->emissions
lat_dim = dimsizes(lat)
lon_dim = dimsizes(lon)
time_dim = 1

delete(nc_in)

begin
    dimnames = (/"lat", "lon", "time"/)
    dim_sizes = (/lat_dim,lon_dim,time_dim/)
    varnames = (/"lat", "lon", "time", "emissions"/)
    vartypes = (/"float", "float", "double", "float"/)

    years = (/2022,2023,2024 /)
    do iy = 0, dimsizes(years)-1
        year = years(iy)
        output_file = output_files(iy)

        system("rm -f " + output_file)  ; Ensure the output file does not already exist
        nc_out = addfile(output_file, "c")
        do i = 0, dimsizes(dimnames) - 1
            filedimdef(nc_out, dimnames(i), dim_sizes(i),False)
        end do

        do i = 0, dimsizes(varnames) - 1
                if (varnames(i) .eq. "emissions") then

                      nc_out->$varnames(i)$ = new((/dim_sizes(2), dim_sizes(0), dim_sizes(1)/), vartypes(i))
            else

                      nc_out->$varnames(i)$ = new(dim_sizes(i), vartypes(i))
                end if
          end do

        time_units = "hours since "+year+"-01-01 00:00:00"
        time_value = cd_inv_calendar(year, 1, 1, 0, 0, 0, time_units,0)
        nc_out->time = time_value

        emissions_var = new((/time_dim, lat_dim, lon_dim/), typeof(emissions))
        nc_out->emissions = emissions_var

        ; Set variable attributes
        nc_out->lat@units = "degrees_north"
        nc_out->lat@standard_name = "latitude"
        nc_out->lat@long_name = "latitude"
       
        nc_out->lon@units = "degrees_east"
        nc_out->lon@standard_name = "longitude"
        nc_out->lon@long_name = "longitude"

        nc_out->time@long_name = "time"
        nc_out->time@standard_name = "time"

        nc_out->emissions@units = "Tonnes"
        nc_out->emissions@long_name = "TOTALS"
        nc_out->emissions@description = "TOTALS"
        nc_out->emissions@substance = "CO2"
        delete(nc_out)
    end do
end

###############################################
生成的netcdf头文件如下:

netcdf v8.0_FT2022_GHG_CO2_2024_TOTALS_emi_new {
dimensions:
        lat = 1800 ;
        lon = 3600 ;
        time = 1 ;
        ncl3 = 1800 ;
        ncl4 = 3600 ;
        ncl5 = 1 ;
        ncl6 = 1 ;
        ncl7 = 1800 ;
        ncl8 = 3600 ;
        ncl9 = 1 ;
        ncl10 = 1 ;
        ncl11 = 1800 ;
        ncl12 = 3600 ;
variables:
        float lat(ncl3) ;
                lat:units = "degrees_north" ;
                lat:standard_name = "latitude" ;
                lat:long_name = "latitude" ;
        float lon(ncl4) ;
                lon:units = "degrees_east" ;
                lon:standard_name = "longitude" ;
                lon:long_name = "longitude" ;
        double time(ncl5) ;
                time:units = "hours since 2024-01-01 00:00:00" ;
                time:calendar = "standard" ;
                time:long_name = "time" ;
                time:standard_name = "time" ;
        float emissions(ncl6, ncl7, ncl8) ;
                emissions:_FillValue = 9.96921e+36f ;
                emissions:units = "Tonnes" ;
                emissions:long_name = "TOTALS" ;
                emissions:description = "TOTALS" ;
                emissions:substance = "CO2" ;
}




错误图片.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2024-7-18 23:16:23 | 显示全部楼层
你认为的错误变量是什么呢?表述的似乎不是很清晰。我猜测可能是认为ncl1—12这几个变量是你不要的。
处理该问题的方法是在输出的变量之前,赋予维度信息,具体如下:
lat!0="lat"
lat&lat= lat
lon和time不赘述
emissions!0="time"
emissions!1="lat"
emissions!2="lon"
emissions&time=time
emissions&lat=lat
emissions&lon=lon
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2024-7-19 13:14:24 | 显示全部楼层
张如也 发表于 2024-7-18 23:16
你认为的错误变量是什么呢?表述的似乎不是很清晰。我猜测可能是认为ncl1—12这几个变量是你不要的。
处理 ...

是的,我没表述清,ncl开头的变量都是冗余的,不需要。我试一下这个方法先,感谢
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表