Skip to content
Snippets Groups Projects
sort_inputs.py 4.1 KiB
Newer Older
Paolo Veglio's avatar
Paolo Veglio committed
import os.path

import numpy as np

from datetime import datetime
from glob import glob

import hires_module as hm


def create_filenames(vnp02mod_fname):

    datapath = os.path.dirname(vnp02mod_fname)
    filename = os.path.basename(vnp02mod_fname)

    vnpdate = filename.split('.')[1]
    vnptime = filename.split('.')[2]

    geos_times = ['0000', '0300', '0600', '0900', '1200', '1500', '1800', '2100']
    geos_date_from_viirs = datetime.strftime(datetime.strptime(vnpdate, 'A%Y%j'), '%Y%m%d')
    geos_flist = glob(f'{datapath}/ancillary/GEOS.fpit.asm.inst3_2d_asm_Nx.GEOS5124.{geos_date_from_viirs}*.nc4')
    geos_flist = np.sort(geos_flist)
    fmt = '%H%M'
    diff_times = [(datetime.strptime(gt, fmt) - datetime.strptime(vnptime, fmt)).total_seconds()
                  for gt in geos_times]

    file_index = np.argmin(np.abs(diff_times))
    if diff_times[file_index] <= 0:
        geos_file1 = geos_flist[file_index]
        geos_file2 = geos_flist[file_index + 1]
    else:
        geos_file1 = geos_flist[file_index - 1]
        geos_file2 = geos_flist[file_index]

    land_ocean_fnames = glob(f'{datapath}/ancillary/GEOS.fpit.asm.tavg1_2d_lnd_Nx.GEOS5124.{geos_date_from_viirs}*.nc4')
    land_ocean_timelist = [t.split('.')[5].split('_')[1] for t in land_ocean_fnames]

    diff_times = [(datetime.strptime(gt, fmt) - datetime.strptime(vnptime, fmt)).total_seconds()
                  for gt in land_ocean_timelist]

    land_ocean_index = np.argmin(np.abs(diff_times))
    geos_land_flist = glob(f'{datapath}/ancillary/GEOS.fpit.asm.tavg1_2d_lnd_Nx.GEOS5124.{geos_date_from_viirs}*.nc4')
    geos_land_file = np.sort(geos_land_flist)[land_ocean_index]
    geos_ocean_flist = glob(f'{datapath}/ancillary/GEOS.fpit.asm.tavg1_2d_ocn_Nx.GEOS5124.{geos_date_from_viirs}*.nc4')
    geos_ocean_file = np.sort(geos_ocean_flist)[land_ocean_index]

    vnp03mod = glob(f'{datapath}/VNP03MOD.{vnpdate}.{vnptime}*.nc')[0]
    vnp02img = glob(f'{datapath}/VNP02IMG.{vnpdate}.{vnptime}*_bowtie_corrected.nc')[0]
    vnp03img = glob(f'{datapath}/VNP03IMG.{vnpdate}.{vnptime}*.nc')[0]
    cld_msk = glob(f'{datapath}/CLDMSK_L2_VIIRS_SNPP.{vnpdate}.{vnptime}*.nc')[0]

    out_fnames = {'VNP03MOD': os.path.basename(vnp03mod),
                  'VNP02IMG': os.path.basename(vnp02img),
                  'VNP03IMG': os.path.basename(vnp03img),
                  'GEOS_atm_1': os.path.basename(geos_file1),
                  'GEOS_atm_2': os.path.basename(geos_file2),
                  'GEOS_land': os.path.basename(geos_land_file),
                  'GEOS_ocean': os.path.basename(geos_ocean_file),
                  'GEOS_constants': 'GEOS.fp.asm.const_2d_asm_Nx.00000000_0000.V01.nc4',
                  'SST_file': 'oisst.20220622',
                  'ECO_file': 'goge1_2_img.v1',
                  'NDVI_file': 'NDVI.FM.c004.v2.0.WS.00-04.177.hdf',
                  'data_path': datapath,
                  'cloud_mask_file': cld_msk
                  }
    for k in list(out_fnames):
        print(f'{k}: {out_fnames[k]}')
    return out_fnames


def call_mvcm():

    flist = np.sort(glob('/ships19/hercules/pveglio/mvcm_viirs_hires/VNP02MOD*.nc'))
    # flist = np.sort(glob('/ships19/hercules/pveglio/mvcm_viirs_hires/VNP02MOD.A2022173.1454*.nc'))

    for fname in flist[:210]:
        print(f'Processing {fname}...')
        fnames_for_mvcm = create_filenames(fname)

        hm.main(data_path=fnames_for_mvcm['data_path'],
                mod02=fname,
                mod03=fnames_for_mvcm['VNP03MOD'],
                img02=fnames_for_mvcm['VNP02IMG'],
                img03=fnames_for_mvcm['VNP03IMG'],
                threshold_file=hm._threshold_file,
                geos_atm_1=fnames_for_mvcm['GEOS_atm_1'],
                geos_atm_2=fnames_for_mvcm['GEOS_atm_2'],
                geos_land=fnames_for_mvcm['GEOS_land'],
                geos_ocean=fnames_for_mvcm['GEOS_ocean'],
                geos_constants=fnames_for_mvcm['GEOS_constants'],
                ndvi_file=fnames_for_mvcm['NDVI_file'],
                sst_file=fnames_for_mvcm['SST_file'],
                cloud_mask_file=fnames_for_mvcm['cloud_mask_file'])


if __name__ == "__main__":
    create_filenames