FY4B_C_SPMS/GRAPES.py

105 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
"""
Created on Thu Feb 23 17:43:31 2023
@author: 11747
"""
import os
import sys
import datetime
import time
import glob
from datetime import timedelta
import math
NWPPATH ='/NWPDATA/GRAPES/'
NWPNAME = 'gmf.gra.{:10s}{:03d}.grb2'
UPGRADEDATA = '202305230001'
class GRAPESMETA:
def __init__(self,nwppath=NWPPATH,nwpname=NWPNAME,add='%Y/%m/'):
self._nwptype = 'GRAPES'
self._nwppath = nwppath
self._nwpname = nwpname
self._add = add
self._resolution = 0.25
@property
def resolution(self):
return self._resolution
def match(self,sattime,nums=3,delttime=10800,delayed=0,fcschoice=12,fcssame = True):
sattimest=datetime.datetime.strptime(sattime,'%Y%m%d%H%M')
if sattimest>datetime.datetime.strptime(UPGRADEDATA,'%Y%m%d%H%M'):
self._resolution = 0.125
satdayc=datetime.datetime(sattimest.year,sattimest.month,sattimest.day)
# if delayed<fcschoice:
ndelayed = (delayed//fcschoice+1)*fcschoice
daychour = fcschoice*((sattimest.hour+ndelayed-delayed)//fcschoice)
nwptfcstimest = satdayc- datetime.timedelta(hours=ndelayed)+datetime.timedelta(hours= daychour)
# else:
# daychour = fcschoice*(sattimest.hour//fcschoice)
# nwptfcstimest = satdayc- datetime.timedelta(hours=delayed)+datetime.timedelta(hours= daychour)
fcsthours=int((sattimest-nwptfcstimest).total_seconds()/delttime)*int(delttime/3600)
nwptfcstime=datetime.datetime.strftime(nwptfcstimest,'%Y%m%d%H')
nwppath = self._nwppath+self._add
nwpfpath = datetime.datetime.strftime(nwptfcstimest,nwppath)
nwpfile1 = nwpfpath+ NWPNAME.format(nwptfcstime,fcsthours)
nwpt1 = nwptfcstimest+datetime.timedelta(hours=fcsthours)
nwptime1 = datetime.datetime.strftime(nwpt1,'%Y%m%d%H')+'00'
nwptstr = [nwptime1]
nwptime = [nwpt1]
nwp1file = [nwpfile1]
nwp2file = [None]
if nums > 1:
for i in range(1,nums):
fcsthours=fcsthours+int(delttime/3600)
if not fcssame:
if fcsthours > delayed +fcschoice:
nwptfcstimest+=datetime.timedelta(hours=fcschoice)
fcsthours-=fcschoice
nwptfcstime=datetime.datetime.strftime(nwptfcstimest,'%Y%m%d%H')
nwpfpath = datetime.datetime.strftime(nwptfcstimest,nwppath)
nwpfile1 = nwpfpath+ NWPNAME.format(nwptfcstime,fcsthours)
nwpt1 = nwptfcstimest+datetime.timedelta(hours=fcsthours)
nwptime1 = datetime.datetime.strftime(nwpt1, '%Y%m%d%H') + '00'
nwptstr.append(nwptime1)
nwptime.append(nwpt1)
nwp1file.append(nwpfile1)
nwp2file.append(None)
return nwp1file+nwp2file,nwptstr,nwptime
if __name__ == '__main__':
A = GRAPESMETA()
print(A.resolution)
print(A.match('202305230015',nums=3,delttime=10800,delayed=0,fcschoice=12,fcssame = True))
print(A.match('202305230015',nums=3,delttime=10800,delayed=6,fcschoice=12,fcssame = True))
print(A.match('202305230500',nums=3,delttime=10800,delayed=15,fcschoice=12,fcssame = True))
print(A.match('202305230500',nums=3,delttime=10800,delayed=6,fcschoice=12,fcssame = True))
print(A.match('202305230500',nums=3,delttime=10800,delayed=3,fcschoice=12,fcssame = True))
print(A.resolution)
# class NWP:
# def __init__(self):
# self = obj
# def match(self,obj,num1=1):
# obj.match()
# era5 = ERA5META()
# (nwppth,nwptime) = nwp.match(era5,num=3)