original_docstring:  This code takes in a 3D gridded field of monthly mean/anomalies (time,lat,lon)
          and anomalises/renormalises (climate anomalies) to a new climatology period
  	which is supplied along side start and end years of the data.
  	It assumes all data go from Jan to Dec 
          INPUTS:
  	TheData: A 3D numpy array (months, latitude, longitude) of monthly means or anomalies 
          TheStYr: The start year of the provided data 
          TheEdYr: The end year of the provided data 
          TheClimSt: The start year of the new climatology period 
          TheClimEd: The end year of the new climatology period 
          TheMDI: The missing data indicator 
          TheMDI=-1e30 # DEFAULT
          TheMDITol: The proportion of data required for a gridbox climatology to be calculated from 0 to 1 
          TheMDITol=0.6 # DEFAULT 
  	OUTPUTS:
          TheAnoms: a 3D array identical in shape to TheData for the output, utilises missing data indicator 
docstring: This code takes in a 3D gridded field of monthly mean/anomalies (time,lat,lon)
  and anomalises/renormalises (climate anomalies) to a new climatology period
  which is supplied along side start and end years of the data.
docstring_tokens: This code takes in a 3D gridded field of monthly mean / anomalies ( time lat lon ) and anomalises / renormalises ( climate anomalies ) to a new climatology period which is supplied along side start and end years of the data .
code: def GetAnomalies(TheData,
  		 TheStYr,
  		 TheEdYr,
  		 TheClimSt,
  		 TheClimEd,
                   TheMDI=-1e30,
  		 TheMDITol=0.6):
      ''' This code takes in a 3D gridded field of monthly mean/anomalies (time,lat,lon)
          and anomalises/renormalises (climate anomalies) to a new climatology period
  	which is supplied along side start and end years of the data.
  	It assumes all data go from Jan to Dec 
          INPUTS:
  	TheData: A 3D numpy array (months, latitude, longitude) of monthly means or anomalies 
          TheStYr: The start year of the provided data 
          TheEdYr: The end year of the provided data 
          TheClimSt: The start year of the new climatology period 
          TheClimEd: The end year of the new climatology period 
          TheMDI: The missing data indicator 
          TheMDI=-1e30 # DEFAULT
          TheMDITol: The proportion of data required for a gridbox climatology to be calculated from 0 to 1 
          TheMDITol=0.6 # DEFAULT 
  	OUTPUTS:
          TheAnoms: a 3D array identical in shape to TheData for the output, utilises missing data indicator '''	    
      
      # Set up python imports
      import numpy as np
      import scipy.stats
      import pdb # pdb.set_trace() or c
      
      # Work out how many years of data in total and for the climatology period
      TheYCount = (TheEdYr - TheStYr) + 1
      TheCCount = (TheClimEd - TheClimSt) + 1
      
      # make empty array for anomalies
      TheAnoms = np.reshape(np.repeat(TheMDI,TheYCount*12*len(TheData[0,:,0])*len(TheData[0,0,:])),(TheYCount*12,len(TheData[0,:,0]),len(TheData[0,0,:])))
      
      # Loop through each latitude and longitude to work on a gridbox at a time
      for lnn in range(len(TheData[0,0,:])):
          for ltt in range(len(TheData[0,:,0])):
  	    # Have to use copies!
              subarr = np.copy(np.reshape(TheData[:,ltt,lnn],(TheYCount,12)))
              newsubarr = np.empty_like(subarr)
              newsubarr.fill(TheMDI)
              # loop through each month
              for mm in range(12):
                  subclimarr = np.copy(subarr[(TheClimSt-TheStYr):(TheClimEd-TheStYr)+1,mm])	# always need +1 for working with subranges
                  subsubarr = np.copy(subarr[:,mm])
                  subclimarr[subclimarr == TheMDI] = np.nan
                  subsubarr[subsubarr == TheMDI] = np.nan
                  #print(np.float(len(subclimarr[np.isfinite(subclimarr)]))/np.float(TheCCount))
                  if (np.float(len(subclimarr[np.isfinite(subclimarr)]))/np.float(TheCCount) >= TheMDITol):
                      subsubarr[np.isfinite(subsubarr)] = subsubarr[np.isfinite(subsubarr)] - np.nanmean(subclimarr)
                      subsubarr[np.isnan(subsubarr)] = TheMDI
                      newsubarr[:,mm] = np.copy(subsubarr)
              TheAnoms[:,ltt,lnn] = np.copy(np.reshape(newsubarr,TheYCount*12))
  	    
      return TheAnoms
