NCL Website header
NCL Home > Documentation > Functions > Array creators

venn2_union

Returns the unique element union of two arrays.

Available in version 6.4.0 and later.

Prototype

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

	function venn2_union (
		a  : numeric, string, character,  
		b  : numeric, string, character   
	)

	return_val [*] :  same type as a

Arguments

a

An array of any type, shape and size.

b

An array of the same type as a. It may have a different size and shape.

Return value

A scalar or one-dimensional array containing the elements that form the unique union of the input arrays. The result is independent of the input order. If both input arrays contain all _FillValue, the return will be a scalar containing the _FillValue associated with the a variable..

Description

The UNION of two sets (here, a and b) is the set of elements which are in either set. For example: let A = (1,2,3) and let B = (3,4,5). Now the UNION of A and B, written A union B = (1,2,3,4,5). There is no need to list the 3 twice.

NCL has a limited suite of simple Venn_diagram functions: (i) venn2_difference, (ii) venn2_intersection and (iii) venn2_union. Pictoral depictions are here.

See Also

venn2_intersection, venn2_difference, get_unique_values, count_unique_values

Examples

Example 1: Return the unique element union of two arrays. The result is independent of the order of the input arguments.

  A   = (/"a", "e", "i", "o", "u", "y" /)
  B   = (/"a", "b", "c", "d", "e", "f", "g", "h" /)

  ABu = venn2_union(A, B)    ; ABu= (/"a","b","c","d","e","f","g","h","i","o","u","y" /)
  BAu = venn2_union(B, A)    ; BAu = ABu

Example 2: Return the unique element union of three arrays.

  A   = (/"a", "e", "i", "o", "u", "y" /)
  B   = (/"a", "b", "c", "d", "e", "f", "g", "h" /)
  C   = (/"a", "b", "q", "y", "z"/)
 
  ABu = venn2_union(A, B)        ; ABu= (/"a","b","c","d","e","f","g","h","i","o","u","y"/)
  AC  = array_append_record(A, C, 0) ; AC = (/"a","e","i","o","u","y","a", "b", "q", "y", "z"/)
  ACBu = venn2_union(AC,B)       ; ACBu= (/"a","b","c","d","e","f","g","h","i","o","q","u","y","z" /) 
Example 3

  date1 = (/"1979-01-01-00000","1979-01-02-00000","1979-01-08-00000","1979-01-06-00000" \
           ,"1980-12-12-00000","1981-01-02-00000","1988-05-06-00000","1988-05-06-00000" /)

  date2 = (/"1979-05-01-00000","1979-01-01-00000","1979-01-06-00000","1981-01-02-00000" \
           ,"1981-01-02-00000","1987-01-02-00000","1988-05-06-00000"/)

  DATE  = venn2_union(date1, date2) 
  print(DATE)

The output would be

   Variable: DATE
   Type: string
   Total Size: 72 bytes
               9 values
   Number of Dimensions: 1
   Dimensions and sizes:   [9]
   Coordinates: 
   Number Of Attributes: 1
     tag : NCL: venn2_union has created the result
   (0)     1979-01-01-00000
   (1)     1979-01-02-00000
   (2)     1979-01-06-00000
   (3)     1979-01-08-00000
   (4)     1979-05-01-00000
   (5)     1980-12-12-00000
   (6)     1981-01-02-00000
   (7)     1987-01-02-00000
   (8)     1988-05-06-00000

Example 4

The venn2_union function allows different size and shape input arrays.

  y   = (/ (/1.5, 2.8, 1.6/), (/3.1, 1.5, 9.9/), (/2.8, 2.8, 2.8/) /)   ; 3 x 3 
  x   = (/   1.5, 1.6, 3.1, 2.8, 2.8 /)   ; [5]   
  yxi = venn2_union(y, x)     ; yxi = (/ 1.5, 1.6, 2.8, 3.1, 9.9 /)

Example 5

The venn2_union function allows for _FillValue.

  a   = (/-999, -999, -999/)
  a@_FillValue = -999

  b   =  (/-888, -888, -888, -888, -888/)
  b@_FillValue = -888

  c   = ispan(1,5,1)

  d   = (/-999, 12  , -999/)
  d@_FillValue = -999
                                    ; all elements of a and b are _FillValue
  ab  = venn2_union(a, b)      ; scalar; size 1; ab[1];     
  print(ab)

  ac  = venn2_union(a, c)      ; ac=(/1,2,3,4,5/)
  print(ac)

  ad  = venn2_union(a, d)      ; ad=(/12/)
  print(ad)


