<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 1. Python basics
This chapter only gives a short introduction to Python to make the explanations in the following chapters more understandable. A detailed description would be too extensive and would go beyond the scope of this tutorial. Take a look at https
Step1:  This is the easiest way to use print. In order to produce a prettier output of the variable contents format specifications can be used. But we will come to this later.
1.2 Data types
Numeric
integer
float
complex
Boolean
True or False
Text
string
and many others
We use the built-in function <font color='blue'><b>type</b></font> to retrieve the type of a variable.
Example
Step2:  Let's see what type the variable really has. You can use the function type as argument to the function print.
Step3:  Change the value of variable x to a floating point value of 5.0.
Step4:  Get the type of the changed variable x.
Step5:  Define variables of different types.
Step6:  1.3 Lists
A list is a compound data type, used to group different values which can have different data types. Lists are written as a list of comma-separated values (items) between square brackets.
Step7:  To select single or multiple elements of a list you can use indexing. A negative value takes the element from the end of the list.
Step8:  To select a subset of a list you can use indices, slicing, [start_index<font color='red'><b>
Step9:  What will be returned when doing the following?
Step10:  The slicing with [
Step11:  Well, how do we do an insertion of an element right after the first element?
Step12:  If you want to add more than one element to a list use extend.
Step13:  If you decide to remove an element use remove.
Step14:  With pop you can remove an element, too. Remove the last element of the list.
Step15:  Remove an element by its index.
Step16:  Use reverse to - yupp - reverse the list.
Step17:  1.4 Tuples
A tuple is like a list, but it's unchangeable (it's also called immutable). Once a tuple is created, you cannot change its values. To change a tuple you have to convert it to a list, change the content, and convert it back to a tuple.
Define the variable tup as tuple.
Step18:  Sometimes it is increasingly necessary to make multiple variable assignments which is very tedious. But  it is very easy with the tuple value packaging method. Here are some examples how to use tuples.
Standard definition of variable of type integer.
Step19:  Tuple packaging
Step20:  You can use tuple packaging to assign the values to a single variable, too.
Step21:  Tuple packaging makes an exchange of the content of variables much easier.
Step22:  Ok, now we've learned a lot about tuples, but not all. There is a very helpful way to unpack a tuple.
Unpacking example with a tuple of integers.
Step23:  Unpacking example with a tuple of strings.
Step24:  1.5 Computations
To do computations you can use the algebraic opertors on numeric values and lists.
Step25:  The built-in functions max(), min(), and sum() for instance can be used to do computations for us.
Step26:  To do computations with lists is not  that simple.
Multiply the content of the list values by 10.
Step27:  Yeah, that is not what you have expected, isn't it? 
To multiply a list by a value means to repeat the list 10-times to the new list. We have to go through the list and multiply each single element by 10. There is a long and a short way to do it.
The long way
Step28:  The more efficient way is to use Python's list comprehension
Step29:  To notice would be the inplace operators += and *=.
Step30:  1.6 Statements
Like other programing languages Python uses similar flow control statements
1.6.1 if statement
The most used statement is the if statement which allows us to control if a condition is True or False. It can contain optional parts like elif and else.
Step31:  1.6.2 while statement
The lines in a while loop is executed until the condition is False.
Step32:  1.6.3 for statement
The use of the for statement differs to other programming languages because it iterates over the items of any sequence, e.g. a list or a string, in the order that they appear in the sequence.
Step33:  1.7 Import Python modules
Usually you need to load some additional Python packages, so called modules, in your program in order to use their functionality. This can be done with the command import, whose usage may look different.
python
import module_name
import module_name as short_name
from module_name import module_part
1.7.1 Module os
We start with a simple example. To get access to the operating system outside our program we have to import the module os.
Step34:  Take a look at the module.
Step35:  Ok, let's see in which directory we are.
Step36:  Go to the parent directory and let us see where we are then.
Step37:  Go back to the directory where we started (that's why we wrote the name of the directory to the variable pwd ;)).
Step38:  To retrieve the content of an environment variable the module os provides os.environment.get function.
Step39:  Concatenate path names with os.path.join.
Step40:  Now, we want to see if the directory really exist.
Step41:  Modify the datadir variable, run the cells and see what happens.
But how to proof if a file exist? Well, there is a function os.path.isfile, who would have thought!
Step42:  Add a cell and play with the os functions in your environment.
1.7.2 Module glob
In the last case we already know the name of the file we are looking for but in most cases we don't know what is in a directory.
To get the file names from a directory the glob module is very helpful.
For example, after importing the glob module the glob function of glob, weired isn't it, will return a list of all netCDF files in the subdirectory data.
Step43:  Now, we can select a file, for instance the second one, of fnames.
Step44:  But how can we get rid of the leading path? And yes, the os module can help us again with its path.basename function.
Step45:  1.7.2 Module sys
The module sys provides access to system variables and functions. The  Module includes functions to read from stdin, write to stdout and stderr, and others.
Here we will give a closer look into the part sys.path of the module, which among other things allows us to extend the search path for loaded modules.
In the subdirectory lib is a file containing user defined python functions called dkrz_utils.py. To load the file like a module we have to extend the system path before calling any function from it.
<ASSISTANT_TASK:>
Python Code:
print('Hello World')
Explanation: 1. Python basics
This chapter only gives a short introduction to Python to make the explanations in the following chapters more understandable. A detailed description would be too extensive and would go beyond the scope of this tutorial. Take a look at https://docs.python.org/tutorial/.
Now let's take our first steps into the Python world.
1.1 Functions
You can see a function as a small subprogram that does a special job. Depending on what should be done, the code is executed and/or something will be returned to the calling part of the script. There is a set of built-in functions available but you can create your own functions, too. 
Each function name in Python 3.x is followed by parentheses () and any input parameter or arguments are placed within it. Functions can perform calculations or actions and return None, a value or multiple values.
For example
function_name()
function_name(parameter1, parameter2,...)
ret = function_name(variable, axis=0)
1.1.1 Print
In our notebooks we use the function <font color='blue'><b>print</b></font> to print contents when running the cells. This gives us the possibility to export the notebooks as Python scripts, which can be run directly in a terminal.
Let's print the string <font color='red'>Hello World</font>. A string can be written in different ways, enclosed in single or double quotes.
End of explanation
x = 5
print(x)
Explanation: This is the easiest way to use print. In order to produce a prettier output of the variable contents format specifications can be used. But we will come to this later.
1.2 Data types
Numeric
integer
float
complex
Boolean
True or False
Text
string
and many others
We use the built-in function <font color='blue'><b>type</b></font> to retrieve the type of a variable.
Example: Define a variable x with value 5 and print the content of x.
End of explanation
print(type(x))
Explanation: Let's see what type the variable really has. You can use the function type as argument to the function print.
End of explanation
x = 5.0
print(x)
Explanation: Change the value of variable x to a floating point value of 5.0.
End of explanation
print(type(x))
Explanation: Get the type of the changed variable x.
End of explanation
x = 1
y = 7.3
is_red = False
title = 'Just a string'
print(type(x), type(y), type(is_red), type(title))
Explanation: Define variables of different types.
End of explanation
names = ['Hugo', 'Charles','Janine']
ages  = [72, 33, 16]
print(type(names), type(ages))
print(names)
print(ages)
Explanation: 1.3 Lists
A list is a compound data type, used to group different values which can have different data types. Lists are written as a list of comma-separated values (items) between square brackets.
End of explanation
first_name = names[0]
last_name  = names[-1]
print('First name: %-10s' % first_name)
print('Last name:  %-10s' % last_name)
print(type(names[0]))
Explanation: To select single or multiple elements of a list you can use indexing. A negative value takes the element from the end of the list.
End of explanation
print(names[0:2])
Explanation: To select a subset of a list you can use indices, slicing, [start_index<font color='red'><b>:</b></font>end_index[<font color='red'><b>:</b></font>step]], where the selected part of the list include the first element and all following elements until the element before end_index. 
The next example will return the first two elements (index 0 and 1) and not the last element.
End of explanation
print(names[0:3:2])
print(names[1:2])
print(names[1:3])
print(names[::-1])
Explanation: What will be returned when doing the following?
End of explanation
names_ln = names
names_cp = names[:]
names[0] = 'Ben'
print(names_ln)
print(names_cp)
names.append('Paul')
print(names)
names += ['Liz']
print(names)
Explanation: The slicing with [::-1] reverses the order of the list.
Using only the colon without any indices for slicing means to create a shallow copy of the list. Working with the new list will not affect the original list.
End of explanation
names.insert(1,'Merle')
print(names)
Explanation: Well, how do we do an insertion of an element right after the first element?
End of explanation
names.extend(['Sophie','Sebastian','James'])
print(names)
Explanation: If you want to add more than one element to a list use extend.
End of explanation
names.remove('Janine')
print(names)
Explanation: If you decide to remove an element use remove.
End of explanation
names.pop()
print(names)
Explanation: With pop you can remove an element, too. Remove the last element of the list.
End of explanation
names.pop(2)
print(names)
Explanation: Remove an element by its index.
End of explanation
names.reverse()
print(names)
Explanation: Use reverse to - yupp - reverse the list.
End of explanation
tup = (0, 1, 1, 5, 3, 8, 5)
print(type(tup))
Explanation: 1.4 Tuples
A tuple is like a list, but it's unchangeable (it's also called immutable). Once a tuple is created, you cannot change its values. To change a tuple you have to convert it to a list, change the content, and convert it back to a tuple.
Define the variable tup as tuple.
End of explanation
td = 15
tm = 12
ty = 2018
print(td,tm,ty)
Explanation: Sometimes it is increasingly necessary to make multiple variable assignments which is very tedious. But  it is very easy with the tuple value packaging method. Here are some examples how to use tuples.
Standard definition of variable of type integer.
End of explanation
td,tm,ty = 15,12,2018
print(td,tm,ty)
print(type(td))
Explanation: Tuple packaging
End of explanation
date = 31,12,2018
print(date)
print(type(date))
(day, month, year) = date
print(year, month, day)
Explanation: You can use tuple packaging to assign the values to a single variable, too.
End of explanation
x,y = 47,11
x,y = y,x
print(x,y)
Explanation: Tuple packaging makes an exchange of the content of variables much easier.
End of explanation
tup = (123,34,79,133)
X,*Y = tup
print(X)
print(Y)
X,*Y,Z = tup
print(X)
print(Y)
print(Z)
X,Y,*Z = tup
print(X)
print(Y)
print(Z)
Explanation: Ok, now we've learned a lot about tuples, but not all. There is a very helpful way to unpack a tuple.
Unpacking example with a tuple of integers.
End of explanation
Name = 'Elizabeth'
A,*B,C = Name
print(A)
print(B)
print(C)
A,B,*C = Name
print(A)
print(B)
print(C)
Explanation: Unpacking example with a tuple of strings.
End of explanation
m = 12
d = 8.1
s = m + d
print(s)
print(type(s))
Explanation: 1.5 Computations
To do computations you can use the algebraic opertors on numeric values and lists.
End of explanation
data = [12.2, 16.7, 22.0, 9.3, 13.1, 18.1, 15.0, 6.8]
data_min = min(data)
data_max = max(data)
data_sum = sum(data)
print('Minimum %6.1f' % data_min)
print('Maximum %6.1f' % data_max)
print('Sum     %6.1f' % data_sum)
Explanation: The built-in functions max(), min(), and sum() for instance can be used to do computations for us.
End of explanation
values = [1,2,3,4,5,6,7,8,9,10]
values10 = values*10
print(values10)
Explanation: To do computations with lists is not  that simple.
Multiply the content of the list values by 10.
End of explanation
values10 = values[:]
for i in range(0,len(values)):
    values10[i] = values[i]*10
    
print(values10)
Explanation: Yeah, that is not what you have expected, isn't it? 
To multiply a list by a value means to repeat the list 10-times to the new list. We have to go through the list and multiply each single element by 10. There is a long and a short way to do it.
The long way:
End of explanation
values10 =  [i * 10 for i in values]
print(values10)
# just to be sure that the original values list is not overwritten.
print(values)
Explanation: The more efficient way is to use Python's list comprehension:
End of explanation
ix  = 1
print(ix)
ix += 3      # same as x = x + 3
print(ix)
ix *= 2      # same as x = x * 2
print(ix)
Explanation: To notice would be the inplace operators += and *=.
End of explanation
x = 0
if(x>0):
    print('x is greater than 0')
elif(x<0):
    print('x is less than 0')
elif(x==0):
    print('x is equal 0')
user = 'George'
if(user):
    print('user is set')
    if(user=='Dennis'):
        print('--> it is Dennis')
    else:
        print('--> but it is not Dennis')
Explanation: 1.6 Statements
Like other programing languages Python uses similar flow control statements
1.6.1 if statement
The most used statement is the if statement which allows us to control if a condition is True or False. It can contain optional parts like elif and else.
End of explanation
a = 0
b = 10
while(a < b):
    print('a =',a)
    a = a + 1
Explanation: 1.6.2 while statement
The lines in a while loop is executed until the condition is False.
End of explanation
s = 0
for x in [1,2,3,4]:
    s = s + x
print('sum = ', s)
# Now, let us find the shortest name of the list names.
# Oh, by the way this is a comment line :), which will not be executed.
index  = -99
length = 50
i = 0
for name in names:
    if(len(name)<length):
        length = len(name)
        index  = i
    i+=1
print('--> shortest name in list names is', names[index])
Explanation: 1.6.3 for statement
The use of the for statement differs to other programming languages because it iterates over the items of any sequence, e.g. a list or a string, in the order that they appear in the sequence.
End of explanation
import os
Explanation: 1.7 Import Python modules
Usually you need to load some additional Python packages, so called modules, in your program in order to use their functionality. This can be done with the command import, whose usage may look different.
python
import module_name
import module_name as short_name
from module_name import module_part
1.7.1 Module os
We start with a simple example. To get access to the operating system outside our program we have to import the module os.
End of explanation
print(help(os))
Explanation: Take a look at the module.
End of explanation
pwd = os.getcwd()
print(pwd)
Explanation: Ok, let's see in which directory we are.
End of explanation
os.chdir('..')
print("Directory changed: ", os.getcwd())
Explanation: Go to the parent directory and let us see where we are then.
End of explanation
os.chdir(pwd)
print("Directory changed: ", os.getcwd())
Explanation: Go back to the directory where we started (that's why we wrote the name of the directory to the variable pwd ;)).
End of explanation
HOME = os.environ.get('HOME')
print('My HOME environment variable is set to ', HOME)
Explanation: To retrieve the content of an environment variable the module os provides os.environment.get function.
End of explanation
datadir = 'data'
newpath = os.path.join(HOME,datadir)
print(newpath)
Explanation: Concatenate path names with os.path.join.
End of explanation
if os.path.isdir(newpath):
    print('--> directory %s exists' % newpath)
else:
    print('--> directory %s does not exist' % newpath)
Explanation: Now, we want to see if the directory really exist.
End of explanation
input_file = os.path.join('data','precip.nc')
if os.path.isfile(input_file):
    print('--> file %s exists' % input_file)
else:
    print('--> file %s does not exist' % input_file)
Explanation: Modify the datadir variable, run the cells and see what happens.
But how to proof if a file exist? Well, there is a function os.path.isfile, who would have thought!
End of explanation
import glob
fnames = sorted(glob.glob('./data/*.nc'))
print(fnames)
Explanation: Add a cell and play with the os functions in your environment.
1.7.2 Module glob
In the last case we already know the name of the file we are looking for but in most cases we don't know what is in a directory.
To get the file names from a directory the glob module is very helpful.
For example, after importing the glob module the glob function of glob, weired isn't it, will return a list of all netCDF files in the subdirectory data.
End of explanation
print(fnames[1])
Explanation: Now, we can select a file, for instance the second one, of fnames.
End of explanation
print(os.path.basename(fnames[1]))
Explanation: But how can we get rid of the leading path? And yes, the os module can help us again with its path.basename function.
End of explanation
import sys
sys.path.append('./lib/')
import dkrz_utils
tempK = 286.5   #-- units Kelvin
print('Convert:  %6.2f degK  == %6.2f degC' % (tempK, (dkrz_utils.conv_K2C(tempK))))
Explanation: 1.7.2 Module sys
The module sys provides access to system variables and functions. The  Module includes functions to read from stdin, write to stdout and stderr, and others.
Here we will give a closer look into the part sys.path of the module, which among other things allows us to extend the search path for loaded modules.
In the subdirectory lib is a file containing user defined python functions called dkrz_utils.py. To load the file like a module we have to extend the system path before calling any function from it.
End of explanation
<END_TASK>