Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/task_getephemtable.py: 84%

67 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-31 18:48 +0000

1import os 

2import re 

3import shutil 

4from casatasks import casalog 

5from casatasks.private.jplhorizons_query import gethorizonsephem 

6 

7 

8def getephemtable(objectname, asis, timerange, interval, outfile, rawdatafile, overwrite): 

9 """Retrieve the ephemeris data of a specific ephemeris object by sending 

10 a query to JPL's Horizons system and creates the ephemeris data stored in a CASA table format. 

11 """ 

12 casalog.origin('getephemtable') 

13 

14#Python script 

15 

16 if isinstance(objectname, str): 

17 if not objectname.strip(): 

18 raise ValueError("objectname must be specified") 

19 

20 match = re.match(r'(\s*)([0-9]+)(\s*)', objectname) 

21 if match is not None and not asis: 

22 raise RuntimeError("objectname is given as an ID number, need to set asis=True") 

23 

24 # split timerange to start and end times 

25 if isinstance(timerange, str): 

26 if not timerange.strip(): 

27 raise ValueError("timerange must be specified") 

28 

29 if timerange.find('~') != -1: 

30 timerange = timerange.replace(' ','') 

31 (starttime, stoptime) = timerange.split('~') 

32 starttime = starttime.upper() 

33 stoptime = stoptime.upper() 

34 if starttime.startswith('JD'): 

35 if not stoptime.startswith('JD'): 

36 try: 

37 float(stoptime) 

38 stoptime = 'JD' + stoptime 

39 except Exception as e: 

40 raise ValueError("Error translating stop time of timerange specified in JD.") from e 

41 # JPL-Horizons does not accept MJD for time specification. 

42 elif starttime.startswith('MJD'): 

43 try: 

44 starttime = 'JD'+str(float(starttime.strip('MJD')) + 2400000.5) 

45 except Exception as e: 

46 raise ValueError("Error translating start time of timerange specified in MJD.") from e 

47 if not stoptime.startswith('JD'): 

48 if stoptime.startswith('MJD'): 

49 stoptime = stoptime.strip('MJD') 

50 try: 

51 stoptime = 'JD' + str(float(stoptime) + 2400000.5) 

52 except Exception as e: 

53 raise ValueError("Error translating stop time of timerange specified in MJD.") from e 

54 else: 

55 matchstart = re.match(r'(\s*)([0-9][0-9][0-9][0-9])\/([0-9][0-9])\/([0-9][0-9])([/:0-9]*)',starttime) 

56 matchstop = re.match(r'(\s*)([0-9][0-9][0-9][0-9])\/([0-9][0-9])\/([0-9][0-9])([/:0-9]*)',stoptime) 

57 #print(f'startime={starttime}, stoptime={stoptime}, matchstart={matchstart}, matchstop={matchstop}') 

58 if matchstart is None or matchstop is None: 

59 raise ValueError("Error in timerange format. Use YYYY/MM/DD/hh:mm or Julian date with a prefix 'JD' Modified Julian date with a prefix 'MJD'") 

60 else: 

61 raise ValueError("timerange needs to be specified with starttime and stoptime delimited by ~ .") 

62 

63 

64 # check for interval 

65 if isinstance(interval, str): 

66 match = re.match(r'(\d+(?:\.\d*)?)(\s*)([a-zA-Z]*)', interval) 

67 if match is not None: 

68 (valstr, _, unitstr) = list(match.groups()) 

69 if valstr.isdigit(): 

70 if unitstr != '': 

71 intervalstr = valstr+unitstr 

72 else: 

73 intervalstr = valstr 

74 else: 

75 raise ValueError("interval value must be integer") 

76 else: 

77 raise ValueError("interval must be a string with integer with unit ") 

78 

79 # outfile and rawdatafile check 

80 if not outfile.strip(): 

81 raise ValueError("outfile must be specified") 

82 elif os.path.exists(outfile): 

83 if not overwrite: 

84 raise ValueError(f'{outfile} exists and overwrite=False') 

85 else: 

86 casalog.post(f'{outfile} exists, will be overwritten', 'WARN') 

87 shutil.rmtree(outfile) 

88 if os.path.exists(rawdatafile): 

89 if not overwrite: 

90 raise ValueError(f'{rawdatafile} exists and overwrite=False') 

91 else: 

92 casalog.post(f'{rawdatafile} exists, will be overwritten', 'WARN') 

93 

94 # call the JPL-Horizons query function 

95 gethorizonsephem(objectname, starttime, stoptime, intervalstr, outfile, asis, rawdatafile)