Search code examples
pythonrequestgoogle-colaboratory

Use Request with NASA data on Google Colaboratory


i am trying to use Request to download lot of liks from NASA Data Access, but i am getting error code 401, probably because i could not made the authentication. Actually, i have a .netrc file that contains the code machine urs.earthdata.nasa.gov login <uid> password <password> but i don´t know how Google Colaboratory recognize and run the .netrc file that allow to download the files.

the code that i am trying to use is:

  URL = rutas_GPM.iloc[i,0]
   
  # Set the FILENAME string to the data file name, the LABEL keyword value, or any customized name. 
  FILENAME = 'GPM{0}.nc'.format(i)
  result = requests.get(URL)
  try:
    result.raise_for_status()
    f = open("/content/drive/MyDrive/Colab Notebooks/Prueba/"+FILENAME,'wb')
    f.write(result.content)
    f.close()
    print('contents of URL written to '+FILENAME)
  except:
    print('requests.get() returned an error code '+str(result.status_code))

can you help me to solve this problem?

Thanks


Solution

  • Google Colaboratory use a virtual enviroment based on Linux, in this order, you have to follow the instructions sugested by NASA for MAC/Linux:

    cd $HOME
    touch .netrc
    echo "machine urs.earthdata.nasa.gov login <uid> password <password>" >> .netrc
    chmod 0600 .netrc
    

    <uid> is your NASA ID and <password> is your password account.

    further, you have to upload in the "home" and "root" folder (on the colab enviroment), the .netrc file that you created previously in Windows OS, on your PC. then, you run the code and start to download the files.