PRO Run_STAR_FM_netcdf, in_file, $
                 out_path, $   
                 fm_source, $
                 is_text=is_text, $
                 sub_version=sub_version, $
                 return_code=return_code

return_code=0

if ~file_test(in_file) then begin
  print, 'non-exist: '+in_file
  return_code=1
  stop
endif                 

if ~keyword_set(sub_version) then sub_version=''

; ----------------------------
spawn, 'hostname -s', hostname

if strpos(hostname, 'rhw') eq 0 then begin  
  command_ini= '/data/data504/xzhou/utility/STAR_fm_netcdf/'
  source_ini = '/data/smcd5/xzhou/' 

endif else if hostname eq 'gnssro' then begin
  command_ini= '/data2/xinjiaz/utility/STAR_fm_netcdf/'
  source_ini = '/data/xinjiaz/'  ; location of raw model data

endif else if hostname eq 'starro' then begin  
  command_ini= '/data/xinjiaz/utility/STAR_fm_netcdf/'
  source_ini = '/GNSSRO_data/xinjiaz/' 

endif else if hostname eq 'gpsmet' then begin  
  command_ini= '/data2/xinjiaz/utility/STAR_fm_netcdf/'
  source_ini = '/GNSSRO_data/xinjiaz/' 

endif else if hostname eq 'rosdc' then begin  
  command_ini= '/data2/xinjiaz/utility/STAR_fm_netcdf/'
  source_ini = '/data3/xinjiaz/' 

endif

file_mkdir, out_path
out_path_array = strsplit(out_path, '/', /extract) 
thisDATE = out_path_array[-1] ; 2019.305
print, 'thisDATE: '+thisDATE

; ---
;thisDATE_array = strsplit(thisDATE, '.', /extract)
;caldat, julday(1, thisDATE_array[1], thisDATE_array[0]), month, day, year
;year = string(year, format='(i4)')
;month= string(month, format='(i2.2)')
;day  = string(day, format='(i2.2)')

; ---
year = strmid(thisDATE, 0, 4)
month= strmid(thisDATE, 5, 2)
day  = strmid(thisDATE, 8, 2)


if keyword_set(is_text) then begin
  list_file = in_file

endif else begin
  list_file = out_path+'/list_'+thisDATE+'.txt'

  openw, lun, list_file, /get_lun
  printf, lun, 'Year,Month,  Day,  Hour,Minute,Second,  lon_in,  lat_in,    Azim,fileStamp'
  ;2019,   10,    1,     1,    11,     1,-118.29825,   9.98527, -84.72016,C2E1.2019.274.01.10.G09

  print, in_file 
  restore, in_file 

  ;data = data[where(data.leo ne 'GN03',/null)]

  for profile_loop = 0, n_elements(data)-1 do begin
  ;for profile_loop = 0, 10 do begin

    print, data[profile_loop].title
    title_array = strsplit(data[profile_loop].title, '_', /extract)

    thisTime = strsplit(title_array[1], '-T:', /extract)
    thisYear = thisTime[0]
    thisMonth= thisTime[1]
    thisDay  = thisTime[2]
    thisHour = thisTime[3]
    thisMinute=thisTime[4]
    thisSecond=thisTime[5]
  
    thisDoy = string(julday(thisMonth, thisDay, thisYear) - julday(1, 0, thisYear), format='(i3.3)')

    lat = data[profile_loop].lat
    lon = data[profile_loop].lon
    if total(strupcase(tag_names(data[profile_loop])) eq 'AZIM') eq 1 then azim= data[profile_loop].azim else azim = 45.
 
    gnss = strupcase(data[profile_loop].gnss)
    leo  = strupcase(data[profile_loop].leo)

    ;fileStamp = 'C2E6.2019.285.23.34.R20'
    fileStamp = leo+'.'+thisYear+'.'+thisDoy+'.'+thisHour+'.'+thisMinute+'.'+gnss

    printf, lun, format='(a4, a, 5(a2, a), 3(f10.5, a), 30a)', thisYear, ',',thisMonth,',', thisDay,',', thisHour,',', thisMinute,',', thisSecond,',', lon,',', lat,',', azim,',', fileStamp
  endfor ; profile_loop  
  close, lun
  free_lun, lun
endelse

