Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 7.44 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using 'get' method of a dictionary to select case. Why does it keep selecting the wrong thing?
  2. di_eqns = {"PSME": Pred_PSME(liLn), "ABAM":Pred_ABAM(liLn),
  3.            "ABCO":Pred_Abies(liLn), "ABGR":Pred_Abies(liLn),
  4.            "ABLA":Pred_Abies(liLn), "ABMA":Pred_Abies(liLn),
  5.            "ABSH":Pred_Abies(liLn), "ABPR":Pred_ABPR(liLn),
  6.            "CADE27":Pred_Cedar(liLn), "CHLA":Pred_Cedar(liLn),
  7.            "CHNO":Pred_Cedar(liLn), "THPL":Pred_Cedar(liLn),
  8.            "LALY":Pred_LALY(liLn), "LAOC":Pred_LAOC(liLn),
  9.            "PIEN":Pred_PIEN(liLn), "PISI":Pred_PISI(liLn),
  10.            "PIAL":Pred_Pinus(liLn), "PIJE":Pred_Pinus(liLn),
  11.            "PIMO3":Pred_Pinus(liLn), "PICO":Pred_PICO(liLn),
  12.            "PILA":Pred_PILA(liLn), "PIPO":Pred_PIPO(liLn),
  13.            "TSHE":Pred_TSHE(liLn), "TSME":Pred_TSME(liLn),
  14.            "JUOC":Pred_JUOC(liLn), "TABR":Pred_TSHE(liLn),
  15.            "ALRU2":Pred_ALRU2(liLn), "ACCI":Pred_SMBGr(liLn),
  16.            "ACGL":Pred_SMBGr(liLn), "ACMA3":Pred_ACMA3(liLn),
  17.            "CONU4":Pred_ACMA3(liLn), "ALRH2":Pred_AACWGr(liLn),
  18.            "SASC":Pred_AACWGr(liLn), "SALIX":Pred_AACWGr(liLn),
  19.            "ARME":Pred_ARME(liLn), "BEPA":Pred_BEPA(liLn),
  20.            "BEPAC":Pred_BEPA(liLn), "CHCH7":Pred_CHCH7(liLn),
  21.            "FRLA":Pred_MHGr(liLn), "PREM":Pred_MHGr(liLn),
  22.            "PRVI":Pred_MHGr(liLn), "PRUNU":Pred_MHGr(liLn),
  23.            "LIDE3":Pred_LIDE3(liLn), "POBAT":Pred_POBAT(liLn),
  24.            "POTR5":Pred_POTR5(liLn), "QUCH2":Pred_QUCH2(liLn),
  25.            "QUGA4":Pred_QUGA4(liLn), "QUKE":Pred_QUKE(liLn),
  26.            "UMCA":Pred_UMCA(liLn)}
  27. try: li_pred = di_eqns.get(liLn[2])
  28.        
  29. ['1347189', '20571', 'PIEN', 'Picea engelmannii', 'Y', 'Y', '66.294',
  30.  '0.3452', '35.7418', '', '5.0996', '0', '1', '1.1324', '3.2803', '2.3057',
  31.  '16.7282', '5520.306', '127.30', '70.73', '92.10', '0.00', '5.68', '2.37',
  32.  '7.25', '79.16', '76.79', '290.15', '219.41', '135.37', '0.00', '0.00',
  33.  '951.78', '719.74', '259.68', '251.89', '444.05', '0.00', '0.00', '',
  34.  '12.70', '10.16', '15.24', '0.02', '0.04', '0.19', '0.19', '0.00', '0.00',
  35.  '0.42', '0.32', '0.11', '0.11rn']
  36.        
  37. def Main():
  38.     srcf = open(bkp, 'r')
  39.     old_fcid = 0
  40.     li_fBQI = []
  41.     i = 0
  42.     for line in srcf:
  43.         liLn = line.split(',')
  44.         liLn = stripquotes(liLn)
  45.         #Check if it's the first line, if so, jump to next line.
  46.         if len(liLn) < 2: continue
  47.         if HdrLine(liLn[:2]): continue
  48.         fcid = liLn[1]
  49.         #Check line fcid against last fcid
  50.         if old_fcid != 0 and fcid != old_fcid:
  51.             #Write the FCID BQI tallies to file
  52.             Write_fBQIs(old_fcid, li_fBQI)
  53.             #Reset FCID BQI tallies
  54.             li_fBQI = []
  55.         old_fcid = fcid
  56.         #Calc BQI's for the tree
  57.         li_tBQI = BQI_Calc(liLn)
  58.         #Add tree BQI's to the FCID tallies
  59.         li_fBQI = AddBQI(li_tBQI, li_fBQI)
  60.         if i % 1000 == 0: print 'Finished line #' + str(i)
  61.         i += 1
  62.     #ADD: Write last FCID's BQI's to file
  63.     Write_fBQIs(old_fcid, li_fBQI)
  64.     srcf.close()
  65.  
  66. def BQI_Calc(liLn):
  67.     di_eqns = {"PSME": Pred_PSME(liLn), "ABAM":Pred_ABAM(liLn),
  68.                "ABCO":Pred_Abies(liLn), "ABGR":Pred_Abies(liLn),
  69.                "ABLA":Pred_Abies(liLn), "ABMA":Pred_Abies(liLn),
  70.                "ABSH":Pred_Abies(liLn), "ABPR":Pred_ABPR(liLn),
  71.                "CADE27":Pred_Cedar(liLn), "CHLA":Pred_Cedar(liLn),
  72.                "CHNO":Pred_Cedar(liLn), "THPL":Pred_Cedar(liLn),
  73.                "LALY":Pred_LALY(liLn), "LAOC":Pred_LAOC(liLn),
  74.                "PIEN":Pred_PIEN(liLn), "PISI":Pred_PISI(liLn),
  75.                "PIAL":Pred_Pinus(liLn), "PIJE":Pred_Pinus(liLn),
  76.                "PIMO3":Pred_Pinus(liLn), "PICO":Pred_PICO(liLn),
  77.                "PILA":Pred_PILA(liLn), "PIPO":Pred_PIPO(liLn),
  78.                "TSHE":Pred_TSHE(liLn), "TSME":Pred_TSME(liLn),
  79.                "JUOC":Pred_JUOC(liLn), "TABR":Pred_TSHE(liLn),
  80.                "ALRU2":Pred_ALRU2(liLn), "ACCI":Pred_SMBGr(liLn),
  81.                "ACGL":Pred_SMBGr(liLn), "ACMA3":Pred_ACMA3(liLn),
  82.                "CONU4":Pred_ACMA3(liLn), "ALRH2":Pred_AACWGr(liLn),
  83.                "SASC":Pred_AACWGr(liLn), "SALIX":Pred_AACWGr(liLn),
  84.                "ARME":Pred_ARME(liLn), "BEPA":Pred_BEPA(liLn),
  85.                "BEPAC":Pred_BEPA(liLn), "CHCH7":Pred_CHCH7(liLn),
  86.                "FRLA":Pred_MHGr(liLn), "PREM":Pred_MHGr(liLn),
  87.                "PRVI":Pred_MHGr(liLn), "PRUNU":Pred_MHGr(liLn),
  88.                "LIDE3":Pred_LIDE3(liLn), "POBAT":Pred_POBAT(liLn),
  89.                "POTR5":Pred_POTR5(liLn), "QUCH2":Pred_QUCH2(liLn),
  90.                "QUGA4":Pred_QUGA4(liLn), "QUKE":Pred_QUKE(liLn),
  91.                "UMCA":Pred_UMCA(liLn)}
  92.     try:
  93.         li_pred = di_eqns.get(liLn[2])
  94.     except:
  95.         print 'nn', 50*'-', 'ERROR: Couldn't find equations for the spp',
  96.               'code:', liLn[2]
  97.  
  98.     #Calc derivative quantities
  99.  
  100.     .....and the function continues from there
  101.        
  102. def Pred_PSME(liLn):
  103.     dbh = float(liLn[6])
  104.     fol = exp(log(float(liLn[19])) + pqty(.0627, 123))
  105.     lbr = exp((-3.6941 + 2.1382*log(dbh)) + pqty(.057, 123))
  106.     dbr = exp((-3.529 + 1.7503*log(dbh)) + pqty(.079, 85))
  107.     sw = exp(log(float(liLn[20])) + pqty(.0311, 99))
  108.     try:
  109.         sb = exp(log(float(liLn[21])) + pqty(.0324, 99))
  110.     except:
  111.         Badtree(liLn)
  112.         print dbh, liLn
  113.     ts = exp(-3.0396 + 2.5951*log(0.865 * mtd) + pqty(.0311, 99)) +
  114.          exp(-4.3103 + 2.4300*log(0.865 * mtd) + pqty(.0324, 99))
  115.     br = lbr +dbr
  116.     agb_nf = br + sw + sb
  117.     agb = agb_nf + fol
  118.     return [fol, br, sw, sb, agb, agb_nf, ts]
  119.  
  120. def Pred_ARME(liLn):
  121.     dbh = float(liLn[6])
  122.     agb_nf = float(liLn[27]) + tvariate(57) * 1.23 * (1 + 1/60)**(1/2)
  123.     agb = agb_nf / (1 - folpct)
  124.     fol = agb - agb_nf
  125.     br = sw = sb = ts = 0
  126.     return [fol, br, sw, sb, agb, agb_nf, ts]
  127.        
  128. di_eqns = {"PSME":Pred_PSME, "ABAM":Pred_ABAM,
  129.            "ABCO":Pred_Abies, "ABGR":Pred_Abies,
  130.            "ABLA":Pred_Abies, "ABMA":Pred_Abies,
  131.            "ABSH":Pred_Abies, "ABPR":Pred_ABPR,
  132.            "CADE27":Pred_Cedar, "CHLA":Pred_Cedar,
  133.            "CHNO":Pred_Cedar, "THPL":Pred_Cedar,
  134.            "LALY":Pred_LALY, "LAOC":Pred_LAOC,
  135.            "PIEN":Pred_PIEN, "PISI":Pred_PISI,
  136.            "PIAL":Pred_Pinus, "PIJE":Pred_Pinus,
  137.            "PIMO3":Pred_Pinus, "PICO":Pred_PICO,
  138.            "PILA":Pred_PILA, "PIPO":Pred_PIPO,
  139.            "TSHE":Pred_TSHE, "TSME":Pred_TSME,
  140.            "JUOC":Pred_JUOC, "TABR":Pred_TSHE,
  141.            "ALRU2":Pred_ALRU2, "ACCI":Pred_SMBGr,
  142.            "ACGL":Pred_SMBGr, "ACMA3":Pred_ACMA3,
  143.            "CONU4":Pred_ACMA3, "ALRH2":Pred_AACWGr,
  144.            "SASC":Pred_AACWGr, "SALIX":Pred_AACWGr,
  145.            "ARME":Pred_ARME, "BEPA":Pred_BEPA,
  146.            "BEPAC":Pred_BEPA, "CHCH7":Pred_CHCH7,
  147.            "FRLA":Pred_MHGr, "PREM":Pred_MHGr,
  148.            "PRVI":Pred_MHGr, "PRUNU":Pred_MHGr,
  149.            "LIDE3":Pred_LIDE3, "POBAT":Pred_POBAT,
  150.            "POTR5":Pred_POTR5, "QUCH2":Pred_QUCH2,
  151.            "QUGA4":Pred_QUGA4, "QUKE":Pred_QUKE,
  152.            "UMCA":Pred_UMCA}
  153.  
  154. try: li_pred = di_eqns.get(liLn[2])(liLn)
  155.        
  156. di_eqns = {
  157.     # for better legibility, arrange the following lines in alphabetical key order;
  158.     # then a duplicate key entry is easier to see.
  159.     "PSME": Pred_PSME,
  160.     "ABAM": Pred_ABAM,
  161.     "ABCO": Pred_Abies,
  162.     "ABGR": Pred_Abies,
  163.     # et cetera
  164.     }
  165.        
  166. if debug: print "liLn", liLn
  167. handler_func = di_eqns[liLn[2]] # be prepared for KeyError here
  168. if debug: print "handler", repr(handler_func)
  169. li_pred = handler_func(liLn)
  170. if debug: print "li_pred", repr(li_pred)
  171.        
  172. for guff in f:
  173.     liLn = guff.split(',')