[ncl-talk] reading station data
Dave Allured - NOAA Affiliate
dave.allured at noaa.gov
Fri Feb 6 11:35:38 MST 2026
To detect the position of the header line, choose a substring in that line
that (1) will be included exactly in every file that you want to read, and
(2) is very unlikely to be used anywhere else in any file. I like the
third header, "Day_of_Year", but most other headers would also be fine.
Then locate the line containing this substring.
iheader = str_match_ind (line(:) .eq. "Day_of_Year")
idata = iheader + 1
Then use these indices in place of the previous hard-coded line indices.
field_names = str_split(lines(iheader),delim)
mhs = str_get_field(lines(idata:),2,delim) ... etc.
If you want to add quality control, you can use ismissing(iheader) to check
for missing header line, or unexpected spelling. You might also want to
verify that your selected field names are actually the ones that you
expected.
The problem with the middle column is that you are using an integer format
code "%3.0i" to print, but you forgot to convert Day from string to
integer. I think you need to use toint(). Printing string with a numeric
format code is always good for chaos.
On Fri, Feb 6, 2026 at 10:07 AM Debasish Hazra <debasish.hazra5 at gmail.com>
wrote:
> Many thanks Dave. It worked fine. I think I need a way to detect the
> position automatically as I found it does not always start at line 7, that
> will be helpful.
> Also, I am trying to write the columns that I read into a csv file, but
> not getting right for the middle column. Any suggestions? Attached the code
> and output csv file as well.
>
> Debasish.
>
> On Fri, Feb 6, 2026 at 2:14 AM Dave Allured - NOAA Affiliate <
> dave.allured at noaa.gov> wrote:
>
>> Debasish, you have almost everything right, except for the position of
>> field names, and start of data. You have:
>>
>> field_names = str_split(lines(0),delim)
>>
>> But the field names are on line 7, not line 1. Change this to get the
>> field names from line 7. Since NCL uses zero-based indexing, the correct
>> line index is 7 - 1 = 6.
>>
>> field_names = str_split(lines(6),delim)
>>
>> Likewise, data lines start on the next line after the field names, at
>> line index 7. This should clear up the header errors:
>>
>> mhs = str_get_field(lines(7:),2,delim) ... etc.
>>
>> If you are processing a lot of data, then the start of data can change
>> between files. Can you have start of data hard coded at 7, or do you need
>> a way to detect the position automatically?
>>
>>
>> On Thu, Feb 5, 2026 at 9:42 AM Debasish Hazra <debasish.hazra5 at gmail.com>
>> wrote:
>>
>>> Thanks Dave and Dennis for your suggestions. I followed the examples and
>>> created a script to read the file I sent earlier, but it seems to be I am
>>> still getting some header information in the column I am trying to extract.
>>> Attached are output and script that was used, any suggestions to overcome
>>> this issue.
>>>
>>> Debasish.
>>>
>>> On Tue, Feb 3, 2026 at 5:50 PM Dave Allured - NOAA Affiliate <
>>> dave.allured at noaa.gov> wrote:
>>>
>>>> Debasish, that is a CSV file with header lines. Use *asciiread* to
>>>> read the entire file as strings. Then use string parsing functions such as
>>>> *str_split_csv* and/or *str_get_field* to get individual columns.
>>>> Examples *csv_3.ncl* and *csv4_ncl* are close to what you want to do.
>>>>
>>>> https://www.ncl.ucar.edu/Applications/read_csv.shtml
>>>>
>>>>
>>>> On Tue, Feb 3, 2026 at 12:38 PM Debasish Hazra via ncl-talk <
>>>> ncl-talk at mailman.ucar.edu> wrote:
>>>>
>>>>> Hi,
>>>>> I am trying to read station data with header and column of data, with
>>>>> COlumn lengths are not equal in files and have attached one example file in
>>>>> the attachment. Can someone help to point what ascii read command to be
>>>>> used to extract time and column information from these files.
>>>>> Thanks
>>>>> Debasish.
>>>>>
>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20260206/135afbbd/attachment.htm>
More information about the ncl-talk
mailing list