1111SAM_path = "https://www.wider.unu.edu/sites/default/files/Data/SASAM-2015-Data-Resource.xlsx"
1212SAM_path_alt = "https://raw.githubusercontent.com/EAPD-DRB/SAM-files/main/Data/ZAF/SASAM-2015-Data-Resource.xlsx"
1313
14- if is_connected ():
15- try :
16- SAM = pd .read_excel (
17- SAM_path ,
18- sheet_name = "Micro SAM 2015" ,
19- skiprows = 6 ,
20- index_col = 0 ,
21- storage_options = storage_options ,
22- )
23- print ("Successfully read SAM from WIDER." )
24- except Exception as e :
25- print (f"Failed to read from WIDER: { e } " )
14+
15+ def read_SAM ():
16+ if is_connected ():
2617 try :
27- # Attempt to read from the GitHub repository
2818 SAM = pd .read_excel (
29- SAM_path_alt ,
19+ SAM_path ,
3020 sheet_name = "Micro SAM 2015" ,
3121 skiprows = 6 ,
3222 index_col = 0 ,
3323 storage_options = storage_options ,
3424 )
35- print ("Successfully read SAM from GitHub repository ." )
25+ print ("Successfully read SAM from WIDER ." )
3626 except Exception as e :
37- print (f"Failed to read from the GitHub repository: { e } " )
38- SAM = None
39- # If both attempts fail, SAM will be None
40- if SAM is None :
41- print ("Failed to read SAM from both sources." )
42- else :
43- SAM = None
44- print ("No internet connection. SAM cannot be read." )
27+ print (f"Failed to read from WIDER: { e } " )
28+ try :
29+ # Attempt to read from the GitHub repository
30+ SAM = pd .read_excel (
31+ SAM_path_alt ,
32+ sheet_name = "Micro SAM 2015" ,
33+ skiprows = 6 ,
34+ index_col = 0 ,
35+ storage_options = storage_options ,
36+ )
37+ print ("Successfully read SAM from GitHub repository." )
38+ except Exception as e :
39+ print (f"Failed to read from the GitHub repository: { e } " )
40+ SAM = None
41+ # If both attempts fail, SAM will be None
42+ if SAM is None :
43+ print ("Failed to read SAM from both sources." )
44+ else : # pragma: no cover
45+ SAM = None
46+ print ("No internet connection. SAM cannot be read." )
47+ return SAM
4548
4649
47- def get_alpha_c (sam = SAM , cons_dict = CONS_DICT ):
50+ def get_alpha_c (sam = None , cons_dict = CONS_DICT ):
4851 """
4952 Calibrate the alpha_c vector, showing the shares of household
5053 expenditures for each consumption category
@@ -56,6 +59,8 @@ def get_alpha_c(sam=SAM, cons_dict=CONS_DICT):
5659 Returns:
5760 alpha_c (dict): Dictionary of shares of household expenditures
5861 """
62+ if sam is None :
63+ sam = read_SAM ()
5964 alpha_c = {}
6065 overall_sum = 0
6166 for key , value in cons_dict .items ():
@@ -72,7 +77,7 @@ def get_alpha_c(sam=SAM, cons_dict=CONS_DICT):
7277 return alpha_c
7378
7479
75- def get_io_matrix (sam = SAM , cons_dict = CONS_DICT , prod_dict = PROD_DICT ):
80+ def get_io_matrix (sam = None , cons_dict = CONS_DICT , prod_dict = PROD_DICT ):
7681 """
7782 Calibrate the io_matrix array. This array relates the share of each
7883 production category in each consumption category
@@ -85,6 +90,8 @@ def get_io_matrix(sam=SAM, cons_dict=CONS_DICT, prod_dict=PROD_DICT):
8590 Returns:
8691 io_df (pd.DataFrame): Dataframe of io_matrix
8792 """
93+ if sam is None :
94+ sam = read_SAM ()
8895 # Create initial matrix as dataframe of 0's to fill in
8996 io_dict = {}
9097 for key in prod_dict .keys ():
0 commit comments