Manual For: Netcdf's Program (Python) : 1. About The Data
Manual For: Netcdf's Program (Python) : 1. About The Data
Manual For: Netcdf's Program (Python) : 1. About The Data
Website: https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-single-levels?tab=form
This website has information since 1979 for different environmental variables. It can be downloaded
in different dates and formats. For this case I chose NetCDF since I’m already familiar to this type of
data.
Once all the required fields are selected. The website send you to a “download” part where the data
you solicited is being transformed to download. This process can take a while but eventually the data
can be downloaded with no problem.
The downloaded data will have different names (and very long). Additionally, the amount of data can
be so big that not everything can be downloaded at once. Therefore, the data has to be cut into
smaller time frames, and a system to give them a proper name to avoid confusions is the following:
Year: year of the data. Ranging from 1979 until present day.
Month: Corresponding month.
Hourly: Since we selected all the hours within the day the measurements are hourly.
.nc: extension of the netcdf file.
2. Coding in Python:
The script was written in Spyder/Anaconda, because I want to be able to see the variables.
For a script that will use different packages is always better to create “environments”. This means that
the packages that I will install will only be available for that script instead that for all.
Go to Anaconda
Go to environment->Create
Give the name of the environment -> pick type of python (better to choose the 3.6 or 3.7) but
depends on the packages that are going to be installed. Pick R if is also needed.
Once the environment is created the packages can be installed. Click the “play” button next to
its name.
And within the computer have to see where the environments are saved. In this case:
C:\Users\userblahblah\AppData\Local\Continuum\anaconda3\envs\netcdf\python.exe
Oher environments could be found in a similar location (or maybe the same).
A copy of the code is shown next (this version might not be the last one):
"""
Created on Wed Apr 17 16:06:33 2019
This program has to run by parts. IT CAN NOT BE RUNNED ALL AT ONCE.
path="C:/Users/userblahblah/Desktop /"
ncfile='swh-01-1979.nc'
file=Dataset(path+ncfile)
print(file.variables)
# -----------------------------------------------------------------------------
# SECOND PART
#
# This part will show the dimensions of this file and will also show the variables
# corresponding into each dimension.
# -----------------------------------------------------------------------------
lon=file.variables["longitude"][:]
lat=file.variables["latitude"][:]
nc_time=file.variables["time"][:]
t_unit=file.variables["time"].units
swh=file.variables["shww"][:]
try:
t_cal=file.variables['time'].calendar
except AttributeError:
t_cal=u"gregorian"
#print(lat)
#print(lon)
print(file.variables['shww'].dimensions)
# -----------------------------------------------------------------------------
# THIRD PART
#
# In this part there are four inputs: The i and f latitude and longitude at
# which we want to reduce our netcdf file.
# The result will show the cut vectors
# -----------------------------------------------------------------------------
lat_i=19.
lat_f=20.
lon_i=109.0
lon_f=112.
ind_lat=(lat>=lat_i)*(lat<=lat_f)
ind_long=(lon>=lon_i)*(lon<=lon_f)
indlat=np.nonzero(ind_lat)[0]
indlon=np.nonzero(ind_long)[0]
print(indlat)
print(indlon)
# -----------------------------------------------------------------------------
# FOURTH PART
#
# In this part the line: subset_swh has to be edited. These numbers are based
# on the printed results of lat and lon. I still dont know how to automatize
# this process, so the numbers have to be put manually.
# -----------------------------------------------------------------------------
lat_subset=lat[indlat]
lon_subset=lon[indlon]
subset_swh=swh[:,140:142,218:224]
print(subset_swh)
np.savez('shww.npz',shww=subset_swh,lat=lat_subset,lon=lon_subset)
# -----------------------------------------------------------------------------
# 5TH PART
#
# The result from the previous section is a masked array but if we want to make
# into a NetCDF file to visualize it into ArcGIS we need to create a NETCDF file
# all over again.
# -----------------------------------------------------------------------------