#!/usr/bin/env python # coding: utf-8 # # Generate national and state-level crosswalks # ## 2000 block group parts to 2010 tracts # # ### NHGIS [block crosswalks](https://www.nhgis.org/user-resources/geographic-crosswalks) # # **James D. Gaboardi, 06/2020** # In[1]: get_ipython().run_line_magic('load_ext', 'watermark') get_ipython().run_line_magic('watermark', '') # In[2]: import nhgisxwalk import inspect import numpy import pandas get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') get_ipython().run_line_magic('watermark', '-w') get_ipython().run_line_magic('watermark', '-iv') # ### Source and target years for the crosswalk # In[3]: source_year, target_year = "2000", "2010" gj_src, gj_trg = "GJOIN%s"%source_year, "GJOIN%s"%target_year # In[4]: data_in = "../../crosswalks/" data_tab = "../../tabular_data/" block_file = "%s_block" % source_year # ### Source-target building base # In[5]: base_xwalk_name = "nhgis_blk%s_blk%s_gj" % (source_year, target_year) data_types = nhgisxwalk.str_types([gj_src, gj_trg]) from_csv_kws = {"path": data_in, "archived": True, "remove_unpacked": True} read_csv_kws = {"dtype": data_types} base_xwalk = nhgisxwalk.xwalk_df_from_csv( base_xwalk_name, **from_csv_kws, **read_csv_kws ) base_xwalk.head() # ### Source summary data # In[6]: base_source_name = "%s/%s.csv" % (block_file, block_file) base_source_file = "%s%s" % (data_tab, base_source_name) base_source_file # ### Convenience code shorthand/lookup # In[7]: nhgisxwalk.valid_geo_shorthand(shorthand_name=False) # ### Set the `nhgisxwalk.GeoCrossWalk` parameters # ##### see [nhgisxwalk.GeoCrossWalk](https://github.com/jGaboardi/nhgisxwalk/blob/92b4fe55de0a9c53d0315dcda8ec121faaf20aef/nhgisxwalk/geocrosswalk.py#L19) for full details # In[8]: nhgisxwalk.desc_code_2000_SF1b # In[9]: input_vars = [ nhgisxwalk.desc_code_2000_SF1b["Persons"]["Total"], nhgisxwalk.desc_code_2000_SF1b["Families"]["Total"], nhgisxwalk.desc_code_2000_SF1b["Households"]["Total"], nhgisxwalk.desc_code_2000_SF1b["Housing Units"]["Total"] ] input_vars # In[10]: input_var_tags = ["pop", "fam", "hh", "hu"] # In[11]: xwalk_args = { "source_year": source_year, "target_year": target_year, "source_geo": "bgp", "target_geo": "tr", "base_source_table": base_source_file, "input_var": input_vars, "weight_var": input_var_tags, "keep_base": False, "add_geoid": True } # ### Generate data product # 1. Create a national crosswalk then split by state # 2. Write out all products with `README.txt` files # In[12]: nhgisxwalk.generate_data_product(base_xwalk, xwalk_args, data_in) # -----------------------------------------------