# This mostly follows the layout described by the TxRRC in the
# manual available at https://rrc.texas.gov/media/1252/gra001.pdf
#
# The main difference is that date fields described separately in the 
# layout provided are instead combined into one date field, as are
# GR_DISTRICT and GR_SERIAL_NUMBER fields. The combination of the 
# latter two fields is used as a foreign key to connect records with
# specific plants and cycles.

root_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('root_id', 2, 6, 'pic_numeric'), # This combines GR_DISTRICT and GR_SERIAL_NUMBER
  ('GR_OPERATOR_NUMBER', 8, 6, 'pic_numeric'),
  ('GR_COUNTY', 14, 3, 'pic_numeric'),
  ('GR_PLANT_NAME', 17, 32, 'pic_any'),
  ('GR_PLANT_TYPE', 49, 1, 'pic_any'),
  ('GR_PLANT_AVG_INTAKE_CAPACITY', 55, 7, 'pic_numeric'),
  ('GR_PLANT_OFFSHORE_INDICATOR', 62, 1, 'pic_any'),
  ('GR_PLANT_TELE', 63, 10, 'pic_numeric'),
  ('GR_PLANT_FIELDS', 73, 50, 'pic_any'),
  ('GR_R3_DATE', 123, 6, 'pic_numeric'),
  ('GR_R5_DATE', 129, 6, 'pic_numeric'),
  ('GR_R5_STATUS', 135, 1, 'pic_any'),
  ('GR_LAST_R3_RPT', 136, 6, 'pic_numeric'),
]
cycle_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_CYCLE_KEY', 2, 4, 'pic_numeric'),
  ('GR_R3_SEC_VI_GAS_WELL_TOT', 6, 4, 'comp'),
  ('GR_R3_SEC_VI_OIL_WELL_TOT', 10, 4, 'comp'),
  ('GR_R3_SEC_VI_BARRELS_TOT', 14, 6, 'comp'),
  ('GR_R3_SEC_VIII_RECEIVE_TOT', 20, 6, 'comp'),
  ('GR_R3_SEC_VIII_DELIVER_TOT', 26, 6, 'comp'),
  ('GR_R3_SEC_IX_TOTAL', 32, 6, 'comp'),
  ('GR_R3_SEC_X_UNPROCESS_TOT', 38, 6, 'comp'),
  ('GR_R3_SEC_X_RESIDUE_TOT', 44, 6, 'comp'),
  ('GR_C1_PLT_FUEL_MONTHLY_VOL', 50, 6, 'comp'),
  ('GR_C1_MANUFAC_MONTHLY_VOL', 56, 6, 'comp'),
  ('GR_R3_CORRECTED_REPORT_FLAG', 62, 1, 'pic_any'),
  ('GR_R3_LETTER_DATE', 63, 8, 'pic_numeric'),  #YYYYMMDD
]
intake_volumes_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_SEC_I_GWG_WELLS', 2, 3, 'comp'),
  ('GR_SEC_I_CAS_WELLS', 5, 3, 'comp'),
  ('GR_SEC_I_TOTAL_WELLS', 8, 4, 'comp'),
  ('GR_SEC_I_GWG_GATHER', 12, 5, 'comp'),
  ('GR_SEC_I_CAS_GATHER', 17, 5, 'comp'),
  ('GR_SEC_I_TOTAL_GATHER', 22, 6, 'comp'),
  ('GR_SEC_I_GWG_DELIVER', 28, 5, 'comp'),
  ('GR_SEC_I_CAS_DELIVER', 33, 5, 'comp'),
  ('GR_SEC_I_TOTAL_DELIVER', 38, 6, 'comp'),
  ('GR_SEC_I_GWG_PROCESS', 44, 5, 'comp'),
  ('GR_SEC_I_CAS_PROCESS', 49, 5, 'comp'),
  ('GR_SEC_I_TOTAL_PROCESS', 54, 6, 'comp'),
  ('GR_SEC_I_GWG_METERS', 60, 5, 'comp'),
  ('GR_SEC_I_CAS_METERS', 65, 5, 'comp'),
  ('GR_SEC_I_TOTAL_METERS', 70, 6, 'comp'),
  ('GR_SEC_I_GWG_LOSS_GAIN', 76, 5, 'comp'),
  ('GR_SEC_I_CAS_LOSS_GAIN', 81, 5, 'comp'),
  ('GR_SEC_I_TOTAL_LOSS_GAIN', 86, 6, 'comp'),
  ('GR_SEC_I_VAPORS', 92, 5, 'comp'),
  ('GR_SEC_I_OTHER_PLANTS_VOL', 97, 5, 'comp'),
  ('GR_SEC_I_PLANT_DIST_ID_1', 102, 6, 'pic_any'), #6
  ('GR_SEC_I_PLANT_DIST_ID_2', 108, 6, 'pic_any'),
  ('GR_SEC_I_PLANT_DIST_ID_3', 114, 6, 'pic_any'),
  ('GR_SEC_I_PLANT_DIST_ID_4', 120, 6, 'pic_any'),
  ('GR_SEC_I_MAIN_LINE', 126, 5, 'comp'),
  ('GR_SEC_I_WITHDRAWN_STORAGE', 131, 5, 'comp'),
  ('GR_SEC_I_IMPORT_FROM_STATES', 136, 5, 'comp'),
  ('GR_SEC_I_IMPORT_STATE_CODE', 141, 2, 'pic_any'),
  ('GR_SEC_I_NET_GAS', 143, 6, 'comp'),
]
disposition_unprocessed_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_SEC_II_GWG_FUEL_SYS', 2, 5, 'comp'),
  ('GR_SEC_II_CAS_FUEL_SYS', 7, 5, 'comp'),
  ('GR_SEC_II_TOTAL_FUEL_SYS', 12, 6, 'comp'),
  ('GR_SEC_II_GWG_GAS_LIFT', 18, 5, 'comp'),
  ('GR_SEC_II_CAS_GAS_LIFT', 23, 5, 'comp'),
  ('GR_SEC_II_TOTAL_GAS_LIFT', 28, 6, 'comp'),
  ('GR_SEC_II_GWG_MAINT', 34, 5, 'comp'),
  ('GR_SEC_II_CAS_MAINT', 39, 5, 'comp'),
  ('GR_SEC_II_TOTAL_MAINT', 44, 6, 'comp'),
  ('GR_SEC_II_GWG_CYCLED', 50, 5, 'comp'),
  ('GR_SEC_II_CAS_CYCLED', 55, 5, 'comp'),
  ('GR_SEC_II_TOTAL_CYCLED', 60, 6, 'comp'),
  ('GR_SEC_II_GWG_UNDGRND', 66, 5, 'comp'),
  ('GR_SEC_II_CAS_UNDGRND', 71, 5, 'comp'),
  ('GR_SEC_II_TOTAL_UNDGRND', 76, 6, 'comp'),
  ('GR_SEC_II_GWG_OTHR_PLT', 82, 5, 'comp'),
  ('GR_SEC_II_CAS_OTHR_PLT', 87, 5, 'comp'),
  ('GR_SEC_II_TOTAL_OTHR_PLT', 92, 6, 'comp'),
  ('GR_SEC_II_GWG_CRBN_BLK', 98, 5, 'comp'),
  ('GR_SEC_II_CAS_CRBN_BLK', 103, 5, 'comp'),
  ('GR_SEC_II_TOTAL_CRBN_BLK', 108, 6, 'comp'),
  ('GR_SEC_II_GWG_TRANS_LN', 114, 5, 'comp'),
  ('GR_SEC_II_CAS_TRANS_LN', 119, 5, 'comp'),
  ('GR_SEC_II_TOTAL_TRANS_LN', 124, 6, 'comp'),
  ('GR_SEC_II_GWG_VENTED', 130, 5, 'comp'),
  ('GR_SEC_II_CAS_VENTED', 135, 5, 'comp'),
  ('GR_SEC_II_TOTAL_VENTED', 140, 6, 'comp'),
  ('GR_SEC_II_GWG_TOTAL', 146, 6, 'comp'),
  ('GR_SEC_II_CAS_TOTAL', 152, 6, 'comp'),
  ('GR_SEC_II_GRAND_TOTAL', 158, 6, 'comp'),
]
plant_liquid_production_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_SEC_III_EXTRACT_LOSS', 2, 5, 'comp'),
  ('GR_SEC_III_H2S_LOSS', 7, 5, 'comp'),
  ('GR_SEC_III_CO2_LOSS', 12, 5, 'comp'),
  ('GR_SEC_III_PLANT_FUEL', 17, 5, 'comp'),
  ('GR_SEC_III_LEASE_FUEL', 22, 5, 'comp'),
  ('GR_SEC_III_GAS_LIFT', 27, 5, 'comp'),
  ('GR_SEC_III_REPRESSURE', 32, 5, 'comp'),
  ('GR_SEC_III_CYCLED', 37, 5, 'comp'),
  ('GR_SEC_III_UNDERGROUND', 42, 5, 'comp'),
  ('GR_SEC_III_CARBON_BLACK', 47, 5, 'comp'),
  ('GR_SEC_III_OTHER_PLANTS', 52, 5, 'comp'),
  ('GR_SEC_III_TRANS_LINE', 57, 5, 'comp'),
  ('GR_SEC_III_VENTED', 62, 5, 'comp'),
  ('GR_SEC_III_METER_DIFF', 67, 5, 'comp'),
  ('GR_SEC_III_WATER', 72, 5, 'comp'),
  ('GR_SEC_III_NITROGEN_LOSS', 77, 5, 'comp'),
  ('GR_SEC_III_TOTAL', 97, 6, 'comp'),
  ('GR_SEC_IV_SULFUR_TONS', 103, 3, 'comp'),
  ('GR_SEC_IV_CO2_MCF', 106, 5, 'comp'),
  ('GR_SEC_V_SCRUBBER_BACK', 121, 5, 'comp'),
  ('GR_SEC_V_GWG_CONDENSATE', 126, 5, 'comp'),
  ('GR_SEC_V_CAS_SCRUBBER', 131, 5, 'comp'),
  ('GR_SEC_V_PLANT_COND', 136, 5, 'comp'),
  ('GR_SEC_V_GASOLINE', 141, 5, 'comp'),
  ('GR_SEC_V_BUTANE', 146, 5, 'comp'),
  ('GR_SEC_V_PROPANE', 151, 5, 'comp'),
  ('GR_SEC_V_MIX', 156, 5, 'comp'),
  ('GR_SEC_V_ISO_BUTANE', 161, 5, 'comp'),
  ('GR_SEC_V_ETHANE', 166, 5, 'comp'),
  ('GR_SEC_V_OTHER', 171, 5, 'comp'),
  ('GR_SEC_V_TOTAL', 186, 6, 'comp'),
]
drip_scrubber_recovery_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_SEC_VI_KEY', 2, 3, 'pic_numeric'),
  ('GR_SEC_VI_NAME', 5, 32, 'pic_any'),
  ('GR_SEC_VI_NO_GAS_WELLS', 37, 3, 'comp'),
  ('GR_SEC_VI_NO_OIL_WELLS', 40, 3, 'comp'),
  ('GR_SEC_VI_TOTAL_BARRELS', 43, 4, 'comp'),
]
plant_stock_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_VII_OPEN_COND', 2, 5, 'comp'),
  ('GR_VII_OPEN_SCRUBBER', 7, 5, 'comp'),
  ('GR_VII_OPEN_GASOLINE', 12, 5, 'comp'),
  ('GR_VII_OPEN_BUTANE', 17, 5, 'comp'),
  ('GR_VII_OPEN_PROPANE', 22, 5, 'comp'),
  ('GR_VII_OPEN_ETHANE', 27, 5, 'comp'),
  ('GR_VII_OPEN_OTHER', 32, 5, 'comp'),
  ('GR_VII_OPEN_TOTAL', 37, 6, 'comp'),
  ('GR_VII_FROZEN_COND', 43, 5, 'comp'),
  ('GR_VII_FROZEN_SCRUBBER', 48, 5, 'comp'),
  ('GR_VII_FROZEN_GASOLINE', 53, 5, 'comp'),
  ('GR_VII_FROZEN_BUTANE', 58, 5, 'comp'),
  ('GR_VII_FROZEN_PROPANE', 63, 5, 'comp'),
  ('GR_VII_FROZEN_ETHANE', 68, 5, 'comp'),
  ('GR_VII_FROZEN_OTHER', 73, 5, 'comp'),
  ('GR_VII_FROZEN_TOTAL', 78, 6, 'comp'),
  ('GR_VII_OPN_STOCK_COND', 84, 5, 'comp'),
  ('GR_VII_OPN_STOCK_SCRUBBER', 89, 5, 'comp'),
  ('GR_VII_OPN_STOCK_GASOLINE', 94, 5, 'comp'),
  ('GR_VII_OPN_STOCK_BUTANE', 99, 5, 'comp'),
  ('GR_VII_OPN_STOCK_PROPANE', 104, 5, 'comp'),
  ('GR_VII_OPN_STOCK_ETHANE', 109, 5, 'comp'),
  ('GR_VII_OPN_STOCK_OTHER', 114, 5, 'comp'),
  ('GR_VII_OPN_STOCK_TOTAL', 119, 6, 'comp'),
  ('GR_VII_RECEIVED_COND', 125, 5, 'comp'),
  ('GR_VII_RECEIVED_SCRUBBER', 130, 5, 'comp'),
  ('GR_VII_RECEIVED_GASOLINE', 135, 5, 'comp'),
  ('GR_VII_RECEIVED_BUTANE', 140, 5, 'comp'),
  ('GR_VII_RECEIVED_PROPANE', 145, 5, 'comp'),
  ('GR_VII_RECEIVED_ETHANE', 150, 5, 'comp'),
  ('GR_VII_RECEIVED_OTHER', 155, 5, 'comp'),
  ('GR_VII_RECEIVED_TOTAL', 160, 6, 'comp'),
  ('GR_VII_PRODUCED_COND', 166, 5, 'comp'),
  ('GR_VII_PRODUCED_SCRUBBER', 171, 5, 'comp'),
  ('GR_VII_PRODUCED_GASOLINE', 176, 5, 'comp'),
  ('GR_VII_PRODUCED_BUTANE', 181, 5, 'comp'),
  ('GR_VII_PRODUCED_PROPANE', 186, 5, 'comp'),
  ('GR_VII_PRODUCED_ETHANE', 191, 5, 'comp'),
  ('GR_VII_PRODUCED_OTHER', 196, 5, 'comp'),
  ('GR_VII_PRODUCED_TOTAL', 201, 6, 'comp'),
  ('GR_VII_DELIVERED_COND', 207, 5, 'comp'),
  ('GR_VII_DELIVERED_SCRUBBER', 212, 5, 'comp'),
  ('GR_VII_DELIVERED_GASOLINE', 217, 5, 'comp'),
  ('GR_VII_DELIVERED_BUTANE', 222, 5, 'comp'),
  ('GR_VII_DELIVERED_PROPANE', 227, 5, 'comp'),
  ('GR_VII_DELIVERED_ETHANE', 232, 5, 'comp'),
  ('GR_VII_DELIVERED_OTHER', 237, 5, 'comp'),
  ('GR_VII_DELIVERED_TOTAL', 242, 6, 'comp'),
  ('GR_VII_LOSS_COND', 248, 5, 'comp'),
  ('GR_VII_LOSS_SCRUBBER', 253, 5, 'comp'),
  ('GR_VII_LOSS_GASOLINE', 258, 5, 'comp'),
  ('GR_VII_LOSS_BUTANE', 263, 5, 'comp'),
  ('GR_VII_LOSS_PROPANE', 268, 5, 'comp'),
  ('GR_VII_LOSS_ETHANE', 273, 5, 'comp'),
  ('GR_VII_LOSS_OTHER', 278, 5, 'comp'),
  ('GR_VII_LOSS_TOTAL', 283, 6, 'comp'),
  ('GR_VII_CLS_STOCK_COND', 289, 5, 'comp'),
  ('GR_VII_CLS_STOCK_SCRUBBER', 294, 5, 'comp'),
  ('GR_VII_CLS_STOCK_GASOLINE', 299, 5, 'comp'),
  ('GR_VII_CLS_STOCK_BUTANE', 304, 5, 'comp'),
  ('GR_VII_CLS_STOCK_PROPANE', 309, 5, 'comp'),
  ('GR_VII_CLS_STOCK_ETHANE', 314, 5, 'comp'),
  ('GR_VII_CLS_STOCK_OTHER', 319, 5, 'comp'),
  ('GR_VII_CLS_STOCK_TOTAL', 324, 6, 'comp'),
  ('GR_VII_FRZ_STOCK_COND', 330, 5, 'comp'),
  ('GR_VII_FRZ_STOCK_SCRUBBER', 335, 5, 'comp'),
  ('GR_VII_FRZ_STOCK_GASOLINE', 340, 5, 'comp'),
  ('GR_VII_FRZ_STOCK_BUTANE', 345, 5, 'comp'),
  ('GR_VII_FRZ_STOCK_PROPANE', 350, 5, 'comp'),
  ('GR_VII_FRZ_STOCK_ETHANE', 355, 5, 'comp'),
  ('GR_VII_FRZ_STOCK_OTHER', 360, 5, 'comp'),
  ('GR_VII_FRZ_STOCK_TOTAL', 365, 6, 'comp'),
  ('GR_VII_CLOSING_COND', 371, 5, 'comp'),
  ('GR_VII_CLOSING_SCRUBBER', 376, 5, 'comp'),
  ('GR_VII_CLOSING_GASOLINE', 381, 5, 'comp'),
  ('GR_VII_CLOSING_BUTANE', 386, 5, 'comp'),
  ('GR_VII_CLOSING_PROPANE', 391, 5, 'comp'),
  ('GR_VII_CLOSING_ETHANE', 396, 5, 'comp'),
  ('GR_VII_CLOSING_OTHER', 401, 5, 'comp'),
  ('GR_VII_CLOSING_TOTAL', 406, 6, 'comp'),
]
gas_delivery_detail_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_SEC_X_KEY', 2, 3, 'pic_numeric'),
  ('GR_SEC_X_DELIVERED_TO', 5, 32, 'pic_any'),
  ('GR_SEC_X_USE_CODE', 37, 1, 'pic_any'),
  ('GR_SEC_X_UNPROCESSED_GAS', 38, 5, 'comp'),
  ('GR_SEC_X_RESIDUE_GAS', 43, 5, 'comp'),
]
remark_key_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_R3_XI_REMARK_KEY', 2, 3, 'pic_numeric'),
  ('GR_R3_XI_REMARK', 5, 70, 'pic_any'),
]
pressure_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_R7_GAS_WELL_GAS', 2, 5, 'comp'),
  ('GR_R7_CASINGHEAD_GAS', 7, 5, 'comp'),
  ('GR_R7_RESIDUE', 12, 5, 'comp'),
  ('GR_R7_PIPE_LINE', 17, 5, 'comp'),
  ('GR_R7_INTAKE_TOTAL', 22, 6, 'comp'),
  ('GR_R7_FUEL_SYSTEM', 28, 5, 'comp'),
  ('GR_R7_PLANT_FUEL', 33, 5, 'comp'),
  ('GR_R7_GAS_LIFT', 38, 5, 'comp'),
  ('GR_R7_PRESSURE_MAINT', 43, 5, 'comp'),
  ('GR_R7_REPRESSURING', 48, 5, 'comp'),
  ('GR_R7_TRANS_LINE', 53, 5, 'comp'),
  ('GR_R7_VENTED', 58, 5, 'comp'),
  ('GR_R7_TOTAL_DISP', 63, 6, 'comp'),
  ('GR_R7_NUMBER_INJ_WELLS', 69, 3, 'comp'),
]
r3_remarks_layout = [
  ('RRC_TAPE_RECORD_ID', 0, 2, 'pic_any'),
  ('GR_R3_XI_REMARK_KEY', 2, 3, 'pic_numeric'),
  ('GR_R3_XI_REMARK', 5, 70, 'pic_any'),
]


def get_layout(record_key):  
  layouts_map = {
    '01' : {'name': 'root', 'layout': root_layout},
    '02' : {'name': 'cycle', 'layout': cycle_layout},
    '03' : {'name': 'intake_volumes', 'layout': intake_volumes_layout},
    '04' : {'name': 'disposition_unprocessed', 'layout': disposition_unprocessed_layout},
    '05' : {'name': 'plant_liquid_production', 'layout': plant_liquid_production_layout},
    '06' : {'name': 'drip_scrubber_recovery', 'layout': drip_scrubber_recovery_layout},
    '07' : {'name': 'plant_stock', 'layout': plant_stock_layout},
    '10' : {'name': 'gas_delivery_detail', 'layout': gas_delivery_detail_layout},
    '11' : {'name': 'remark_key', 'layout': remark_key_layout},
    '12' : {'name': 'pressure', 'layout': pressure_layout},
    '13' : {'name': 'r3_remarks', 'layout': r3_remarks_layout},
  }
  
  return layouts_map[record_key]