Function temp_rd_netcdf, file
  
  fid = ncdf_open(file)
  ncdf_varget, fid, 0, time
  ncdf_varget, fid, 1, hcomp
  ncdf_varget, fid, 2, dcomp
  ncdf_varget, fid, 3, zcomp

  Return, {time:time, hcomp:hcomp, dcomp:dcomp, zcomp:zcomp}

End
;+
; NAME:
;       netcdf2cdf_gmag.pro
;
; PURPOSE:
;       Combine 1 hour CDFs (that have been created from 1 hour netCDFs) into 1 day CDFs
;
; CALLING SEQUENCE:
;       netcdf2cdf_gmag, Station, Year, Doy
;
; INPUTS:
;       Station: Where the data is coming from 
;				Year: The year of the data.
;				Doy:  The day of year (0-365).
;
;	NOTE: The necessary CDFs must be in the directory where IDL is being run.
;		These files have names like 2006FtYukon152-01.cdf. There should be
;		24 of these files for any given day.  This program was created with 
;		the intention of running off a Korn Shell script that creates and
;		places these files.
;
; OUTPUTS:
;       None, but it creates a CDF file in the directory that IDL is being run.
;
; PROCEDURE:
;       Open 1 hour CDFs generated from netCDFs and create 1 day CDFs by inserting
;       data into ISTP compliant skeleton/master CDFs.
;
;
; EXAMPLE:
;       netcdf2cdf_gmag,'FtYukon','2006','152'
;
; MODIFICATION HISTORY:
;       Written by:       Matt Davis
;       September 25, 2006     Initial version
;       5-Mar-2009, jmm, Changed to read netCDFs directly, to avoid
;       problem with read_mycdf being unable to read the hourly cdfs
;
; NOTE:
;	See note at bottom of program.
;
;-


pro netcdf2cdf_gmag, station, station_abr, year,doy

;INTRODUCE MASTER CDF FILE

master_cdf=strcompress('thg_l2_mag_'+station_abr+'_00000000_v01.cdf',/remove_all)
out_cdf='temp.cdf'
buf1=read_master_cdf(master_cdf,out_cdf)

;
;get all the 1 hour CDFs that were created from netCDFs
;---------------------------------------------------------------------------------
;Use netCDFs directly, jmm, 5-mar-2009
hour_files=file_search('*'+station+'*.nc')

for i=0,n_elements(hour_files)-1 do begin

print, hour_files(i)
output=temp_rd_netcdf(hour_files(i))
if i eq 0 then begin
  ttime = [double(output.time)]
  hcomp = [double(output.hcomp)]
  dcomp = [double(output.dcomp)]
  zcomp = [double(output.zcomp)]
endif else begin
  ttime = [double(ttime), output.time]
  hcomp = [double(hcomp), output.hcomp]
  dcomp = [double(dcomp), output.dcomp]
  zcomp = [double(zcomp), output.zcomp]
endelse


endfor

;---------------------------------------------------------------------------------

;convert time into seconds since 1970
;NOTE: there is a rounding issue here
;	time is coming in as a decimal day (ie 152.3764856) and is converted to 
;	seconds since 1970
;------------------------------------
doy_to_month_date,year,doy,month,date
stime=time_double(strcompress(string(year)+'-'+string(month)+string(date)))
dtime=stime+(ttime-doy)*86400.
;this deals with repeat times

index=where(histogram(dtime,min=min(dtime)) ge 2)
if index(0) ne -1 then num_repeats=n_elements(index) else num_repeats=0
tt=dblarr(n_elements(dtime)-num_repeats)
bb=dblarr(3,n_elements(dtime)-num_repeats)

i=0.
j=0.

while j le n_elements(dtime)-2 do begin
if i ge n_elements(tt) then begin
  print, 'Problem removing repeat times.' 
 ;test for monotonicity
  tmp_ind = where(((dtime-shift(dtime,1)) gt 0), tmp_pts)
  if ~(tmp_pts eq n_elements(dtime)-1) then begin
    print, 'Time array appears to be nonmonotonic.'
  endif
  return
endif
if dtime(j) eq dtime(j+1) then begin
tt(i)=dtime(j)
bb(0,i)=(hcomp(j)+hcomp(j+1))/2
bb(1,i)=(dcomp(j)+dcomp(j+1))/2
bb(2,i)=(zcomp(j)+zcomp(j+1))/2
print,'Repeat time:'
print,'Indices:	'
print, i
print, j
print,'Times:	'
print, format='(g13.10)',ttime(j)
print, format='(g13.10)',ttime(j+1)
print,''
i=i+1
j=j+2
endif else begin
tt(i)=dtime(j)
bb(0,i)=hcomp(j)
bb(1,i)=dcomp(j)
bb(2,i)=zcomp(j)
i=i+1
j=j+1
endelse

endwhile

