Join several. nc files?

Asked

Viewed 234 times

0

I’m a beginner in python and work with Data Analysis applied to Oceanography and I’m having difficulty working with reading files . nc ! Is the following:

I have 30 . nc satellite data files, each data corresponds to 1 day of data that the satellite collected. My intention is to use all this data and plot the Mid-sea Level Anomaly in a certain region in those 30 days. The code I used to plot 1 data equivalent to 1 day was this:

from netCDF4 import Dataset

data = Dataset('/home/maou/Área de Trabalho/Victor/Estágio/Dados de Satelite/01.09.2015 Antartica.nc', mode='r') **#Aqui eu gostaria de colocar outros 29 dados e não sei como.**


lons = data.variables['longitude'][:]

lats = data.variables['latitude'][:]

sla = data.variables['sla'][:,:,:]

sla = sla[0,:,:]


import numpy as np

import matplotlib.pyplot as plt

import matplotlib.cm as cm

from mpl_toolkits.basemap import Basemap



map = Basemap(projection='spaeqd',boundinglat=-55,lon_0=90,resolution='h')

lons, lats = np.meshgrid(lons, lats)

xi, yi = map(lons, lats)

cs = map.pcolor(xi,yi,np.squeeze(sla), vmin=np.min(sla), vmax=np.max(sla), cmap=cm.jet)

cs.set_edgecolor('face')



map.drawparallels(np.arange(-80.,81.,20.), labels=[1,0,0,0], fontsize=5)

map.drawmeridians(np.arange(-180.,181.,20.), labels=[0,0,0,1], fontsize=4)

map.drawcoastlines()

map.drawstates()

map.drawcountries()



cbar = map.colorbar(cs, location='right', pad="10%")

cbar.set_label('(m)')

cbar.ax.tick_params(labelsize=10)


plt.rcParams['figure.figsize'] = (11,7)

plt.title(' Sea Level Anomaly (2015/09/01-30) ')

plt.savefig('Sea Level Anomaly', format='pdf', dpi=600)

1 answer

0

You can use MFDataset in place of Dataset and include a "wildcard" (*) on the date for files to be automatically selected.

from netCDF4 import MFDataset

data = MFDataset('/home/maou/Área de Trabalho/Victor/Estágio/Dados de Satelite/*.09.2015 Antartica.nc', mode='r') **#Aqui eu gostaria de colocar outros 29 dados e não sei como.**

Source: https://unidata.github.io/netcdf4-python/netCDF4/index.html#section8

  • Thank you so much! It worked Perfectly!

  • I am happy to have helped. If the answer is correct, mark it as the answer accepts help and help other users who have the same problem ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.