from getpass import getuser
from pathlib import Path
from socket import gethostname

# common-----------------
if '__file__' in globals():
    main_dir = Path(__file__).resolve().parent
else:
    main_dir = Path.cwd()

# iers-----------------
iers_download_url = 'https://maia.usno.navy.mil/ser7/'
iers_output_dir = main_dir / 'iers'

# interp_gnss-----------
gnss_dir = main_dir / 'gnss'

# excess retrieval-----------------
hostname = gethostname()
mission  = 'planetiq'
#version  = 'v3.11'  ## 01/08/2026
#version  = 'v3.14'  ## 02/17/2026
version  = 'v4.11'  ## 11/07/2026

dev_dir = Path('/dev/shm/') / getuser()
dev_dir.mkdir(exist_ok=True)

working_dir = main_dir / 'planetiq'

if hostname.endswith('.umd.edu'):
    #gnss_download_url= 'ftps://gdc.cddis.eosdis.nasa.gov/gnss/products/latest/ultra/'
    gnss_download_url1= 'ftps://gdc.cddis.eosdis.nasa.gov/gnss/products/' # Jun modified 20260504
    gnss_download_url2= 'https://gpsmet.umd.edu/GNSSOrb_Ext/'  # Jun modified 20260504
    #rfsi_dir         = working_dir / 's04_ropp'
    #rfsi_dir         = f"/data3/xinjiaz/data_GPSRO/RFSI_UMD/{version}/{mission}rt"
    rfsi_dir         = f"/data3/xinjiaz/data_GPSRO/RFSI_UMD/v3.14/{mission}rt"
    ropp_path        = '/data2/xinjiaz/utility'
    suffix           = '_rosdc'
    data_dir = Path('/data3/xinjiaz/data_GPSRO/planetiq_scdr/GNOMES_PYXIS_L1A/')

elif hostname.endswith('ec2.internal'):
    gnss_download_url= 'ftps://gdc.cddis.eosdis.nasa.gov/gnss/products/latest/ultra/'
    rfsi_dir         = f"/shared/home/xinjiaz/data_GPSRO/RFSI_UMD/{version}/{mission}rt"
    ropp_path        = '/shared/home/xinjiaz/utility'
    data_dir="s3://5006-sandboxstar-ing-aac1rggtxbzqx5ubsuogyiafe1h1quse1b-s3alias/PlanetIQ/GNOMES/Pyxis/PlanetiQ_GNOMES-A_Pyxis-A_Level1a_NeutralWx/"

elif hostname.endswith('.star1.nesdis.noaa.gov'):
    gnss_download_url= 'https://cddis.nasa.gov/archive/gnss/products/latest/ultra/'
    rfsi_dir         = f"/data/data504/xzhou/data_GPSRO/RFSI_UMD/{version}/{mission}rt"
    ropp_path        = '/data/data504/xzhou/utility'
    scdr_executable = "/data/starfs1/bin/scdr-files" 

else:
    print('No configuration for '+hostname+' ,pls check!')

# =====================================================================================================
# STAR 2026-11-01: excess-phase processing options (planetiq/tools.py; see README_exph_STARC.md, section 2).
# Without any exph_clock / exph_connect key the code runs the delivered processing (STAR_Legacy), bit for bit.
# =====================================================================================================

# ---- setting for near-real-time processing (and for historical runs without --models): STARC
#      STAR implementation of the CDAAC algorithm: zero difference (PlanetiQ POD clock), no reference-GNSS search,
#      smoothed 1 s LEO orbit, CDAAC-style open-loop phase connection. Output: planetiq/s04_atmPhs/<date>/
exph_clock      = 'zero_diff'
exph_clk_interp = 'linear'
exph_connect    = 'starc'
#      STAR_Updated instead: exph_connect = 'star'.  STAR_Legacy instead: comment out the three lines above.

# ---- reprocessing several settings in one run (historical mode only): one excess-phase run per model,
#      output planetiq/s04_atmPhs/<model>/<date>/. Same as the command-line option --models. None = off.
exph_reproc_models = None        # e.g. ['STAR_Legacy', 'STAR_Updated', 'STARC']

# ---- all settings
exph_out_date = 'input'          # <date> = name of the input folder the tar came from (arrival date);
                                 # 'observation' = day of the occultation (delivered behaviour)
exph_pod_qc   = 'flag'           # pod_qc = record of what was found and done; L1A bad = "1" when the POD is still bad after the fixes; 'reject' = not written
exph_pod_fit  = 300              # STAR 2026-11-02: replace the first 300 s of POD orbit after a gap by a dynamic fit (pod_qc bit 3 = 4);
                                 # needs 900 s of POD after the gap in the current tar (else flag only) and pip package satkit; 0 = off
exph_pod_gap_win = 200            # STAR 2026-11-03: pod_qc bit 1 when the orbit resumed < 200 s before the occultation (was 120)
exph_pod_pv_max  = 0.01           # STAR 2026-11-03: pod_qc bit 2 when the 30 s mean |dP/dt - V| > 0.01 m/s (was 0.02)
exph_pod_fit_pv  = True           # STAR 2026-11-04: DMF across flag-2 stretches (bridge fit + 120 s edge blend; pod_qc bit 4 = 8);
                                  # no POD after the stretch in the current tar -> flag 2 kept. False = off
# L1A bad = "1" (errstr = "POD <reason>") = POD still bad after the fixes; ropp_pp keeps it in L1C with qc_l1a_bad = .true.

exph_skip_dup    = True           # STAR 2026-11-04: skip an occultation already written from an earlier tar (PlanetiQ re-sends the
                                 # overlap of consecutive tars; 3.9% of 8-11 Oct were written twice). False = delivered check

# ---- in-line L1A -> L1C and BUFR (real time; runs when ropp_path exists, as delivered)
exph_inline_ropp   = True        # near real time: in-line L1A -> L1C and BUFR on (delivered behaviour)
exph_reproc_inline = False       # historical / reprocessing runs: in-line L1A -> L1C off (default); True = on for every
                                 # model of --models; command-line --inline / --no-inline override it
#      Retrieval configuration of the in-line step: delivered cosmic_pp_rfsi_planetiq.cf (default), or the STAR
#      Xi_PM_GO_bangle_sm4 as second option (uncomment both lines below)
# exph_ropp_cf      = 'cosmic_pp_rfsi_planetiq.cf'        # delivered default (file in working_dir)
# exph_ropp_timeout = 20                                  # s, delivered default
# STAR retrieval Xi_PM_GO_bangle_sm4 (needs the STAR ropp_pp build under ropp_path, and working_dir/data -> ropp_pp/data):
# exph_ropp_cf      = 'star_xi_pm_go_bangle_sm4_planetiq.cf'
# exph_ropp_timeout = 120
# STAR 2026-11-04: Y_PM_GO (Y. Chen's PM+GO with CDAAC-style QC; its own ropp_pp_y build under ropp_path, see the STAR
# ROPP package README, section Y_PM_GO):
# exph_ropp_cf      = 'star_y_pm_go_planetiq.cf'
# exph_ropp_pp      = 'ropp_pp_y'                         # ropp_pp source folder of the tool (default 'ropp_pp')
# exph_ropp_timeout = 120