if j eq n_elements(dtime)-1 then begin
tt(i)=dtime(j)
bb(0,i)=hcomp(j)
bb(1,i)=dcomp(j)
bb(2,i)=zcomp(j)
endif


print, 'Number of repeat times: ', num_repeats

; now check to make sure that the last records 
; weren't repeats that were left blank
ind = where(tt NE 0., ncnt)
if ncnt GT 0 then begin
   tt=tt[ind]
   bb=bb[*,ind]
endif

;---------------------------------------------------------------------------------

;make start and end epochs for file

start_date_string=time_string(tt(0))
year_s=strmid(start_date_string,0,4)
month_s=strmid(start_date_string,5,2)
day_s=strmid(start_date_string,8,2)
hour_s=strmid(start_date_string,11,2)
min_s=strmid(start_date_string,14,2)
sec_s=strmid(start_date_string,17,2)

end_date_string=time_string(tt(n_elements(tt)-1))
year_e=strmid(end_date_string,0,4)
month_e=strmid(end_date_string,5,2)
day_e=strmid(end_date_string,8,2)
hour_e=strmid(end_date_string,11,2)
min_e=strmid(end_date_string,14,2)
sec_e=strmid(end_date_string,17,2)

cdf_epoch,Epoch_start,year_s,month_s,day_s,hour_s,min_s,sec_s,/COMPUTE_EPOCH
cdf_epoch,Epoch_end,year_e,month_e,day_e,hour_e,min_e,sec_e,/COMPUTE_EPOCH
file_erange=dblarr(2)
file_erange(0)=Epoch_start
file_erange(1)=Epoch_end

;---------------------------------------------------------------------------------

;COPY DATA INTO SKELETONCDF/MASTERCDF

ttime_copy="*buf1.THG_MAG_"+station_abr+"_TIME.data=tt"
data_copy="*buf1.THG_MAG_"+station_abr+".data=bb"
epoch_copy="*buf1.RANGE_EPOCH.data=file_erange"

a1=execute(ttime_copy)
a2=execute(data_copy)
a3=execute(epoch_copy)

; write output CDF file

s = write_data_to_cdf(out_cdf, buf1)

;insert generation date

cdf_id = cdf_open(out_cdf)
gendate=strmid(time_string(systime(/seconds)-28800.),0,10)
cdf_attput, cdf_id, 'Generation_date', 0, gendate
cdf_varput, cdf_id, 'range_epoch', file_erange
cdf_close, cdf_id

print, 'cdf id'
print, cdf_id

;RENAME THE TEMP CDF WITH A NAME DERIVED FROM THE CDFS (THAT WERE CREATED FROM NETCDFS) DATA

change_cdf_name='mv '+out_cdf+' '+strcompress('thg_l2_mag_'+station_abr+'_'+year_s+month_s+day_s+'_v01.cdf',/remove_all)
spawn,change_cdf_name

print, 'CDF written: ', strcompress('thg_l2_mag_'+station_abr+'_'+year_s+month_s+day_s+'_v01.cdf',/remove_all)

;---------------------------------------------------------------------------------

;MAKE ASCII FILE

ascii_name=strcompress('thg_l2_mag_'+station_abr+'_'+year_s+month_s+day_s+'_v01.txt',/remove_all)

sec_o_day=tt-stime
bx=transpose(bb(0,*))
by=transpose(bb(1,*))
bz=transpose(bb(2,*))

write_ascii,[[sec_o_day],[bx],[by],[bz]],ascii_name,'(f10.3,3(f10.3))'

print, 'ASCII written: ', ascii_name


;---------------------------------------------------------------------------------


;NOTE: So this program has an odd structure that I introduced to circumvent an error message I was receiving.
;	Namely, running the read_master_cdf() function after running the read_mycdf() function caused the following error:
;
;	1, Number of variables exceeds the current size of the table structure, please increase it, current size is 
;	<Expression>    STRING    = Array[5]
;	STATUS= Error reading CDF. 
;	Unable to convert variable tagname to type struct.get_allvarnames.pro
;	1, Number of variables exceeds the current size of the table structure, please increase it, current size is 
;	<Expression>    STRING    = Array[5]
;	STATUS= Error reading CDF. 
;	Unable to convert variable tagname to type struct.get_allvarnames.pro
;	% Unable to convert variable tagname to type struct.
;	% Execution halted at: READ_MASTER_CDF   131 /home/mattd/CDAWlib/IDLmakecdf.pro
;	%                      $MAIN$      
;
;	I believe this error derives from the fact that the skeletoncdf/mastercdf has many more variables than the CDFs
;	obtained from the netCDFs.  So by introducing the skeletoncdf/mastercdf first the problem was circumvented;
;	however, I wanted to name the new CDF based on the information in the CDFs (that were created from the netCDFs)
;	so I made a spawn call at the end of the program.    


end