key off
on error goto handle
f$=command$
if f$<>"" then jmpe
f$=","
cls
locate 1,1,0
PRINT "                   ***** ICES to CSV Format Conversion *****"
print "                      Provided with the compliments of the .."
print "              International Council for the Exploration of the Sea"
Print "                                  (ICES)"
print string$(80,196)
Color 9,0
print spc(55);"*":print:
print spc(30);"*"
print:
color 23,0:print spc(25);"*";:color 7,0
print spc(9);"*"
print:print spc(40);"*";spc(6);"*":print:print spc(40);"*     ";
print "*"
print string$(80,196)
Print "                              Pal‘gade 2-4"
print "                              1261 Copenhagen K"
print "                              Denmark"
print "                   Telemail - ices.dk/omnet"
Print "                   I-net    - ocean@server.ices.inst.dk"
print

locate 25,1
color 0,7
print " F5 - Information";tab (69) "<esc> - Quit";
color 7,0

while not instat
wend
in$=inkey$
if len (in$)=2 and asc(right$(in$,1))=63 then gosub info
if asc(in$)=27 then cls:end
jmpe:
cls
print "                      ********ICES-CSV********"
print
print "                  Converts ICES format to CSV format"

dim o3st(4),o3ln(4),o3dc(4)
dim chst(15),chln(15),chdc(15)
dim opu$(0:9),opv$(0:9)


print
locate,10:Input "Enter Name of Data File (ICES Format) "; B$
OPEN "I",#1,B$
locate 10,10:print "Press <enter> if depth (pressure), t and S file only required "
print
locate ,10:print "(Press any other key for these + full chemistry set)"
pts$=input$(1)
if asc(pts$)=13 then
print
locate ,10:print "Only p, t, and S selected"
lst=3
else
print
locate ,10:print "p, t, S, O2, nutrients, etc., selected"
lst=15
end if

if instr(b$,".")<>0 then
cout$=left$(b$,instr(b$,".")-1)
else
cout$=b$
end if
cout1$=cout$+".csv"

open "O",#2,cout1$

data 28,4,0,32,4,2,36,5,3,58,3,2 '03 record
for i = 1 to 4
read o3st(i),o3ln(i),o3dc(i)
next i
data 28,4,0,32,4,2,36,4,2,40,3,2,43,3,2,46,3,2,49,3,1 '76 record
data 52,3,1,55,3,2,58,3,1,61,3,1,64,3,1,67,3,2,70,4,3,74,3,1
for i = 1 to 15
read chst(i),chln(i),chdc(i)
next i

data "}","J","K","L","M","N","O","P","Q","R"
for i = 0 to 9
read opu$(i)
next i
data "{","A","B","C","D","E","F","G","H","I"
for i = 0 to 9
read opv$(i)
next i

line input #1,a$
while not eof(1)
line input #1,b$
rec$=mid$(a$,79,2)
gosub sel
a$=b$
wend
last=1
a$=b$
rec$=mid$(a$,79,2)
gosub sel
locate 20,10
print "CSV formatted data are stored in file..... ";cout1$
locate 21,13
print "(See information page for file layout)"
kill "tmp.tmp"
end

sel:
select case rec$
case="0J","02"
recno=0:stn=stn+1
locate 16,20:print stn;"  Stations Converted"
nop=1:'dpt$=""
sup=0
gosub mas
if rec$="02" then
gosub hydr
end if
case="03"
gosub sethydr
gosub hydr
case "76","56"
pol=0
gosub chem
case="P6"
pol=1
gosub chem
case "13","66"
rem do nothing
case else
ill=ill+1
if ill=10 then
print "Run aborted - not properly structured ICES data file"
end
end if
end select
return

mas:
datb$=""
datb$=mid$(a$,1,4)
par$=mid$(a$,5,4)
gosub lnt
datb$=datb$+f$+par$
'lat
qu$=mid$(a$,18,1)
if qu$="0" or qu$="1" then qun$="+" else qun$="-"
if qu$="0" or qu$="2" then que$="+" else que$="-"
latmin$=mid$(a$,11,2)+"."+mid$(a$,65,2)
latmin=val(latmin$)/60
par=val(mid$(a$,9,2))+latmin
fm$="##.##"
gosub usng
if qun$="-" then par$=qun$+par$
datb$=datb$+f$+par$

'long
longmin$=mid$(a$,16,2)+"."+mid$(a$,67,2)
longmin=val(longmin$)/60
par=val(mid$(a$,13,3))+longmin
fm$="###.##"
gosub usng

if que$="-" then par$=que$+par$
datb$=datb$+f$+par$

'date
datb$=datb$+f$+mid$(a$,20,2)
par$=mid$(a$,22,2)
gosub lnt
datb$=datb$+f$+par$
par$=mid$(a$,24,2)
gosub lnt
datb$=datb$+f$+par$
par$=mid$(a$,26,2)
if par$="  " then par$="-9"
gosub lnt
datb$=datb$+f$+par$

