%% plasigbf.m takes the output of a global run generated
%% by plasigb.m and stores the electron densities in a
%% text file with the name GPIDyyyydddtt.txt where yyyy is
%% the year, ddd is the day number and tt is the UT time
%% (rounded to nearest hour). 
%% The file contains the altitudes of the denities, the 
%% longitudes and latitudes. It then records the densities
%% at each altitude for a given longitude and latitude point.
%% Note that the resulting files can be VERY large. 
%%

%% Determine the correct day number string
if dno<10
 dnos=['00',int2str(dno)];
elseif dno<100
 dnos=['0',int2str(dno)];
else
 dnos=int2str(dno);
end

%% Determine the correct hour string
if timmat<10
 tims=['0',int2str(round(timmat))];
else
 tims=int2str(round(timmat));
end

filse=['GPID',int2str(year),dnos,tims,'_e.txt'];

fid=fopen(filse,'w');
tits=['GPID Electron Density (m^-3)  UT=',tims,'  Day number=',int2str(dno),'  Year=',int2str(year)];
fprintf(fid,[tits,'\n\n']);
fprintf(fid,'Altitudes(km)\n');
altv=disv-re;
fprintf(fid,'%5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f\n',altv);
fprintf(fid,'\n');
fprintf(fid,'Longitudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(lonmat(1,:)));
fprintf(fid,'\n\n');
fprintf(fid,'Lattudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(latmat(:,1)));
fprintf(fid,'\n\n');


noc=prod(size(lonmat));
nocm=reshape(1:noc,size(lonmat,1),size(lonmat,2));
for i=1:noc
 fprintf(fid,'%4.1f\n',rad2deg(lonmat(i)));
 fprintf(fid,'%4.1f\n',rad2deg(latmat(i)));
 [yi,xi]=find(nocm==i);
 deni=squeeze(eden(yi,xi,:));
 fprintf(fid,'%3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e\n',deni);
 fprintf(fid,'\n');
end
fclose(fid);

filso=['GPID',int2str(year),dnos,tims,'_Ox.txt'];

fid=fopen(filso,'w');
tits=['GPID Oxygen Ion Density (m^-3)  UT=',tims,'  Day number=',int2str(dno),'  Year=',int2str(year)];
fprintf(fid,[tits,'\n\n']);
fprintf(fid,'Altitudes(km)\n');
altv=disv-re;
fprintf(fid,'%5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f\n',altv);
fprintf(fid,'\n');
fprintf(fid,'Longitudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(lonmat(1,:)));
fprintf(fid,'\n\n');
fprintf(fid,'Lattudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(latmat(:,1)));
fprintf(fid,'\n\n');


for i=1:noc
 fprintf(fid,'%4.1f\n',rad2deg(lonmat(i)));
 fprintf(fid,'%4.1f\n',rad2deg(latmat(i)));
 [yi,xi]=find(nocm==i);
 deni=squeeze(oxden(yi,xi,:));
 fprintf(fid,'%3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e\n',deni);
 fprintf(fid,'\n');
end
fclose(fid);

filsh=['GPID',int2str(year),dnos,tims,'_Hy.txt'];

fid=fopen(filsh,'w');
tims=num2str(timmat,'%4.2f');
tits=['GPID Hydrogen Ion Density (m^-3)  UT=',tims,'  Day number=',int2str(dno),'  Year=',int2str(year)];
fprintf(fid,[tits,'\n\n']);
fprintf(fid,'Altitudes(km)\n');
altv=disv-re;
fprintf(fid,'%5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f\n',altv);
fprintf(fid,'\n');
fprintf(fid,'Longitudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(lonmat(1,:)));
fprintf(fid,'\n\n');
fprintf(fid,'Lattudes (deg)\n');
fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(latmat(:,1)));
fprintf(fid,'\n\n');

for i=1:noc
 fprintf(fid,'%4.1f\n',rad2deg(lonmat(i)));
 fprintf(fid,'%4.1f\n',rad2deg(latmat(i)));
 [yi,xi]=find(nocm==i);
 deni=squeeze(hyden(yi,xi,:));
 fprintf(fid,'%3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e\n',deni);
 fprintf(fid,'\n');
end
fclose(fid);

if hes==1
 filsh=['GPID',int2str(year),dnos,tims,'_He.txt'];

 fid=fopen(filsh,'w');
 tims=num2str(timmat,'%4.2f');
 tits=['GPID Helium Ion Density (m^-3)  UT=',tims,'  Day number=',int2str(dno),'  Year=',int2str(year)];
 fprintf(fid,[tits,'\n\n']);
 fprintf(fid,'Altitudes(km)\n');
 altv=disv-re;
 fprintf(fid,'%5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f\n',altv);
 fprintf(fid,'\n');
 fprintf(fid,'Longitudes (deg)\n');
 fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(lonmat(1,:)));
 fprintf(fid,'\n\n');
 fprintf(fid,'Lattudes (deg)\n');
 fprintf(fid,'%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f\n',rad2deg(latmat(:,1)));
 fprintf(fid,'\n\n');

 for i=1:noc
  fprintf(fid,'%4.1f\n',rad2deg(lonmat(i)));
  fprintf(fid,'%4.1f\n',rad2deg(latmat(i)));
  [yi,xi]=find(nocm==i);
  deni=squeeze(heden(yi,xi,:));
  fprintf(fid,'%3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e %3.2e\n',deni);
  fprintf(fid,'\n');
 end
 fclose(fid);
end