pro poet_edg

	close, /all
	
	tab = string(9B)
	car = string(9B)	
	nom = ' '
	ligne = ' '
	field = 0
	i = 0
	j = 0
	ok = 0

	base = 'POET'
	espece = ' '
	n1 = 0
	n2 = 0
	units = 'kg(species)/m²/s'
	s_reso = '1°x1°'
	t_reso = 'monthly'
 	sources = ' '
	
	print, 'nom du fichier à convertir :'
	read, nom

	n1 = strpos(nom, '.')
	n2 = strpos(nom, '.', n1+1)
	espece = strmid(nom, n1+1, n2-n1-1)
	sources = strmid(nom, 0, n1)
	print, 'espece : ', espece
	print, 'source : ', sources
	
	openr, 1, nom
	while not EOF(1) do begin
		readf, 1, ligne
		ligne = strcompress(ligne,/remove_all)
		car = strmid(ligne,0,1)
		if ((car ne '0') and (car ne '1') and (car ne '2') and (car ne '3') and (car ne '4') and (car ne '5') and (car ne '6') and (car ne '7') $
		   and (car ne '8') and (car ne '9') and (car ne '-') and (car ne tab) and (car ne ' ')) then begin
	   		field++
			print, field
 	   		print, ligne
		endif
	endwhile
	close, 1

	nom_var = strarr(field)
	nb_val = fltarr(field)

	i = -1
	
	openr, 1, nom
	while not EOF(1) do begin
		readf, 1, ligne
		ligne = strcompress(ligne,/remove_all)
		car = strmid(ligne,0,1)
		if ((car ne '0') and (car ne '1') and (car ne '2') and (car ne '3') and (car ne '4') and (car ne '5') and (car ne '6') and (car ne '7') $
		   and (car ne '8') and (car ne '9') and (car ne '-') and (car ne tab) and (car ne ' ')) then begin
			i++
		   	nom_var(i) = ligne
			print, nom_var(i)
		endif else begin
			nb_val(i) = nb_val(i) + 8
		endelse
	endwhile
	close, 1

	nb_val(where(strupcase(nom_var) eq 'TIME')) = nb_val(where(strupcase(nom_var) eq 'TIME')) - 4
  nb_val(where(strupcase(nom_var) eq 'LAT')) = nb_val(where(strupcase(nom_var) eq 'LAT')) - 4
	if strpos(nom, 'bio') ne -1 then begin
		nb_val(where(strupcase(nom_var) eq 'BIO')) = nb_val(where(strupcase(nom_var) eq 'BIO')) / 12
	endif
	
	print, nom_var
	print, nb_val
	
	mois = fltarr(nb_val(where(strupcase(nom_var) eq 'TIME')))
	lon = fltarr(nb_val(where(strupcase(nom_var) eq 'LON')))
	lat = fltarr(nb_val(where(strupcase(nom_var) eq 'LAT')))
	if (nb_val(where(strupcase(nom_var) eq 'TIME')) eq 12) then begin
		val1 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val2 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val3 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val4 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val5 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val6 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val7 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val8 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val9 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val10 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val11 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
		val12 = dblarr(nb_val(where(strupcase(nom_var) eq 'LON')) , nb_val(where(strupcase(nom_var) eq 'LAT')))
	endif
	
	openr, 1, nom
	
	while not EOF(1) do begin
		readf, 1, ligne
		ligne = strcompress(ligne, /remove_all)
		if (strupcase(ligne) eq 'TIME') then readf, 1, mois
		if strupcase(ligne) eq 'LON' then readf, 1, lon
		if strupcase(ligne) eq 'LAT' then readf, 1, lat
		if (strupcase(ligne) eq 'BIO' or strupcase(ligne) eq 'OCEANS') then begin	
			print, ligne
 			readf, 1, val1
			print, 'val1'
	 		readf, 1, val2
			print, 'val2'
	 		readf, 1, val3
			print, 'val3'
	 		readf, 1, val4
			print, 'val4'
	 		readf, 1, val5
			print, 'val5'
			readf, 1, val6
			print, 'val6'
	 		readf, 1, val7
			print, 'val7'
	 		readf, 1, val8
			print, 'val8'
	 		readf, 1, val9
			print, 'val9'
 			readf, 1, val10
			print, 'val10'
 			readf, 1, val11
			print, 'val11'
	 		readf, 1, val12
			print, 'val12'
		endif
	endwhile
 	close, 1
	
	print, 'Recuperation terminee.'

        imax = nb_val(where(strupcase(nom_var) eq 'LON')) - 1

        for i=0,imax(0) do begin
                if lon(i) gt 180. then begin
                        lon(i) = lon(i)-360.
                endif
        endfor

        imax = size(lon, /n_elements) - 1
        jmax = size(lat, /n_elements) - 1

	print, 'Debut de la conversion de molecules/cm**2/s en kg/m**2/s'
	tab_especes = ['acet','c2h4','c3h8','co','terpenes','c2h6','nox','ch3oh','isoprene']
	tab_masses = [9.644,4.658,7.323,4.650,22.62,4.993,4.982,5.321,11.31] ;pour les terpenes : 100% monoterpenes
	massmol = 1.E-26*tab_masses(where(tab_especes eq espece))
	for i=0,imax do begin
	  for j=0,jmax do begin
	val1(i,j) = val1(i,j)*massmol*10000.
	val2(i,j) = val2(i,j)*massmol*10000.
	val3(i,j) = val3(i,j)*massmol*10000.
	val4(i,j) = val4(i,j)*massmol*10000.
	val5(i,j) = val5(i,j)*massmol*10000.
	val6(i,j) = val6(i,j)*massmol*10000.
	val7(i,j) = val7(i,j)*massmol*10000.
	val8(i,j) = val8(i,j)*massmol*10000.
	val9(i,j) = val9(i,j)*massmol*10000.
	val10(i,j) = val10(i,j)*massmol*10000.
	val11(i,j) = val11(i,j)*massmol*10000.
	val12(i,j) = val12(i,j)*massmol*10000.
	  endfor
	endfor
	print, 'Masse moleculaire : ', massmol
	print, 'Conversion terminee.'
	
	print, 'Nom du fichier de sortie ?'
	read, nom
	
 	openw, 1, nom
		printf, 1, '# Database used : '+base
		printf, 1, '# Emissions for '+espece;+' in '+year
		printf, 1, '# Units : '+units
		printf, 1, '# Spatial resolution : '+s_reso
		printf, 1, '# Temporal resolution : '+t_reso
		printf, 1, '# Sources : '+sources ;(see the README file) : '+sources
		printf, 1, '# longitude (°N) , latitude (°E) , flux1...12'
		for i=0,imax do begin
			for j=0,jmax do begin
				if ((val1(i,j) ne 0.) or (val2(i,j) ne 0.) or (val3(i,j) ne 0.) or (val4(i,j) ne 0.)$
			   	or (val5(i,j) ne 0.) or (val6(i,j) ne 0.) or (val7(i,j) ne 0.) or (val8(i,j) ne 0.)$
			   	or (val9(i,j) ne 0.) or (val10(i,j) ne 0.) or (val11(i,j) ne 0.) or (val12(i,j) ne 0.)) then begin
					ligne =  $
				string(format='(f7.2)',lon(i))+' '+string(format='(f7.2)',lat(j))+' '+string(format='(e13.7)',val1(i,j))+' '+string(format='(e13.7)',val2(i,j))$
                                +' '+string(format='(e13.7)',val3(i,j))+' '+string(format='(e13.7)',val4(i,j))+' '+string(format='(e13.7)',val5(i,j))+' '+string(format='(e13.7)',val6(i,j))$
                                +' '+string(format='(e13.7)',val7(i,j))+' '+string(format='(e13.7)',val8(i,j))+' '+string(format='(e13.7)',val9(i,j))+' '+string(format='(e13.7)',val10(i,j))$
                                +' '+string(format='(e13.7)',val11(i,j))+' '+string(format='(e13.7)',val12(i,j))
						printf, 1,ligne
					endif
				endfor
			endfor
 	close, 1

	print, 'Fait !!'
stop
end