if fm_source eq 'era5v2' then begin
  source = source_ini+'data_ERA/ERA5_cds_v2/' ; 2024/e5.oper.an.pl.128_20240510.nc

  command=command_ini+'star_fm_ERA5v2_main.exe'
  cmd=command+' '+year+' '+month+' '+day+' '+source+' '+list_file+' '+ out_path

  ; star_fm_ERA5v2_main.exe 2024 09 01 'data_ERA/ERA5_cds_v2/' 'list_file' 'out_path'

endif else if fm_source eq 'era5fv2' then begin
  source = source_ini+'data_ERA/ERA5_forecast_v2/' ; 2023/e5.oper.fc.pl.128_20230114.nc'

  command=command_ini+'star_fm_ERA5fv2_main.exe'
  cmd=command+' '+year+' '+month+' '+day+' '+source+' '+list_file+' '+ out_path

  ; /data2/xinjiaz/utility/STAR_fm_netcdf/star_fm_ERA5fv2_main.exe 2023 01 01 'data_ERA/ERA5_forecast/' list_file /output:q

endif else if fm_source eq 'era5hres' then begin
  source = source_ini+'data_ERA/ERA5_hres/' ; 2023/e5.oper.fc.pl.128_20230114.nc'

  command=command_ini+'star_fm_ERA5hres_main.exe'
  cmd=command+' '+year+' '+month+' '+day+' '+source+' '+list_file+' '+ out_path

  ;print, cmd
  ; /data2/xinjiaz/utility/STAR_fm_netcdf/star_fm_ERA5fv2_main.exe 2023 01 01 'data_ERA/ERA5_forecast/' list_file /output:q

endif else if fm_source eq 'ech' then begin
  source = source_ini+'data_ECMWF/' ; 2026/01/UAD01071200010721001_ml.nc
  command=command_ini+'star_fm_ECH_main.exe'
  cmd=command+' '+year+' '+month+' '+day+' '+source+' '+list_file+' '+ out_path

endif else if fm_source eq 'gfshres' then begin
  source = '/data4/xinjiaz/data_GFS/GFS_atmf006/' ; 2026/01/UAD01071200010721001_ml.nc
  command=command_ini+'star_fm_GFShres_main_v2.exe'
  cmd=command+' '+year+' '+month+' '+day+' '+source+' '+list_file+' '+ out_path

endif else if fm_source eq 'gdashres' then begin
  source = '/data4/xinjiaz/data_GFS/GDAS_atmanl/' ; 2026/01/UAD01071200010721001_ml.nc
  command=command_ini+'star_fm_GDAShres_main_v2.exe'
  cmd=command+' '+year+' '+month+' '+day+' '+source+' '+list_file+' '+ out_path

endif else if strmid(fm_source, 0, 3) eq 'gfs' then begin
  if      fm_source eq 'gfs00' then begin
    source = source_ini+'data_GFS/GFS_h00/' 
    stype='A'
  endif else if fm_source eq 'gfs06' then begin
    source = source_ini+'data_GFS/GFS_h06/' 
    stype='B'
  endif else if fm_source eq 'gfs12' then begin
    source = source_ini+'data_GFS/GFS_h12/' 
    stype='C'
  endif else if fm_source eq 'gfs24' then begin
    source = source_ini+'data_GFS/GFS_h24/' 
    stype='D'
  endif

;  if year+'-'+month+'-'+day ge '2021-03-24' then begin
;    ; GFS data in 41 levels
;    ;command='/data2/xinjiaz/task_icvs_gpsro/codes/for_Lok/star_forward_v2_test/star_fm_GFS_main_v2.exe'
    command=command_ini+'star_fm_GFS_main_v2.exe'
;  endif else begin
;    ; GFS data in 37 levels
;    ;command='/data2/xinjiaz/task_icvs_gpsro/codes/for_Lok/star_forward_v1/star_fm_GFS_main_v2.exe'
;    command='/data2/xinjiaz/utility/STAR_fm/star_fm_GFS_main_v2.exe'
;  endelse
  cmd=command+' '+year+' '+month+' '+day+' '+source+' '+list_file+' '+ out_path+' '+stype

endif

;cmd=command+'  2019  10  1  /data/smcd5/xzhou/data_ERA/ERA5/  list_2019.274.txt '+ out_path
print, cmd
spawn, cmd, Result, ErrResult
;spawn, cmd

print, 'Result:',Result
print, 'ErrResult:', ErrResult

print,'end at : ',systime() 
return_code=2  

stop
END
