<?xml version='1.0' encoding='UTF-8'?>
<Library xmlns="http://hl7.org/fhir" xmlns:html="http://www.w3.org/1999/xhtml">
  <id value="Ccczscore" />
  <text>
    <status value="extensions" /><html:div>
<html:div>
    <html:table class="grid dict">
        
        
        <html:tr>
            <html:th scope="row"><html:b>Title: </html:b></html:th>
            <html:td style="padding-left: 4px;">Ccc.Z.Score</html:td>
        </html:tr>
        

        
        
        <html:tr>
            <html:th scope="row"><html:b>Id: </html:b></html:th>
            <html:td style="padding-left: 4px;">Ccczscore</html:td>
        </html:tr>
        

        
        
        <html:tr>
            <html:th scope="row"><html:b>Version: </html:b></html:th>
            <html:td style="padding-left: 4px;">0.1.0</html:td>
        </html:tr>
        

        
        <html:tr>
            <html:th scope="row"><html:b>Url: </html:b></html:th>
            <html:td style="padding-left: 4px;"><html:a href="Library-Ccczscore.html">Ccc.Z.Score</html:a></html:td>
        </html:tr>
        

        
        <html:tr>
            <html:th scope="row">
                <html:b>
                    
                        Official
                    
                </html:b>
            </html:th>
            <html:td style="padding-left: 4px;">
                
                <html:p style="margin-bottom: 5px;">
                    <html:span>CccZScore</html:span>
                </html:p>
                
            </html:td>
        </html:tr>
        

        

        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Type: </html:b></html:th>
            <html:td style="padding-left: 4px;">
                
                    
                        
                        <html:p style="margin-bottom: 5px;">
                            <html:b>system: </html:b> <html:span><html:a href="http://hl7.org/fhir/R4/valueset-library-type.html">http://hl7.org/fhir/ValueSet/library-type</html:a></html:span>
                        </html:p>
                        
                        
                        <html:p style="margin-bottom: 5px;">
                            <html:b>code: </html:b> <html:span>logic-library</html:span>
                        </html:p>
                        
                        
                    
                
                
            </html:td>
        </html:tr>
        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Date: </html:b></html:th>
            <html:td style="padding-left: 4px;">2026-04-07 13:36:32+0000</html:td>
        </html:tr>
        

        
        <html:tr>
            <html:th scope="row"><html:b>Publisher: </html:b></html:th>
            <html:td style="padding-left: 4px;">World Health Organization (WHO)</html:td>
        </html:tr>
        

        

        

        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Jurisdiction: </html:b></html:th>
            <html:td style="padding-left: 4px;">001</html:td>
        </html:tr>
        

        

        

        

        

        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Related Artifacts: </html:b></html:th>
            <html:td style="padding-left: 4px;">
                
                
                
                <html:p><html:b>Dependencies</html:b></html:p>
                <html:ul>
                  
                    <html:li><html:code>http://fhir.org/guides/who/anc-cds/Library/FHIRHelpers</html:code></html:li>
                  
                    <html:li><html:a href="Library-Cccbase.html">CccBase</html:a></html:li>
                  
                    <html:li><html:code>https://smart.who.int/ccc/Library/weightforage</html:code></html:li>
                  
                    <html:li><html:code>https://smart.who.int/ccc/Library/anthrobase</html:code></html:li>
                  
                    <html:li><html:code>https://smart.who.int/ccc/Library/weightforlength</html:code></html:li>
                  
                    <html:li><html:code>https://smart.who.int/ccc/Library/weightforheight</html:code></html:li>
                  
                </html:ul>
                
                
                
                
                
            </html:td>
        </html:tr>
        

        
        <html:tr>
          <html:th scope="row"><html:b>Parameters: </html:b></html:th>
          <html:td style="padding-left: 4px;">
            <html:table class="grid-dict">
              <html:tr><html:th><html:b>Name</html:b></html:th><html:th><html:b>Type</html:b></html:th><html:th><html:b>Min</html:b></html:th><html:th><html:b>Max</html:b></html:th><html:th><html:b>In/Out</html:b></html:th></html:tr>
              
                <html:tr><html:th>encounter</html:th><html:th>Encounter</html:th><html:th /><html:th /><html:th>In</html:th></html:tr>
              
                <html:tr><html:th>pastweight</html:th><html:th>decimal</html:th><html:th /><html:th /><html:th>Out</html:th></html:tr>
              
                <html:tr><html:th>patientsex</html:th><html:th>string</html:th><html:th /><html:th /><html:th>Out</html:th></html:tr>
              
                <html:tr><html:th>ageatpastweight</html:th><html:th>integer</html:th><html:th /><html:th /><html:th>Out</html:th></html:tr>
              
                <html:tr><html:th>pastweightzscore</html:th><html:th>decimal</html:th><html:th /><html:th /><html:th>Out</html:th></html:tr>
              
                <html:tr><html:th>pastweightactualised</html:th><html:th>decimal</html:th><html:th /><html:th /><html:th>Out</html:th></html:tr>
              
                <html:tr><html:th>WAZ</html:th><html:th>decimal</html:th><html:th /><html:th /><html:th>Out</html:th></html:tr>
              
            </html:table>
          </html:td>
        </html:tr>
        

        
        <html:tr>
          <html:th scope="row"><html:b>Data Requirements:</html:b></html:th>
          <html:td style="padding-left: 4px;">
            <html:table class="grid-dict">
              <html:tr><html:th><html:b>Type</html:b></html:th><html:th><html:b>Profile</html:b></html:th><html:th><html:b>MS</html:b></html:th><html:th><html:b>Code Filter</html:b></html:th></html:tr>
              
                <html:tr>
                  <html:th>Patient</html:th>
                  <html:th>http://hl7.org/fhir/StructureDefinition/Patient</html:th>
                  <html:th />
                  <html:th>
                    
                  </html:th>
                </html:tr>
              
                <html:tr>
                  <html:th>Encounter</html:th>
                  <html:th>http://hl7.org/fhir/StructureDefinition/Encounter</html:th>
                  <html:th />
                  <html:th>
                    
                  </html:th>
                </html:tr>
              
                <html:tr>
                  <html:th>Observation</html:th>
                  <html:th>http://hl7.org/fhir/StructureDefinition/Observation</html:th>
                  <html:th>;;;;;</html:th>
                  <html:th>
                    
                      <html:b>code filter: </html:b>
                      
                      <html:br />
                      
                      
                      <html:span style="padding-left: 4px;">
                          <html:b>path: </html:b><html:span>code</html:span>
                      </html:span>
                      
                      <html:br />
                      
                      
                      
                      
                          
                          
                          <html:p style="margin-bottom: 5px;">
                              <html:b>code: </html:b> <html:span>Ccc.B6.DE06</html:span>
                          </html:p>
                          
                          
                      
                    
                  </html:th>
                </html:tr>
              
            </html:table>
          </html:td>
        </html:tr>
        

        
        
        <html:tr>
          <html:td colspan="2">
            <html:table>
              <html:tr><html:th><html:a id="cql-content"><html:b>Content: </html:b></html:a> text/cql</html:th></html:tr>
              <html:tr><html:td><html:pre><html:code class="language-cql">/*
@author: Patrick Delcroix
@description: This library is part of the project EmCare
*/
library emcarezscore version '1.0.1.rc12.build.156'
using FHIR version '4.0.1'
include FHIRHelpers version '4.0.1' called FHIRHelpers 
include emcarebase version '1.0.1.rc12.build.156' called Base
include WeightForAge version '1.0.0' called wfa
include WeightForLength version '1.0.0' called wfl
include WeightForHeight version '1.0.0' called wfh



