Commit 36cec6b6 authored by Qusai Al Shidi's avatar Qusai Al Shidi 💬
Browse files

Added cleaned Zhenguang code.

parent 063e33ca
......@@ -9,9 +9,7 @@ this module mostly requires an internet connection.
__author__ = 'Qusai Al Shidi'
__email__ = 'qusai@umich.edu'
import urllib.request
import datetime as dt
from dateutil import rrule
def get_omni_data(time_from, time_to, **kwargs):
......@@ -45,6 +43,9 @@ def get_omni_data(time_from, time_to, **kwargs):
# Author: Qusai Al Shidi
# Email: qusai@umich.edu
import urllib.request
from dateutil import rrule
# This is straight from the format guide on spdf
col_names = ('ID for IMF spacecraft',
'ID for SW Plasma spacecraft',
......@@ -123,3 +124,131 @@ def get_omni_data(time_from, time_to, **kwargs):
return_data[col_names[num]].append(float(value))
return return_data # dictionary with omni values where index is the row
def download_magnetogram_adapt(time, map_type='fixed', **kwargs):
'''This routine downloads GONG ADAPT magnetograms.
Downloads ADAPT magnetograms from ftp://gong2.nso.edu/adapt/maps/gong/
to a local directory.
Args:
time (datetime.datetime): Time in which you want the magnetogram.
map_type (str): (default: 'fixed')
Choose either 'fixed' or 'central' for
the map type you want.
**kwargs:
download_dir (str): (default is current dir) Absolute directory
where you want the maps to be downloaded.
Be sure to prefix './' if relative to
current directory.
Raises:
FileNotFoundError: If the map is not found on the server.
ValueError: If map_type is not recognized.
(i.e. not 'fixed' or 'central')
FileNotFoundError: If the map could not be downloaded for any
reason.
Examples:
```python
import datetime as dt
# Use datetime objects for the time
time_flare = dt.datetime(2018, 2, 12)
swmfpy.web.download_magnetogram_adapt(time=time_flare,
map_type='central',
download_dir='./mymaps/')
```
'''
# Author: Zhenguang Huang
# Email: zghuang@umich.edu
import math
import ftplib
from ftplib import FTP
import gzip
import shutil
if map_type == 'fixed':
map_id = '0'
elif map_type == 'central':
map_id = '1'
else:
print('Not recognized type of ADAPT map')
raise ValueError
# ADAPT maps only contains the hours for even numbers
hour = time.hour # To ensure even hour
if hour % 2 != 0:
hour = math.floor(hour/2)*2
print('Warning: Hour must be an even number.',
'The entered hour value is changed to', hour)
# Go to the the ADAPT ftp server
ftp = FTP('gong2.nso.edu')
ftp.login()
# Only ADAPT GONG is considered
ftp.cwd('adapt/maps/gong')
# Go to the specific year
try:
ftp.cwd(str(time.year))
except ftplib.all_errors:
print('Cannot go to the specific year directory')
raise FileNotFoundError
finally:
ftp.quit()
# Only consider the public (4) Carrington Fixed (0) GONG (3) ADAPT maps
file_pattern = 'adapt4' + map_id + '3*' \
+ str(time.year).zfill(4) \
+ str(time.month).zfill(2) \
+ str(time.day).zfill(2) \
+ str(hour).zfill(2) + '*'
# adapt4[0,1]3*yyymmddhh
# time_string = \
# str(time.year).zfill(4) + '-' \
# + str(time.month).zfill(2) + '-' \
# + str(time.day).zfill(2) + 'T' \
# + str(hour).zfill(2)
# print('Trying to download the', map_type, 'ADAPT map',
# ' for date:', time_string)
# print('The file pattern is:', file_pattern)
filenames = ftp.nlst(file_pattern)
if len(filenames) < 1:
print('Could not find a file that matches the pattern.')
raise FileNotFoundError
for filename in filenames:
# open the file locally
directory = kwargs.get('download_dir', './')
if directory[-1] != '/':
directory += '/'
with open(directory + filename, 'wb') as fhandle:
# try to download the magnetogram
try:
ftp.retrbinary('RETR ' + filename, fhandle.write)
except ftplib.all_errors:
print('Cannot download ', filename)
raise FileNotFoundError
finally:
ftp.quit()
# close the file
# print('Downloaded:',filename)
# unzip the file
if '.gz' in filename:
# print('Unzip',filename)
filename_unzip = filename.replace('.gz', '')
with gzip.open(directory + filename, 'rb') as s_file:
with open(directory + filename_unzip, 'wb') as d_file:
shutil.copyfileobj(s_file, d_file, 65536)
# close the connection
ftp.quit()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment