|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +DESCRIPTION |
| 5 | +
|
| 6 | +Aquire MODIS cloud properties. |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +# Import modules |
| 11 | +import numpy as np |
| 12 | +import pandas as pd |
| 13 | +import glob |
| 14 | +import os |
| 15 | +import netCDF4 |
| 16 | +import pyresample |
| 17 | +from functions import hdf_read |
| 18 | + |
| 19 | +# Define files |
| 20 | +modis_list = sorted(glob.glob('/media/johnny/Cooley_Data/Johnny/Clouds_Data/MYD06_L2/*.hdf')) |
| 21 | + |
| 22 | +# Define destination for predicted data |
| 23 | +dest = '/media/johnny/Cooley_Data/Johnny/Clouds_Data/3_MYD06_Cloud_Props_NC/' |
| 24 | + |
| 25 | +# Define ice sheet grid |
| 26 | +ismip = netCDF4.Dataset('/home/johnny/Documents/Clouds/Data/Masks/1km-ISMIP6.nc') |
| 27 | +ismip_lon = ismip.variables['lon'][:] |
| 28 | +ismip_lat = ismip.variables['lat'][:] |
| 29 | + |
| 30 | +# Define years |
| 31 | +years = np.arange(2003, 2021, 1) |
| 32 | + |
| 33 | +# Define months |
| 34 | +months = [152, 182, 213, 244] |
| 35 | + |
| 36 | +# Define good hours |
| 37 | +good_hours = ['06', '07', '08', '09', '10', '11', '12', '13', '14'] |
| 38 | + |
| 39 | +good_files = [] |
| 40 | +for file in modis_list: |
| 41 | + # Get path and filename seperately |
| 42 | + infilepath, infilename = os.path.split(file) |
| 43 | + # Get file name without extension |
| 44 | + infilehortname, extension = os.path.splitext(infilename) |
| 45 | + |
| 46 | + # Append hour |
| 47 | + hour = infilehortname[18:20] |
| 48 | + if hour in good_hours: |
| 49 | + good_files.append(file) |
| 50 | + |
| 51 | +for year in years: |
| 52 | + for month in range(len(months)-1): |
| 53 | + |
| 54 | + # Get MODIS files |
| 55 | + modis_list_by_years = [] |
| 56 | + for j in range(len(good_files)): |
| 57 | + |
| 58 | + # Get path and filename seperately |
| 59 | + infilepath, infilename = os.path.split(good_files[j]) |
| 60 | + # Get file name without extension |
| 61 | + infileshortname, extension = os.path.splitext(infilename) |
| 62 | + |
| 63 | + if (infileshortname[10:14] == str(year)) & (int(infileshortname[14:17]) >= months[month]) & (int(infileshortname[14:17]) <= months[month+1]): |
| 64 | + modis_list_by_years.append(good_files[j]) |
| 65 | + |
| 66 | + # Chunk into groups of 75 |
| 67 | + bounds = np.arange(0, len(modis_list_by_years), 75) |
| 68 | + |
| 69 | + print('Processing year... %s and month... %.0f' %(str(year), month+1)) |
| 70 | + for bound in bounds: |
| 71 | + |
| 72 | + # Get slice of list |
| 73 | + files_sliced = modis_list_by_years[bound:bound+75] |
| 74 | + |
| 75 | + if os.path.exists(dest + 'MYD06_Cloud_' + str(year) + '_' + str(month+1) + '_' + str(bound) + '_' + str(bound+75) + '.nc'): |
| 76 | + pass |
| 77 | + else: |
| 78 | + |
| 79 | + data_stacked = np.zeros((2881, 1681)) |
| 80 | + for i in range(len(files_sliced)): |
| 81 | + print('Processing... %.0f out of %.0f' %(i+1, len(files_sliced))) |
| 82 | + |
| 83 | + # Read HDF |
| 84 | + data = hdf_read.MYD06_L2_Read(files_sliced[i], 'Cloud_Optical_Thickness') |
| 85 | + |
| 86 | + # Set zeros to NaNs |
| 87 | + data[data == 0] = np.nan |
| 88 | + |
| 89 | + # 2 = CTH, 3 = CTT, 4 = CTP, 5 = PHASE, 6 = COT, 7 = CER, 8 = CWP |
| 90 | + if np.nansum(np.isfinite(data[:,:,2])) > 200000: |
| 91 | + # Resample radiative fluxes to ISMIP grid |
| 92 | + swath_def = pyresample.geometry.SwathDefinition(lons=data[:,:,1], lats=data[:,:,0]) |
| 93 | + swath_con = pyresample.geometry.GridDefinition(lons=ismip_lon, lats=ismip_lat) |
| 94 | + |
| 95 | + |
| 96 | + # Determine nearest (w.r.t. great circle distance) neighbour in the grid. |
| 97 | + data_resampled = pyresample.kd_tree.resample_nearest(source_geo_def=swath_def, |
| 98 | + target_geo_def=swath_con, |
| 99 | + data=data[:,:,2], |
| 100 | + radius_of_influence=5000) |
| 101 | + |
| 102 | + # Set zeros to NaNs |
| 103 | + data_resampled[data_resampled == 0] = np.nan |
| 104 | + |
| 105 | + # Stack |
| 106 | + data_stacked = np.dstack((data_stacked, data_resampled)) |
| 107 | + |
| 108 | + # Remove first layer |
| 109 | + data_stacked = data_stacked[:, :, 1:] |
| 110 | + |
| 111 | + # Average |
| 112 | + data_mean = np.nanmean(data_stacked, axis=2) |
| 113 | + |
0 commit comments