
    i8                     Z   d Z ddlZddlZddlmZ ddlmZ ddlmZ  ee      j                  j                  Z
e
dz  Ze
dz  dz  Zd	ed
efdZd
efdZded
efdZd
efdZded
efdZdddddddddddddddddZddd d!dZded"ed
efd#Zd
efd$Zed%k(  r eej6                        d&kD  r eej6                  d&         ndZ ee      Z ed'e d(        ed)ed*    d+ed,    d-        ed.ed/    d+ed0    d-        ed1ed    d2ed3   d4   d5d6        ed7d8j?                  ed3   d9         xs d:         e         e       Z  ed;e d<            ed=e d>   d?d-        e         eee      Z! ed@e!dA            edBe!dC            edDe!dE           yy)Fus  
EMF Brain Integration — Aggregate all three EMF layers + thalamic routing.
Reads Layer 1 (earth-breath.md), Layer 2 (self-breath.md), Layer 3 (perturbation.json)
on every thalamus pass and generates emergent prescription.

This module is the "antenna" of ghojualamanchu — the perceptual bridge between
the electromagnetic environment and the cognitive architecture.
    N)Path)datetime)Optionalmemorydataemf
layer_namereturnc                    t         |  dz  }|j                         sd| dS |j                         }i }d}|j                  d      D ]o  }|j	                         }|j                  d      r%|j                  d      j                         }d||<   I|sL|j                         s]|j                         ||<   q d	|d
<   |S )z$Read a layer's markdown output file.z.mdF)existslayerN
z##z##  Tr   )
MEMORY_DIRr   	read_textsplitrstrip
startswithlstripstrip)r	   pathcontentstatecurrent_keylines         ./home/workspace/ghojualamanchu-v3/emf/brain.py
read_layerr      s    :,c**D;;=*55nnGEKd# .{{}??4 ++e,224K!#E+TZZ\!%E+. E(OL    c                      t         dz  } | j                         r*t        j                  | j	                               }d|d<   |S ddiS )zRead perturbation layer output.zperturbation_latest.jsonTr   F)DATA_DIRr   jsonloadsr   )	json_pathr   s     r   read_perturbationr$   '   sI    55Izz)--/0Xer   beatc                 *   t        d      }t        d      }t               }d}d}g }|j                  dd      rF|j                  dd      }|j                  di       j                  d	d      }|j                  d
g       }d}d}|j                  d      r?|j                  dd      }	 t        |j                  dd      j	                         d         }d}	d}
|j                  d      r?|j                  dd      }		 t        |j                  dd      j	                         d         }
| t        j                         j                         |||	|
|||||j                  dd      ddS # t
        t        f$ r d}Y w xY w# t
        t        f$ r d}
Y jw xY w)zq
    Read all three EMF layers and return aggregated EMF state.
    Called by thalamus on every signal pass.
    zearth-breathzself-breathDEADg      Tr   Ffield_statemetricsamplitude_rms_dbfssources_detectedHOLDRQ@Phase	Frequencyz7.83r   )r(   amplitude_dbfssourcesr   )r%   	timestampearth_phaseearth_hz
self_phaseself_hzr(   perturbation)
r   r$   getfloatr   
ValueError
IndexErrorr   now	isoformat)r%   earthself_breathr7   r(   r0   r1   r3   r4   r5   r6   s              r   aggregate_emfr@   1   s   
 ~&E]+K$&L KNG%("&&}f=%)))R8<<=QSXY""#5r:KHyyii0	UYY{F;AACAFGH JGx  __Wf5
	KOOK@FFHKLG
 \\^--/" "&,"&&x7	
  J' 	H	 J' 	G	s$   ',E' ;,E> 'E;:E;>FFc                     t         dz  dz  } | j                         r6t        j                  | j	                               }|j                  dd      }nd}t        |      }|t        |d         t        |d   |d   z
        dS )	z\
    Called by thalamus on every pass.
    Returns EMF context for conflict resolution.
    r   z
state.jsoncurrent_beatr   r(   r6   r4   )emf_readingthreshold_actionbeat_frequency)	BASE_DIRr   r!   r"   r   r8   r@   rD   abs)
state_pathr   r%   r   s       r   thalamic_inputrI   f   s    
 F"\1J

://12yy+

C,S-?@c)ns:>? r   r(   c                 4    ddddd}|j                  | d      S )z:
    Map perturbation field state to thalamic action.
    u/   alert — signal lost, check antenna connectionu%   inform only — log to akashic if newu/   log to perturbation.md — minor field activityu1   flag for cortex attention — major perturbationsr'   CALMSTIRSTORMunknown)r8   )r(   actionss     r   rD   rD   z   s+    
 B7AD	G ;;{I..r   surveilTg        )actioncortex_skipencoding_modifierobserveFg      ?attendg333333?alertg      ?rK   u)   Antenna down — operate in degraded modeu#   Quiet field — standard processingu,   Active field — slightly elevated attentionu1   Storm field — elevated encoding, full attention	emf_statec                    |j                  dd      }|j                  dd      }|j                  dd      }t        |j                  dd      |j                  dd      z
        }t        j                  |t        d	         }| t        j                         j                         |||||d
   |d   |d   t        j                  |d      d
S )zy
    Generate emergent prescription from EMF state.
    This is what the EMF environment "prescribes" for this beat.
    r(   r'   r3   r,   r5   r6   r-   r4   rL   rR   rS   rT   rO   )
r%   r2   r(   beat_frequency_hzr3   r5   prescriptionrS   rT   description)r8   rG   PRESCRIPTIONSr   r<   r=   PRESCRIPTION_DESCRIPTIONS)r%   rX   r(   r3   r5   	beat_freqbases          r   emergent_prescriptionra      s    
 --v6K--v6K|V4JIMM)T2Y]]:t5TTUI[-*?@D \\^--/"&" XM*!"56044[)L r   c                      t        d      S )z&Read current EMF state for inspection.r   )r@    r   r   read_current_staterd      s    r   __main__   z=== EMF Brain State (beat z) ===zEarth: r3   z @ r4   z HzzSelf:  r5   r6   zField: z | r7   r0   z.1fz dBFSz	Sources: z, r1   nonezThalamic input: rD   zBeat frequency: rE   z.3fzEmergent prescription: r[   zCortex skip: rS   zEncoding modifier: rT   )"__doc__r!   syspathlibr   r   typingr   __file__parentrF   r   r    strdictr   r$   intr@   rI   rD   r]   r^   ra   rd   __name__lenargvr%   r   printjoinrxeprc   r   r   <module>rx      s    
   >  '' 
fu$3 4 &4 2 2 2j (
/# 
/# 
/ "4sS!5sS %cR%cR	 92;@	    4D 
 z"388}q03sxx{aD

C	&tfE
23	GC&'s3z?*;3
?@	GC%&c#i.)9
=>	GC&'s3~+>?O+PQT*UUZ
[\	IdiiN 3I >?I6J
KL	G		B	R 234
56	R 01#6c
:;	G	tS	)B	#B~$6#7
89	M"]+,
-.	#6 78
9:! r   