//codesystem "EmCare" : 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes'



//code "Weight": 'EmCare.B6.DE06' from "EmCare" display 'Weight'
//code "Estimated Weight": 'EmCare.B6.DE08' from "EmCare" display 'Estimated Weight'
//code "MUAC": 'EmCare.B6.DE17' from "EmCare" display 'MUAC'
//code "Length": 'EmCare.B6.DE12' from "EmCare" display 'Length'
//code "Height": 'EmCare.B6.DE09' from "EmCare" display 'Height'

parameter "encounterid" String

context Patient

define "Weight":
  Base.coding('EmCare.B6.DE06')
define "Estimated Weight":
  Base.coding('EmCare.B6.DE08')
define "MUAC":
  Base.coding('EmCare.B6.DE17')
define "Length":
  Base.coding('EmCare.B6.DE12')
define "Height":
  Base.coding('EmCare.B6.DE09')
define "Visually assess":
  Base.coding('EmCare.B6.DE18')
define "Underweight":
  Base.coding('EmCare.B6.DE20') 
define "Severely Underweight":
  Base.coding('EmCare.B6.DE21') 

/* pastweight : Previous Weight*/
define "pastweight":
    if AgeInMonths()&lt; 4 and Base.HasObsHistory("Weight", 63) is not null then
      Base.HasObsHistory("Weight", 63)  as FHIR.Quantity
    else  if AgeInMonths()&lt; 6 and Base.HasObsHistory("Weight", 94)   is not null then
      Base.HasObsHistory("Weight", 94)  as  FHIR.Quantity
    else if AgeInMonths()&lt; 6 and Base.HasObsHistory("Weight", 126)   is not null then
       Base.HasObsHistory("Weight", 126)  as FHIR.Quantity
    else if Base.HasObsHistory("Weight", 201)  is not null then
       Base.HasObsHistory("Weight", 201)  as FHIR.Quantity
    else
      null


