NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/qscatv4/trunk/qscatv4.py

Revision 461 (checked in by cbc, 13 years ago)

Remove debug statement.

  • Property svn:executable set to
Line 
1 #!/usr/bin/python2
2
3 """
4 Fetch and extract SSMI Version-04 QuikSCAT Ocean Surface Wind Vectors
5
6 Usage:
7     python2 qscatv4.py start_year start_month end_year end_month
8
9 Where:
10     start_year  is YYYY format.
11     start_month is 1-12 for Jan-Dec.                         
12     end_year    is YYYY format.             
13     end_month   is 1-12 for Jan-Dec.
14 """
15
16 __author__    = 'Chris Calloway'
17 __email__     = 'cbc@unc.edu'
18 __copyright__ = 'Copyright 2011 UNC-CH Dept. of Marine Sciences'
19 __license__   = 'GPL2'
20
21 import os
22 import sys
23 import traceback
24 import calendar
25 import ftplib
26 import gzip
27
28 ftp_server = 'ftp.ssmi.com'
29 ftp_user   = 'anonymous'
30 ftp_pass   = 'cbc@unc.edu'
31
32 work_dir           = os.path.join(os.path.sep,
33                                   'afs',
34                                   'isis.unc.edu',
35                                   'depts',
36                                   'marine',
37                                   'workspace',
38                                   'hseim',
39                                   'volumes',
40                                   'vol1',
41                                   'nc_wind_2011',
42                                   'satellite')
43 base_subdir        = os.path.join('qscat', 'bmaps_v04')
44 year_subdir_spec   = 'y%(year)4u'
45 month_subdir_spec  = 'm%(month)02u'
46
47 gz_spec    = 'qscat_%(year)4u%(month)02u%(day)02uv4'
48
49 def _fetch(f, gz_path):
50     """
51     Fetch a single gzipped file.
52     """
53
54     try:
55         g = open(gz_path, 'wb')
56     except KeyboardInterrupt:
57         raise
58     except:
59         traceback.print_exc()
60         return False
61     try:
62         gz = os.path.basename(gz_path)
63         print 'Fetching:   %s.' % gz_path
64         print f.retrbinary('RETR ' + gz, g.write)
65     except KeyboardInterrupt:
66         raise
67     except:
68         traceback.print_exc()
69         g.close()
70         return False
71     else:
72         g.close()
73     return True
74
75 def _extract(gz_path):
76     """
77     Extract a single gzipped file.
78     """
79
80     gz_name = os.path.splitext(gz_path)[0]
81     print 'Extracting: %s.' % gz_name
82     try:
83         dest = open(gz_name, 'wb')
84     except KeyboardInterrupt:
85         raise
86     except:
87         traceback.print_exc()
88         return False
89     try:
90         src  = gzip.GzipFile(gz_path)
91     except KeyboardInterrupt:
92         raise
93     except:
94         dest.close()
95         traceback.print_exc()
96         return False
97     try:
98         content = src.read()
99     except KeyboardInterrupt:
100         raise
101     except:
102         dest.close()
103         src.close()
104         traceback.print_exc()
105         return False
106     else:
107         src.close()
108     try:
109         dest.write(content)
110     except KeyboardInterrupt:
111         raise
112     except:
113         dest.close()
114         traceback.print_exc()
115         return False
116     else:
117         dest.close()
118     try:
119         os.remove(gz_path)
120     except KeyboardInterrupt:
121         raise
122     except:
123         traceback.print_exc()
124         return False
125     return True
126                
127
128 def _process(f, year, month):
129     """
130     Fetch and extract all the files for a month.
131     """
132
133     format = {'year': year, 'month': month,}
134     try:
135         subdir = os.path.join(base_subdir,
136                               year_subdir_spec % format,
137                               month_subdir_spec % format)
138         os.makedirs(subdir)
139     except OSError, e:
140         if not str(e).startswith("[Errno 17] File exists"):
141             traceback.print_exc()
142             return False
143     try:
144         print f.cwd('/' + subdir.replace(os.path.sep, '/'))
145     except ftplib.error_perm, e:
146         traceback.print_exc()
147         return False
148     day_range = range(1, calendar.monthrange(year, month)[1] + 1)
149     for day in day_range:
150         format.update({'day': day,})
151         gz      = (gz_spec % format) + os.path.extsep + 'gz'
152         gz_path = os.path.join(subdir, gz)
153         if not _fetch(f, gz_path): continue
154         if not _extract(gz_path): continue
155     return True
156
157 def qscatv4(start_year, start_month, end_year, end_month):
158     """
159     Fetch and extract a range of files.
160
161     qscatv4(start_year, start_month, end_year, end_month) -> None
162
163     start_year  is inclusive.
164     start_month is 1-12 inclusive.
165     end_year    is exclusive.
166     end_month   is 2-13 exclusive.
167     """
168
169     f = ftplib.FTP(ftp_server)
170     print f.login(ftp_user, ftp_pass)
171     year_range = range(start_year, end_year)
172     for year in year_range:
173         if year == year_range[0]:
174             _start_month = start_month
175         else:
176             _start_month = 1
177         if year == year_range[-1]:
178             _end_month = end_month
179         else:
180             _end_month = 13
181         month_range = range(_start_month, _end_month)
182         for month in month_range:
183             if not _process(f, year, month): continue
184     print f.quit()
185
186 if __name__ == '__main__':
187     try:
188         if len(sys.argv) != 5: raise
189         start_year  = int(sys.argv[1])
190         start_month = int(sys.argv[2])
191         end_year    = int(sys.argv[3]) + 1
192         end_month   = int(sys.argv[4]) + 1
193     except KeyboardInterrupt:
194         raise
195     except:
196         print __doc__
197         sys.exit()
198     os.chdir(work_dir)
199     qscatv4(start_year, start_month, end_year, end_month)
Note: See TracBrowser for help on using the browser.