爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 11475|回复: 7

[其他] ncl调用自定义function

[复制链接]

新浪微博达人勋

发表于 2016-8-26 10:43:58 | 显示全部楼层 |阅读模式

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

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

x
最近想把以前的程序改写下,所以想到了ncl自带的function功能,做了一个简单的测试程序。开始放置于主程序的路径下,但怕找不到这个函数,就在$NCARG_ROOT/lib/ncarg/nclscripts/csm路径下又放置了一份子程序的代码。但从运行结果看,似乎还是没能找到这个函数,我参考了一下调用contributed的函数调用方式,调用方式应该是没有问题的。所以请教该如何修改程序。

子程序myfunc.ncl的内容为
undef ("foo")
function my_func(x:numeric,opt:logical)
local dimx,rankx,xx
begin

    dimx = dimsizes(x)
    rankx = dimsizes(dimx)
    if(typeof(x).eq."float")then
        if(opt.and.isatt(opt,"scale"))then
            xx = x*opt@scale
        else
            xx = x
        end if
    else
        return(x)
    end if
end


主程序main.ncl的内容为:
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/myfunc.ncl"
begin


opt = True
opt@scale = 10
opt@add = 1000
opt@wgts = (/0.25,0.50,0.25/)
;opt@a3d = array_3D


data=(/1.0,2.0,3.0/)
var=data

print(var)
;printVarSummary(opt)
scale_var= my_func(var,opt)    ;调用子程序,但这一行出问题了,为第20行
print(scale_var)
end


错误提示为:
fatal:Illegal right-hand side type for assignment
fatal:["Execute.c":8578]:Execute: Error occurred at or near line 20 in file main.ncl


密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-8-31 09:59:02 | 显示全部楼层

回帖奖励 +2 金钱

楼主的问题解决了没有,我也想调用一下自己的函数,但是也出现问题
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-8-31 10:00:35 | 显示全部楼层
本帖最后由 zongqing123 于 2018-6-19 12:45 编辑

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"         
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"            
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
load "./ftest.ncl"
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-11-29 22:19:53 | 显示全部楼层
修改后的代码如下:
(1)建立一个主程序,名为try.main.ncl,内容如下:
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "./try.func.ncl"
begin


opt = True
opt@scale = 10

data=(/1.0,2.0,3.0/)

;printVarSummary(opt)
unpacked_values= my_func(data,opt)
;myfunction is used to multiply  the data by 10(opt@scale)
print(unpacked_values)
end

(2)建立一个子程序,名为try.func.ncl,内容如下:


load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;you don't need to do tihs(load libraries)if your don't need to use the  Built in function of  NCL


undef ("my_func")  ;undefine one function name in one time
undef ("my_func2") ;you don't need to do tihs if your function name is not a Built in function.


;====================================================================================================
;this function canbe used to get the true unpacked value
;(unpacked values) = scale_factor*(packed value)


function my_func( x[*]:numeric , opt :logical)  

;define the parameters you need,in which dimension must be declare
;the declare should include all the varibles(include demension and type) you need to pass
;numeric contains double,flost,long,integer,short,and byte et. al.

local dimx,xx     ;declare the local varibles you want to use(not a must,but recomended)
begin

    if(typeof(x).eq."float")then
        if(opt.and.isatt(opt,"scale"))then
            xx = x*opt@scale
            return(xx)        ;note return is a must,because function procedure need return values
        else
            xx = x
            return(xx)        
        end if
    end if
end



密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-11-29 22:23:47 | 显示全部楼层
关键点有3:
其一:实参和虚参的维数必须一致,否则出现:“Illegal right-hand side type for assignment”的问题提示
其二:必须要保证function具备返还值,否则无法调用,因为你的格式是:reture_value=function(a,b)
其三:你所书写的function只需要指定正确的路径即可,无需放在特定的文件夹下。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-11-29 22:27:22 | 显示全部楼层
此外,关于return values:
(1)you can reture a varible which ony contains value, like return(a),
(2)you can retrun a array, like retrun(a(:)) or retuan(a), where a=a(1:10)   
(3)you can return a varible or arry, which contains some meteinformation ,such as units

此外,你可以在一个文件里面同时定义主程序和子程序,并执行之。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-11-10 09:45:06 | 显示全部楼层
学到了!!!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2019-8-8 17:24:40 | 显示全部楼层
本帖最后由 luwen 于 2019-8-8 17:26 编辑

环境变量中加载export NCL_DEF_SCRIPTS_DIR=/your/scripts/path
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

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