[ncl-talk] how to separate data with tab using function write_matrix

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Tue Apr 24 21:21:21 MDT 2018


This omits the double loop.  It probably runs faster if the array is
large.  This also omits leading spaces (%0.1f), because they are not
usually wanted with tab formatted text files.

  do nr=0,nrow-1
        TabData(nr+1) = str_join (sprintf("%0.1f",x(nr,:)))
  end do

--Dave


On Tue, Apr 24, 2018 at 5:18 PM, Dennis Shea <shea at ucar.edu> wrote:

> The 'write_matrix' only handles numeric data types. No characters.
>
> --
>    diro  = "./"
>    filo  = "example.txt"
>    ptho  = diro +filo
>
> ;---Bogus data
>    nrow  = 5
>    ncol  = 7
>    x     = new((/nrow,ncol/),float, -999.9)
>    x     = 9.9
>
> ;---Tab character
>    tab = str_get_tab()
>
> ;---Array to hold data; Initialize
>    NROW       = nrow+1
>    TabData    = new( NROW, "string") ; , "No_FillValue")  ; extra 'row'
> for title
>    TabData    = ""
>
> ;---First a title (optional)
>    TabData(0) = "                         Title                        "
>
> ;---Loop over ech row (line) and appended values
>    do nr=0,nrow-1
>      do nc=0,ncol-1
>         TabData(nr+1) = TabData(nr+1) + tab + sprintf("%5.1f",x(nr,nc)) ;
> append
>      end do
>    end do
>
> ;---Create the file
>    system("/bin/rm -f "+ptho)
>    asciiwrite(ptho, TabData)
>
> ++++++++ OUTPUT ++++++++++++++
>
>                          Title
>           9.9     9.9     9.9     9.9     9.9     9.9     9.9
>           9.9     9.9     9.9     9.9     9.9     9.9     9.9
>           9.9     9.9     9.9     9.9     9.9     9.9     9.9
>           9.9     9.9     9.9     9.9     9.9     9.9     9.9
>           9.9     9.9     9.9     9.9     9.9     9.9     9.9
>
> On Mon, Apr 23, 2018 at 8:52 PM, wen <wenguanhuan at 163.com> wrote:
>
>> Hi Dennis,
>>   Thank you for help. I have check the suggested method. It didn't work.
>> I came up with  error message. Following is the ncl code and error message.
>> Do you have any other idea?
>>
>>  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"
>>
>> begin
>> x = new((/5,5/),float)
>> x = 9.9
>> opt = True
>> opt at fout = "test.txt"
>> opt at title  = "Normal"
>> tab = str_get_tab()
>> fmt = "5("+tab+",f4.1)"
>> write_matrix (x, fmt , opt)
>>  end
>>
>>  "writematrix.f", line 110: 1525-078 An extra comma was found in a
>> format specification.  The program will recover by ignoring the extra comma.
>> "writematrix.f", line 110: 1525-078 An extra comma was found in a format
>> specification.  The program will recover by ignoring the extra comma.
>> "writematrix.f", line 110: 1525-078 An extra comma was found in a format
>> specification.  The program will recover by ignoring the extra comma.
>> "writematrix.f", line 110: 1525-078 An extra comma was found in a format
>> specification.  The program will recover by ignoring the extra comma.
>> "writematrix.f", line 110: 1525-078 An extra comma was found in a format
>> specification.  The program will recover by ignoring the extra comma.
>>
>> --
>> Best regards,
>> Guanhuan Wen
>>
>> At 2018-04-24 00:43:52, "Dennis Shea" <shea at ucar.edu> wrote:
>>
>> https://www.ncl.ucar.edu/Document/Functions/Built-in/str_get_tab.shtml
>>
>> *UNTESTED*:  Lets say you want 10 numbers for each row with all numbers
>> preceded by a
>> *tab.*
>> *Note: *
>> *fout* String specifying the output file name. If not present, the data
>> is sent to standard out.
>> *The maximum length of fout is 96 characters.*
>>
>> I think the fortran character variable is set at 96
>>
>> *---*
>>
>> tab = *str_get_tab*()
>>
>> fmt = "10("+tab+",f7.1)"   ; clarity
>>
>> print("fmt="+fmt)
>>
>> opt = True
>>
>> opt at fout "./TEXT_with_TAB"        ; "./test/text_with_tab/"
>>
>> system("/bin/rm -f "+opt at fout)    ; rm any pre-existing file
>>
>> write_matrix (x,  fmt, False)
>>
>> Let ncl-talk know if this works.
>>
>> THX
>>
>> On Sun, Apr 22, 2018 at 8:20 PM, wen <wenguanhuan at 163.com> wrote:
>>
>>> Hi all,
>>>     I want to write a 2 dimensional data into a file using function
>>> write_matrix. I expect to separate data with character tab. How can I
>>> realize that?  Hope you can help. Thank you!
>>>
>>> --
>>> Best regards,
>>> Guanhuan Wen
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180424/45fb17d4/attachment.html>


More information about the ncl-talk mailing list