'check hydro files

ON ERROR GOTO handle
CLS
LOCATE 2, 8: PRINT "*** Quality check of ICES data Files ***": PRINT
DIM SUP$(20), PAR(14), RAP(14), ros$(14), JUL(12)
FOR i = 0 TO 11
 READ JUL(i)
 NEXT i
DATA 0,31,59,90,120,151,181,212,243,273,304,334
F$=COMMAND$
INPUT "File Name "; aa$
OPEN "I", #1, aa$

j = INSTR(aa$, ".")

IF j = 0 THEN
bind$ = aa$
ELSE
bind$ = LEFT$(aa$, j - 1)
END IF

bout$ = bind$ + ".ERR"
OPEN "O", #2, bout$
LOCATE 10, 20
PRINT "stations checked"
strt:

WHILE NOT EOF(1)
LINE INPUT #1, A$
IF LEN(A$) <> 80 THEN
  CLS
  LOCATE 10, 5: PRINT "Invalid record length "; LEN(A$); " at record no "; recno + 1
  END
END IF
b$ = RIGHT$(A$, 1)
recno = recno + 1
IF b$ = "J" OR b$ = "1" THEN
  sond1$ = sond$
  COSR$ = LEFT$(A$, 4): DITT$ = MID$(A$, 20, 6)
  lz$ = MID$(A$, 69, 2): GOSUB fill:
  lz = (VAL(lz$)) / 60
  titt = VAL(MID$(A$, 26, 2)) + lz
  dep$ = ""
   IF FI = 0 THEN
     DITF$ = DITT$: RECN1 = 1
     COSL$ = COSR$: DITL$ = DITT$
     yr1 = VAL(MID$(DITL$, 1, 2))
     GOSUB stdat
     latl = latt: longl = longt
     GOSUB JUL
     FI = 1
     stno = stno + 1
     GOTO strt
   END IF
 stno = stno + 1
LOCATE 10, 12: PRINT stno
GOSUB stdat

IF val(sond1$)+4 < val(left$(dep1$,4)) AND sond1$ > "    " THEN
  ercount = ercount + 1
  PRINT #2, "record:";
  PRINT #2, USING "#######"; recno;
  PRINT #2, " / sounding < depth  ";
  PRINT #2, "/ Ship: "; COSR$; " / Station: "; NO$
END IF

GOSUB JUL
END IF

IF b$ = "3" OR b$ = "6" THEN
dep1$ = MID$(A$, 28, 4) + b$
 IF val(dep$) > val(dep1$) and ucase$(f$)<>"D" THEN

  ercount = ercount + 1
  PRINT #2, "record:";
  PRINT #2, USING "#######"; recno;
  PRINT #2, " / decreasing depth  ";
  PRINT #2, "/ Ship: "; COSR$; " / Station: "; NO$
 END IF
dep$ = dep1$
END IF
IF b$ = "3" OR b$ = "J" OR b$ = "1" OR b$ = "6" THEN
' ok
ELSE
  ercount = ercount + 1
  PRINT #2, "record:";
  PRINT #2, USING "#######"; recno;
  PRINT #2, " / in valid record   ";
  PRINT #2, "/ Ship: "; COSR$; " / Station: "; NO$
END IF
IF FI = 0 THEN PRINT "BAD START": END
WEND
PRINT
LOCATE 12, 12
IF ercount = 0 THEN PRINT "No suspect data encountered!!": END
PRINT ercount; "suspect data flagged in file "; bout$
END

fill:
FOR i = 1 TO LEN(lz$)
IF MID$(lz$, i, 1) = " " THEN MID$(lz$, i) = "0"
NEXT i
RETURN

stdat:

700 IF MID$(A$, 18, 1) = "0" THEN n$ = "N": e$ = "E": GOTO 750
710 IF MID$(A$, 18, 1) = "1" THEN n$ = "N": e$ = "W": GOTO 750
720 IF MID$(A$, 18, 1) = "2" THEN n$ = "S": e$ = "E": GOTO 750
730 IF MID$(A$, 18, 1) = "3" THEN n$ = "S": e$ = "W": GOTO 750
740 n$ = " ": e$ = " "
750 NO$ = MID$(A$, 5, 4) + " "
lz$ = MID$(A$, 65, 2): GOSUB fill:
p1$ = MID$(A$, 11, 2): p2$ = MID$(A$, 16, 2): p3$ = MID$(A$, 69, 2)
p4$=mid$(a$,26,2)