/* patientsex : Patient sex*/
define "patientsex":
    if Patient.gender = 'female' then 'female' else 'male'

/* ageatpastweight : Age at Previous Weight*/
define "ageatpastweight":
    if "pastweight" is not null  then
      (difference in days between Patient.birthDate and ToDate(Last([Observation:"Weight" ] O where  O.status in { 'final', 'amended', 'corrected'}).issued) ) * ( 1.0 as System.Decimal )
    else
     null

/* pastweightzscore : Z-Score at Previous Weight*/
define "pastweightzscore":
    if "pastweight" is not null  then
      wfa.generateZScoreWeightForAge("patientsex", "ageatpastweight" , ((convert "pastweight" to 'kg').value * ( 1.0 as System.Decimal)))
    else
      null

/* pastweightactualised : Weight from  at Previous Weight*/
define "pastweightactualised":
    if "pastweightzscore" is not null then
      System.Quantity { value: Round(wfa.generateWeightFromAge("patientsex",AgeInDays() * ( 1.0 as System.Decimal ),  "pastweightzscore" ),2), unit: 'Kg'} 
    else 
       null

define existingweight:
  Base.GetObsValue("Weight")

define existingestimnatedweight:
  if existingweight is null then  
    Base.GetObsValue("Estimated Weight")
  else 
    null

define existingheight:
  Base.GetObsValue("Height")

define existinglength:
  Base.GetObsValue("Length")

define existingmuac:
  Base.GetObsValue("MUAC")

define existingvisual:
  Base.GetObsValue("Visually assess")

define zscorefrommuac:
  if existingmuac is null then 0.0
  else if existingmuac &lt; 115 'mnm' then -3.0
  else if existingmuac &lt; 125 'mnm' then -2.0
  else 0.0

define estimatedweight:
  case  
  when existingweight is not null then null
  when existingestimnatedweight is not null then existingestimnatedweight
  when pastweightactualised is not null then pastweightactualised
  when AgeInMonths()&gt;24 and existingheight then weightfromheight
  when AgeInMonths()&lt;=24 and existinglength then weightfromlength
  else weightfromage
  end

define zscorefromvisual:
    if existingvisual is not null then
      case  
        when existingvisual ~ "Severely Underweight" then -3.0
        when existingvisual ~ "Underweight" then -2.0
        else 0.0
      end
    else  null


define weightfromlength:
    System.Quantity { value: Round(wfl.generateWeightFromLength("patientsex",(convert existinglength to 'cm').value * ( 1.0 as System.Decimal ), zscorefrommuac),2), unit: 'Kg'}

define weightfromheight:
    System.Quantity { value:Round(wfh.generateWeightFromHeight("patientsex",(convert existingheight to 'cm').value * ( 1.0 as System.Decimal ), zscorefrommuac),2), unit: 'Kg'}

define weightfromage:
    System.Quantity { value:Round(wfa.generateWeightFromAge("patientsex",AgeInDays() * ( 1.0 as System.Decimal ), zscorefrommuac),2), unit: 'Kg'}
