|
| 1 | +################ This is the import statement required to reference scripts within the package |
| 2 | +import os,sys,glob |
| 3 | +ndh_tools_path_opts = [ |
| 4 | + '/mnt/data01/Code/', |
| 5 | + '/home/common/HolschuhLab/Code/' |
| 6 | +] |
| 7 | +for i in ndh_tools_path_opts: |
| 8 | + if os.path.isfile(i): sys.path.append(i) |
| 9 | +################################################################################################ |
| 10 | + |
| 11 | + |
| 12 | +def cresis_season(y,m=0,d=0,ant1_gre2=1): |
| 13 | + """ |
| 14 | + % (C) Nick Holschuh - Amherst College -- 2022 ([email protected]) |
| 15 | + % |
| 16 | + % This function finds the season name associated with a flight day |
| 17 | + % There are a small number of flight days in which both campaigns |
| 18 | + % were running apparently. By default, this code will assume you want |
| 19 | + % the Antarctic Season. If you would prefer it prompt you to ask for |
| 20 | + % a selection, chanage the value to zero. |
| 21 | + % |
| 22 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 23 | + % The inputs are: |
| 24 | + % |
| 25 | + % y - either the year, or a string for the filename you want the season for |
| 26 | + % m - the month |
| 27 | + % d - the day |
| 28 | + % ant1_gre2 - force a particular continent for days where there are surveys in both places |
| 29 | + % |
| 30 | + %%%%%%%%%%%%%%% |
| 31 | + % The outputs are: |
| 32 | + % season_out - A dictionary with information about the matching season |
| 33 | + % |
| 34 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | + """ |
| 36 | + from datetime import date |
| 37 | + import numpy as np |
| 38 | + import os |
| 39 | + import sys |
| 40 | + import glob |
| 41 | + sys.path.append('/mnt/data01/Code/') |
| 42 | + import NDH_Tools as ndh |
| 43 | + |
| 44 | + ########################### Here we find the season metadata. This file was produced |
| 45 | + ########################### from an external matlab script |
| 46 | + season_metadata_dirs = [ |
| 47 | + '/mnt/data01/Data/RadarData/CReSIS_Filestructure/' |
| 48 | + ] |
| 49 | + for i in season_metadata_dirs: |
| 50 | + if os.path.isdir(i): season_metadata_path=i |
| 51 | + |
| 52 | + |
| 53 | + season_opts = ndh.loadmat(season_metadata_path+'season_metadata.mat') |
| 54 | + |
| 55 | + if isinstance(y,str) == 1: |
| 56 | + if y[0] == 'D': |
| 57 | + m = int(y[9:11]) |
| 58 | + d = int(y[11:13]) |
| 59 | + y = int(y[5:9]) |
| 60 | + else: |
| 61 | + m = int(y[4:6]) |
| 62 | + d = int(y[6:8]) |
| 63 | + y = int(y[0:4]) |
| 64 | + |
| 65 | + target_date = date.toordinal(date(y,m,d)) |
| 66 | + |
| 67 | + full_dates = np.concatenate([season_opts['a_dates'],season_opts['g_dates']]) |
| 68 | + ############### Matlab starts at year 0, python at year 1. So there is a 1 year offset applied here |
| 69 | + full_dates[:,0] = full_dates[:,0]-366 |
| 70 | + full_dates = np.concatenate([full_dates,np.expand_dims(np.concatenate([np.ones(len(season_opts['a_dates'][:,0])), |
| 71 | + np.ones(len(season_opts['g_dates'][:,0]))*2]),0).T],1) |
| 72 | + |
| 73 | + match_ind = np.where(full_dates[:,0] == target_date)[0] |
| 74 | + exact_flag = 1; |
| 75 | + |
| 76 | + if ant1_gre2 == 1: |
| 77 | + if len(match_ind) > 1: |
| 78 | + match_ind = match_ind[0] |
| 79 | + else: |
| 80 | + if len(match_ind) > 1: |
| 81 | + match_ind = match_ind[1] |
| 82 | + |
| 83 | + if len(match_ind) == 0: |
| 84 | + match_ind = ndh.find_nearest(full_dates[:,0],target_date); |
| 85 | + match_ind = match_ind['index'][0] |
| 86 | + exact_flag = 0; |
| 87 | + |
| 88 | + ############### You have to subtract one from the match ind to deal with matlabs indexing |
| 89 | + if full_dates[match_ind,2] == 1: |
| 90 | + season_out = season_opts['a_names'][int(full_dates[match_ind,1]-1)] |
| 91 | + else: |
| 92 | + season_out = season_opts['g_names'][int(full_dates[match_ind,1]-1)] |
| 93 | + |
| 94 | + if exact_flag == 0: |
| 95 | + print('No exact match was found -- closest suggested season is the following: '+season_out[0]) |
| 96 | + |
| 97 | + season_out = {'exact_match':exact_flag,'season':season_out[0],'data_dir':season_out[1],'Date':season_out[2]} |
| 98 | + return season_out |
0 commit comments