nasa/EMIT-Data-Resources

Keyerror with 'features' in Visualizing Methane Time Series Tutorial

c-vollrath opened this issue · 4 comments

Thank you for these jupyter notebook tutorials! I downloaded the code and I'm working locally in VS Code.

I'm getting a Keyerror for 'features' when calling the fetch_ch4_metadata function on plm_gdf, even though it appears that 'features' is being indexed correctly from the json object:

def fetch_ch4_metadata(row):
response = requests.get(get_asset_url(row, 'CH4PLMMETA'))
return response.json()['features'][0]['properties']

fetch_ch4_metadata(plm_gdf.iloc[0])

Thanks in advance for any help you can provide.
Coleman Vollrath

It sounds like maybe you've already checked the json output, but if you run the notebook up to where you get an error and run the code below, are you getting a response back that looks something like the output I included also?

response = requests.get(get_asset_url(plm_gdf.iloc[0], 'CH4PLMMETA'))
response.json()
{'crs': {'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'},
  'type': 'name'},
 'features': [{'geometry': {'coordinates': [[[36.1725975, 31.9951906],
      [36.1709708, 31.9935639],
      [36.1709708, 31.9924794],
      [36.1688018, 31.9908527],
      [36.1682596, 31.9854304],
      [36.1682596, 31.9800081],
      [36.1704285, 31.9713323],
      [36.171513, 31.9697056],
      [36.171513, 31.9675367],
      [36.1725975, 31.96591],
      [36.1725975, 31.9637411],
      [36.1747664, 31.958861],
      [36.1747664, 31.9572343],
      [36.171513, 31.9539809],
      [36.1704285, 31.9501853],
      [36.1704285, 31.9409673],
      [36.171513, 31.9377139],
      [36.1791042, 31.9301227],
      [36.1812732, 31.9295804],
      [36.1818154, 31.928496],
      [36.1926601, 31.9279537],
      [36.1932023, 31.9290382],
      [36.1959135, 31.9301227],
      [36.2029625, 31.935545],
      [36.2067581, 31.9447629],
      [36.208927, 31.9610299],
      [36.2170605, 31.9686212],
      [36.2176028, 31.9762124],
      [36.2045892, 31.9886838],
      [36.2002513, 31.9908527],
      [36.1986246, 31.9908527],
      [36.1953712, 31.9930216],
      [36.1839843, 31.9951906],
      [36.1725975, 31.9951906]]],
    'type': 'Polygon'},
   'properties': {'Concentration Uncertainty (ppm m)': 472.0,
    'DAAC Scene Numbers': ['012'],
    'DCID': '1360369862',
    'Latitude of max concentration': 31.93635834020197,
    'Longitude of max concentration': 36.185882153048006,
    'Max Plume Concentration (ppm m)': 2989.0,
    'Orbit': '2304807',
    'Plume ID': 'CH4_PlumeComplex-653',
    'Scene FIDs': ['emit20230217t111603'],
    'UTC Time Observed': '2023-02-17T11:16:03Z',
    'map_endtime': '2023-02-17T11:16:04Z',
    'DAAC Scene Names': ['EMIT_L2B_CH4ENH_V001_20230217T111603_2304807_012']},
   'type': 'Feature'}],
 'name': 'methane_metadata',
 'type': 'FeatureCollection'}

Hi Erik, thanks for your quick response! Yeah I get the exact same output if I take the indexing off of response.json() like you have above. Since I see 'features' there, I'm not sure what's causing the keyerror with the indexing.

That's strange. I haven't run into this issue personally, but a colleague is saying they have had to store a json as a variable before indexing it. Maybe try changing the fetch_ch4_metadata function:

def fetch_ch4_metadata(row):
    response = requests.get(get_asset_url(row, 'CH4PLMMETA'))
    json = response.json()
    return json['features'][0]['properties']

That worked! Thanks very much for the help Erik! Much appreciated. Looks like I do need to store the json as a variable before indexing.

I tried to debug the function last night by doing something similar but did not have the indexing on the same line as the return statement in my function. All good now! Thanks again.