[ncl-talk] avoiding loops

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Sun Sep 22 18:30:52 MDT 2019


Write_matrix is strictly for output to only a text file, or Unix standard
output.  There is no internal write capability.  However, you can simulate
internal writes by using one or more temporary files.

Read the formatted lines back in from a temp file with asciiread
("tmp.txt", -1, "string").  This gives you a 1-D array of strings, one full
line for each of your grid points.  Then repeat for each of your other
variables.  This will efficiently give you arrays of full formatted line
strings for several variables.

You should then be able to join these line strings together, however you
have in mind.  Also, now that you are operating on strings, you can add
text columns at the beginning or end of each string, if this would be of
any help.  For example:

long_lines = "precip," + p_lines(:) + ",Tmin," + tmin_lines(:) + ",Tmax," +
tmax_lines(:)

This example is for joining horizontally.  You could also join vertically
by simply joining the string arrays, with or without interleaving.  There
are many variations on this theme, depending on how you want your final
output to be structured.


On Sun, Sep 22, 2019 at 5:29 PM Micah Sklut <micahs2005 at gmail.com> wrote:

> Instead of sending output to a file, is it possible to do a write_matrix
> formatting to 1d array?  I need to add more than 1 of these variables
> together, so if I can create 1d arrays for all of the variables and
> metadata, such as lat/lon, then I can use write_table to join everything
> together. When I'm re-ordering or reshaping the arrays, I'll just need to
> delete the old arrays, to free up memory.
>
> On Sun, Sep 22, 2019 at 1:51 PM Dave Allured - NOAA Affiliate <
> dave.allured at noaa.gov> wrote:
>
>> There is a mistake in the untested example that I sent.  Please change
>> the format spec from "384(f0.1,','),f0.1" to "383(f0.1,','),f0.1".  The
>> latter is correct for 384 numbers per line.  I split the format spec into
>> two parts so that there would not be a trailing comma at the end of each
>> line.
>>
>> Generally speaking, the format spec in write_matrix is specified for a
>> single output line.  It is repeated for each output line.  It is important
>> that the total item count in the format spec (383 + 1 in this case) exactly
>> matches the intended number of items per line.
>>
>>
>> On Sun, Sep 22, 2019 at 11:32 AM Micah Sklut <micahs2005 at gmail.com>
>> wrote:
>>
>>> Okay, thanks, Dave.  That sounds like what I need. I've always struggled
>>> with the formatting in NCL, but I'll give that a go and see where I get.
>>>
>>> On Sun, Sep 22, 2019 at 1:26 PM Dave Allured - NOAA Affiliate <
>>> dave.allured at noaa.gov> wrote:
>>>
>>>> Try write_matrix and the F0 format descriptor.  F0 format is part of
>>>> fortran 90 and up.  It is described in modern fortran documentation.  It is
>>>> like the common Fw.d format descriptor, except leading blanks to the left
>>>> of the decimal point are suppressed.
>>>>
>>>> Reshape your array from, e.g. (lat,lon,time), to a 2-D array in the
>>>> desired output order, (points,time).  Npoints = Nlats x Nlons.  Then a
>>>> single call to write_matrix should write the entire output file:
>>>>
>>>>     opt = True
>>>>     opt at fout = "temperature.csv"
>>>>     write_matrix (data_out, "384(f0.1,','),f0.1", opt)
>>>>
>>>> You can add arbitrary extra numeric columns to the output array, if it
>>>> would be helpful to have lats and lons, or other such metadata, on each
>>>> line of output.
>>>>
>>>>
>>>> On Sat, Sep 21, 2019 at 2:53 PM Micah Sklut via ncl-talk <
>>>> ncl-talk at ucar.edu> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I have a task to take GFS data and concatenate variables across all
>>>>> the forecast hours, for each grid point.
>>>>>
>>>>> For example, taking a temperature variable for each grid point, for
>>>>> all latitude and longitudes, and creating a string that represents the
>>>>> values for each forecast hour.
>>>>> So, if we have for latitude y, and longitude x, there would be a
>>>>> string value like "70.5,71.5,71.5,72.0,...nHours".  The end product will be
>>>>> a line for each grid point for the variables i"m looking at that will be
>>>>> written to a file to be imported into a database.
>>>>>
>>>>> Creating loops through all hours, latitudes, and longitudes will get
>>>>> the job done, but is expensive and was looking to see if there were any NCL
>>>>> functions that would help here.
>>>>>
>>>>> Thank you.
>>>>>
>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190922/2c3e3818/attachment.html>


More information about the ncl-talk mailing list