IF p1$ >= "60" OR p2$ >= "60" OR p3$ >= "60" THEN

ercount = ercount + 1
PRINT #2, "record:";
PRINT #2, USING "#######"; recno;
PRINT #2, " / minutes >= 60     ";
PRINT #2, "/ Ship: "; COSR$; " / Station: "; NO$
END IF

if p4$>"24" then
ercount=ercount+1
print #2,  "record:";
print #2, using "#######"; recno;
print #2, " / hours > 24        ";
print #2, "/ Ship: "; COSR$; " / Station: ";NO$
end if

latt = VAL(MID$(A$, 9, 2)) + VAL(MID$(A$, 11, 2)) / 60 + VAL(lz$) / 1000
IF n$ = "S" THEN latt = -latt
lz$ = MID$(A$, 67, 2): GOSUB fill
longt = VAL(MID$(A$, 13, 3)) + VAL(MID$(A$, 16, 2)) / 60 + VAL(lz$) / 1000
IF e$ = "W" THEN longt = -longt
COSH$ = LEFT$(A$, 4)
770 TME$ = MID$(A$, 26, 2) + ":" + MID$(A$, 69, 2) + " "
780 NME$ = LEFT$(A$, 4) + " ": sond$ = MID$(A$, 28, 4)
790 DTE$ = MID$(A$, 24, 2) + "/" + MID$(A$, 22, 2) + "/" + MID$(A$, 20, 2) + " "

RETURN

JUL:

1110 yr2 = VAL(MID$(DITT$, 1, 2))

1130 MN2 = VAL(MID$(DITT$, 3, 2))

1150 DY2 = VAL(MID$(DITT$, 5, 2))

1152 jl2 = DY2 + JUL(MN2 - 1) + (yr2 - yr1) * 365
1153 jl = jl2 * 24 + titt - (jl1 * 24 + titl)

IF FI <> 0 AND COSR$ = COSL$ THEN
dist1 = (latt - latl) ^ 2
dist2 = ((longt - longl) * COS(.01745329 * (latt + latl) / 2)) ^ 2
dist = sqr((dist2 + dist1)) * 60
IF jl <> 0 THEN spd = dist / jl ELSE spd = 0
if spd>0 then
if dist <5 and jl<0.5 then spd=0
end if
IF spd > 25 OR spd < -0.01 THEN

 ercount = ercount + 1
PRINT #2, "record:";
PRINT #2, USING "#######"; recno;
IF spd > 40 THEN PRINT #2, " / gross speed error ";
IF spd < 0 THEN PRINT #2, " / -ve speed error   ";
IF spd > 25 AND spd < 40 THEN
PRINT #2, " / suspect speed     ";
end if
PRINT #2, "/ Ship: "; COSR$; " / Station: "; NO$
END IF
 if mid$(a$,26,2)="  " then ' time not recorded
  if dist>30 then
    ercount = ercount + 1
    PRINT #2, "record:";
    PRINT #2, USING "#######"; recno;
    PRINT #2, " / large stn distance";
    PRINT #2, "/ Ship: "; COSR$; " / Station: "; NO$
   end if
 end if

END IF
1160 IF COSR$ = COSL$ THEN
1161 DITL$ = DITT$: titl = titt
yr1 = yr2:
IF jl2 > 365 THEN jl2 = jl2 - 365
jl1 = jl2
latl = latt
longl = longt
GOTO 1260
END IF
1180 COSL$ = COSR$: DITL$ = DITT$: DITF$ = DITT$: titl = titt
IF jl2 > 365 THEN jl2 = jl2 - 365
jl1 = jl2
: yr1 = yr2
latl = latt
longl = longt
1260 RETURN



handle:
IF ERR = 53 THEN
 PRINT :
 PRINT "Data file "; aa$; " is not present in current directory - run aborted"
END
END IF
PRINT "error "; ERR
END

