'Average Cudls data 'MEC56 '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$ 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 REM Variables with an e prefix =endeco; c prefix=cudls CLS done = 0 PRINT PRINT COLOR 2 PRINT "This program will merge and average endeco and culds data into time" PRINT "bins as specified by the user." PRINT PRINT COLOR 11 a$ = "d:\programs\basic\cudl-end\asc9504.end" REM INPUT "Input the endeco data path and filename. "; a$ OPEN a$ FOR INPUT AS #1 REM b$ = "d:\programs\basic\cudl-end\asc9504.cud" INPUT "Input the culds data path and filename. "; b$ OPEN b$ FOR INPUT AS #2 REM c$ = "d:\programs\basic\cudl-end\salida.dat" INPUT "input the path and filename for averaged culds data. "; c$ OPEN c$ FOR OUTPUT AS #3 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 do you want to use when averaging the data? " PRINT "(use 5,10 15,20 or 30 minute average)" INPUT d 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 cudls.start.time DO WHILE done = 0 build.cudls.array cudls.data.average print.cudls.array LOOP cudls.start.time build.cudls.array cudls.data.average 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$ = "cudls" REM PRINT #4, "build.cudls.array start" REM PRINT #4, "VAL(clocmin$(i))=", VAL(clocmin$(i)) REM 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 Checks for large blocks of missing data. IF VAL(clocmin$(i)) > setmin OR VAL(clochr$(i)) <> sethr THEN REM PRINT #4, "Missing cudls data. Resetting start time." cvar$ = "missing.data" cudls.start.time END IF 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 ****" REM IF direct$ = "endeco.only" GOTO 77 PRINT #4, "i="; i PRINT #4, cloctime$(i) REM Input line with the * at the end. clocmin$(i) = MID$(cloctime$(i), 4, 2) clochr$(i) = LEFT$(cloctime$(i), 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), co(i), cp(i), cq(i), star$(i) REM PRINT 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) REM PRINT #4, (clochr$(i)) REM 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 REM lprint PRINT #4, "k=", k PRINT #4, "i=", i REM PRINT #4, clat(k) REM lprint 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) IF cn(k) = 0 THEN ancount = ancount - 1 END IF an = an + cn(k) IF co(k) = 0 THEN aocount = aocount - 1 END IF ao = ao + co(k) IF cp(k) = 0 THEN apcount = apcount - 1 END IF ap = ap + cp(k) IF cq(k) = 0 THEN aqcount = aqcount - 1 END IF aq = aq + cq(k) NEXT REM ***** CALCULATING AVERAGE VALUES ***** REM The following IF's are to avoid division by zero, which is caused when REM zeros are logged, i.e. GPS data, any channel voltage. IF alacount > 0 THEN alat = alat / alatcount END IF PRINT #4, alat IF aloncount > 0 THEN alon = alon / aloncount END IF PRINT #4, alon IF agcount > 0 THEN ag = ag / agcount END IF PRINT #4, ag IF aicount > 0 THEN ai = ai / aicount END IF PRINT #4, ai IF ajcount > 0 THEN aj = aj / ajcount END IF PRINT #4, aj IF akcount > 0 THEN ak = ak / akcount END IF PRINT #4, ak IF alcount > 0 THEN al = al / alcount END IF PRINT #4, al IF amcount > 0 THEN am = am / amcount END IF PRINT #4, am IF ancount > 0 THEN an = an / ancount END IF PRINT #4, an IF aocount > 0 THEN ao = ao / aocount END IF PRINT #4, ao IF apcount > 0 THEN ap = ap / apcount END IF PRINT #4, ap IF aqcount > 0 THEN aq = aq / aqcount END IF PRINT #4, aq PRINT #4, "cudls.data.average end" END SUB SUB cudls.start.time DIM star$(maxlines) where$ = "cudls" sethr = 0: REM Start averaging at midnight 00:00 setmin = 0 PRINT #4, "d=", 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 #4, "First time ever read = ", cloctime$(i) REM Input line with the * at the end. clocmin$(i) = MID$(cloctime$(i), 4, 2) clochr$(i) = LEFT$(cloctime$(i), 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), co(i), cp(i), cq(i), star$(i) PRINT #4, "start.time.record= ", 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) REM PRINT #4, (clochr$(i)) REM PRINT #4, (clocmin$(i)) REM Set hours 0-24. 1000 IF VAL(clochr$(i)) > sethr THEN sethr = sethr + 1 GOTO 1000 ELSE REM PRINT #4, "sethr="; sethr END IF REM Set starting minute interval 2000 IF VAL(clocmin$(i)) >= setmin THEN setmin = setmin + d GOTO 2000 ELSEIF setmin = 0 THEN setmin = d - 1 ELSE setmin = setmin - 1 REM PRINT #4, "setmin="; setmin END IF 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 #4, "print.cudls.array begin" GOSUB 240 REM i-1 and j-1 may be better for date REM PRINT #4, "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) co(1) = co(i) cp(1) = cp(i) cq(1) = cq(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 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 #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