====== Many to One Mapping with Non-Summation Aggregation Rule ====== The map tool can perform an index mapping in which multiple elements of informant X are mapped to the same element of informant Y. To aggregate multiple elements into one, the map tool sums the values of the composite elements. However, there may be a need to use a different aggregation function such as minimum, maximum, or average. The following example demonstrates how to implement this logic. ===== Background ===== In this example, the variable indexed by the informant unitID will be mapped to a variable indexed by the informant plantID. The contents of these informants are: plantID[]: set version: 3 desc: localinformant plantID[] = create (; object=set, data="Plant1 Plant2") data: 2 elements ----------------------------------- 1 Plant1 2 Plant2 unitID[]: set version: 3 desc: localinformant unitID[] = create (; object=set, data="Unit1 Unit2 Unit3 Unit4 Unit5") data: 5 elements ----------------------------------- 1 Unit1 2 Unit2 3 Unit3 4 Unit4 5 Unit5 The variable unitComYr contains commissioning years by unitID {{:howtos:toolcoding:unitcomyr.png|}} ===== Implementation ===== 1. Create the following desired mapping from unitID to plantID is:\\ Unit1 -> Plant1\\ Unit2 -> Plant2\\ Unit3 -> Plant1\\ Unit4 -> Plant1\\ Unit5 -> Plant2 local unitToPlantMap[uid] = create (; dataType=integer, dim=unitID, data="1 2 1 1 2") 2. Create a mask that will expand unitComYr to include plantID local pid[pid,pid] = diag (1.0; dim=plantID) local plantIDMask[uid,pid] = boolne (dimtran (pid[pid,pid]; map1=unitToPlantMap[uid], newDims=1, checkData=off),0) {{:howtos:toolcoding:plantidmask.png|}} local unitComYrx[uid,pid] = unitComYr[uid] * plantIDMask[uid,pid] {{:howtos:toolcoding:unitcomyrx.png|}} 3. Add offset to remove 0's before taking minimum local unitComYry[uid,pid] = booleq (unitComYrx[uid,pid],0) * 9999 + unitComYrx[uid,pid] 4. Use coordmin to create a variable that contains online date, which is the earliest commissioning year of the units that comprise the plant, by plantId local plantOnlineDate[pid] = coordmin (reorder (unitComYry[uid,pid]; plantID, unitID); numDims=1) {{:howtos:toolcoding:plantonlinedate.png|}} Click {{:howtos:toolcoding:mapmin.t|here}} to download the tool file that encapsulates the above example.