snd$=mid$(a$,28,4)
if snd$="    " then snd$="-9"
par$=snd$
gosub lnt
datb$=datb$+f$+par$
dats$=datb$
'print mas$
return

sethydr:
'dat1$=dat$
'if mid$(b$,80,1)="J" then mid$(dat1$,79)="1"
'if mid$(b$,80,1)="3" or mid$(b$,80,1)="6" then mid$(dat1$,79)="3"
return

hydr:
datb$=dats$
if lst=3 then lst1=3 else lst1=4
for ii = 1 to lst1
par$=mid$(a$,o3st(ii),o3ln(ii))
if par$<>string$(o3ln(ii),32) then
gosub fill
select case ii

case=1 ' depth
dpt$=par$
ps=2
gosub to11 'test for overpunch 11
  if f11=1 then 'overpunch 11 identified
   f11=0
   'questionable depth
  end if

ps=4
gosub to11
  if f11=1 then 'overpunch identified
   f11=0
'   mid$(dat1$,7)="T"
   ' unprotected thermometer
   end if
ps=1
gosub to11
 if f11=1 then
  f11=0
  par$="1"+par$
  else
  par$="0"+par$
  end if
gosub lnt
datb$=datb$+f$+par$

case =2 'temperature
dec=o3dc(2)
ps=1
gosub to11
neg$="0"
  if f11=1 then
   f11=0
   neg$="-"
   end if
ps=2
gosub to11
if f11=1 then
  f11=0
 'questionable value
end if
gosub short
nodc=5
gosub long
mid$(par$,1)=neg$
gosub plce
gosub lnt
datb$=datb$+f$+par$

case=3 'salinity
dec=o3dc(3)
ps=2
gosub to11
 if f11=1 then
  f11=0
  'questionable value
  end if
gosub short
nodc=5
gosub long
gosub plce
gosub lnt
datb$=datb$+f$+par$


case 4 'oxygen

dec=o3dc(4)

ps=2
gosub to11
 if f11=1 then
 f11=0
 'questionable value
 end if

ps=1
gosub to11
 if f11=1 then
  f11=0
  par$="1"+par$
  end if

gosub short
nodc=4
gosub long
gosub plce
gosub lnt
datb$=datb$+f$+par$

end select
else
if ii=1 and rec$="02" then
datb$=datb$+f$+"0"
else
datb$=datb$+f$+"-9"
end if
end if
next ii


if mid$(b$,28,4)=dpt$ and mid$(b$,80,1)="6" and last=0 then
nop=5
rem no print yet
else
nop=1
if lst=15 then
datb$=datb$+f$+"-9"+f$+"-9"+f$+"-9"+f$+"-9"+f$+"-9"+f$+"-9"+f$+"-9"
datb$=datb$+f$+"-9"+f$+"-9"+f$+"-9"+f$+"-9"
end if
print #2,datb$ ' print hydro record now if no chemistry follows
datb$=dats$
end if
return

chem:
if nop=1 then gosub sethydr
if nop=1 then datb$=dats$
if lst=3 and nop=5 then
nop=1
print #2,datb$
datb$=dats$
return
end if
if lst=3 then lst1=3 else lst1=15
for ii = nop to lst1
par$=mid$(a$,chst(ii),chln(ii))
if par$<>string$(chln(ii),32) then
gosub fill

select case ii

case=1 ' depth
dpt$=par$
ps=2
gosub to11 'test for overpunch 11
  if f11=1 then 'overpunch 11 identified
   f11=0
   'questionable depth
  end if

ps=4
gosub to11
  if f11=1 then 'overpunch identified
   f11=0
'   mid$(dat1$,7)="T"
   ' unprotected thermometer
   end if
ps=1
gosub to11
 if f11=1 then
  f11=0
  par$="1"+par$
    end if
  gosub lnt
datb$=datb$+f$+par$

case =2 'temperature
dec=chdc(2)
ps=1
gosub to11
neg$="0"
  if f11=1 then
   f11=0
   neg$="-"
   end if
ps=2
gosub to11
if f11=1 then
  f11=0
 'questionable value
end if
gosub short
mid$(par$,1)=neg$
gosub plce
gosub lnt
datb$=datb$+f$+par$

case=3 'salinity
dec=chdc(3)
ps=2
gosub to11
 if f11=1 then
  f11=0
  'questionable value
  end if
gosub short
gosub plce
gosub lnt
datb$=datb$+f$+par$

case=4 'oxygen
dec=chdc(4)

ps=2
gosub to11
 if f11=1 then
 f11=0
 'questionable value
 end if

ps=1
gosub to11
 if f11=1 then
  f11=0
  par$="1"+par$
  end if

gosub short
gosub plce
gosub lnt
datb$=datb$+f$+par$

case 5 'phosphate
nodc=4
dec=chdc(ii)-pol
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$  'phosphate

case 6 'total phosphate
nodc=4
dec=chdc(ii)-pol
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$  'total phosphate

case 7 'silicate
nodc=4
dec=chdc(ii)-pol
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$ 'silicate

 case = 8 'nitrate
 nodc=3
 dec=chdc(ii)-pol
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$ 'nitrate

 case=9 'nitrite
 nodc=3
