-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpycomp-randomSH.py
More file actions
executable file
·70 lines (63 loc) · 2.26 KB
/
pycomp-randomSH.py
File metadata and controls
executable file
·70 lines (63 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time, string
import random
import sys, getopt
import numpy as np
import pyshtools
def disp_help():
proname = (sys.argv[0]).strip().split('/')
print ('This is a python template generates a series of random distributed spherical harmonic coefficients.\n\
For more information please go to the offical site of shtools "https://shtools.oca.eu/shtools/"\n\
Author: Yi Zhang (zhangyi.cugwuhan@gmail.com)\n')
print ('Usage: '+proname[-1]+' [-d<order-num>] [-o<out-file>] [-p<power-factor>] [-h]\n\
-d --order-num\tset order number of the output spherical harmonic coefficients, the default is 180\n\
-o --ofile\toutput name, the default is \'XXXXXXXX.SHcoeffs\' in which Xs are replaced by random symbols\n\
-p --power\tset multiply factor of the power spectrum, the default is 1.0\n\
-h --help\tshow this information')
def randomCoeffs(orderNum,fileName,powerFactor):
proName = (sys.argv[0]).strip().split('/')
degrees = np.arange(orderNum, dtype=float)
degrees[0] = np.inf
power = powerFactor*degrees**(-2)
clm = pyshtools.SHCoeffs.from_random(power)
#clm.to_file(fileName)
coeffs = clm.to_array()
fp = open(fileName,'w')
outstr = '# This file is generated by '+proName[-1]+' on '+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+'\n'
fp.write(outstr)
outstr = '# number of orders = '+str(orderNum-1)+'\n'
fp.write(outstr)
for i in range(coeffs.shape[1]):
for j in range(i+1):
outstr = str(i)+' '+str(j)+' '+str(coeffs[0][i][j])+' '+str(coeffs[1][i][j])+'\n'
fp.write(outstr)
pass
pass
fp.close()
def main(argv):
#定义参数初始值
numOrder = 181
nameFile = ''.join(random.sample(string.ascii_letters + string.digits, 8))+".SHcoeffs"
factorPower = 1.0
#提取命令行参数
try:
opts, args = getopt.getopt(argv,"hd:o:p:",["help","order-num=","ofile=","power="])
except getopt.GetoptError:
disp_help()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
disp_help()
sys.exit()
elif opt in ("-d", "--order-num"):
numOrder = int(arg)
numOrder = numOrder + 1
elif opt in ("-p", "--power"):
factorPower = float(arg)
elif opt in ("-o", "--ofile"):
nameFile = arg
#执行函数
randomCoeffs(numOrder,nameFile,factorPower)
if __name__ == "__main__":
main(sys.argv[1:])