'Average Cudls data 'AVERCUD.BAS- modified to process 9304, weird format. 'July 1995 modified by FR '14dec93 by kmp 'Modified to accomodate the new cudls format. Three new fields added. 'Modified 14apr94 to fix problem when endeco time is exactly 24:00:00 and 'date changed to the next day. Changed build.endeco.array ehr$(j). DECLARE FUNCTION fixdate$ (c$) DECLARE SUB build.endeco.array () DECLARE SUB build.endeco.array () DECLARE SUB endeco.data.average () DECLARE SUB endeco.start.time () DECLARE SUB compare.and.print () DECLARE SUB cudls.data.average () DECLARE SUB cudls.start.time () DECLARE SUB check.time.error () DECLARE SUB build.cudls.array () DECLARE SUB print.cudls.array () CONST maxlines = 40 DIM SHARED alat, alon, ag, ai, aj, ak, al, am, an, ao, ap, aq: REM cudls averaged data DIM SHARED direct$, where$, a$ DIM SHARED etmp1, eox1, eox2, etmp2: REM endeco.data.average DIM SHARED clocmin$(maxlines), clochr$(maxlines), sethr, setmin, i, cvar$: REM cudls.start.time DIM SHARED d, cte: REM check.time.error DIM SHARED alatcount, aloncount, aicount, agcount, ajcount, akcount, alcount, amcount, ancount, aocount, apcount, aqcount: DIM SHARED edate$(maxlines), etime$(maxlines), etemp1(maxlines), eoxy1(maxlines), etemp2(maxlines), eoxy2(maxlines), evar$ DIM SHARED cloctime$(maxlines), clocdate$(maxlines), clat(maxlines), clon(maxlines), utc$(maxlines), cf(maxlines) DIM SHARED cg(maxlines), ch$(maxlines), ci(maxlines), cj(maxlines), ck(maxlines), cl(maxlines), cm(maxlines), cn(maxlines), co(maxlines), cp(maxlines), cq(maxlines) DIM SHARED emin$(maxlines), ehr$(maxlines), j, esetmin, esethr, ghost(maxlines) REM Variables with an e prefix =endeco; c prefix=cudls CLS done = 0 PRINT PRINT COLOR 4 PRINT "This program will average cudls data into" PRINT "time bins as specified by the user." PRINT PRINT COLOR 15 a$ = "d:\programs\basic\cudl-end\asc9504.end" REM INPUT "Input the endeco data path and filename. "; a$ REM OPEN a$ FOR INPUT AS #1 b$ = "d:mocho.cud" REM INPUT "Input the CUDLS data path and filename. "; b$ OPEN b$ FOR INPUT AS #2 c$ = "d:a109304.cud" REM INPUT "input the path and filename for averaged CUDLS data. "; c$ OPEN c$ FOR OUTPUT AS #3 d$ = "d:corrida.out" REM INPUT "input the path and filename for the merged endeco and culds data. "; c$ OPEN d$ FOR OUTPUT AS #4 20 PRINT "What time block (in units of 5 minutes) do you " PRINT "want to use when averaging the data? " PRINT "(use 5,10 15,20 or 30 minute average)" INPUT d setmin = 0 sethr = 0 REM Make sure d equals one of five numbers listed above. check.time.error IF cte = 1 THEN PRINT COLOR 2 PRINT "Try again" COLOR 15 PRINT GOTO 20 END IF ON ERROR GOTO handler DO WHILE done = 0 cudls.start.time build.cudls.array cudls.data.average print.cudls.array LOOP REM cudls.start.time REM build.cudls.array REM cudls.data.average REM print.cudls.array 66 CLOSE END handler: PRINT "err=", ERR PRINT "where$=", where$ IF where$ = "endeco" AND ERR = 62 THEN direct$ = "cudls.only" done = done + 1 PRINT "direct$=", direct$ RESUME NEXT ELSEIF where$ = "cudls" AND ERR = 62 THEN direct$ = "endeco.only" done = done + 1 RESUME NEXT ELSE PRINT "stopping for unknown reason" PRINT "runtime error=", ERR CLOSE STOP END IF SUB build.cudls.array DIM star$(maxlines) where$ = "building cudls array" PRINT #4, "build.cudls.array start" PRINT #4, "VAL(clocmin$(i))=", VAL(clocmin$(i)) PRINT #4, "VAL(clochr$(i))=", VAL(clochr$(i)) PRINT #4, "start of subroutine build cudls array i=", i PRINT #4, "sethr=", sethr, " setmin= ", setmin REM cudls.start.time DO WHILE VAL(clocmin$(i)) <= setmin AND VAL(clochr$(i)) = sethr i = i + 1 INPUT #2, cloctime$(i) done = EOF(2) IF done <> 0 THEN PRINT #4, "***** End of File encountered ****" PRINT #4, "i="; i PRINT #4, cloctime$(i) clocmin$(i) = MID$(cloctime$(i), 5, 2) clochr$(i) = MID$(cloctime$(i), 2, 2) INPUT #2, clocdate$(i), clat(i), clon(i), utc$(i), cf(i), cg(i), ch$(i), ci(i), cj(i), ck(i), cl(i), cm(i), cn(i) REM PRINT "from build", cloctime$(i), clocdate$(i), clat(i), clon(i), utc$(i), cf(i), cg(i), ch$(i), ci(i), cj(i), ck(i), cl(i), cm(i), cn(i), co(i), cp(i), cq(i), star$(i) PRINT #4, (clochr$(i)) PRINT #4, (clocmin$(i)) LOOP 77 PRINT #4, "build.cudls.array end" END SUB SUB build.endeco.array where$ = "endeco" REM PRINT #4, "build.endeco.array begin" REM PRINT #4, VAL(emin$(j)) REM PRINT #4, VAL(ehr$(j)) REM PRINT #4, esetmin REM PRINT #4, esethr REM 14apr94 patch, kmp REM Special case where endeco time is exactly 24:00:00 and date has changed to the next day. REM Change ehr$ to 00:00:00 rather than 24:00:00. IF ehr$(j) = "24" THEN ehr$(j) = " 00" REM Check for missing block of data. If true reset start time. IF VAL(emin$(j)) > esetmin OR VAL(ehr$(j)) <> esethr THEN evar$ = "missing.data" endeco.start.time END IF DO WHILE VAL(emin$(j)) <= esetmin AND VAL(ehr$(j)) = esethr j = j + 1 REM PRINT #4, j LINE INPUT #1, E$ IF direct$ = "cudls.only" GOTO 88 edate$(j) = LEFT$(E$, 10) etime$(j) = MID$(E$, 12, 8) etemp1(j) = VAL(MID$(E$, 23, 5)) eoxy1(j) = VAL(MID$(E$, 29, 5)) etemp2(j) = VAL(MID$(E$, 38, 5)) eoxy2(j) = VAL(MID$(E$, 44, 5)) REM PRINT #4, edate$(j), etime$(j), etemp1(j), eoxy1(j), etemp2(j), eoxy2(j) emin$(j) = MID$(etime$(j), 4, 2) ehr$(j) = LEFT$(etime$(j), 2) REM PRINT #4, "ehr="; (ehr$(j)) REM PRINT #4, "emin="; (emin$(j)) LOOP 88 REM PRINT #4, "build.endeco.array end" END SUB SUB check.time.error IF d = 5 OR d = 10 OR d = 15 OR d = 20 OR d = 30 THEN cte = 0 ELSE cte = 1 END IF END SUB SUB cudls.data.average PRINT #4, "culds.data.average, start value of i=", i ag = 0: alat = 0: alon = 0: ai = 0: aj = 0: ak = 0: al = 0: am = 0: an = 0: ao = 0: ap = 0: aq = 0: REM reset to zero on new loop alatcount = i: aloncount = i: aicount = i: agcount = i: ajcount = i: akcount = i: alcount = i: amcount = i: ancount = i: aocount = i: apcount = i: aqcount = i: FOR k = 1 TO i PRINT #4, "k=", k PRINT #4, "i=", i REM If data is missing (=0) then do not use in mean and decrease denominator. IF clat(k) = 0 THEN alatcount = alatcount - 1 END IF alat = alat + clat(k) IF clon(k) = 0 THEN aloncount = aloncount - 1 END IF alon = alon + clon(k) IF cg(k) = 0 THEN agcount = agcount - 1 END IF ag = ag + cg(k) IF ci(k) = 0 THEN aicount = aicount - 1 END IF ai = ai + ci(k) IF cj(k) = 0 THEN ajcount = ajcount - 1 END IF aj = aj + cj(k) IF ck(k) = 0 THEN akcount = akcount - 1 END IF ak = ak + ck(k) IF cl(k) = 0 THEN alcount = alcount - 1 END IF al = al + cl(k) IF cm(k) = 0 THEN amcount = amcount - 1 END IF am = am + cm(k) NEXT REM CALCULATING AVERAGE VALUES IF alatcount > 0 THEN alat = alat / alatcount ELSE alat = 0 PRINT #4, alat END IF IF aloncount > 0 THEN alon = alon / aloncount ELSE alon = 0 PRINT #4, alon END IF IF agcount > 0 THEN ag = ag / agcount ELSE ag = 0 PRINT #4, ag END IF IF aicount > 0 THEN ai = ai / aicount ELSE ai = 0 PRINT #4, ai END IF IF ajcount > 0 THEN aj = aj / ajcount ELSE aj = 0 PRINT #4, aj END IF IF akcount > 0 THEN ak = ak / akcount ELSE ak = 0 PRINT #4, ak END IF IF alcount > 0 THEN al = al / alcount ELSE al = 0 PRINT #4, al END IF IF amcount > 0 THEN am = am / amcount ELSE am = 0 PRINT #4, am END IF PRINT #4, alat PRINT #4, alon PRINT #4, ag PRINT #4, ai PRINT #4, aj PRINT #4, ak PRINT #4, al PRINT #4, am PRINT #4, "cudls.data.average end" END SUB SUB cudls.start.time DIM star$(maxlines) where$ = "start time cudls" PRINT #4, "Time interval=", d i = 1 REM Reset start time if missing block of data ie. culds went off line. REM IF cvar$ = "missing.data" GOTO 1000 INPUT #2, cloctime$(i) PRINT "First time ever read=", cloctime$(i) PRINT #4, "First time ever in cudls start time read =", cloctime$(i) clocmin$(i) = MID$(cloctime$(i), 5, 2) clochr$(i) = MID$(cloctime$(i), 2, 2) PRINT "Extracted hour ", (clochr$(i)) PRINT "Extracted minute ", (clocmin$(i)) INPUT #2, clocdate$(i), clat(i), clon(i), utc$(i), cf(i), cg(i), ch$(i), ci(i), cj(i), ck(i), cl(i), cm(i), cn(i) LINE INPUT #2, a$ REM Set hours 0-24. 1000 IF sethr < VAL(clochr$(i)) THEN sethr = sethr + 1 GOTO 1000 ELSE PRINT "sethr="; sethr END IF setmin = VAL(clocmin$(i)) + d IF setmin > 59 THEN setmin = setmin - 60 IF (sethr + 1) > 23 THEN sethr = 0 ELSE sethr = sethr + 1 END IF REM Set starting minute interval REM 2000 IF setmin <= VAL(clocmin$(i)) THEN REM setmin = setmin + d REM PRINT , "defining setmin="; setmin REM GOTO 2000 REM ELSEIF setmin = 0 THEN REM setmin = d - 1 REM ELSE setmin = setmin - 1 REM END IF REM PRINT , "setmin="; setmin END SUB SUB endeco.data.average REM PRINT #4, "endeco.data.average begin" etmp1 = 0: etmp2 = 0: eox1 = 0: eox2 = 0: REM reset mean to zero at new loop etmp1count = j - 1: eox1count = j - 1: etmp2count = j - 1: eox2count = j - 1 FOR k = 1 TO (j - 1) REM lprint REM PRINT #4, k REM Test for missing (=0) data. If true decrease denominator by 1. IF etemp1(k) = 0 THEN etmp1count = etmp1count - 1 END IF etmp1 = etmp1 + etemp1(k) IF eoxy1(k) = 0 THEN eox1count = eox1count - 1 END IF eox1 = eox1 + eoxy1(k) REM PRINT #4, "eox1="; eox1 IF etemp2(k) = 0 THEN etmp2count = etmp2count - 1 END IF etmp2 = etmp2 + etemp2(k) IF eoxy2(k) = 0 THEN eox2count = eox2count - 1 END IF eox2 = eox2 + eoxy2(k) REM PRINT #4, "eox2="; eox2 NEXT etmp1 = etmp1 / etmp1count eox1 = eox1 / eox1count REM PRINT #4, "eox1="; eox1 etmp2 = etmp2 / etmp2count eox2 = eox2 / eox2count REM PRINT #4, "eox2="; eox2 REM PRINT #4, etmp1 REM PRINT #4, eox1 REM PRINT #4, etmp2 REM PRINT #4, eox2 REM PRINT #4, "endeco.data.average end" END SUB SUB endeco.start.time where$ = "endeco" REM PRINT #4, "endeco.start.time begin" esethr = 0: REM Start averaging at midnight 00:00 esetmin = 0 j = 1 REM Resets start time when block of data is missing. IF evar$ = "missing.data" GOTO 500 LINE INPUT #1, E$ edate$(j) = LEFT$(E$, 10) etime$(j) = MID$(E$, 12, 8) etemp1(j) = VAL(MID$(E$, 23, 5)) eoxy1(j) = VAL(MID$(E$, 29, 5)) etemp2(j) = VAL(MID$(E$, 38, 5)) eoxy2(j) = VAL(MID$(E$, 44, 5)) REM PRINT #4, edate$(j), etime$(j), etemp1(j), eoxy1(j), etemp2(j), eoxy2(j) emin$(j) = MID$(etime$(j), 4, 2) ehr$(j) = LEFT$(etime$(j), 2) REM PRINT #4, "ehr="; (ehr$(j)) REM PRINT #4, "emin="; (emin$(j)) REM Set hours 0-24. 500 IF VAL(ehr$(j)) > esethr THEN esethr = esethr + 1 GOTO 500 ELSE REM PRINT #4, "esethr="; esethr END IF REM Set starting minute interval 600 IF VAL(emin$(j)) >= esetmin THEN esetmin = esetmin + d GOTO 600 ELSEIF esetmin = 0 THEN esetmin = d - 1: REM where first endeco time in minutes is 00 ELSE esetmin = esetmin - 1 REM PRINT #4, "esetmin="; esetmin END IF REM PRINT #4, "endeco.start.time end" END SUB FUNCTION fixdate$ (c$) fixdate$ = LEFT$(c$, 2) + "/" + MID$(c$, 4, 2) + "/" + RIGHT$(c$, 2) END FUNCTION SUB print.cudls.array PRINT "print.cudls.array begin" GOSUB 240 REM i-1 and j-1 may be better for date PRINT "max i="; i PRINT #4, "cudls local date="; clocdate$(i): REM not sure if 'i' is the correct variable to use. REM PRINT #4, "j="; j REM PRINT #4, "endeco date="; edate$(j): REM ditto for 'j' REM compare dates cyr = VAL(RIGHT$(clocdate$(i - 1), 4)) PRINT #4, "culds local yr="; cyr cmo = VAL(LEFT$(clocdate$(i - 1), 2)) PRINT #4, "cudls local mo="; cmo cdy = VAL(MID$(clocdate$(i - 1), 4, 2)) PRINT #4, "cudls local dy="; cdy GOTO 400 200 IF esetmin = 59 THEN esethr = esethr + 1 esetmin = 0 END IF REM Need to check to see if esethr=24 IF esethr = 24 THEN esethr = 0 END IF IF esetmin = 0 THEN esetmin = (esetmin + d) - 1 ELSE esetmin = esetmin + d END IF edate$(1) = edate$(j) emin$(1) = MID$(etime$(j), 4, 2) ehr$(1) = LEFT$(etime$(j), 2) etime$(1) = etime$(j) etemp1(1) = etemp1(j) eoxy1(1) = eoxy1(j) etemp2(1) = etemp2(j) eoxy2(1) = eoxy2(j) j = 1 220 REM NEXT BIN READING NEXT BLOCK OF DATA IF setmin = 59 THEN sethr = sethr + 1 setmin = 0 END IF REM Need to check to see if sethr=24 IF sethr = 24 THEN sethr = 0 END IF IF setmin = 0 THEN setmin = (setmin + d) - 1 ELSE setmin = setmin + d END IF clocdate$(1) = clocdate$(i) clocmin$(1) = MID$(cloctime$(i), 4, 2) clochr$(1) = LEFT$(cloctime$(i), 2) cloctime$(1) = cloctime$(i) clat(1) = clat(i) clon(1) = clon(i) utc$(1) = utc$(i) cf(1) = cf(i) cg(1) = cg(i) ch$(1) = ch$(i) ci(1) = ci(i) cj(1) = cj(i) ck(1) = ck(i) cl(1) = cl(i) cm(1) = cm(i) cn(1) = cn(i) i = 1 240 REM print cudls data quote$ = STRING$(1, 34) cmidptmin = FIX((setmin + (setmin - (d - 1))) / 2) REM Fix minutes when < 10 by adding a leading zero. IF cmidptmin < 10 THEN cmpm$ = "0" + RIGHT$(STR$(cmidptmin), 1) ELSE cmpm$ = RIGHT$(STR$(cmidptmin), 2) END IF REM Fix hours when < 10 by adding a leading zero. IF sethr < 10 THEN cmphr$ = "0" + RIGHT$(STR$(sethr), 1) ELSE cmphr$ = RIGHT$(STR$(sethr), 2) END IF REM Patch together the midpoint time for the mean. IF d = 10 OR d = 20 OR d = 30 THEN cmpt$ = cmphr$ + ":" + cmpm$ + ":30" ELSE cmpt$ = cmphr$ + ":" + cmpm$ + ":00" END IF cuddate$ = fixdate$(clocdate$(i - 1)) REM PRINT #4, "ag="; ag REM PRINT #3, quote$; cmpt$; quote$; ","; quote$; cloctime$(i - 1); quote$; ","; quote$; cuddate$; quote$; ","; alat; ","; alon; ","; quote$; utc$(i - 1); quote$; ","; cf(i - 1); ","; ag; ","; ch$(i - 1); ","; ai; ","; aj; ","; ak; ","; al; ","; am; ","; an; ","; ao; ","; ap; ","; aq; ","; i - 1; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$ PRINT #3, quote$; cmpt$; quote$; ","; quote$; cuddate$; quote$; ","; alat; ","; alon; ","; quote$; utc$(i - 1); quote$; ","; cf(i - 1); ","; ag; ","; ch$(i - 1); ","; ai; ","; aj; ","; ak; ","; al; ","; am; ","; an; ","; ao; ","; ap; ","; aq; ","; i - 1; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$; ","; quote$; quote$ PRINT #4, quote$; cmpt$; quote$; ","; quote$; cloctime$(i - 1); quote$; ","; quote$; cuddate$; quote$; ","; alat; ","; alon; ","; quote$; utc$(i - 1); quote$; ","; cf(i - 1); ","; ag; ","; ch$(i - 1); ","; ai; ","; aj; ","; ak; ","; al; ","; am; ","; an; RETURN 400 REM PRINT #4, "print.cudls end" END SUB