1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | # import |
---|
5 | from pylab import * |
---|
6 | import glob |
---|
7 | from datetime import date |
---|
8 | from useCorrTb_daily import * |
---|
9 | import itertools as it |
---|
10 | from multiprocessing import Pool |
---|
11 | from functools import partial |
---|
12 | import matplotlib.gridspec as gridspec |
---|
13 | import time |
---|
14 | |
---|
15 | rep_fic='/homedata/mleduc/CorrTb/' |
---|
16 | ### Satellite, Instrument, Produit Level, Channel |
---|
17 | sat = ['M02','M01','N19','N18','N16','N15'] |
---|
18 | inst = ['MHS','MHS','MHS','MHS','AMSUB','AMSUB'] |
---|
19 | lvl = 'L1C' |
---|
20 | ch = ['B1','B2','B3','B4','B5'] |
---|
21 | ### Period |
---|
22 | dmin = date(2008,5,1) |
---|
23 | dmax = date(2008,5,3) |
---|
24 | # Zone |
---|
25 | #LS = -1. |
---|
26 | #LN = 4. |
---|
27 | #LS = 7. |
---|
28 | #LN = 12. |
---|
29 | #LW = -10. |
---|
30 | #LE = 5. |
---|
31 | |
---|
32 | # New Grid |
---|
33 | resol = .5 |
---|
34 | latvec = arange(-34.5,34.6,resol) |
---|
35 | lonvec = arange(-180.,179.76,resol) |
---|
36 | |
---|
37 | # number of processes |
---|
38 | npr = 10 |
---|
39 | |
---|
40 | for day in rrule(DAILY,dtstart=dmin,until=dmax): |
---|
41 | print day |
---|
42 | ### File list |
---|
43 | repday = [day.strftime('%Y_%m_%d')]#[ff.strftime('%Y_%m_%d') for ff in rrule(DAILY,dtstart=day+relativedelta(days=-1),until=day+relativedelta(days=+1))] |
---|
44 | repsat = [ii+ss for ii,ss in zip(inst,sat)] |
---|
45 | fname = hstack([glob.glob('/bdd/AMSU-1C/%s/L1C/*/%s/*.L1C'%(reps,repd)) for reps,repd in it.product(repsat,repday)]) |
---|
46 | |
---|
47 | ### Read files (in parallel with Pool) |
---|
48 | pool = Pool(processes=npr) |
---|
49 | res = pool.map(partial(applycorr2orb,lonvec=lonvec,latvec=latvec,ch=ch),fname) |
---|
50 | pool.close() |
---|
51 | pool.join() |
---|
52 | |
---|
53 | print 'Join' |
---|
54 | tbcorr, tobs, satvec = [dstack([proc[var] for proc in res if size(proc[var])]) for var in xrange(3)] |
---|
55 | del res |
---|
56 | |
---|
57 | # Sort ??? sur le temps ??? |
---|
58 | isort = argsort(nanmean(tobs.squeeze(),axis=0)) |
---|
59 | tbcorr = tbcorr[:,:,isort] |
---|
60 | tobs = tobs.squeeze()[:,isort] |
---|
61 | satvec = satvec.squeeze()[isort] |
---|
62 | |
---|
63 | # Satellite/Instrument identification |
---|
64 | satdef = SatName() |
---|
65 | satnum = satdef.get_satnum(satvec.squeeze()) |
---|
66 | |
---|
67 | ### Fillvalue |
---|
68 | tbcorr[isnan(tbcorr)] = -99.99 |
---|
69 | |
---|
70 | ### Save in netcdf file |
---|
71 | print 'Save in nc' |
---|
72 | nsat = satvec.size |
---|
73 | nlon = lonvec.size |
---|
74 | nlat = latvec.size |
---|
75 | torig = date(day.year,1,1) |
---|
76 | for cc, ich in enumerate(ch): |
---|
77 | # Output file name |
---|
78 | output = '%sTbCorr_AMSUMHS_%s_%3.2fdeg_%s.nc'%(rep_fic,ich,resol,day.strftime('%Y%m%d')) |
---|
79 | # Creation du fichier |
---|
80 | ncfile = netCDF4.Dataset(output,'w',format='NETCDF4_CLASSIC') |
---|
81 | # Definition des dimensions |
---|
82 | ncfile.createDimension('lon',nlon) |
---|
83 | ncfile.createDimension('lat',nlat) |
---|
84 | ncfile.createDimension('satellite',nsat) |
---|
85 | |
---|
86 | # Creation des variables dimensions |
---|
87 | lons = ncfile.createVariable('lon',dtype('float32').char,('lon',)) |
---|
88 | lons.long_name = 'Longitude' |
---|
89 | lons.units = 'degrees_east' |
---|
90 | lons.actual_range = '%4.2f,%4.2f'%(lonvec.min(),lonvec.max()) |
---|
91 | lons[:] = lonvec |
---|
92 | |
---|
93 | lats = ncfile.createVariable('lat',dtype('float32').char,('lat',)) |
---|
94 | lats.long_name = 'Latitude' |
---|
95 | lats.units = 'degrees_north' |
---|
96 | lats.actual_range = '%4.2f,%4.2f'%(latvec.min(),latvec.max()) |
---|
97 | lats[:] = latvec |
---|
98 | |
---|
99 | sats = ncfile.createVariable('satellite',dtype('int16').char,('satellite',)) |
---|
100 | sats.long_name = 'Satellite' |
---|
101 | sats.description = [str(num)+' = '+ns +', ' for num,ns in zip(satdef.satnum,satdef.satname)] |
---|
102 | sats[:] = satnum |
---|
103 | |
---|
104 | # Creation variable |
---|
105 | dates = ncfile.createVariable('time',dtype('float').char,('lat','lon','satellite',)) |
---|
106 | dates.long_name = 'Time' |
---|
107 | dates.units = 'hours since %s'%torig.strftime('%Y-%m-%d %H:%M:%S') |
---|
108 | #dates[:] = (fix((tobs-date2num(torig))*24.*100)/100.).reshape(nlat,nlon,nsat) |
---|
109 | dates[:] = ((tobs-date2num(torig))*24.).reshape(nlat,nlon,nsat) |
---|
110 | |
---|
111 | sclfactor = .01 |
---|
112 | tbfinal = ncfile.createVariable('tbcorr',dtype('int16').char,('lat','lon','satellite',)) |
---|
113 | tbfinal.description = 'Corrected Tb of channel %s'%ich |
---|
114 | tbfinal.units = 'Kelvin' |
---|
115 | tbfinal.scale_factor = sclfactor |
---|
116 | tbfinal._Fillvalue = -9999. |
---|
117 | tbfinal[:] = tbcorr[cc,:,:].reshape(nlat,nlon,nsat) |
---|
118 | |
---|
119 | # Fermeture du fichier |
---|
120 | ncfile.close() |
---|
121 | print output+' created' |
---|
122 | |
---|