[ncl-talk] get index for A associated with B
Thomas Tobian
thomastobian at gmail.com
Wed Jun 10 14:50:11 MDT 2015
Just in case if you need to return all values in A which exist in B (just
a small modification from Alans)
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
A = (/1990,1997,1999,2002,2002,2005,2010,2010/)
B = (/1993,1997,2002,2004/)
A_ind = get1Dindex(B,A)
i=ind(.not.ismissing(A_ind))
r=A(i)
print(r)
Copyright (C) 1995-2015 - All Rights Reserved
University Corporation for Atmospheric Research
NCAR Command Language Version 6.3.0
The use of this software is governed by a License Agreement.
See http://www.ncl.ucar.edu/ for more details.
Variable: r
Type: integer
Total Size: 12 bytes
3 values
Number of Dimensions: 1
Dimensions and sizes: [3]
Coordinates:
(0) 1997
(1) 2002
(2) 2002
Cheers,
Thomas
On Wed, Jun 10, 2015 at 10:38 PM, Alan Brammer <abrammer at albany.edu> wrote:
> I'm not sure which version of ncl you're using but those 3 lines work for
> me in 6.2.x and 6.3.0.
> The documentation for get1Dindex explicitly states that repeated values
> are allowed and the first occurrence is returned.
> That error usually means that A_ind already exists and has a different
> size to the return value.
> To be sure you can use the := operator to redefine the variable.
>
>
> ncl
>
>
>
> ncl 0> A = (/1990,1997,1999,2002,2002,2005,2010,2010/)
>
> ncl 1> B = (/1993,1997,2002,2004/)
>
> ncl 2>
>
> ncl 3> A_ind = get1Dindex(A,B)
>
> ncl 4> print(A_ind)
>
>
>
> Variable: A_ind
>
> Type: integer
>
> Total Size: 16 bytes
>
> 4 values
>
> Number of Dimensions: 1
>
> Dimensions and sizes: [4]
>
> Coordinates:
>
> Number Of Attributes: 3
>
> _FillValue : -2147483647
>
> index_info : Out of nWant=4 : multiple index occurrences=1
>
> nMultInd : 1
>
> (0) -2147483647
>
> (1) 1
>
> (2) 3
>
> (3) -2147483647
>
> ncl 5>
>
>
>
> On 10 June 2015 at 16:30, Xi Chang <xi.chang01 at gmail.com> wrote:
>
>> HI Alan,
>>
>> Thanks, and I knew that. The problem is that I have file A containts
>> repeatation years that make this function does not work properly, an
>> example:
>>
>> ncl 7> A = (/1990,1997,1999,2002,2002,2005,2010,2010/)
>>
>> ncl 8> B = (/1993,1997,2002,2004/)
>>
>> ncl 9>
>>
>> ncl 10> A_ind = get1Dindex(A,B)
>>
>> fatal:Dimension sizes on right hand side of assignment do not match
>> dimension sizes of left hand side
>>
>> fatal:Execute: Error occurred at or near line 1242 in file
>> $NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl
>>
>>
>> fatal:Execute: Error occurred at or near line 10
>>
>> It seeems like this function does not allow repeatation of the year. any
>> suggestion?>
>>
>> Chang
>>
>>
>>
>>
>>
>> On Wed, Jun 10, 2015 at 10:18 PM, Alan Brammer <abrammer at albany.edu>
>> wrote:
>>
>>> Take look at get1Dindex linked below. It should do what you want. Below
>>> are a few quick example lines I threw together to get the matching indices
>>> for both A and B.
>>>
>>> https://www.ncl.ucar.edu/Document/Functions/Contributed/get1Dindex.shtml
>>>
>>>
>>>
>>> e.g.
>>>
>>> A = (/1990,1997,1999,2002,2005,2010/)
>>>
>>> B = (/1993,1997,2002,2004/)
>>>
>>>
>>> A_ind = get1Dindex(A,B) ;; array size B of where A is in B, Fill
>>> if not in B
>>> A_ind_nm = A_ind(ind(.not.ismissing(A_ind))) ;; get rid of missing
>>> elements
>>> Ca = A(A_ind_nm) ;; probably want an error check in case
>>> there are no overlaps.
>>>
>>>
>>> B_ind = get1Dindex(B,A) ;; array size B of where A is in B, Fill
>>> if not in B
>>> B_ind_nm = B_ind(ind(.not.ismissing(B_ind))) ;; get rid of missing
>>> elements
>>> Cb = B(B_ind_nm) ;; probably want an error check in case
>>> there are no overlaps.
>>>
>>>
>>> Gd Luck.
>>>
>>> Alan.
>>>
>>>
>>> On 10 June 2015 at 15:33, Xi Chang <xi.chang01 at gmail.com> wrote:
>>>
>>>> Hallo all,
>>>>
>>>> Anyone can give me a hint how to get list of index for A which is
>>>> associated with B,
>>>> for illustration:
>>>>
>>>> file A containts :
>>>> -1990
>>>> -1997
>>>> -1999
>>>> -2002
>>>>
>>>> file B
>>>> -1993
>>>> -1997
>>>> -2002
>>>> -2005
>>>>
>>>> and in the end, will produce file C which only containts:
>>>> -1997
>>>> -2002
>>>>
>>>> I cant do it mannually becaise the time series are too long..
>>>> any suggestion for a clean alogirhtm?
>>>>
>>>> Thanks
>>>> Chang
>>>>
>>>> _______________________________________________
>>>> ncl-talk mailing list
>>>> ncl-talk at ucar.edu
>>>> List instructions, subscriber options, unsubscribe:
>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>
>>>>
>>>
>>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150610/aa4d91e5/attachment.html
More information about the ncl-talk
mailing list