daily pastebin goal
80%
SHARE
TWEET

FMDA Excercise 4.2

a guest Oct 13th, 2017 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #%% Exercise on population 4_2
  2. """
  3. Assignment:
  4.    1  download file
  5.    2  calculate pop change P[d] 'Population delta'
  6.    3  calculate pop growth rate 'r' in % on yearly basis (see calculation below)
  7.       r = P[d] / ( P[t-1] * t[d] )
  8.    4a print a table; headers & data
  9.    4b print a sequence of lines that read;
  10.       - Time period (initial year (t0) – final year (t1)).
  11.       - Initial population (million) at t0.
  12.       - Population change (million) <-- your results.
  13.       - Population growth rate (%) <-- your results.
  14. """
  15.  
  16. file_name = "your file name.dat"
  17. file_path1 = "your file path"
  18. file_path2 = "your alternative file path"
  19. file_path_complete1 = file_path1 + file_name
  20. file_path_complete2 = file_path2 + file_name
  21.  
  22.  
  23. line_data = 0 # emty this string before starting
  24. tab_char = 0 # location of tab
  25. prev_line_read = str() # empty string befrore starting
  26. pop_list = [] # list of population
  27. year_list = [] # list of year
  28. pop_incr = 0 # population increase per timestep
  29. a = 0 # iteration value for making lists (used multiple times)
  30. p_incr_list = [] # list of population increase per timestep
  31. growth_rate = 0 # speaks for itself
  32. r_list = [] # list of growthrates
  33.  
  34.  
  35.  
  36. #open file
  37. try:
  38.     file_data = open(file_path_complete1, "r")
  39. except:
  40.     try:
  41.         file_data = open(file_path_complete2, "r")
  42.     except:
  43.         print("Open Failed!")
  44.  
  45. # read data into list of population and year (and close file)
  46. while True:
  47.     line_read = file_data.readline()
  48.     try: # Check if line is data or something else (header or end)
  49.         line_data = int(line_read[:1]) + 1
  50.         tab_char = line_read.find("\t", 1)
  51.         line_read_len = len(line_read) - 1
  52.        
  53.         population = int(line_read[tab_char + 1:line_read_len])
  54.         pop_list.append(population)
  55.         year = int(line_read[:tab_char])
  56.         year_list.append(year)
  57.     except:
  58.         if line_read == "":
  59.             break
  60. file_data.close()
  61.  
  62. # print lists as check
  63. #print(year_list, "\n")
  64. #print(pop_list, "\n")
  65.  
  66. #(step 2) make list of population increase per timestep
  67. for i in pop_list:
  68.     if a != 0:
  69.         b = a - 1
  70.         pop_incr = (pop_list[a]) - (pop_list[b])
  71.         p_incr_list.append(pop_incr)
  72.     else:
  73.         p_incr_list.append(0)
  74.     a = a + 1
  75.  
  76. # print list as check
  77. #print(p_incr_list, "\n")
  78.  
  79. #(step 3) calculate growthrate 'r'
  80. a = 0
  81. for i in pop_list:
  82.     if a != 0:
  83.         b = a - 1
  84.         growth_rate = p_incr_list[a] / ( pop_list[b] * ( year_list[a] - year_list[b] ) )
  85.         growth_rate = round(growth_rate, 4)
  86.         r_list.append(growth_rate)
  87.     else:
  88.         r_list.append(0)
  89.     a = a + 1
  90.  
  91. # print list as check
  92. #print(r_list, "\n")
  93.  
  94. #(step 4a) print a table
  95. print("Year\tPopulation\tPop. increase\tGrowth rate")
  96. a = 0
  97. for i in pop_list:
  98.     print(year_list[a], "\t", pop_list[a], "\t", p_incr_list[a], "\t", r_list[a])
  99.     a = a + 1
  100.  
  101. #(step 4b) print line sequence
  102. print("\n- Time period ", year_list[0], " to ", year_list[len(year_list)-1])
  103. population = round(float(pop_list[0])/1000,1)
  104. print("- Initial population", population, " (million) at year ", year_list[0])
  105. pop_change = round(float((pop_list[len(pop_list)-1] - pop_list[0]))/1000, 1)
  106. print("- Population change ", pop_change, " (million)")
  107. growth_rate = round(float( sum(p_incr_list) / ( pop_list[0] * ( year_list[len(year_list)-1] - year_list[0] ) ))*100,2)
  108. print("- Population growth rate ", growth_rate, "% (yearly average)")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top