code_tokens: def GetAnomalies ( TheData , TheStYr , TheEdYr , TheClimSt , TheClimEd , TheMDI = - 1e30 , TheMDITol = 0.6 ) : import numpy as np import scipy . stats import pdb TheYCount = ( TheEdYr - TheStYr ) + 1 TheCCount = ( TheClimEd - TheClimSt ) + 1 TheAnoms = np . reshape ( np . repeat ( TheMDI , TheYCount * 12 * len ( TheData [ 0 , : , 0 ] ) * len ( TheData [ 0 , 0 , : ] ) ) , ( TheYCount * 12 , len ( TheData [ 0 , : , 0 ] ) , len ( TheData [ 0 , 0 , : ] ) ) ) for lnn in range ( len ( TheData [ 0 , 0 , : ] ) ) : for ltt in range ( len ( TheData [ 0 , : , 0 ] ) ) : subarr = np . copy ( np . reshape ( TheData [ : , ltt , lnn ] , ( TheYCount , 12 ) ) ) newsubarr = np . empty_like ( subarr ) newsubarr . fill ( TheMDI ) for mm in range ( 12 ) : subclimarr = np . copy ( subarr [ ( TheClimSt - TheStYr ) : ( TheClimEd - TheStYr ) + 1 , mm ] ) subsubarr = np . copy ( subarr [ : , mm ] ) subclimarr [ subclimarr == TheMDI ] = np . nan subsubarr [ subsubarr == TheMDI ] = np . nan if ( np . float ( len ( subclimarr [ np . isfinite ( subclimarr ) ] ) ) / np . float ( TheCCount ) >= TheMDITol ) : subsubarr [ np . isfinite ( subsubarr ) ] = subsubarr [ np . isfinite ( subsubarr ) ] - np . nanmean ( subclimarr ) subsubarr [ np . isnan ( subsubarr ) ] = TheMDI newsubarr [ : , mm ] = np . copy ( subsubarr ) TheAnoms [ : , ltt , lnn ] = np . copy ( np . reshape ( newsubarr , TheYCount * 12 ) ) return TheAnoms
short_docstring: This code takes in a 3D gridded field of monthly mean/anomalies (time,lat,lon)
  and anomalises/renormalises (climate anomalies) to a new climatology period
  which is supplied along side start and end years of the data.
short_docstring_tokens: This code takes in a 3D gridded field of monthly mean / anomalies ( time lat lon ) and anomalises / renormalises ( climate anomalies ) to a new climatology period which is supplied along side start and end years of the data .
comment: ''' This code takes in a 3D gridded field of monthly mean/anomalies (time,lat,lon)
          and anomalises/renormalises (climate anomalies) to a new climatology period
  	which is supplied along side start and end years of the data.
  	It assumes all data go from Jan to Dec 
          INPUTS:
  	TheData: A 3D numpy array (months, latitude, longitude) of monthly means or anomalies 
          TheStYr: The start year of the provided data 
          TheEdYr: The end year of the provided data 
          TheClimSt: The start year of the new climatology period 
          TheClimEd: The end year of the new climatology period 
          TheMDI: The missing data indicator 
          TheMDI=-1e30 # DEFAULT
          TheMDITol: The proportion of data required for a gridbox climatology to be calculated from 0 to 1 
          TheMDITol=0.6 # DEFAULT 
  	OUTPUTS:
          TheAnoms: a 3D array identical in shape to TheData for the output, utilises missing data indicator '''$SEP$# Set up python imports$SEP$# pdb.set_trace() or c$SEP$# Work out how many years of data in total and for the climatology period$SEP$# make empty array for anomalies$SEP$# Loop through each latitude and longitude to work on a gridbox at a time$SEP$# Have to use copies!$SEP$# loop through each month$SEP$# always need +1 for working with subranges$SEP$#print(np.float(len(subclimarr[np.isfinite(subclimarr)]))/np.float(TheCCount))