Post Reply 
Air Quality Index calculator?
09-14-2020, 08:02 PM (This post was last modified: 09-14-2020 08:25 PM by Didier Lachieze.)
Post: #2
RE: Air Quality Index calculator?
Here is one based on the table and formula from the Wikipedia article, it's a bit rough (no test for out of range values...) but should work. I suppose you have access to the correct data about the different air pollutants with 1-hour / 8-hour /24-hour average monitoring as required.

Code:
#pragma mode( separator(.,;) integer(h32) )

AQI_bkp:={{0,50},{51,100},{101,150},{151,200},{201,300},{301,400},{401,500}};

AQI_formula(Pol_bkp,C)
BEGIN
  LOCAL Index, I;
  LOCAL Ilow, Ihigh, Clow, Chigh;

  FOR I FROM 1 TO 7 DO
    IF (C <= Pol_bkp(I,2)) THEN
      Ilow:=AQI_bkp(I,1); Ihigh:=AQI_bkp(I,2);
      Clow:=Pol_bkp(I,1); Chigh:=Pol_bkp(I,2);
      Index:= (Ihigh-Ilow)*(C-Clow)/(Chigh-Clow)+Ilow;
      BREAK;
    END;
  END;
  RETURN ROUND(Index,0);
END;

EXPORT AQI()
BEGIN
  LOCAL O38hr,O31hr,PM25,PM10,CO,SO2,NO2;
  LOCAL O38hr_bkp:={{0,54},{55,70},{71,85},{86,105},{106,200},{0,0},{0,0}};
  LOCAL O31hr_bkp:={{0,0},{0,0},{125,164},{165,204},{205,404},{405,504},{505,604}};​
  LOCAL PM25_bkp:={{0,12},{12.1,35.4},{35.5,55.4},{55.5,150.4},{150.5,250.4},{250.5​,350.4},{350.5,500.4}};
  LOCAL PM10_bkp:={{0,54},{55,154},{155,254},{255,354},{355,424},{425,504},{505,604​}};
  LOCAL CO_bkp:={{0.0,4.4},{4.5,9.4},{9.5,12.4},{12.5,15.4},{15.5,30.4},{30.5,40.4}​,{40.5,50.4}};
  LOCAL SO2_bkp:={{0,35},{36,75},{76,185},{186,304},{305,604},{605,804},{805,1004}}​;
  LOCAL NO2_bkp:={{0,53},{54,100},{101,360},{361,649},{650,1249},{1250,1649},{1650,​2049}};
  LOCAL AQI_cat:={{"Good",58368,0},{"Moderate",16776965,0},{"Unhealthy for Sensitive Groups",16743936,0},
                  {"Unhealthy",16711680,16777215},{"Very Unhealthy",10027084,16777215},{"Hazardous",8257571,16777215},{"Hazardous",8257571,16777215}};
  LOCAL AQI_list:={0,0,0,0,0,0};
  LOCAL Category, Color_b, Color_f, Index, I, X;

  INPUT({{O38hr,[0],{30,50,0}},{O31hr,[0],{30,50,1}},{PM25,[0],{30,50,2}},{PM10,[0],{30,50,3}},{CO,[0],{30,50,4}},{SO2,[0],{30,50,5}},{NO2,[0],{30,50,6}}}, 
       "AQI - Pollutants", {"O3 8-hr ","O3 1-hr ","PM2.5 ","PM10 ","CO ","SO2 ","NO2 "},
        {"8-hour ozone value (ppb)","1-hour ozone value (ppb)","24-hour average fine particle PM2.5 (μg/m3)","24-hour average fine particle PM10 (μg/m3)",
         "8-hour carbon monoxide value (ppm)","1 or 24-hour sulfur dioxide value (ppb)","1-hour nitrogen dioxide value (ppb)"});

  AQI_list(1):=MAX(AQI_formula(O38hr_bkp,O38hr), IFTE(O31hr>=125,AQI_formula(O31hr_bkp,O31hr),0));
  AQI_list(2):=AQI_formula(PM25_bkp,PM25);
  AQI_list(3):=AQI_formula(PM10_bkp,PM10);
  AQI_list(4):=AQI_formula(CO_bkp,CO);
  AQI_list(5):=AQI_formula(SO2_bkp,SO2);
  AQI_list(6):=AQI_formula(NO2_bkp,NO2);

  Index:=MAX(AQI_list);

  FOR I FROM 1 TO 7 DO
    IF (Index <= AQI_bkp(I,2)) THEN
      Category:=AQI_cat(I,1);
      Color_b:=AQI_cat(I,2);
      Color_f:=AQI_cat(I,3);
      BREAK;
    END;
  END;
  RECT();
  X:=TEXTOUT_P("Air Quality Index : "+Index,40,20,0,#FFFFFF);
  TEXTOUT_P("Air Quality Index : "+Index,40+(280-X)/2,20);
  RECT_P(40,40,280,80,Color_b);
  X:=TEXTOUT_P(Category,40,54,0,Color_b);
  TEXTOUT_P(Category,40+(280-X)/2,54,0,Color_f);
  FREEZE;
  RETURN(AQI_list);
END;

It displays the overall Air Quality Index, and returns a list of the Air Quality Index calculated for each pollutant (O3, PM2.5, PM10, CO, SO2, NO2).
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Air Quality Index calculator? - DrD - 09-14-2020, 10:56 AM
RE: Air Quality Index calculator? - Didier Lachieze - 09-14-2020 08:02 PM
RE: Air Quality Index calculator? - DrD - 09-14-2020, 09:53 PM
RE: Air Quality Index calculator? - Benjer - 09-15-2020, 10:13 PM



User(s) browsing this thread: 1 Guest(s)