dec=chdc(ii)-pol
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$ 'nitrite

 case=10 ' ammonia
nodc=4
dec=chdc(ii)-pol
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$

 case=11 'total nitrogen
nodc=4
dec=chdc(ii)-pol
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$

 case=12 'H2s
nodc=4
dec=chdc(ii)
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$


 case=13 'pH
nodc=3
dec=chdc(ii)
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$

 case=14 'alkalinity
nodc=4
 dec=chdc(ii)
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$

 case=15 'chl
nodc=4
dec=chdc(ii)
gosub par
gosub plce
gosub lnt
datb$=datb$+f$+par$
  end select
else
datb$=datb$+f$+"-9"
end if
next ii

print #2,datb$ ' print hydro record now if no chemistry follows

nop=1
return


par:
ps=2
gosub to11
 if f11=1 then
 f11=0
 'questionable value
 end if

ps=3
gosub to12
 if f12 then
  f12=0:'dec=4 in nodc format'(trace)
end if
ps=3
gosub to11
 if f11=1 then
 f11=0
 'dec=4 'in nodc format(trace - second definition)
end if

ps=1
gosub to11
 if f11=1 then
  f11=0
  par$="1"+par$
  end if

gosub short
gosub long

'while len(par$)>nodc
'dec=dec-1
'if dec<0 then
'dec=5
'par$=string$(nodc,"9")
'exit loop
'exit if
'end if
'par$=left$(par$,len(par$)-1)
'wend

return

opu1: 'identifying overpunch 11
for o11 = 0 to 9
if opu$(o11)=opu1$ then fu
next o11
fu:
return

to11: 'test for opunch 11
opu1$=mid$(par$,ps,1)
if asc(opu1$)>57 then
f11=1
gosub opu1
mid$(par$,ps)=right$(str$(o11),1)
end if
return

to12: 'test for opunch 12
opu1$=mid$(par$,ps,1)
if asc(opu1$)>57 then
f12=1
gosub opv1
if o12=10 then f12=0:exit if 'must be an opunch 11
mid$(par$,ps)=right$(str$(o12),1)
end if
return

opv1: 'identifying overpunch 12
for o12 = 0 to 9
if opv$(o12)=opv1$ then fv
next o12
fv:
return

fill: 'put in leading zeroes

IF par$ = STRING$(LEN(par$), " ") THEN RETURN
FOR ldz = 1 TO LEN(par$)
IF MID$(par$, ldz, 1) <> " " THEN EXIT FOR
MID$(par$, ldz) = "0"
NEXT ldz
RETURN

short: ' trim off trailing spaces
while right$(par$,1)=" "
par$=left$(par$,len(par$)-1)
dec=dec-1
wend
return

long:
while len(par$)<nodc
par$="0"+par$
wend
return

plce: 'place decimal point
dc=val(right$(str$(dec),1))
if dc>0 then
leng=len(par$)
par$=left$(par$,leng-dc)+"."+right$(par$,dc)
end if
return

lnt: 'remove leading zeroes
lnt=len(par$)
while lnt>1
parl$=left$(par$,1)
if parl$="0" or parl$=" " then
lnt=lnt-1
par$=right$(par$,lnt)
else
exit loop
end if
wend
return

info:
cls
color 0,7
print"                          Information about ICES-CSV                           "
color 7,0
print"       This utility removes the drudgery of having to read ICES "
print"       formatted oceanographic station data. Its main purpose is to"
print"       produce a comma-delimited file of station master and data"

print"       values.  ',' is the default separator on start-up. However whatever"
print"       key you press on leaving this page will be used as the value separator"
print"       (or invoke from command line). All missing values are <-9>."
print
print"                              OUTPUT FILE LAYOUT              "
print
print"       <1>  Country/ship                   <13> Oxygen"
print"       <2>  Station No.                    <14> Phosphate"
print"       <3>  Latitude (degrees/decimals)    <15> Total Phosphorus"
print"       <4>  Longitude (degrees/decimals)   <16> Silicate"
print"       <5>  Year (eg 92)                   <17> Nitrate"
print"       <6>  Month                          <18> Nitrite"
print"       <7>  Day                            <19> Ammonium"
print"       <8>  Time                           <20> Total Nitrogen"
print"       <9>  Sounding                       <21> Hydrogen Sulphide"
print"       <10> Observation depth              <22> pH"
print"       <11> Temperature                    <23> Alkalinity"
print"       <12> Salinity                       <24> Chlorophyll 'a'"
print
print"       [You may restrict your output to first column parameters (1-12)]";
locate 25,1
color 0,7:print string$(80,32);
locate 25,1
print "<Esc> - quit";
color 7,0
in$=input$(1)
f$=in$
return

usng: ' using$
open "tmp.tmp" for output as #10
print #10 ,using fm$;par
close #10
OPEN "TMP.tmp" for input as #10
input #10 ,par$
close #10
return


handle:
print "You have encountered a problem (";err;").. run aborted"

end
