function tests_conceptnames_midW(conceptname)

% TESTS_CONCEPTNAMES_MIDW: Génère un arbre à partir d'un concept, query VARS et renvoie le nombre d'annotations trouvées pour chaque sous-concept
%
% tests_conceptnames_midW(conceptname)
% e.g. tests_conceptnames_midW('Siphonophorae')
%
% Monique, octobre 2014, MBARI


[liste_concepts,liste_ranks]=tree_VARS(conceptname,'down');
v=sqlquery('SELECT DISTINCT rov, dive, day_night, year FROM transect_data','midwater'); 
iok = eqStrtot(v.day_night,'D') & v.year>=1997; for varname=permute(fieldnames(v),[2 1]), varname=varname{:}; v.(varname)=v.(varname)(iok); end
counts=obj_intTS(liste_concepts,'depth',[50 100:100:1000],'time',ones(length(v.dive),1)*NaN,'datablank'); counts.attributs.ROVdive=cell(length(v.dive),1);
filename='/home/monique/Desktop/tree.txt';

for idive=1:length(v.dive), ROVdive=[v.rov{idive},num2str(v.dive(idive))]; 
	disp_pct(idive,length(v.dive))
	annotations=charge_VARS(ROVdive,liste_concepts);
	if ~isempty(annotations), counts.all(:,:,idive)=annotations.all; counts.attributs.ROVdive{idive}=ROVdive; counts.time(idive)=annotations.time; 
	else write_output(['no data ~ ',ROVdive],filename), end
end
counts=extract(counts,'time',~isnan(counts.time));
counts=extract(counts,'species',meanNaN_2D(permute(counts.all,[2 3 1]))>0);
%data_transects=extract(charge_transectsMidW('counts3D','transects'),'period',[1997 2020]);
%data_transects=extract(data_transects,'species',liste_concepts);
% l'ordre temps/species est différent mais faire tourner sur data_transects au lieu de counts donne la même chose (quasi, encore qqs problèmes).


ispecies=eqStrtot(liste_concepts,counts.species); 
liste_concepts=liste_concepts(ispecies); liste_ranks=liste_ranks(ispecies);
[liste_concepts,ii]=sort(liste_concepts); liste_ranks=liste_ranks(ii);

isdone = logical(zeros(length(liste_concepts),1));
liste_order = [reshape_ligne(liste_concepts(eqStrtot(liste_ranks,'order'))),{'other'}];
% loop on order names
if ~isempty(liste_order), for order = liste_order, order=order{:}; write_output(' ',filename), str=['Order: ',order];
	if ~strcmp(order,'other'), 
		isdone(eqStrtot(liste_concepts,order))=true; str=[str,' - ',num2str(sumNaN(aplat(counts.all(eqStrtot(counts.species,order),:,:))))];
		liste_family = [reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_concepts,tree_VARS(order,'down')) & eqStrtot(liste_ranks,'family'))),{'other'}];
	else 
		liste_family = [reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_concepts,tree_VARS(order,'down')) & eqStrtot(liste_ranks,'family'))),{'other'}];
	end, write_output(str,filename)
	% loop on family names
	if ~isempty(liste_family), for family = liste_family, family=family{:};	str=['   Family: ',family];
		if ~strcmp(family,'other'), 
			isdone(eqStrtot(liste_concepts,family))=true; str=[str,' - ',num2str(sumNaN(aplat(counts.all(eqStrtot(counts.species,family),:,:))))];
			liste_genus = [reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_concepts,tree_VARS(family,'down')) & eqStrtot(liste_ranks,'genus'))),{'other'}];
		else
			if ~strcmp(order,'other'), liste_genus = [reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_concepts,tree_VARS(order,'down')) & eqStrtot(liste_ranks,'genus'))),{'other'}];
			else liste_genus = [reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_ranks,'genus'))),{'other'}];
			end
		end, write_output(str,filename)
		% loop on genus names
		if ~isempty(liste_genus), for genus = liste_genus, genus=genus{:}; str=['      Genus: ',genus];
			if ~strcmp(genus,'other'), 
				isdone(eqStrtot(liste_concepts,genus))=true; str=[str,' - ',num2str(sumNaN(aplat(counts.all(eqStrtot(counts.species,genus),:,:))))];
				liste_species = [reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_concepts,tree_VARS(genus,'down')) & eqStrtot(liste_ranks,'species')))];
			else 
				if ~strcmp(family,'other'), liste_species = reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_concepts,tree_VARS(family,'down')) & eqStrtot(liste_ranks,'species')));
				elseif ~strcmp(order,'other'), liste_species = reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_concepts,tree_VARS(order,'down')) & eqStrtot(liste_ranks,'species')));
				else liste_species = reshape_ligne(liste_concepts(~isdone & eqStrtot(liste_ranks,'species')));
				end
			end, write_output(str,filename)
			% loop on species name
			if ~isempty(liste_species), for species = liste_species, species=species{:}; str=['         Species: ',species];
				isdone(eqStrtot(liste_concepts,species))=true; str=[str,' - ',num2str(sumNaN(aplat(counts.all(eqStrtot(counts.species,species),:,:))))]; write_output(str,filename)
			end, end
		end, end
	end, end
end, end

write_output(' ',filename)
liste_badranks=trie_vecteur(liste_ranks(~isdone));
if ~isempty(liste_badranks), error('??'), end
for concept = reshape_ligne(liste_concepts(~isdone)), concept=concept{:};	
	write_output(['??: ',concept,' - ',num2str(sumNaN(aplat(counts.all(eqStrtot(counts.species,concept),:,:))))],filename)
end


return









