0% found this document useful (0 votes)
90 views7 pages

Manual For: Netcdf's Program (Python) : 1. About The Data

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

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:

Name File: YEAR_MONTH_HOURLY.nc

 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:

Location of the file: C:\Users\userblahblah\Desktop

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.

The process start like this:

 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.

 Click on open terminal

 Type the following:


o VERY IMPORTANT TO INSTALL THE KERNELS:
o conda install spyder‑kernels=0.* or pip install spyder‑kernels==0.*
o pip install numpy
o pip install matplotlib
o pip install math
o pip install pandas
o pip install netCDF4

You can include all the packages needed.

Now, to start working in spyder:

Go to Python interpreter -> Use the following python interpreter:

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).

Then the code can created and run with no problem:

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

@author: user blahblah

This is a program to cut NETCDF files into smaller subsets.


For example form a global scale to just a section.

This program has to run by parts. IT CAN NOT BE RUNNED ALL AT ONCE.

Each part is determined by a header to make running easier.


"""
# -----------------------------------------------------------------------------
# FIRST PART
#
# When this part is run the output would be to see the names of the variables:
# Latitude, Longitude and environmental variables of interested.
# This is important because different data sets can call their variables in a different way.
# That's why the code can be general.
# -----------------------------------------------------------------------------

from netCDF4 import Dataset


import numpy as np

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.
# -----------------------------------------------------------------------------

You might also like