1

I am trying to crop a bunch of netCDF4 files and then combine them into one CSV file. I have tried using the code below but can't seem to get the for loop to work.

  import os
  import xarray as xr
  import netCDF4
  import pandas as pd
  import numpy as np
  import glob


  max_lat= -15
  min_lat= -20
  max_lon= -68
  min_lon= -71


 #for filename in glob.glob("/path/to/assignment/file*.txt"):
  for filename in glob.glob("D:\\Precip\\*.nc"):
    print(filename)
    data=xr.open_dataset(filename,decode_times=False)

 mask_lon = (data.longitude >= min_lon) & (data.longitude <= max_lon)
 mask_lat = (data.latitude >= min_lat) & (data.latitude <= max_lat)   

 cropped_data = data.where(mask_lon & mask_lat , drop=True)

 print(cropped_data['E'])

 df=data['P'].to_dataframe()
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
  • Welcome to stack overflow! When asking about errors, please always include the [full traceback](//realpython.com/python-traceback). You can [edit] your question to include it. Thanks! – Michael Delgado Feb 15 '23 at 10:02
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 15 '23 at 10:04

0 Answers0