The return for 'ab' is a scalar containing the _FillValue associated with 'a'

   Variable: ab
   Type: integer
   Total Size: 4 bytes
               1 values
   Number of Dimensions: 1
   Dimensions and sizes:	[1]
   Coordinates: 
   Number Of Attributes: 2
     _FillValue :	-999
  tag :	NCL: venn2_union: all values of input arrays are _FillValue

(0)	-999

Example 6: Use all three Venn based venn2_* functions.


  models_rcp26 = (/"CanESM2", "CCSM4", "CESM1-CAM5", "CNRM-CM5", "CSIRO-Mk3-6-0"     \
                  ,"FIO-ESM", "GFDL-CM3", "GFDL-ESM2G", "GFDL-ESM2M", "GISS-E2-H"    \
                  ,"GISS-E2-R", "HadGEM2-AO", "HadGEM2-ES", "IPSL-CM5A-LR"           \
                  ,"IPSL-CM5A-MR", "MIROC5", "MIROC-ESM", "MIROC-ESM-CHEM"           \
                  ,"MPI-ESM-LR", "MPI-ESM-MR", "MRI-CGCM3", "NorESM1-M", "NorESM1-ME"/)

  models_rcp45 = (/"ACCESS1-0", "ACCESS1-3", "bcc-csm1-1", "bcc-csm1-1-m", "BNU-ESM"     \
                  ,"CanCM4", "CanESM2", "CCSM4", "CESM1-BGC", "CESM1-CAM5"               \
                  ,"CESM1-CAM5-1-FV2", "CMCC-CM", "CMCC-CMS", "CNRM-CM5", "CSIRO-Mk3-6-0"\
                  ,"CSIRO-Mk3L-1-2", "FIO-ESM", "GFDL-CM2p1", "GFDL-CM3", "GFDL-ESM2G"   \
                  ,"GFDL-ESM2M", "GISS-E2-H", "GISS-E2-H-CC", "GISS-E2-R", "GISS-E2-R-CC"\
                  ,"HadCM3", "HadGEM2-AO", "HadGEM2-CC", "HadGEM2-ES", "inmcm4"          \
                  ,"IPSL-CM5A-LR", "IPSL-CM5A-MR", "IPSL-CM5B-LR", "MIROC5", "MIROC-ESM" \
                  ,"MIROC-ESM-CHEM", "MPI-ESM-LR", "MPI-ESM-MR", "MRI-CGCM3", "NorESM1-M"\
                  ,"NorESM1-ME"/)

  models_rcp85 = (/"ACCESS1-0", "ACCESS1-3", "bcc-csm1-1", "bcc-csm1-1-m", "BNU-ESM"     \
                  ,"CanESM2", "CCSM4", "CESM1-BGC", "CESM1-CAM5", "CESM1-CAM5-1-FV2"     \
                  ,"CMCC-CESM", "CMCC-CM", "CMCC-CMS", "CNRM-CM5", "CSIRO-Mk3-6-0", "FIO-ESM" \
                  ,"GFDL-CM3", "GFDL-ESM2G", "GFDL-ESM2M", "GISS-E2-H", "GISS-E2-H-CC"   \ 
                  ,"GISS-E2-R", "GISS-E2-R-CC", "HadGEM2-AO", "HadGEM2-CC", "HadGEM2-ES" \
                  ,"inmcm4", "IPSL-CM5A-LR", "IPSL-CM5A-MR", "IPSL-CM5B-LR", "MIROC5"    \
                  ,"MIROC-ESM", "MIROC-ESM-CHEM", "MPI-ESM-LR", "MPI-ESM-MR", "MRI-CGCM3"\
                  ,"MRI-ESM1", "NorESM1-M", "NorESM1-ME"/)

  printVarSummary(models_rcp26)                                       ; 23
  printVarSummary(models_rcp45)                                       ; 41
  printVarSummary(models_rcp85)                                       ; 39

; Venn results on two data sets; all three operations

  uu_rcp_2645 = venn2_union(models_rcp26, models_rcp45  )        ; union
  print(uu_rcp_2645)
  
  ui_rcp_2645 = venn2_intersection(models_rcp26, models_rcp45  ) ; intersection
  print(ui_rcp_2645)
  
  ud_rcp_2645 = venn2_difference(models_rcp26, models_rcp45  )   ; difference
  print(ud_rcp_2645)
  
  print("==================================")

; Use the above '2645'  results with the rcp_85

  uu_rcp_2645_85 = venn2_union(uu_rcp_2645, models_rcp85  )
  print(uu_rcp_2645_85)
  
  ui_rcp_2645_85 = venn2_intersection(ui_rcp_2645, models_rcp85  )
  print(ui_rcp_2645_85)
  
  ud_rcp_2645_85 = venn2_difference(ud_rcp_2645, models_rcp85  )
  print(ud_rcp_2645_85)

  print("==================================")
The printed output is here.