pro ulf_make_mud_sp, station_id, start_date, file_list

;Created:
;20161013 Mark Chutter
;
;Args:
;station_id 3 character station ID - must be SPA or MCM
;start_date first day of month in the form yymmdd
;file_list  list of .dat from which data will be extracted
;
;Description:
;I think MUD stands for Monthly ULF Database
;ulf_make_mud takes a list of input .dat files and makes a monthly MUD
;file whose first data point is the first point after midnight on the
;first day of the month - no data is padded with 0

;it appears the MUD format includes a 32 byte header header:
;file_flag = bytarr(4) 3 letter station ID and NULL
;num_channels = 0
;sample_rate = 0
;sample_size = 0
;start_year = 0
;start_month = 0
;start_day = 0
;station_name = bytarr(4) 3 letter station ID and NULL
;num_seconds = long(0)
;reserved1 = long(0)
;reserved2 = long(0)

;example:
;f_list = findfile('/mirl/ULF/incoming/database/MCM/MCM_2017-05*', count=count)
;ulf_make_mud_sp, 'MCM', '20170501', f_list

if station_id eq 'MCM' then num_channels = 2
if station_id eq 'SPA' then num_channels = 3

mud_dir = '/mirl/ULF/Database_test/'

NUM_TIMES = 2678400L ;86400 seconds * 31 days
NUM_POINTS = 26784000L ;86400 seconds * 10 per second * 31 days
mud_time = fltarr(NUM_TIMES)
mud_data = fltarr(3L, NUM_POINTS)

file_flag_bytes = bytarr(4) 
file_flag_bytes[0:2] = byte('MUD') 
file_flag_bytes[3] = 0

sample_rate = 10 ;in Hz
sample_size = 16 ;in bits

year = strmid(start_date, 0, 4)
year_short = strmid(start_date, 2, 2)
month = strmid(start_date, 4, 2)
day = strmid(start_date, 6, 2)
year_short_long = long(year_short);luv the name
month_long = long(month)
day_long = long(day)

if month eq '01' or $
   month eq '03' or $
   month eq '05' or $
   month eq '07' or $
   month eq '08' or $
   month eq '10' or $
   month eq '12' then num_days = 31

if month eq '04' or $
   month eq '06' or $
   month eq '09' or $
   month eq '11' then num_days = 30

if month eq '02' then begin

   if year eq '2000' or $
      year eq '2004' or $
      year eq '2008' or $
      year eq '2012' or $
      year eq '2016' or $
      year eq '2020' or $
      year eq '2024' then num_days = 29 $
   else num_days = 28

endif

station_name_bytes = bytarr(4) 
station_name_bytes[0:2] = byte(station_id) 
station_name_bytes[3] = 0

year_fix = fix(year)
month_fix = fix(month)
day_fix = fix(day)
num_seconds = num_days * 86400L
reserved1 = 0L
reserved2 = 0L

mud_name = mud_dir + station_id + '_' + year + '_' + month + '.MUD' 
print, mud_name
openw, mud_lun, mud_name, /get_lun
writeu, mud_lun, file_flag_bytes
writeu, mud_lun, num_channels
writeu, mud_lun, sample_rate
writeu, mud_lun, sample_size
writeu, mud_lun, year_fix
writeu, mud_lun, month_fix
writeu, mud_lun, day_fix
writeu, mud_lun, station_name_bytes
writeu, mud_lun, num_seconds
writeu, mud_lun, reserved1
writeu, mud_lun, reserved2

time_index0 = 0L
for i=0, n_elements(file_list)-1 do begin

   filepath = file_list[i]
   ulf_read_sp, $
      filepath, $
      data_count, $
      data_arr, $
      num_channels
   
   data_index0 = time_index0 * 10L
   mud_time[time_index0:time_index0+(data_count / 10L)-1L] = $
      time_index0 + lindgen(data_count / 10L)
   mud_data[0, data_index0:data_index0+data_count] = data_arr[0, 0:data_count]
   mud_data[1, data_index0:data_index0+data_count] = data_arr[1, 0:data_count]
   if num_channels eq 3 then begin
      mud_data[2, data_index0:data_index0+data_count] = $
         data_arr[2, 0:data_count]
   endif

   time_index0 = time_index0 + 86400L

endfor

for i = 0, num_seconds-1 do begin
   writeu, mud_lun, mud_time[i]
   out_data0 = mud_data[0, i * 10L: i * 10L + 9]
   writeu, mud_lun, out_data0
   out_data1 = mud_data[1, i * 10L: i * 10L + 9]
   writeu, mud_lun, out_data1
   if num_channels eq 3 then begin
      out_data2 = mud_data[2, i * 10L: i * 10L + 9]
      writeu, mud_lun, out_data2
   endif
endfor

close, mud_lun
free_lun, mud_lun

end