/*
(convert Base.HasObs("Weight") to 'Kg') does not work, so let's assume the weight is in Kg
*/
define "WAZ":
    if existingweight is not null then
         wfa.generateZScoreWeightForAge("patientsex", "ageatpastweight" , ( Base.HasObs("Weight").value * ( 1.0 as System.Decimal )))
    else if existingmuac is not null then
        zscorefrommuac
    else if existingvisual then
        zscorefromvisual
    else null

</html:code></html:pre></html:td></html:tr>
            </html:table>
          </html:td>
        </html:tr>
        
        
        
    </html:table>
</html:div>
</html:div>
  </text>
  <url value="https://smart.who.int/ccc/Library/Ccczscore" />
  <identifier>
    <use value="official" />
    <value value="CccZScore" />
  </identifier>
  <version value="0.1.0" />
  <name value="Ccczscore" />
  <title value="Ccc.Z.Score" />
  <status value="active" />
  <type>
    <coding>
      <system value="http://hl7.org/fhir/ValueSet/library-type" />
      <code value="logic-library" />
    </coding>
  </type>
  <date value="2026-04-07T13:36:32+00:00" />
  <publisher value="World Health Organization (WHO)" />
  <contact>
    <name value="World Health Organization (WHO)" />
    <telecom>
      <system value="url" />
      <value value="https://www.who.int" />
    </telecom>
  </contact>
  <contact>
    <telecom>
      <system value="url" />
      <value value="https://www.who.int" />
    </telecom>
  </contact>
  <jurisdiction>
    <coding>
      <system value="http://unstats.un.org/unsd/methods/m49/m49.htm" />
      <code value="001" />
    </coding>
  </jurisdiction>
  <relatedArtifact>
    <type value="depends-on" />
    <resource value="http://fhir.org/guides/who/anc-cds/Library/FHIRHelpers" />
  </relatedArtifact>
  <relatedArtifact>
    <type value="depends-on" />
    <resource value="https://smart.who.int/ccc/Library/Cccbase" />
  </relatedArtifact>
  <relatedArtifact>
    <type value="depends-on" />
    <resource value="https://smart.who.int/ccc/Library/weightforage" />
  </relatedArtifact>
  <relatedArtifact>
    <type value="depends-on" />
    <resource value="https://smart.who.int/ccc/Library/anthrobase" />
  </relatedArtifact>
  <relatedArtifact>
    <type value="depends-on" />
    <resource value="https://smart.who.int/ccc/Library/weightforlength" />
  </relatedArtifact>
  <relatedArtifact>
    <type value="depends-on" />
    <resource value="https://smart.who.int/ccc/Library/weightforheight" />
  </relatedArtifact>
  <parameter>
    <name value="encounter" />
    <use value="in" />
    <type value="Encounter" />
  </parameter>
  <parameter>
    <name value="pastweight" />
    <use value="out" />
    <type value="decimal" />
  </parameter>
  <parameter>
    <name value="patientsex" />
    <use value="out" />
    <type value="string" />
  </parameter>
  <parameter>
    <name value="ageatpastweight" />
    <use value="out" />
    <type value="integer" />
  </parameter>
  <parameter>
    <name value="pastweightzscore" />
    <use value="out" />
    <type value="decimal" />
  </parameter>
  <parameter>
    <name value="pastweightactualised" />
    <use value="out" />
    <type value="decimal" />
  </parameter>
  <parameter>
    <name value="WAZ" />
    <use value="out" />
    <type value="decimal" />
  </parameter>
  <dataRequirement>
    <type value="Patient" />
    <profile value="http://hl7.org/fhir/StructureDefinition/Patient" />
  </dataRequirement>
  <dataRequirement>
    <type value="Encounter" />
    <profile value="http://hl7.org/fhir/StructureDefinition/Encounter" />
  </dataRequirement>
  <dataRequirement>
    <type value="Observation" />
    <profile value="http://hl7.org/fhir/StructureDefinition/Observation" />
    <mustSupport value="code" />
    <mustSupport value="encounter.reference" />
    <mustSupport value="encounter" />
    <mustSupport value="value" />
    <mustSupport value="status" />
    <codeFilter>
      <path value="code" />
      <code>
        <code value="Ccc.B6.DE06" />
      </code>
    </codeFilter>
  </dataRequirement>
  <content id="ig-loader-Ccczscore.cql">
    <contentType value="text/cql" />
    <url value="Library-Ccczscore.cql" /></content>
</Library>