Python (what else?) script to generate large sky models - and its cluster files - for adequate profiling

This commit is contained in:
Hanno Spreeuw 2017-05-12 14:16:06 +02:00
parent d96359d476
commit 2792b81fe4
1 changed files with 91 additions and 0 deletions

91
test/Generate_sources.py Normal file
View File

@ -0,0 +1,91 @@
# The goal is here to generate thousands of random sources to populate a sky model.
# This sky model is solely used for profiling.
# It has to be pretty large because we need sufficiently long run times for adequate profiling.
# fields are (where h:m:s is RA, d:m:s is Dec):
# name h m s d m s I Q U V spectral_index0 spectral_index1 spectral_index2 RM extent_X(rad) extent_Y(rad)
# pos_angle(rad) freq0
import numpy as np
import warnings
number_of_parameters = 19
number_of_digits_for_sources = 5
number_of_sources = 5e3
try:
assert number_of_sources < 10**number_of_digits_for_sources
except AssertionError:
number_of_sources = 10**number_of_digits_for_sources - 1
print("Sorry, number of sources too large, reset to {0}".format(number_of_sources))
# For the declinations I have chosen sources between 45 and 90 degrees declination.
decl_low = 45
decl_high = 90
I_low = 10
I_high = 100
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="using a non-integer number instead of an integer")
# Maximum of 1e5 sources, so a 'P' + number_of_digits_for_sources digits,
# that's why we have '...np.str_, number_of_digits_for_sources + 1'.
source_parms_dtype = ((np.str_, number_of_digits_for_sources + 1),
int, int, float, int, int, float, float, int, int, int,
float, float, int, int, int, int, float, float)
names = ('name', 'rah', 'ram', 'ras', 'dad', 'dam', 'das', 'I', 'Q', 'U', 'V',
'sp0', 'sp1', 'sp2', 'RM', 'extX', 'extY',
'pos_angle', 'freq0')
formats = ['U6', 'i4', 'i4', 'f8', 'i4', 'i4', 'f8', 'f8', 'i4', 'i4', 'i4', 'f8', 'f8',
'i4', 'i4', 'i4', 'i4', 'f8', 'f8']
formats_reformatted = '%s %d %d %f %d %d %f %f %d %d %d %f %f %d %d %d %d %f %f'
sources_parameters = np.recarray((number_of_sources,), formats=formats,
names=names)
for source_name in range(sources_parameters.shape[0]):
sources_parameters.name[source_name] = 'P' + str(source_name).zfill(number_of_digits_for_sources)
# Right ascension can have all values.
sources_parameters.rah = np.random.randint(0, 24, size=number_of_sources)
sources_parameters.ram = np.random.randint(0, 59, size=number_of_sources)
sources_parameters.ras = 60 * np.random.rand(number_of_sources)
sources_parameters.dad = np.random.randint(decl_low, decl_high, size=number_of_sources)
sources_parameters.dam = np.random.randint(0, 59, size=number_of_sources)
sources_parameters.das = 60 * np.random.rand(number_of_sources)
sources_parameters.I = (I_high - I_low) * np.random.rand(number_of_sources) + I_low
sources_parameters.Q = 0
sources_parameters.U = 0
sources_parameters.V = 0
# These spectral indices
sources_parameters.sp0 = -np.random.rand(number_of_sources)
sources_parameters.sp1 = 2 * np.random.rand(number_of_sources) - 1
sources_parameters.sp2 = 0
sources_parameters.RM = 0
sources_parameters.extX = 0
sources_parameters.extY = 0
sources_parameters.pos_angle = 0.
sources_parameters.freq0 = 143000000.0
print([elem for elem in sources_parameters[100]])
sources_parameters.tofile("extended_source_list_using_tofile.txt", sep='\n')
with open("extended_source_list.txt", 'wb') as f:
f.write(b"## From Generate_sources.py by Hanno Spreeuw.\n")
f.write(b"## Generates point sources at random positions with random brighnesses within some range.\n")
f.write(b"## this is an LSM text (hms/dms) file\n")
f.write(b"## fields are (where h:m:s is RA, d:m:s is Dec):\n")
f.write(b"## name h m s d m s I Q U V spectral_index0 spectral_index1 spectral_index2 " +
b"RM extent_X(rad) extent_Y(rad) pos_angle(rad) freq0\n")
np.savetxt(f, sources_parameters, fmt=formats_reformatted)
# Now write the cluster file
with open("extended_source_list.cluster", 'wb') as f:
np.savetxt(f, (sources_parameters.name).reshape(1, sources_parameters.name.shape[0]), fmt='%s', delimiter=' ')