[ncl-talk] NCL with systemfunc
Dave Allured - NOAA Affiliate
dave.allured at noaa.gov
Sun Jan 17 22:41:52 MST 2021
Systemfunc can do this, but it would be messy. Let's try something in
native NCL.
It would be helpful to have a custom function to make fixed length
strings. There are enough tricks and nuances that it is easier to just
write this up, rather than explain how to do it. You can put this function
into your private library, and use it any time you need more fixed length
strings.
undef ("fixed_left")
function fixed_left (str, length)
local blanks
begin
blanks = new (length, "character")
blanks = tochar (" ")
return (str_get_cols (str+tostring(blanks), 0, length-1))
end
begin
longi = -104.67
lati = 39.85
string1 = "DENVER/STAPLETON INT., CO. / U.S.A."
string2 = sprintf ("%20.5f", longi) \
+ sprintf ("%20.5f", lati) \
+ fixed_left (string1, 40)
print ("[" + string2 + "]")
end
On Sun, Jan 17, 2021 at 8:22 PM 时光足迹 via ncl-talk <ncl-talk at mailman.ucar.edu>
wrote:
> Hi,
> I have trouble writing formatted string using NCL.
>
> As I typed: system("printf "+dq+"%-40s"+dq+" "+ dq+"DENVER/STAPLETON
> INT., CO. / U.S.A."+dq), output showed DENVER/STAPLETON INT., CO. / U.S.A. ;
>
> I wanted to store the output to a variable, so I changed the command to:
> print(systemfunc("printf "+dq+"%-40s"+dq+" "+ dq+"DENVER/STAPLETON INT.,
> CO. / U.S.A."+dq)), however, I got missing.
>
> What the matter with 'systemfunc' here?
>
> ---------------------------------------------------------------------
>
> Here are Fortran codes to write formatted string:
>
> WRITE(fout, '(2f20.5,A40)') longi, lati, STRING1
>
> I tried write_table to write, but spaces will be automaticlly inserted
> between different kind of varibles.
>
> Is there any efficient way to write formatted string the same as
> Fortran?
>
> Thanks,
>
> HUANG
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20210117/566b5aad/attachment.html>
More information about the ncl-talk
mailing list