/**
Load climate data sets for the SBEII
Two text files are loaded into two arrays: RO_refr.txt and RO_temp.txt

KV, BCL, 02/2010
**/
(
q = q ?? ();
q.myPath = Document.current.dir; 
q.refracfile = q.myPath ++ "/RO_refr.txt";
q.tempfile = q.myPath ++ "/RO_temp.txt";
)
// CHOOSE:
q.myfile = q.refracfile;
q.myfile = q.tempfile;

// File.exists(q.myfile);
// g = File(q.myfile, "r")

(
a = Array.newFromFile(q.myfile, $ , $\n);
//a.printAll;

// header: a[0..19].printAll;

// latitudes north (18)
q.latitudes = a[20..22].collect({|item| item.asFloat}).flat.removeEvery([0.0]);
//q.latitudes.size;

// header: a[23..24].printAll;
// height levels in m (9)
q.heightlevs = a[25..26].collect({|item| item.asFloat}).flat.removeEvery([0.0]);
//q.heightlevs.size;

// header: a[27..28];
// time steps (julian time)
q.timejul = a[29..49].collect({|item| item.asFloat}).flat.removeEvery([0.0]);
//q.timejul.size;

//header: a[50];
// time steps: calender time, YYYY-MM-DD
q.timecal = a[51..71].collect({|item, i| 
	var aii = Array.new; 
	item.do{|jtem| aii = aii ++ ([jtem.keep(4)++jtem[5..6]++jtem[8..9]]);}; 
	// 	change date to YYYYMMDD without "-"
	aii.asFloat;
}).flat.removeEvery([0.0]);
//q.timecal.size;

// header: a[72]
// first time step, 162 entries (18 latitudes x 9 heightlevs):
a[73..99].asFloat.collect({|item| item}).flat.removeEvery([0.0]);

q.blanks = (q.timejul.size).collect{|i| (73+(27*(i))+i-1) };
q.data = Array.fill(q.timejul.size);
//first entry:
//a[73..100].collect({|item| item.asFloat}).flat.removeEvery([0.0]);
// all the rest of data:
q.blanks.do{|bs, bi|
	if (bi < (q.timejul.size-1)) 
		{q.data[bi] = 
		(a[bs..(q.blanks[(bi+1)]-1)].collect({
			|item| item.asFloat
		}).flat.removeEvery([0.0])).clump(q.latitudes.size)}
		{// last data index
		q.data[bi] = 
		(a[bs..(a.lastIndex)].collect({
			|item| item.asFloat
		}).flat.removeEvery([0.0])).clump(q.latitudes.size)};
};
)
//q.data.shape
//q.blanks.last
q.data.flat.printAll
(
// eliminate the nans and replace them by -999s
q.data.do{|planes, p|
	planes.do{|rows, r|
		rows.do{|val, c|
			if (val>0) // thus not nan!
			{val}
			{q.data[p][r][c] = -999;};
		}
	}
};
)
//q.data = q.data.flop