Guest User

Untitled

a guest
Jan 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. import pyspeckit.spectrum.models.ammonia as ammonia
  2. import pyspeckit.spectrum.models.ammonia_constants as nh3con
  3. from pyspeckit.spectrum.units import SpectroscopicAxis as spaxis
  4. import os
  5. import sys
  6. import numpy as np
  7. import astropy.units as u
  8. from astropy.io import fits
  9. from spectral_cube import SpectralCube
  10. from astropy.utils.console import ProgressBar
  11. from astropy import log
  12. log.setLevel('ERROR')
  13.  
  14. def generate_cubes(nCubes=100, nBorder=1, noise_rms=0.1,
  15. output_dir='random_cubes', random_seed=None):
  16. """
  17. This places nCubes random cubes into the specified output directory
  18. """
  19.  
  20. if not os.path.isdir(output_dir):
  21. os.mkdir(output_dir)
  22. xarr11 = spaxis((np.linspace(-500, 499, 1000) * 5.72e-6
  23. + nh3con.freq_dict['oneone'] / 1e9),
  24. unit='GHz',
  25. refX=nh3con.freq_dict['oneone'] / 1e9,
  26. velocity_convention='radio', refX_unit='GHz')
  27. xarr22 = spaxis((np.linspace(-500, 499, 1000) * 5.72e-6
  28. + nh3con.freq_dict['twotwo'] / 1e9), unit='GHz',
  29. refX=nh3con.freq_dict['twotwo'] / 1e9,
  30. velocity_convention='radio', refX_unit='GHz')
  31.  
  32. nDigits = int(np.ceil(np.log10(nCubes)))
  33. if random_seed:
  34. np.random.seed(random_seed)
  35. nComps = np.random.choice([1, 2], nCubes)
  36.  
  37. Temp1 = 8 + np.random.rand(nCubes) * 17
  38. Temp2 = 8 + np.random.rand(nCubes) * 17
  39.  
  40. Voff1 = np.random.rand(nCubes) * 5 - 2.5
  41. Voff2 = np.random.rand(nCubes) * 5 - 2.5
  42.  
  43. logN1 = 13 + 2 * np.random.rand(nCubes)
  44. logN2 = 13 + 2 * np.random.rand(nCubes)
  45.  
  46. Width1NT = 0.3 * np.exp(0.5 * np.random.randn(nCubes))
  47. Width2NT = 0.3 * np.exp(0.5 * np.random.randn(nCubes))
  48.  
  49. Width1 = np.sqrt(Width1NT + 0.08**2)
  50. Width2 = np.sqrt(Width2NT + 0.08**2)
  51.  
  52. scale = np.array([[0.2, 0.1, 0.5, 0.01]])
  53. gradX1 = np.random.randn(nCubes, 4) * scale
  54. gradY1 = np.random.randn(nCubes, 4) * scale
  55. gradX2 = np.random.randn(nCubes, 4) * scale
  56. gradY2 = np.random.randn(nCubes, 4) * scale
  57.  
  58. params1 = [{'ntot':14,
  59. 'width':1,
  60. 'xoff_v':0.0}] * nCubes
  61. params2 = [{'ntot':14,
  62. 'width':1,
  63. 'xoff_v':0.0}] * nCubes
  64.  
  65. hdrkwds = {'BUNIT': 'K',
  66. 'INSTRUME': 'KFPA ',
  67. 'BMAJ': 0.008554169991270138,
  68. 'BMIN': 0.008554169991270138,
  69. 'TELESCOP': 'GBT',
  70. 'WCSAXES': 3,
  71. 'CRPIX1': 2,
  72. 'CRPIX2': 2,
  73. 'CRPIX3': 500,
  74. 'CDELT1': -0.008554169991270138,
  75. 'CDELT2': 0.008554169991270138,
  76. 'CDELT3': 5720.0,
  77. 'CUNIT1': 'deg',
  78. 'CUNIT2': 'deg',
  79. 'CUNIT3': 'Hz',
  80. 'CTYPE1': 'RA---TAN',
  81. 'CTYPE2': 'DEC--TAN',
  82. 'CTYPE3': 'FREQ',
  83. 'CRVAL1': 0.0,
  84. 'CRVAL2': 0.0,
  85. 'LONPOLE': 180.0,
  86. 'LATPOLE': 0.0,
  87. 'EQUINOX': 2000.0,
  88. 'SPECSYS': 'LSRK',
  89. 'RADESYS': 'FK5',
  90. 'SSYSOBS': 'TOPOCENT'}
  91. truekwds = ['NCOMP', 'LOGN1', 'LOGN2', 'VLSR1', 'VLSR2',
  92. 'SIG1', 'SIG2', 'TKIN1', 'TKIN2']
  93.  
  94. for i in ProgressBar(range(nCubes)):
  95. xmat, ymat = np.indices((2 * nBorder + 1, 2 * nBorder + 1))
  96. cube11 = np.zeros((xarr11.shape[0], 2 * nBorder + 1, 2 * nBorder + 1))
  97. cube22 = np.zeros((xarr22.shape[0], 2 * nBorder + 1, 2 * nBorder + 1))
  98.  
  99. for xx, yy in zip(xmat.flatten(), ymat.flatten()):
  100. T1 = Temp1[i] * (1 + gradX1[i, 0] * (xx - 1)
  101. + gradY1[i, 0] * (yy - 1)) + 5
  102. T2 = Temp2[i] * (1 + gradX2[i, 0] * (xx - 1)
  103. + gradY2[i, 0] * (yy - 1)) + 5
  104. if T1 < 2.74:
  105. T1 = 2.74
  106. if T2 < 2.74:
  107. T2 = 2.74
  108. W1 = np.abs(Width1[i] * (1 + gradX1[i, 1] * (xx - 1)
  109. + gradY1[i, 1] * (yy - 1)))
  110. W2 = np.abs(Width2[i] * (1 + gradX2[i, 1] * (xx - 1)
  111. + gradY2[i, 1] * (yy - 1)))
  112. V1 = Voff1[i] + (gradX1[i, 2] * (xx - 1) + gradY1[i, 2] * (yy - 1))
  113. V2 = Voff2[i] + (gradX2[i, 2] * (xx - 1) + gradY2[i, 2] * (yy - 1))
  114. N1 = logN1[i] * (1 + gradX1[i, 3] * (xx - 1)
  115. + gradY1[i, 3] * (yy - 1))
  116. N2 = logN2[i] * (1 + gradX2[i, 3] * (xx - 1)
  117. + gradY2[i, 3] * (yy - 1))
  118. if nComps[i] == 1:
  119. spec11 = ammonia.cold_ammonia(xarr11, T1,
  120. ntot=N1,
  121. width=W1,
  122. xoff_v=V1)
  123. spec22 = ammonia.cold_ammonia(xarr22, T1,
  124. ntot=N1,
  125. width=W1,
  126. xoff_v=V1)
  127. if nComps[i] == 2:
  128. spec11 = (ammonia.cold_ammonia(xarr11, T1,
  129. ntot=N1,
  130. width=W1,
  131. xoff_v=V1)
  132. + ammonia.cold_ammonia(xarr11, T2,
  133. ntot=N2,
  134. width=W2,
  135. xoff_v=V2))
  136. spec22 = (ammonia.cold_ammonia(xarr22, T1,
  137. ntot=N1,
  138. width=W1,
  139. xoff_v=V1)
  140. + ammonia.cold_ammonia(xarr22, T2,
  141. ntot=N2,
  142. width=W2,
  143. xoff_v=V2))
  144. cube11[:, yy, xx] = spec11
  145. cube22[:, yy, xx] = spec22
  146. cube11 += np.random.randn(*cube11.shape) * noise_rms
  147. cube22 += np.random.randn(*cube22.shape) * noise_rms
  148. hdu11 = fits.PrimaryHDU(cube11)
  149. for kk in hdrkwds:
  150. hdu11.header[kk] = hdrkwds[kk]
  151. for kk, vv in zip(truekwds, [nComps[i], logN1[i], logN2[i],
  152. Voff1[i], Voff2[i], Width1[i], Width2[i],
  153. Temp1[i], Temp2[i]]):
  154. hdu11.header[kk] = vv
  155. hdu11.header['CRVAL3'] = 23694495500.0
  156. hdu11.header['RESTFRQ'] = 23694495500.0
  157. hdu11.writeto(output_dir + '/random_cube_NH3_11_'
  158. + '{0}'.format(i).zfill(nDigits)
  159. + '.fits',
  160. overwrite=True)
  161. hdu22 = fits.PrimaryHDU(cube22)
  162. for kk in hdrkwds:
  163. hdu22.header[kk] = hdrkwds[kk]
  164. for kk, vv in zip(truekwds, [nComps[i], logN1[i], logN2[i],
  165. Voff1[i], Voff2[i], Width1[i], Width2[i],
  166. Temp1[i], Temp2[i]]):
  167. hdu22.header[kk] = vv
  168. hdu22.header['CRVAL3'] = 23722633600.0
  169. hdu22.header['RESTFRQ'] = 23722633600.0
  170. hdu22.writeto(output_dir + '/random_cube_NH3_22_'
  171. + '{0}'.format(i).zfill(nDigits) + '.fits',
  172. overwrite=True)
  173.  
  174. if __name__ == '__main__':
  175. print(sys.argv)
  176. if len(sys.argv) > 1:
  177. generate_cubes(nCubes=int(sys.argv[1]))
  178. else:
  179. generate_cubes()
Add Comment
Please, Sign In to add comment