[ncl-talk] matching two files

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Sat Sep 12 14:48:05 MDT 2020


Here is a working demo.  Read the station ID's from the first file into a
local array.  Also read the station ID's and heights from the second file
into two other local arrays.  Create a new array for the new heights, and
the same size as the first ID array.  Then use the following method to
match ID's between arrays.  The new heights array is then written out to
the first file.

This program will work for either numeric or string station ID's.  The
first three statements below are to create dummy data for demo only.  Your
code to read the file arrays should replace these dummy data statements.
Also note that the "ind" function below will change size if there are any
duplicate ID's in file 2, so its output needs to be handled carefully, as
follows.

begin
  id1  = (/  3, 5, 7, 2, 1, 4, 6, 8, 7, 6 /)
  id2  = (/  1, 2, 3, 4, 4, 6, 7, 8 /)
  hgt2 = (/ 30,70,45,60,55,78,95,30 /)

  n1   = dimsizes (id1)
  hgt1 = new (n1, integer, -9)

  do i1 = 0, n1-1
    i2 := ind (id1(i1) .eq. id2(:))
    if (.not. ismissing (i2(0))) then
      hgt1(i1) = hgt2(i2(0))
    end if

    if (dimsizes (i2) .gt. 1) then
      print ("*** File 2 contains more than one ID = " + id1(i1))
      print ("    Assigning value for the first ID found in file 2.")
    end if

    if (ismissing (i2(0))) then
      print ("*** ID not found in file 2 = " + id1(i1))
      print ("    No value is assigned for this ID.")
    end if
  end do

  print ("ID1  =" + str_concat (sprinti ("%4i",  id1(:))))
  print ("Hgt1 =" + str_concat (sprinti ("%4i", hgt1(:))))
end

This demo makes the following output.  Note that -9 is a missing value in
the output array.

(0) *** ID not found in file 2 = 5
(0)    No value is assigned for this ID.
(0) *** File 2 contains more than one ID = 4
(0)    Assigning value from the first ID found in file 2.
(0)
(0) ID1  =   3   5   7   2   1   4   6   8   7   6
(0) Hgt1 =  45  -9  95  70  30  60  78  30  95  78


On Sat, Sep 12, 2020 at 11:08 AM Ehsan Taghizadeh via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Dear NCL-talk
> I hope you are doing great.
> My question is about how to match two files based on the same id from one
> column.
>
> I have two files with different columns and rows. However, each of them
> has a column including station ids. This column has same ids for two files
> but not in the same order or the same number. Based on the same ids in this
> column I want to pass a column, including height, from one file to the
> other one. I mean I want to pass height of the each station from file2 to
> file1.
> May I ask how I could find the correct height from file2 and write for the
> proper row in file?
> I want to ask only about the matching part.
>
> I hope I could explain the problem correctly.
> Any help will be appreciated.
>
> *Sincerely*
> *Ehsan*
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20200912/e384010f/attachment.html>


More information about the ncl-talk mailing list