key off
REM TO SELECT from FLat files (chemistry, generated by ICES-CSV.
ON ERROR GOTO handle
CLS
f$ = COMMAND$
DIM a(25), gp1(1500), gp2(1500), gpn(1500)
print "                      ****CSV-SUR****"
print
print "    Prepares selections from .CSV files for use in graphic packages etc"
print "      .CSV files are produced from ICES files by running ICES-CSV"
print "(Default field separator is <space> - Use command Line argument to change)"

PRINT "Enter CSV File name"; : INPUT b$
OPEN "I", #1, b$
LINE INPUT #1, a$
INO = 1
FOR ii = 1 TO LEN(a$)
IF MID$(a$, ii, 1) = "," THEN INO = INO + 1
NEXT ii

IF INO = 12 OR INO = 24 THEN
' all ok
inq=ino
ELSE
CLS
PRINT "CSV File of incorrect format - run aborted": PRINT INO: END
END IF
CLOSE #1
OPEN "I", #1, b$

IF INSTR(b$, ".") <> 0 THEN
cout$ = LEFT$(b$, INSTR(b$, ".") - 1)
ELSE
cout$ = b$
END IF
cout$ = cout$ + ".flt"' output file given flt extension
OPEN "O", #2, cout$
ren:
PRINT
PRINT "<1>. Coun/ship  <2>. Station No  <3>. Lat  <4>. Lon <5>. Year  <6>. Month"
print "<7>. Day        <8>. time        <9>. Sounding"
PRINT "<10>.Obs depth  <11>.Temperature <12>.Salinity"
IF INO > 12 THEN
PRINT "<13>.Oxygen     <14>.Phosphate   <15>.Total Phosphorus <16>.Silicate"
print "<17>.Nitrate    <18>.Nitrite     <19>.Ammonium         <20>.Total Nitrogen"
print "<21>.Hydrogen Sulphide  <22>. pH <23>.Alkalinity       <24>. chlorophyll a"
END IF
PRINT
PRINT "If you select <3> (latitude) then <4> (longitude) is automatically taken"
PRINT "...you have also the option to pick a depth range"
PRINT
PRINT "Enter independent var. now "; : INPUT ind

PRINT "enter dependent var now "; : INPUT dep
IF ind > INO OR dep > INO THEN PRINT "cant do that - try again!": GOTO ren
IF ind < 1 OR dep < 1 THEN PRINT "cant do that - try again!": GOTO ren
IF ind = 3 THEN GOSUB lmt'limited area
GOSUB dps 'depth selection

IF dep <> 17 THEN GOTO winon
PRINT "Say Y if you want the sum of nitrate+nitrite": INPUT sum$
sum$=ucase$(sum$)
IF sum$ = "Y" THEN PRINT "OK, sum will be done" ELSE PRINT "OK, will not be summed"
winon:
PRINT "Enter Y for winter data only"; : INPUT wint$
wint$ = UCASE$(wint$)
IF ind <> 3 THEN
PRINT "Interval width for grouped indep variable (return to pass) "; : INPUT WID
IF WID <> 0 THEN
PRINT "Max value of indep var for grouping "; : INPUT mav
END IF
END IF
WHILE NOT EOF(1)
I = I + 1
IF I = INO + 1 THEN
I = 1: GOSUB proc
END IF
INPUT #1, a(I)
WEND

GOSUB proc
IF WID <> 0 THEN GOSUB mavout
locate 25,1:
PRINT "Number of cycles = "; cn;
PRINT " Stored in file "; cout$;
if i<>inq then
print " caution -";b$;" corrupted";
end if
end
END

proc:
IF a(dep) < -8 THEN RETURN
IF lim$ <> "Y" THEN GOTO aIl
IF a(3) < Slat OR a(3) > Nlat THEN RETURN
IF a(4) < Wlon OR a(4) > Elon THEN RETURN

aIl:
IF wint$ <> "Y" THEN GOTO nowin
IF a(6) = 12 OR a(6) = 1 OR a(6) = 2 OR a(6) = 3 THEN GOTO nowin ELSE RETURN
nowin:
G0 = 1
GOSUB nogo
IF G0 = 0 THEN RETURN
IF ind <> 3 THEN GOTO nopos
cn = cn + 1:
PRINT #2, using "-##.##"; a(4);
PRINT #2, f$;
PRINT #2, USING " -###.##"; a(3);
PRINT #2, f$;
PRINT #2, USING "####.###"; a(dep)
locate 25,10: print "Selection ";cn;
RETURN

nopos:
IF a(ind) < -8 OR a(dep) <= -8 THEN RETURN
IF dep = 17 AND sum$ = "Y" THEN a(17) = a(17) + a(18)
cn = cn + 1
IF WID > 0 THEN ax = a(ind): ay = a(dep): GOSUB aver: RETURN
IF ind = 10 THEN a(ind) = -a(ind)
if ind=5 then
a(5)=a(5)+(a(6)-1)/12
end if
PRINT #2, USING "#####.###"; a(ind);
PRINT #2, f$;
PRINT #2, USING "####.###"; a(dep)
locate 25,10: print "Selection ";cn;
RETURN

dps: 'depth selection
PRINT "If all depths press return else press S(urface) or B(ottom)"; : INPUT SUDEP$
IF SUDEP$ = "" THEN RETURN
SUDEP$ = UCASE$(SUDEP$)
if sudep$="S" or sudep$="B" then
rem
else
sudep$=""
return
end if
low1:
rem PRINT "If Depth range relative to surface then enter <S> else enter <B>"
rem INPUT SUDEP$
rem SUDEP$ = UCASE$(SUDEP$)
rem IF SUDEP$ = "S" OR SUDEP$ = "B" THEN GOTO low ELSE GOTO low1
low:
if sudep$="S" then
print "OK - relative to surface"
else
print "OK - relative to bottom"
end if
print
PRINT "Enter LOWEST Depth rel to top or bottom": INPUT MIDEP
PRINT "Enter HIGHEST Depth rel to top or bottom": INPUT MADEP
RETURN

lmt: ' subroutine for luimited area selection
PRINT "Say Y if you want limited area only"; : INPUT lim$
IF lim$ <> "Y" THEN RETURN
PRINT "Respond to the following with 4 digits, as in ICES format"
PRINT "Enter Northern Latitude"; : INPUT Nlat
PRINT "Enter Southern latitude"; : INPUT Slat
PRINT "Enter Eastern Longitude (- for west of 0)"; : INPUT Elon
PRINT "Enter Western Longitude (- for west of 0)"; : INPUT Wlon
RETURN

nogo:
IF a(10) < 0 THEN G0 = 0: RETURN
IF SUDEP$ = "" THEN G0 = 1: RETURN
IF a(9) < 0 AND SUDEP$ = "B" THEN G0 = 0: RETURN
IF SUDEP$ = "B" THEN MIND = a(9) - MADEP: MAND = a(9) - MIDEP
IF MIND < 0 THEN MIND = 0
IF MAND < 0 THEN MAND = 0
IF SUDEP$ = "S" THEN MIND = MIDEP: MAND = MADEP
IF SUDEP$ = "" THEN RETURN
IF a(10) >= MIND AND a(10) <= MAND THEN G0 = 1 ELSE G0 = 0
RETURN

aver: 'groups into intervals
ss = 0
FOR ii = 0 TO mav STEP WID
ss = ss + 1
IF ax >= ii AND ax < ii + WID THEN gp2(ss) = gp2(ss) + ay: gpn(ss) = gpn(ss) + 1
NEXT ii
RETURN

mavout: 'output of grouped averages
ss = 0: cn = 0
FOR ii = 0 TO mav STEP WID
ss = ss + 1
IF gpn(ss) = 0 THEN GOTO nt1
PRINT #2, USING "####.##"; ii + WID / 2;
PRINT #2, f$;
PRINT #2, USING "####.##"; gp2(ss) / gpn(ss)
cn = cn + 1
PRINT USING "####.###  ####.###"; ii + WID / 2; gp2(ss) / gpn(ss)
nt1:
NEXT ii

RETURN

handle:
CLS
if err=64 then end
IF ERR = 53 THEN
PRINT "File "; b$; " not available on current directory - run aborted": END
END IF
IF ERR = 9 THEN
PRINT "Too many ranges selected - run aborted": END
END IF
PRINT "Error "; ERR; " encountered"
END

