You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network.
141
+
This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network.
143
142
144
-
7. Go back to the top-level directory:
143
+
8. Go back to the top-level directory:
145
144
146
-
```
147
-
$ cd ..
148
-
```
145
+
```
146
+
$ cd ..
147
+
```
149
148
150
-
8. And now that you've set up our workspace, create a blank Python script, called `mysci.py`:
149
+
9. And now that you've set up our workspace, create a blank Python script, called `mysci.py`:
151
150
152
-
```
153
-
$ touch mysci.py
154
-
```
151
+
```
152
+
$ touch mysci.py
153
+
```
155
154
156
-
If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`.
155
+
If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`.
157
156
158
-
9. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!".
157
+
10. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!".
159
158
160
-
```python
161
-
print("Hello, world!")
162
-
```
159
+
```python
160
+
print("Hello, world!")
161
+
```
163
162
164
-
On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor.
163
+
On a Windows machine, it is possible `nano`or`vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor.
165
164
166
-
10. To run a Python script from the command line type, "python" and then the name of your script:
165
+
11. To run a Python script from the command line type, "python"and then the name of your script:
167
166
168
-
```
169
-
$ python mysci.py
170
-
```
167
+
```
168
+
$ python mysci.py
169
+
```
171
170
172
-
11. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile.
171
+
12. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile.
173
172
174
-
Change the `mysci.py` script to read:
173
+
Change the `mysci.py` script to read:
175
174
176
-
```python
177
-
# Read the data file
178
-
filename ="data/wxobs20170821.txt"
179
-
datafile =open(filename, 'r')
175
+
```python
176
+
# Read the data file
177
+
filename ="data/wxobs20170821.txt"
178
+
datafile =open(filename, 'r')
180
179
181
-
print(datafile.readline())
182
-
print(datafile.readline())
183
-
print(datafile.readline())
184
-
print(datafile.readline())
180
+
print(datafile.readline())
181
+
print(datafile.readline())
182
+
print(datafile.readline())
183
+
print(datafile.readline())
185
184
186
-
datafile.close()
185
+
datafile.close()
187
186
188
-
```
187
+
```
189
188
190
-
First create a variable for your datafile name, which is a string - this can be in single or double quotes.
189
+
First create a variable for your datafile name, which is a string - this can be in single or double quotes.
191
190
192
-
Then create a variable associated with the opened file, here it is called `datafile`.
191
+
Then create a variable associated with the opened file, here it is called `datafile`.
193
192
194
-
The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file.
193
+
The `'r'` argument in the open command indicates that we are opening the filefor reading capabilities. Other input arguments foropen include `'w'`, for example, if you wanted to write to the file.
195
194
196
-
The readline command moves through the open file, always reading the next line.
195
+
The readline command moves through the openfile, always reading the next line.
197
196
198
-
And remember to close your datafile.
197
+
And remember to close your datafile.
199
198
200
-
Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter.
199
+
Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter.
201
200
202
-
12. Run your script by typing:
201
+
13. Run your script by typing:
203
202
204
-
```
205
-
$ python mysci.py
206
-
```
203
+
```
204
+
$ python mysci.py
205
+
```
207
206
208
-
Testing of your script with `python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script.
207
+
Testing of your script with`python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script.
209
208
210
-
13. Change the `mysci.py` script to read your whole data file:
209
+
14. Change the `mysci.py` script to read your whole data file:
211
210
212
-
```python
213
-
# Read the data file
214
-
filename ="data/wxobs20170821.txt"
215
-
datafile =open(filename, 'r')
211
+
```python
212
+
# Read the data file
213
+
filename ="data/wxobs20170821.txt"
214
+
datafile =open(filename, 'r')
216
215
217
-
data = datafile.read()
216
+
data = datafile.read()
218
217
219
-
datafile.close()
218
+
datafile.close()
220
219
221
-
# DEBUG
222
-
print(data)
223
-
print('data')
224
-
```
220
+
# DEBUG
221
+
print(data)
222
+
print('data')
223
+
```
225
224
226
-
Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object.
225
+
Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object.
227
226
228
-
Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data.
227
+
Try `print('data')`as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data.
229
228
230
-
Don't forget to execute with :bash:`python mysci.py`.
229
+
Don't forget to execute with :bash:`python mysci.py`.
231
230
232
-
14. Change the `mysci.py` script to read your whole data file using a context
231
+
15. Change the `mysci.py` script to read your whole data file using a context
233
232
manager with:
234
233
235
-
```python
236
-
# Read the data file
237
-
filename ="data/wxobs20170821.txt"
238
-
withopen(filename, 'r') as datafile:
239
-
data = datafile.read()
234
+
```python
235
+
# Read the data file
236
+
filename ="data/wxobs20170821.txt"
237
+
withopen(filename, 'r') as datafile:
238
+
data = datafile.read()
240
239
241
-
# DEBUG
242
-
print(data)
243
-
```
240
+
# DEBUG
241
+
print(data)
242
+
```
244
243
245
-
Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it.
244
+
Again this is a similar method of opening the datafile, but we now use `withopen`. The `with` statement is a context manager that provides clean-up and assures that the fileis automatically closed after you've read it.
246
245
247
-
The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors).
246
+
The indendation of the line `data = datafile.read()`is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width isnot consistent between editors).
248
247
249
-
Combined these two lines mean: with the datafile opened, I'd like to read it.
248
+
Combined these two lines mean: with the datafile opened, I'd like to read it.
250
249
251
-
And execute with `python mysci.py`.
250
+
And execute with`python mysci.py`.
252
251
253
-
15. What did we just see? What is the data object? What type is data? How do we find out?
252
+
16. What did we just see? What is the data object? What typeis data? How do we find out?
254
253
255
-
Change the DEBUG section of our script to:
254
+
Change the DEBUG section of our script to:
256
255
257
-
```python
258
-
# DEBUG
259
-
print(type(data))
260
-
```
256
+
```python
257
+
# DEBUG
258
+
print(type(data))
259
+
```
261
260
262
-
And execute with `python mysci.py`.
261
+
And execute with`python mysci.py`.
263
262
264
-
Object types refer to `float`, `integer`, `string` or other types that you can create.
263
+
Object types refer to `float`, `integer`, `string`or other types that you can create.
265
264
266
-
Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data.
265
+
Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data.
267
266
268
267
In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt fileand read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and`print()`, as well as the context manager `with`. You should be familiar with the `str` datatype.
0 commit comments