ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/JSOC/proj/globalhs/sosh/soshdata.py
(Generate patch)

Comparing proj/globalhs/sosh/soshdata.py (file contents):
Revision 1.3 by tplarson, Mon Mar 4 17:33:22 2019 UTC vs.
Revision 1.4 by tplarson, Mon Apr 8 16:52:19 2019 UTC

# Line 1 | Line 1
1   import numpy as np
2   import os
3 + import re
4   import drms
5   from astropy.io import fits
6   from urllib.request import urlretrieve
7   import soundfile as sf
8 + from scipy import fftpack
9  
10   import warnings
11   warnings.simplefilter(action='ignore', category=FutureWarning)
# Line 168 | Line 170 | def getwavfiles(instrument=None, day=Non
170        print("Invalid number, try again.")
171        return None
172  
173 <  wblank = '%s.%id_l=%i_m=%i_data%s.wav'
173 >  wblank = '%s.%id_72d.l=%i_m=%i_data%s.wav'
174    wfile=os.path.join(datadir,wblank % (inst.lower(),day,l,m,'r'))
175    wfile2=os.path.join(datadir,wblank % (inst.lower(),day,l,m,'i'))
176    if os.path.exists(wfile) and (m==0 or os.path.exists(wfile2)):
# Line 293 | Line 295 | def apols(lin, amaxin):
295    return pols
296  
297  
298 + def splitwavfile(wavfile=None, splitfactor=2, splitboth=True):
299 +
300 +  wblank = '%s.%id_%id.l=%i_m=%i_data%s.wav'
301 +  outlist=[]
302 +
303 +  if (wavfile == None):
304 +    inst=input("Enter name of instrument: ")
305 +    inst=inst.lower()
306 +    if (inst != 'mdi' and inst != 'hmi'):
307 +      print("Invalid instrument, try again.")
308 +      return None
309 +    daystr=input("Enter day number: ")
310 +    try:
311 +      firstday=int(daystr)
312 +    except:
313 +      print("Invalid number, try again.")
314 +      return None
315 +    ndstr=input("Enter number of days: ")
316 +    try:
317 +      ndaysin=int(ndstr)
318 +    except:
319 +      print("Invalid number, try again.")
320 +      return None
321 +    lstr=input("Enter spherical harmonic degree: ")
322 +    try:
323 +      l=int(lstr)
324 +      if (l < 0):
325 +        print("Degree l must be nonnegative, try again.")
326 +        return None
327 +    except:
328 +      print("Invalid number, try again.")
329 +      return None
330 +    mstr=input("Enter azimuthal order: ")
331 +    try:
332 +      m=int(mstr)
333 +      m=np.abs(m)
334 +      if (m > l):
335 +        print("Abs(m) must be less than l, try again.")
336 +        return None
337 +    except:
338 +      print("Invalid number, try again.")
339 +      return None
340 +    ri='r'
341 +    wavfile=os.path.join(datadir,wblank % (inst,firstday,ndaysin,l,m,ri))
342 +  else:
343 +    regmatch = re.search(r"(\S+)\.(\d+)d_(\d+)d\.l=(\d+)_m=(\d+)_data(\S+).wav", wavfile)
344 +    if (regmatch == None):
345 +      print("Invalid file name, try again.")
346 +      return None
347 +    else:
348 +      inst=regmatch.group(1)
349 +      firstday=int(regmatch.group(2))
350 +      ndaysin=int(regmatch.group(3))
351 +      l=int(regmatch.group(4))
352 +      m=int(regmatch.group(5))
353 +      ri=regmatch.group(6)
354 +
355 +  test= ndaysin % splitfactor
356 +  if (test != 0):
357 +    print("Timeseries not divisible by that factor.")
358 +    return None
359 +
360 + #  wfile = os.path.join(datadir,wavfile)
361 +  try:
362 +    x, sr = sf.read(wavfile)
363 +  except:
364 +    print("Invalid file, try again.")
365 +    return None
366 +
367 +  if (inst == 'mdi'):
368 +    nperday = 1440
369 +  elif (inst == 'hmi'):
370 +    nperday = 1920
371 +
372 +  ndaysout = ndaysin / splitfactor
373 +  ndt=int(ndaysout*nperday)
374 +  day = firstday
375 +  index=0
376 +  while (day < firstday+ndaysin):
377 +    xout=x[index:index+ndt]
378 +    wfileout = os.path.join(datadir,wblank % (inst,day,ndaysout,l,m,ri))
379 +    sf.write(wfileout, xout, sr, subtype='FLOAT')
380 +    day += ndaysout
381 +    index += ndt
382 +    outlist.append(wfileout)
383 +
384 +  if (splitboth and m > 0):
385 +    if (ri == 'i'):
386 +      ir = 'r'
387 +    elif (ri == 'r'):
388 +      ir = 'i'
389 +    wfile=wblank % (inst,firstday,ndaysin,l,m,ir)
390 +    wavfile = os.path.join(datadir,wfile)
391 +    try:
392 +      x, sr = sf.read(wavfile)
393 +    except:
394 +      print("Invalid file, try again.")
395 +      return None
396 +    day=firstday
397 +    index=0
398 +    while (day < firstday+ndaysin):
399 +      xout=x[index:index+ndt]
400 +      wfileout = os.path.join(datadir,wblank % (inst,day,ndaysout,l,m,ir))
401 +      sf.write(wfileout, xout, sr, subtype='FLOAT')
402 +      day += ndaysout
403 +      index += ndt
404 +      outlist.append(wfileout)
405 +
406 +  return outlist
407 +
408 +
409 + def scanspectrum(wavfile=None,downshift=4,firstbin=None,lastbin=None,nbins=200,ndt=10000,binstep=None,ramp=50):
410 +
411 +  if (binstep == None):
412 +    binstep = int(nbins/2)
413 +
414 +  wblank = '%s.%id_%id.l=%i_m=%i_data%s.wav'
415 +  if (wavfile == None):
416 +    inst=input("Enter name of instrument: ")
417 +    inst=inst.lower()
418 +    if (inst != 'mdi' and inst != 'hmi'):
419 +      print("Invalid instrument, try again.")
420 +      return None
421 +    daystr=input("Enter day number: ")
422 +    try:
423 +      firstday=int(daystr)
424 +    except:
425 +      print("Invalid number, try again.")
426 +      return None
427 +    ndstr=input("Enter number of days: ")
428 +    try:
429 +      ndaysin=int(ndstr)
430 +    except:
431 +      print("Invalid number, try again.")
432 +      return None
433 +    lstr=input("Enter spherical harmonic degree: ")
434 +    try:
435 +      l=int(lstr)
436 +      if (l < 0):
437 +        print("Degree l must be nonnegative, try again.")
438 +        return None
439 +    except:
440 +      print("Invalid number, try again.")
441 +      return None
442 +    mstr=input("Enter azimuthal order: ")
443 +    try:
444 +      m=int(mstr)
445 +      m=np.abs(m)
446 +      if (m > l):
447 +        print("Abs(m) must be less than l, try again.")
448 +        return None
449 +    except:
450 +      print("Invalid number, try again.")
451 +      return None
452 +    ri='r'
453 +    wavfile=os.path.join(datadir,wblank % (inst,firstday,ndaysin,l,m,ri))
454 +    print("Wav file is %s" % wavfile)
455 +  else:
456 +    regmatch = re.search(r"(\S+)\.(\d+)d_(\d+)d\.l=(\d+)_m=(\d+)_data(\S+).wav", wavfile)
457 +    if (regmatch == None):
458 +      print("Invalid file name, try again.")
459 +      return None
460 +    else:
461 +      inst=regmatch.group(1)
462 +      firstday=int(regmatch.group(2))
463 +      ndaysin=int(regmatch.group(3))
464 +      l=int(regmatch.group(4))
465 +      m=int(regmatch.group(5))
466 +      ri=regmatch.group(6)
467 +
468 +  try:
469 +    x, sr = sf.read(wavfile)
470 +  except:
471 +    print("Invalid file, try again.")
472 +    return None
473 +
474 +  z=np.zeros((len(x)),dtype=np.complex_)
475 +  if (ri == 'r'):
476 +    z.real=x
477 +  else:
478 +    z.imag=x
479 +
480 +  if (m > 0):
481 +    if (ri == 'i'):
482 +      ir = 'r'
483 +    elif (ri == 'r'):
484 +      ir = 'i'
485 +    wfile=wblank % (inst,firstday,ndaysin,l,m,ir)
486 +    wavfile = os.path.join(datadir,wfile)
487 +    try:
488 +      x, sr = sf.read(wavfile)
489 +    except:
490 +      print("Invalid file, try again.")
491 +      return None
492 +  else:
493 +    x=0.0*x
494 +
495 +  if (ri == 'r'):
496 +    z.imag=x
497 +  else:
498 +    z.real=x
499 +
500 +  window=np.ones(ndt)
501 +  if (ramp != 0):
502 +    rampsamples=int(sr*ramp/1000)
503 +    if (rampsamples > ndt/2):
504 +      print("Ramp longer than ndt/2, try again.")
505 +      return None
506 +    window[0:rampsamples]=np.linspace(0.0,1.0,rampsamples,endpoint=False)
507 +    window[ndt-1:ndt-rampsamples-1:-1]=np.linspace(0.0,1.0,rampsamples,endpoint=False)
508 +
509 +  nx=len(x)
510 +  nx2=int(nx/2)
511 +  if (firstbin == None):
512 +    firstbin = int(nx2/6)
513 +  if (lastbin == None):
514 +    lastbin = int(0.9*nx2)
515 +  ftz = fftpack.fft(z)
516 +  freq = fftpack.fftfreq(nx) #*sr
517 +  if (m >= 0):
518 +    mag = np.abs(ftz[0:nx2])
519 +    phase = np.arctan2(ftz[0:nx2].imag,ftz[0:nx2].real)
520 +    f = freq[0:nx2]/downshift
521 +  else:
522 +    mag = np.abs(ftz[nx:nx2:-1])
523 +    phase = np.arctan2(-1*ftz[nx:nx2:-1].imag,ftz[nx:nx2:-1].real)
524 +    f = -1*freq[nx:nx2:-1]/downshift
525 +
526 +  nstep=int((lastbin-firstbin)/binstep)
527 +  tlen=nstep*ndt
528 +  t=np.linspace(0,ndt-1,ndt)
529 +  xout=np.zeros((tlen))
530 +  bindex=firstbin
531 +  tindex=0
532 +  t0in=0
533 +  while (bindex + nbins <= lastbin):
534 +    for i in range(nbins):
535 +      xout[tindex:tindex+ndt]+=mag[bindex+i]*np.sin(2*np.pi*f[bindex+i]*(t+tindex)+phase[bindex+i])
536 +    xout[tindex:tindex+ndt]*=window
537 +    bindex+=binstep
538 +    tindex+=ndt
539 +
540 +  mx=np.abs(xout).max()
541 +  xout /= mx
542 +  sf.write('output.wav', xout, sr, subtype='FLOAT')
543 +
544 +  return xout
545 +
546 +
547 + #def plotspectrum(wavfile):
548 + #  x, sr = sf.read(wavfile)
549 + #  ftx = fftpack.fft(x)
550 + #  freqs = fftpack.fftfreq(len(x)) * sr
551 +
552 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines