-
Notifications
You must be signed in to change notification settings - Fork 2
/
aveSat.py
150 lines (121 loc) · 5.36 KB
/
aveSat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import argparse
def argument():
parser = argparse.ArgumentParser(description = '''
Generic averager for sat files.
It works with one mesh, without performing interpolations.
Files with dates used for the average provided (dirdates).
Empty dirdates is provided when the number of input daily files is lesser than 3.
''',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument( '--checkdir', '-i',
type = str,
required = True,
help = ''' CHECKED sat directory, e.g. /gss/gss_work/DRES_OGS_BiGe/Observations/TIME_RAW_DATA/ONLINE/SAT/MODIS/DAILY/CHECKED/'''
)
parser.add_argument( '--outdir', '-o',
type = str,
required = True,
help = ''' OUT average dates sat directory'''
)
parser.add_argument( '--dirdates', '-d',
type = str,
required = True,
help = ''' OUT dates sat directory'''
)
parser.add_argument( '--mesh', '-m',
type = str,
required = True,
choices = ['SatOrigMesh','V4mesh','V1mesh','KD490mesh','SAT1km_mesh', 'Mesh24', 'Mesh4'],
help = ''' Name of the mesh of sat ORIG and used to dump checked data.'''
)
parser.add_argument( '--varname', '-v',
type = str,
required = True,
choices = ['CHL','KD490','DIATO','NANO','PICO', 'DINO','RRS412','RRS443','RRS490','RRS510','RRS555','RRS670']
)
parser.add_argument( '--timeaverage', '-t',
type = str,
required = True,
choices = ['monthly','weekly_tuesday','weekly_friday','weekly_monday','weekly_thursday','tendays'],
help = ''' Name of the mesh of sat ORIG and used to dump checked data.'''
)
parser.add_argument( '--force', '-f',
action='store_true',
help = """Overwrite existing variables in files
""")
return parser.parse_args()
args = argument()
from commons.Timelist import TimeList
from commons.time_interval import TimeInterval
from postproc import masks
import numpy as np
import os
import Sat.SatManager as Sat
from commons.utils import addsep
try:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nranks =comm.size
isParallel = True
except:
rank = 0
nranks = 1
isParallel = False
CHECKDIR = addsep(args.checkdir)
OUTDIR = addsep(args.outdir)
DIRDATES = addsep(args.dirdates)
maskSat = getattr(masks,args.mesh)
Timestart="19500101"
Time__end="20500101"
TI = TimeInterval(Timestart,Time__end,"%Y%m%d")
TLCheck = TimeList.fromfilenames(TI, CHECKDIR,"*.nc",prefix='',dateformat='%Y%m%d')
suffix = os.path.basename(TLCheck.filelist[0])[8:]
if args.timeaverage == 'monthly' : TIME_reqs=TLCheck.getMonthlist()
if args.timeaverage == 'weekly_tuesday' : TIME_reqs=TLCheck.getWeeklyList(2)
if args.timeaverage == 'weekly_friday' : TIME_reqs=TLCheck.getWeeklyList(5)
if args.timeaverage == 'weekly_monday' : TIME_reqs=TLCheck.getWeeklyList(1)
if args.timeaverage == 'weekly_thursday': TIME_reqs=TLCheck.getWeeklyList(4)
if args.timeaverage == 'tendays' : TIME_reqs=TLCheck.getSpecificIntervalList(10,"19971001-12:00:00")
jpi = maskSat.jpi
jpj = maskSat.jpj
for req in TIME_reqs[rank::nranks]:
outfile = OUTDIR + req.string + suffix
writing_mode = Sat.writing_mode(outfile)
ii, w = TLCheck.select(req)
nFiles = len(ii)
if os.path.exists(outfile):
outdates = DIRDATES + req.string + 'weekdates.txt'
weekdates = np.loadtxt(outdates,dtype=str)
nDates = len(weekdates)
if nFiles>nDates:
print('Not skipping ' + req.string)
else:
condition_to_write = not Sat.exist_valid_variable(args.varname,outfile)
if args.force: condition_to_write=True
if not condition_to_write: continue
print(outfile, flush=True)
dateweek = []
if nFiles < 3 :
print(req, "less than 3 files - Skipping average generation")
filedates = DIRDATES + req.string + 'weekdates.txt'
print('See ' + filedates)
np.savetxt(filedates,dateweek,fmt='%s')
continue
M = np.zeros((nFiles,jpj,jpi),np.float32)
for iFrame, j in enumerate(ii):
inputfile = TLCheck.filelist[j]
VALUES = Sat.readfromfile(inputfile, args.varname)
M[iFrame,:,:] = VALUES
idate = TLCheck.Timelist[j]
date8 = idate.strftime('%Y%m%d')
dateweek.append(date8)
if args.varname == 'KD490':
OUT = Sat.averager(M)
else:
OUT = Sat.logAverager(M)
Sat.dumpGenericfile(outfile, OUT, args.varname, mesh=maskSat, mode=writing_mode)
filedates = DIRDATES + req.string + 'weekdates.txt'
print(filedates)
np.savetxt(filedates,dateweek,fmt='%s')