├── getSNODAS.sh ├── untarSNODAS.sh ├── addtimeSNODAS.ncl ├── README.md ├── plotNESNODAS.ncl ├── plotNESNODAS_snowdays.ncl ├── subsetSNODAS_avgSNWZ.ncl ├── subsetSNODAS_avgSNWDAYS.ncl └── procSNODAS.sh /getSNODAS.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # 2017-07-10 3 | # E.A. Burakowski 4 | # Retrieves Nov-Apr (2004-2017) NOHRSC SNODAS .tar from National Snow and Ice Data Center (NSDIC) 5 | # server (sidads.colorado.edu) 6 | 7 | mkdir -p download_data 8 | cd download_data 9 | 10 | for months in "11_Nov" "12_Dec" "01_Jan" "02_Feb" "03_Mar" "04_Apr" "05_Mar" "06_Jun" "07_Jul" "08_Aug" "09_Sep" "10_Oct" 11 | do 12 | wget -N ftp://sidads.colorado.edu/DATASETS/NOAA/G02158/unmasked/{2014..2015}/"$months"/*.tar 13 | done 14 | -------------------------------------------------------------------------------- /untarSNODAS.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Stop of get any simple error 4 | set -e 5 | 6 | # process NOHRSC SNODAS files 7 | # (1) untar 8 | # (2) unzip 9 | # (3) copy .HDR to .hdr 10 | # (4) move .dat to variable specific dirs 11 | # PRLQ = liquid precipitation 12 | # PRSL = solid precipitation 13 | # SWEM = snow water equivalent, in meters 14 | # SNWZ = snow depth 15 | # SNWT = snowpack temperature 16 | # SUBB = sublimation from blowing snow 17 | # SUBS = sublimation from snow pack 18 | 19 | # Move to data dir 20 | cd download_data 21 | 22 | # Loop over SNODAS tar files 23 | for file in *.tar; 24 | do 25 | 26 | # (1) untar 27 | tar -xvf $file 28 | 29 | # (2) unzip 30 | gunzip -f *.gz 31 | 32 | # (3) move all .Hdr files to another dir (messes up gdal command) 33 | mkdir -p Hdr 34 | mv *.Hdr Hdr/. 35 | 36 | # List the .dat files from the unzipped folder 37 | FILES=*.dat 38 | 39 | # (4) loop over .dat files and move to specific dirs 40 | for datfiles in $FILES 41 | do 42 | case $datfiles in 43 | *1025SlL00*) 44 | mkdir -p PRLQ 45 | mv $datfiles PRLQ/. 46 | ;; 47 | *1025SlL01*) 48 | mkdir -p PRSL 49 | mv $datfiles PRSL/. 50 | ;; 51 | *1034*) 52 | mkdir -p SWEM 53 | mv $datfiles SWEM/. 54 | ;; 55 | *1036*) 56 | mkdir -p SNWZ 57 | mv $datfiles SNWZ/. 58 | ;; 59 | *1038*) 60 | mkdir -p SNWT 61 | mv $datfiles SNWT/. 62 | ;; 63 | *1039*) 64 | mkdir -p SUBB 65 | mv $datfiles SUBB/. 66 | ;; 67 | *1044*) 68 | mkdir -p SNWM 69 | mv $datfiles SNWM/. 70 | ;; 71 | *1050*) 72 | mkdir -p SUBS 73 | mv $datfiles SUBS/. 74 | ;; 75 | esac 76 | done 77 | done 78 | 79 | -------------------------------------------------------------------------------- /addtimeSNODAS.ncl: -------------------------------------------------------------------------------- 1 | 2 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" 3 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" 4 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" 5 | 6 | ; --------------------------------------------------------------------- 7 | ; 8 | ; addtimeSNODAS.ncl 9 | ; 10 | ; adds an unlimited time dimension to SNODAS .nc files created using 11 | ; procSNODAS.sh. 12 | ; - filename must conform to $VAR_snodas_$yyyymmdd.nc 13 | ; - where $VAR is a variable abbreviation of any length 14 | ; - $yyyymmdd is the year, month, day of the daily file 15 | ; 16 | ; --------------------------------------------------------------------- 17 | 18 | ;====================================================================== 19 | ; The main code 20 | ;====================================================================== 21 | 22 | begin 23 | 24 | ;--- A few constants 25 | 26 | ;-- Directory where .nc files are stored 27 | dir = "/net/nfs/yukon/raid5/data/NOHRSC_SNODAS/nc/" 28 | fils = systemfunc("cd "+dir+" ; ls SWEM*.nc") 29 | 30 | ;--- Loop over files 31 | do i = 0,dimsizes(fils)-1 32 | 33 | ;--- extract date from filename 34 | yyyymmdd = toint(str_get_field(fils(i),3,"_")) 35 | yyyy = yyyymmdd/10000 36 | mmdd = yyyymmdd-yyyy*10000 37 | mm = mmdd/100 38 | dd = mmdd-mm*100 39 | hh = dd 40 | mn = dd 41 | sc = dd 42 | hh = 0 43 | mn = 0 44 | sc = 0 45 | 46 | ;--- assign units 47 | units = "days since 2003-01-01" 48 | 49 | ;--- convert yyyymmdd to new units 50 | time = cd_inv_calendar(yyyy,mm,dd,hh,mn,sc,units,0) 51 | 52 | ;--- add file to write 53 | a = addfile(dir+fils(i),"w") 54 | 55 | ;--- add unlimited time dimension to file 56 | filedimdef(a,"time",-1,True) 57 | a->time = time 58 | 59 | end do ; fils 60 | 61 | end 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NOHRSC_SNODAS 2 | Scripts for retrieving and processing NOHRSC SNODAS daily binary files. 3 | Includes a number of scripts in bash, NCL, R, and/or matlab. 4 | This is a work in progress started in July 2017, so keep posted for additional scripts & edits. 5 | Codes will require customization and will not run as is. 6 | I'm happy to answer questions and welcome helpful suggestions for improving efficiency of any code: 7 | elizabeth.burakowski@unh.edu 8 | 9 | Files listed in order of use in my processing & analysis: 10 | 11 | getSNODAS.sh 12 | - a short bash script for retrieving Nov-Apr (2004-2017) .tar files from the National Snow and Ice Data Center (NSIDC) ftp site. Uses wget. 13 | 14 | untarSNODAS.sh 15 | - bash script that untars and unzips SNODAS files. 16 | - moves all .Hdr files to a separate directory. I do this because the gdal_translate command usually spits out errors if the .Hdr file is in the same directory as the .dat files. I haven't found a need for them yet. 17 | - moves all .dat files into separate subdirectories by variable. 18 | 19 | procSNODAS.sh 20 | - bash script that converts .dat files to .nc using gdal_translate. Also uses nco's ncrename and ncatted. 21 | 22 | addtimeSNODAS.ncl 23 | - adds unlimited time dimension to .nc files created in procSNODAS.sh. Surely there's a way to do it with NCO, but 24 | I found it to be easier in NCL. 25 | 26 | subsetSNODAS_avgSNWZ.ncl 27 | - selects a regional subset from SNODAS files, calculates a seasonal (or monthly) mean, max, and min snow depth for each 28 | season,and then calculates a climatological (well, 2004-2017) mean, max, and min for the period of record. 29 | - output is a single .nc file of the climatological mean, max, and min snow depth for regional subset. 30 | - could combine with plotting script (plotNESNODAS.ncl) if region is small. 31 | 32 | plotNESNODAS.ncl 33 | - plots up map of seasonal or monthly average snow depth for region in US. Output is .png graphic of data. 34 | 35 | subsetSNODAS_avgSNWDAYS.ncl 36 | - similar to subsetSNODAS_avgSNWZ.ncl, this script selects a regional subset from SNODAS files, calculates the number of 37 | days per winter (Nov-Apr) with snow depth > user-defined threshold for each year in record (2004-2017), and then 38 | calculates the climatological mean number of snow days > user-defined snow depth threshold. 39 | - output is a single .nc file of climatological mean number of snow days with snow depth > user-defined snow depth 40 | - could combine with plotting script (plotNESNODAS_snowdays.ncl) if region is small. 41 | 42 | plotNESNODAS_snowdays.ncl 43 | - plots up map of number of days per winter with snow depth > user-defined threshold. Includes contour at 20 days. 44 | - Output is .png graphic of data. 45 | -------------------------------------------------------------------------------- /plotNESNODAS.ncl: -------------------------------------------------------------------------------- 1 | 2 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" 3 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" 4 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" 5 | load "$NCARG_ROOT/lib/ncarg/nclscripts/esmf/ESMF_regridding.ncl" 6 | 7 | ;====================================================================== 8 | ; The main code 9 | ;====================================================================== 10 | 11 | begin 12 | 13 | ;---A few constants 14 | dir = "/net/nfs/yukon/raid5/data/NOHRSC_SNODAS/nc/" 15 | 16 | ;-- load avg snow depth file 17 | a = addfile(dir+"NE_Avg_Jan_SNWZ_2004-2017.nc","r") 18 | avgSnwz = a->avg_snwz 19 | 20 | printVarSummary(avgSnwz) 21 | 22 | lat = a->lat 23 | lon = a->lon 24 | minlat = 40 25 | maxlat = 47.6 26 | minlon = -76.5 27 | maxlon = max(lon) 28 | bndadd = 0.25 29 | 30 | ;---------------------------------------------------------------------- 31 | ; Plotting options section 32 | ;---------------------------------------------------------------------- 33 | 34 | pltType = "png" ; plot type 35 | pltDir = "./" ; plot directory 36 | pltName = "NE_Jan_Avg_SNWZ_2004-2017" ; plot name (ps file) 37 | pltPath = pltDir+pltName ; plot path 38 | 39 | wks = gsn_open_wks(pltType,pltPath) ; create workstation for ps file 40 | 41 | gsn_define_colormap(wks,"MPL_cool") ; define color table 42 | 43 | res = True 44 | 45 | res@gsnMaximize = True 46 | res@gsnDraw = False 47 | res@gsnFrame = False 48 | 49 | res@cnFillOn = True ; color plot desired 50 | res@cnLinesOn = False ; turn off contour lines 51 | res@cnLineLabelsOn = False ; turn off contour labels 52 | res@cnInfoLabelOn = False ; turn off info label (top labels of indvid. plots) 53 | res@cnFillMode = "RasterFill" ; turn raster on 54 | res@cnLevelSelectionMode = "ManualLevels" ; Set contour levels manually 55 | res@cnMinLevelValF = 0 ; minimum contour, mm 56 | res@cnMaxLevelValF = 650 ; maximum contour, mm 57 | res@cnLevelSpacingF = 100 ; countour interval (100 mm) 58 | res@lbLabelBarOn = True 59 | res@mpOutlineOn = True 60 | res@mpDataBaseVersion = "MediumRes" 61 | res@mpOutlineBoundarySets = "GeophysicalAndUSStates" 62 | res@mpProjection = "CylindricalEquidistant" 63 | 64 | res@mpLimitMode = "LatLon" ; required 65 | res@mpMinLatF = minlat-bndadd 66 | res@mpMaxLatF = maxlat+bndadd 67 | res@mpMinLonF = minlon-bndadd 68 | res@mpMaxLonF = maxlon+bndadd 69 | res@mpCenterLonF = (minlon + maxlon)*0.5 70 | res@mpCenterLatF = (minlat + maxlat)*0.5 71 | res@pmTickMarkDisplayMode = "Always" 72 | res@tmXTOn = False 73 | res@tmYLOn = False 74 | 75 | 76 | res@gsnLeftString = "" ; Turn off left subtitle 77 | res@gsnRightString = "" ; Turn off right subtitle 78 | res@gsnMajorLatSpacing = 1 79 | res@gsnMajorLonSpacing = 1 80 | res@gsnMinorLonSpacing = 1 81 | 82 | res@gsnAddCyclic = False ; regional grid (changes central meridian)/xwo 83 | 84 | plot = gsn_csm_contour_map(wks,avgSnwz,res) 85 | 86 | draw(plot) 87 | frame(wks) 88 | 89 | end 90 | -------------------------------------------------------------------------------- /plotNESNODAS_snowdays.ncl: -------------------------------------------------------------------------------- 1 | 2 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" 3 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" 4 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" 5 | 6 | ;====================================================================== 7 | ; The main code 8 | ;====================================================================== 9 | 10 | begin 11 | 12 | ;---A few constants 13 | dir = "/net/home/eos/ean2/SNODAS_processed/nc/" 14 | 15 | ;-- load avg snow depth file 16 | a = addfile(dir+"NE_SnowDays_Nov-Apr_SNWZGT200_mm_2004-2017.nc","r") 17 | avgSnowDays = a->SnowDays 18 | 19 | printVarSummary(avgSnowDays) 20 | 21 | lat = a->lat 22 | lon = a->lon 23 | minlat = 35 24 | maxlat = 47.5 25 | minlon = -79 26 | maxlon = max(lon) 27 | bndadd = 0.25 28 | 29 | ;---------------------------------------------------------------------- 30 | ; Plotting options section 31 | ;---------------------------------------------------------------------- 32 | 33 | pltType = "png" ; plot type 34 | pltDir = "/net/home/eos/ean2/SNODAS_processed/figures/" ; plot directory 35 | pltName = "NE_SnowDays_Nov-Apr_SNWZGT200_2004-2017" ; plot name (ps file) 36 | pltPath = pltDir+pltName ; plot path 37 | 38 | wks = gsn_open_wks(pltType,pltPath) ; create workstation for ps file 39 | 40 | gsn_define_colormap(wks,"MPL_cool") ; define color table 41 | 42 | res = True 43 | 44 | res@gsnMaximize = True 45 | res@gsnDraw = False 46 | res@gsnFrame = False 47 | 48 | ;-- Contour fill properties 49 | res@cnFillOn = True ; color plot desired 50 | res@cnInfoLabelOn = False ; turn off info label (top labels of indvid. plots) 51 | res@cnFillMode = "RasterFill" ; turn raster on 52 | res@gsnContourZeroLineThicknessF = 2 53 | 54 | ;-- Contour levels 55 | res@cnLinesOn = True ; turn off contour lines 56 | res@cnLineLabelsOn = True ; turn off contour labels 57 | res@cnLevelSelectionMode = "ManualLevels" ; Set contour levels manually 58 | res@cnMinLevelValF = 0 ; minimum contour, mm 59 | res@cnMaxLevelValF = 180 ; maximum contour, mm 60 | res@cnLevelSpacingF = 20 ; countour interval (100 mm) 61 | res@cnLevelFlags = (/"LineOnly","LineOnly","NoLine","NoLine","NoLine","NoLine",\ 62 | "NoLine","NoLine","NoLine","NoLine"/) 63 | ;-- Label bar properties 64 | res@lbLabelBarOn = True 65 | res@lbOrientation = "Horizontal" 66 | res@pmLabelBarWidthF = 0.4 67 | 68 | ;-- Map state outlines 69 | res@mpOutlineOn = True 70 | res@mpDataBaseVersion = "MediumRes" 71 | res@mpOutlineBoundarySets = "GeophysicalAndUSStates" 72 | res@mpProjection = "CylindricalEquidistant" 73 | 74 | ;-- Map domain boundaries, grid/tick properties 75 | res@mpLimitMode = "LatLon" ; required 76 | res@mpMinLatF = minlat-bndadd 77 | res@mpMaxLatF = maxlat+bndadd 78 | res@mpMinLonF = minlon-bndadd 79 | res@mpMaxLonF = maxlon+bndadd 80 | res@mpCenterLonF = (minlon + maxlon)*0.5 81 | res@mpCenterLatF = (minlat + maxlat)*0.5 82 | res@pmTickMarkDisplayMode = "Always" 83 | res@tmXTOn = False 84 | res@tmYLOn = False 85 | 86 | ;-- Labels & titles 87 | res@gsnLeftString = "" ; Turn off left subtitle 88 | res@gsnRightString = "" ; Turn off right subtitle 89 | res@gsnMajorLatSpacing = 1 90 | res@gsnMajorLonSpacing = 1 91 | res@gsnMinorLonSpacing = 1 92 | 93 | res@gsnAddCyclic = False ; regional grid (changes central meridian)/xwo 94 | 95 | plot = gsn_csm_contour_map(wks,avgSnowDays,res) 96 | 97 | draw(plot) 98 | frame(wks) 99 | 100 | end 101 | -------------------------------------------------------------------------------- /subsetSNODAS_avgSNWZ.ncl: -------------------------------------------------------------------------------- 1 | 2 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" 3 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" 4 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" 5 | 6 | ;---------------------------------------------------------------------- 7 | ; 8 | ; subsetSNODAS_avgSNWZ.ncl 9 | ; EA Burakowski 10 | ; 2017-08-07 11 | ; 12 | ; subsetSNODAS_avgSNWZ.ncl 13 | ; (1) selects a regional subset from the contiguous 14 | ; United States SNODAS dataset and 15 | ; (2) caculates a seasonal or monthly average, max, & min 16 | ; snow depth (SNWZ) 17 | ; (3) calculates a climatological (well, 2003-2017) average, 18 | ; max, & min 19 | ; 20 | ; output is a single .nc file of the climatological average, max, & 21 | ; min snow depth, in mm, for regional subset for use with plotting scripts 22 | ; scripts (ie. plotNESNODAS.ncl) 23 | ; 24 | ; * could probably combine plotting into the end of this script if 25 | ; region is small. 26 | ; 27 | ; --------------------------------------------------------------------- 28 | 29 | 30 | 31 | ;====================================================================== 32 | ; The main code 33 | ;====================================================================== 34 | 35 | begin 36 | 37 | ;---A few constants 38 | years = ispan(2004,2017,1) 39 | years!0 = "year" 40 | 41 | ;-- (1) select regional subset bounds (NE US below) 42 | latmin = 38 43 | latmax = 50 44 | lonmin = -78 45 | lonmax = -66.9421 46 | 47 | ;-- define variable and directory. SNWZ = snow depth 48 | var = "SNWZ" 49 | dir = "/net/nfs/yukon/raid5/data/NOHRSC_SNODAS/nc/" 50 | 51 | ;-- load a sample file to get dims & create empty array nyears x nlat x nlon 52 | a = addfile(dir+"/SNWZ_snodas_20140101.nc","r") 53 | b = a->SNWZ 54 | b_ne = b({latmin:latmax},{lonmin:lonmax}) 55 | lat = a->lat({latmin:latmax}) 56 | lon = a->lon({lonmin:lonmax}) 57 | 58 | ;-- create empty array to hold seasonal average for each year of record 59 | avg_seas = new((/dimsizes(years),dimsizes(b_ne(:,0)),dimsizes(b_ne(0,:))/),float) 60 | avg_seas@_FillValue = -9999 61 | avg_seas!0 = "year" 62 | avg_seas&year = years 63 | 64 | ;-- create empty array to hold seasonal maximum for each year of record 65 | max_seas = new((/dimsizes(years),dimsizes(b_ne(:,0)),dimsizes(b_ne(0,:))/),float) 66 | max_seas@_FillValue = -9999 67 | max_seas!0 = "year" 68 | max_seas&year = years 69 | 70 | ;-- create empty array to hold seasonal minimum for each year of record 71 | min_seas = new((/dimsizes(years),dimsizes(b_ne(:,0)),dimsizes(b_ne(0,:))/),float) 72 | min_seas@_FillValue = -9999 73 | min_seas!0 = "year" 74 | min_seas&year = years 75 | 76 | ;-- create list of file names and split by yyyy, mm, dd 77 | f = systemfunc("cd "+dir+" ; ls "+var+"*.nc") 78 | yyyymmdd = toint(str_get_field(f,3,"_")) 79 | yyyy = yyyymmdd/10000 80 | yyyymm = yyyymmdd/100 81 | mm = yyyymm-(yyyy*100) 82 | 83 | ;-- loop over years of record to calculate seasonal avg, max, and min 84 | do iyr=0,dimsizes(years)-2 85 | print("Working on "+years(iyr)+" and "+years(iyr+1)) 86 | 87 | ;-- Seasonal Nov-Apr code 88 | ;nd = ind(yyyy.eq.years(iyr) .and. mm.ge.12) ; nov dec 89 | ;jfma = ind(yyyy.eq.years(iyr+1) .and. mm.le.3) ; jan feb mar apr 90 | ;seas = array_append_record(nd,jfma,0) ; season appended 91 | 92 | ;-- Monthly code,in this case, just January (mm.eq.1). 93 | ; Note starts in 2004 (no Jan 2003 data) 94 | seas = ind(yyyy.eq.years(iyr+1) .and. mm.eq.1) 95 | 96 | ;-- add all files for one season in loop, and concatenate 97 | fils = addfiles(dir+f(seas),"r") 98 | ListSetType(fils,"join") 99 | snwz = fils[:]->SNWZ(:,{latmin:latmax},{lonmin:lonmax}) 100 | printVarSummary(snwz) 101 | 102 | ;-- (2) Calculate seasonal average, max, and min snow depth 103 | avg_seas(iyr,:,:) = dim_avg_n_Wrap(snwz,0) 104 | max_seas(iyr,:,:) = dim_max_n_Wrap(snwz,0) 105 | min_seas(iyr,:,:) = dim_min_n_Wrap(snwz,0) 106 | 107 | ;-- Delete temporary variables at end of loop 108 | ;delete([/nd,jfma,seas,fils,snwz/]) ; seasonal 109 | delete([/seas,fils,snwz/]) ; monthly 110 | end do ; years 111 | 112 | ;--- (3) Calculate 2004-2017 average snow depth 113 | avg_2004_2017 = dim_avg_n_Wrap(avg_seas,0) 114 | avgMax_2004_2017 = dim_avg_n_Wrap(max_seas,0) 115 | avgMin_2004_2017 = dim_avg_n_Wrap(min_seas,0) 116 | 117 | ;--- Write average snow depth, 2003-2017 to .nc file for plotting 118 | 119 | ;--- Filename for nc4 file. Change if not calculating January. 120 | fn = "NE_AvgMaxMin_Jan_"+var+"_"+years(0)+"-"+years(dimsizes(years)-1)+"" 121 | 122 | ;--- Write variables to nc4 file 123 | setfileoption("nc","FileStructure","Advanced") 124 | setfileoption("nc","Format","NetCDF4") 125 | 126 | ;--- remove old file 127 | system("/bin/rm -f "+dir+fn+".nc") 128 | 129 | ;--- create new file 130 | ncdf = addfile(dir+fn+".nc","c") 131 | fAtt = True 132 | fAtt@title = fn 133 | fAtt@orig_fil = "us_ssmv11036tS__T0001TTNATS*05HP001.dat" 134 | fAtt@Conventions = "COARDS/CF-1.0" 135 | fAtt@creation_date= systemfunc("date") 136 | fAtt@author = "Elizabeth Burakowski (elizabeth.burakowski@unh.edu)" 137 | 138 | ;--- file attribute, description. Change appropriately 139 | fAtt@description = "Average, Max, & Min January "+var+" [snow depth] in mm, "+years(0)+"-"+years(dimsizes(years)-1)+"" 140 | 141 | ;--- file attributes, size 142 | fileattdef(ncdf,fAtt) 143 | dimNames = (/"lat", "lon"/) 144 | dimSizes = (/ dimsizes(b_ne(:,0)), dimsizes(b_ne(0,:)) /) 145 | dimUnlim = (/ False, False /) 146 | filedimdef(ncdf,dimNames,dimSizes,dimUnlim) 147 | 148 | ;--- Define file variables 149 | filevardef(ncdf,"lat",typeof(lat),getvardims(lat)) 150 | filevardef(ncdf,"lon",typeof(lon),getvardims(lon)) 151 | filevardef(ncdf,"avg_snwz",typeof(avg_2004_2017),getvardims(avg_2004_2017)) 152 | filevardef(ncdf,"max_snwz",typeof(avgMax_2004_2017),getvardims(avgMax_2004_2017)) 153 | filevardef(ncdf,"min_snwz",typeof(avgMin_2004_2017),getvardims(avgMin_2004_2017)) 154 | 155 | ;--- Define file attributes 156 | filevarattdef(ncdf,"lat",lat) 157 | filevarattdef(ncdf,"lon",lon) 158 | filevarattdef(ncdf,"avg_snwz",avg_2004_2017) 159 | filevarattdef(ncdf,"max_snwz",avgMax_2004_2017) 160 | filevarattdef(ncdf,"min_snwz",avgMin_2004_2017) 161 | 162 | setfileoption(ncdf,"DefineMode",False) 163 | 164 | ;--- write variable to file 165 | ncdf->lat = (/lat/) 166 | ncdf->lon = (/lon/) 167 | ncdf->avg_snwz = (/avg_2004_2017/) 168 | ncdf->max_snwz = (/avgMax_2004_2017/) 169 | ncdf->min_snwz = (/avgMin_2004_2017/) 170 | 171 | end 172 | 173 | -------------------------------------------------------------------------------- /subsetSNODAS_avgSNWDAYS.ncl: -------------------------------------------------------------------------------- 1 | 2 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" 3 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" 4 | load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" 5 | 6 | ;---------------------------------------------------------------------- 7 | ; 8 | ; subsetSNODAS_avgSNWDAYS.ncl 9 | ; EA Burakowski 10 | ; 2017-08-15 11 | ; 12 | ; subsetSNODAS_avgSNWDAYS.ncl 13 | ; (1) selects a regional subset from the contiguous 14 | ; United States SNODAS dataset and 15 | ; (2) caculates a seasonal or monthly number of days with snow 16 | ; depth greater than [threshold] mm. 17 | ; (3) calculates a climatological (well, 2003-2017) snow days 18 | ; 19 | ; output is a single .nc file of the climatological snow dayss 20 | ; 21 | ;---------------------------------------------------------------------- 22 | 23 | ;================================================================== 24 | ; 25 | ; SnowDays function 26 | ; - E Burakowski 27 | ; 2017-08-15 28 | ; 29 | ;================================================================== 30 | 31 | undef("SnowDays") 32 | function SnowDays(x[*][*][*]:numeric,thresh[1]:numeric) 33 | local dimx, ntim, nlat, mlon, snwd, nl, ml, ii 34 | 35 | begin 36 | dimx = dimsizes(x) 37 | ntim = dimx(0) 38 | nlat = dimx(1) 39 | mlon = dimx(2) 40 | 41 | snwd = new( dimx(1:2), "integer", -9999) 42 | 43 | do nl=0,nlat-1,1 44 | do ml=0,mlon-1,1 45 | ii := ind(x(:,nl,ml) .ge. thresh) 46 | if (.not.ismissing(ii(0))) then 47 | nii = dimsizes(ii) 48 | snwd(nl,ml) = nii 49 | end if 50 | end do ; lon 51 | end do ; lat 52 | 53 | snwd@long_name = "Days per winter (Nov - Apr) with snow depth > "+thresh 54 | snwd@units = "days per winter" 55 | snwd!0 = "lat" 56 | snwd&lat = x&lat 57 | snwd!1 = "lon" 58 | snwd&lon = x&lon 59 | return(snwd) 60 | 61 | end 62 | 63 | ;====================================================================== 64 | ; The main code 65 | ;====================================================================== 66 | 67 | begin 68 | 69 | ;---A few constants 70 | thresh = 200 ; minimum snow depth threshold, in mm 71 | years = ispan(2004,2017,1) 72 | years!0 = "year" 73 | 74 | ;-- (1) select regional subset bounds (Northeast below) 75 | latmin = 30 76 | latmax = 50 77 | lonmin = -80 78 | lonmax = -66 79 | 80 | ;-- define variable and directory. SNWZ = snow depth 81 | var = "SNWZ" 82 | dir = "/net/nfs/yukon/raid5/data/NOHRSC_SNODAS/nc/" 83 | odir = "/net/home/eos/ean2/SNODAS_processed/" 84 | 85 | ;-- load a sample file to get dims & create empty array nyears x nlat x nlon 86 | a = addfile(dir+"/SNWZ_snodas_20140101.nc","r") 87 | b = a->SNWZ({latmin:latmax},{lonmin:lonmax}) 88 | lat = a->lat({latmin:latmax}) 89 | lon = a->lon({lonmin:lonmax}) 90 | 91 | ;-- create empty array to hold seasonal total days with snow for each year of record 92 | snwdays = new((/dimsizes(years),dimsizes(b(:,0)),dimsizes(b(0,:))/),float) 93 | snwdays@_FillValue = -9999 94 | snwdays!0 = "year" 95 | snwdays&year = years 96 | 97 | ;-- create list of file names and split to obtain yyyy, mm, dd 98 | f = systemfunc("cd "+dir+" ; ls "+var+"*.nc") 99 | yyyymmdd = toint(str_get_field(f,3,"_")) 100 | yyyy = yyyymmdd/10000 101 | yyyymm = yyyymmdd/100 102 | mm = yyyymm-(yyyy*100) 103 | 104 | ;-- loop over years of record to calculate seasonal avg, max, and min 105 | do iyr=0,dimsizes(years)-2 106 | print("Working on "+years(iyr)+" and "+years(iyr+1)) 107 | 108 | ;-- Seasonal Nov-Apr code 109 | nd = ind(yyyy.eq.years(iyr) .and. mm.ge.11) ; nov dec 110 | jfma = ind(yyyy.eq.years(iyr+1) .and. mm.le.4) ; jan feb mar apr 111 | seas = array_append_record(nd,jfma,0) ; season appended 112 | 113 | ;-- add all files for one season in loop, and concatenate 114 | fils = addfiles(dir+f(seas),"r") 115 | ListSetType(fils,"join") 116 | snwz = fils[:]->SNWZ(:,{latmin:latmax},{lonmin:lonmax}) 117 | snwz!0 = "days" 118 | snwz&days = ispan(1,dimsizes(seas),1) 119 | printVarSummary(snwz) 120 | 121 | ;-- (2) Get indices of days > threshold 122 | snwdays(iyr,:,:) = SnowDays(snwz,200) 123 | 124 | print("Max snow days for year "+years(iyr)+" = "+max(snwdays(iyr,:,:))) 125 | print("Min snow days for year "+years(iyr)+" = "+min(snwdays(iyr,:,:))) 126 | 127 | ;-- Delete temporary variables at end of loop 128 | delete([/nd,jfma,seas,fils,snwz/]) 129 | 130 | end do ; years 131 | 132 | printVarSummary(snwdays) 133 | 134 | ;--- (3) Calculate 2004-2017 average snow depth 135 | snowdays_2004_2017= dim_avg_n_Wrap(snwdays,0) 136 | 137 | printVarSummary(snowdays_2004_2017) 138 | print("Max Snow Days = "+max(snowdays_2004_2017)) 139 | print("Min Snow Days = "+min(snowdays_2004_2017)) 140 | 141 | 142 | ;---------------------------------------------------------------------- 143 | ; Plotting options section 144 | ;---------------------------------------------------------------------- 145 | 146 | pltType = "png" ; plot type 147 | pltDir = "./" ; plot directory 148 | pltName = "NE_SnowDays_Nov-Apr_SNWZGT200_2004-2017" ; plot name (ps file) 149 | pltPath = pltDir+pltName ; plot path 150 | 151 | wks = gsn_open_wks(pltType,pltPath) ; create workstation for ps file 152 | 153 | gsn_define_colormap(wks,"MPL_cool") ; define color table 154 | 155 | res = True 156 | 157 | res@gsnMaximize = True 158 | res@gsnDraw = False 159 | res@gsnFrame = False 160 | 161 | res@cnFillOn = True ; color plot desired 162 | res@cnLinesOn = True ; turn off contour lines 163 | res@cnLineLabelsOn = True ; turn off contour labels 164 | res@cnInfoLabelOn = False ; turn off info label (top labels of indvid. plots) 165 | res@cnFillMode = "RasterFill" ; turn raster on 166 | res@cnLevelSelectionMode = "ManualLevels" ; Set contour levels manually 167 | res@cnMinLevelValF = 0 ; minimum contour, mm 168 | res@cnMaxLevelValF = 180 ; maximum contour, mm 169 | res@cnLevelSpacingF = 20 ; countour interval (100 mm) 170 | res@lbLabelBarOn = True 171 | res@mpOutlineOn = True 172 | res@mpDataBaseVersion = "MediumRes" 173 | res@mpOutlineBoundarySets = "GeophysicalAndUSStates" 174 | res@mpProjection = "CylindricalEquidistant" 175 | bndadd = 0.25 176 | res@mpLimitMode = "LatLon" ; required 177 | res@mpMinLatF = latmin-bndadd 178 | res@mpMaxLatF = latmax+bndadd 179 | res@mpMinLonF = lonmin-bndadd 180 | res@mpMaxLonF = lonmax+bndadd 181 | res@mpCenterLonF = (lonmin + lonmax)*0.5 182 | res@mpCenterLatF = (latmin + latmax)*0.5 183 | res@pmTickMarkDisplayMode = "Always" 184 | res@tmXTOn = False 185 | res@tmYLOn = False 186 | 187 | 188 | res@gsnLeftString = "" ; Turn off left subtitle 189 | res@gsnRightString = "" ; Turn off right subtitle 190 | res@gsnMajorLatSpacing = 1 191 | res@gsnMajorLonSpacing = 1 192 | res@gsnMinorLonSpacing = 1 193 | 194 | res@gsnAddCyclic = False ; regional grid (changes central meridian)/xwo 195 | 196 | plot = gsn_csm_contour_map(wks,snowdays_2004_2017,res) 197 | 198 | draw(plot) 199 | frame(wks) 200 | 201 | 202 | ;--- Write average snow depth, 2003-2017 to .nc file for plotting 203 | 204 | ;--- Filename for nc4 file. Change if not calculating January. 205 | fn = "NE_SnowDays_Nov-Apr_"+var+"GT"+thresh+"_mm_"+years(0)+"-"+years(dimsizes(years)-1)+"" 206 | 207 | ;--- Write variables to nc4 file 208 | setfileoption("nc","FileStructure","Advanced") 209 | setfileoption("nc","Format","NetCDF4") 210 | 211 | ;--- remove old file 212 | system("/bin/rm -f "+odir+fn+".nc") 213 | 214 | ;--- create new file 215 | ncdf = addfile(odir+fn+".nc","c") 216 | fAtt = True 217 | fAtt@title = fn 218 | fAtt@orig_fil = "us_ssmv11036tS__T0001TTNATS*05HP001.dat" 219 | fAtt@Conventions = "COARDS/CF-1.0" 220 | fAtt@creation_date= systemfunc("date") 221 | fAtt@author = "Elizabeth Burakowski (elizabeth.burakowski@unh.edu)" 222 | 223 | ;--- file attribute, description. Change appropriately 224 | fAtt@description = "Average snow days "+var+" greater than "+thresh+" mm, "+years(0)+"-"+years(dimsizes(years)-1)+"" 225 | 226 | ;--- file attributes, size 227 | fileattdef(ncdf,fAtt) 228 | dimNames = (/"lat", "lon"/) 229 | dimSizes = (/ dimsizes(b(:,0)), dimsizes(b(0,:)) /) 230 | dimUnlim = (/ False, False /) 231 | filedimdef(ncdf,dimNames,dimSizes,dimUnlim) 232 | 233 | 234 | ;--- Define file variables 235 | filevardef(ncdf,"lat",typeof(lat),getvardims(lat)) 236 | filevardef(ncdf,"lon",typeof(lon),getvardims(lon)) 237 | filevardef(ncdf,"SnowDays",typeof(snowdays_2004_2017),getvardims(snowdays_2004_2017)) 238 | 239 | ;--- Define file attributes 240 | filevarattdef(ncdf,"lat",lat) 241 | filevarattdef(ncdf,"lon",lon) 242 | filevarattdef(ncdf,"SnowDays",snowdays_2004_2017) 243 | 244 | setfileoption(ncdf,"DefineMode",False) 245 | 246 | ;--- write variable to file 247 | ncdf->lat = (/lat/) 248 | ncdf->lon = (/lon/) 249 | ncdf->SnowDays = (/snowdays_2004_2017/) 250 | 251 | end 252 | 253 | -------------------------------------------------------------------------------- /procSNODAS.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Stop of get any simple error 4 | set -e 5 | 6 | # EA Burakowski 7 | # 2017-07-17 8 | 9 | # Process NOHRSC SNODAS daily .dat files, convert to .nc following recommended procedures from NSIDC: https://nsidc.org/support/how/how-do-i-convert-snodas-binary-files-geotiff-or-netcdf 10 | 11 | 12 | # (1) Create generic .hdr file: 13 | # ENVI 14 | # samples = 6935 15 | # lines = 3351 16 | # bands = 1 17 | # header offset = 0 18 | # file type = ENVI Standard 19 | # data type = 2 20 | # interleave = bsq 21 | # byte order = 1 22 | # map info = {Geographic, 0, 0, -124.729583333331703, ... 23 | # 52.871249516804028, 0.00833333333, 0.00833333333} 24 | # (2) gdal_translate: convert .dat (binary) to .nc 25 | # (3) ncrename: change variable name from gdal band1 to appropriate variable name 26 | # (4) ncatted: add long_name to variable 27 | # (5) ncatted: add units to variable 28 | 29 | 30 | # Assumes .dat files are stored in separate directories for each variable. 31 | # Sub-directory (SNODAS_DATA_DIR) names are 4-letter abbreviations. Feel free to change, however 32 | # they should be consistent with untarSNODAS.sh 33 | # Options include: 34 | # PRLQ - liquid precip 35 | # PRSL - solid precip 36 | # SNWM - snow melt (from base of snowpack) 37 | # SNWT - snowpack integrated temperature 38 | # SNWZ - snow depth 39 | # SUBB - sublimation from blowing snow 40 | # SUBS - sublimation from snow pack 41 | # SWEM - snow water equivalent 42 | # convert .dat (binary) to .nc (netCDF) 43 | 44 | ############################################################################## 45 | # 46 | # Main Code 47 | # 48 | ############################################################################## 49 | 50 | # Make sure the SNODAS_DATA_DIR environment variable is set 51 | if [ -z "$SNODAS_DATA_DIR" ]; then 52 | echo "Need to set SNODAS_DATA_DIR" 53 | exit 1 54 | fi 55 | 56 | # Flag if using masked or unmasked SNODAS extent 57 | masked=false 58 | 59 | # Flag to subset netcdf files (removes original one!) 60 | subset=true 61 | 62 | # Define sub Region Of Interest (ROI) 63 | # CRHO - SnowCast 64 | lon_min=-116.645 65 | lon_max=-114.769166666667 66 | lat_min=50.66 67 | lat_max=51.7933333333333 68 | 69 | # Define directory with variable subdirs of dat files 70 | cd $SNODAS_DATA_DIR 71 | 72 | # Define path to generic .hdr 73 | genhdr=$SNODAS_DATA_DIR"/generic.hdr" 74 | 75 | # Define out directory for .nc files 76 | odir=$SNODAS_DATA_DIR"/nc" 77 | mkdir -p $odir 78 | 79 | # Loop over .dat subdirs. 80 | # Copy/pasta 'elif' block below and edit accordingly (e.g., units, long_name) 81 | # to include additional .dat subdirs. Mind the character spacing on filenames! 82 | # Filenames are not consistent across variables. 83 | # PRLQ PRSL SWEM SNWZ 84 | for dirs in SWEM SNWZ 85 | do 86 | echo "Now in working in the $dirs directory" 87 | FILES=${dirs}/*.dat 88 | 89 | # loop over fils in subdir 90 | for infiles in ${FILES} 91 | do 92 | echo "Working on $infiles" 93 | varn=`echo ${infiles} | cut -c 14-17` 94 | echo "And this variable code: $varn" 95 | pflg=`echo ${infiles} | cut -c 21-22` 96 | echo "This is a precip flag: $pflg" 97 | 98 | # Series of if/else follows for each variable. There's probably a more 99 | # efficient way of doing this. 100 | 101 | # Liquid Precipitation (PRLQ) 102 | if [[ "$varn" == "1025" && "$pflg" == "00" ]]; 103 | then 104 | # cut out and print filename (minus extension) 105 | Hdrfile=`echo ${infiles} | cut -c 6-48` 106 | echo "Now working on : $Hdrfile" 107 | 108 | # define the variable abbreviation for .nc files 109 | var=`echo PRCPL` 110 | echo "And this variable: ${var}" 111 | 112 | # extract date from filename and print to screen 113 | date=`echo $infiles | cut -c 34-41` 114 | echo "For this day: $date" 115 | 116 | # (1) copy generic ENVI header file to filename 117 | # place in working directory for gdal_translate 118 | cp $genhdr $dirs/$Hdrfile.hdr 119 | echo "created generic .hdr: $dirs/$Hdrfile.hdr" 120 | 121 | # (2) Generate command to convert binary dat (infile) to netCDF (outfile) 122 | ofile=`echo ${var}_snodas_${date}.nc` 123 | echo "Will use this for the .nc filename: $ofile" 124 | # Check if masked or not 125 | if [ "$masked" = true ] ; then 126 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84' -a_nodata -9999 -A_ullr -124.7337 52.8754 -66.9421 24.9496 $infiles $odir/$ofile 127 | else 128 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' -a_nodata -9999 -a_ullr -130.516666666661 58.2333333333310 -62.2499999999975 24.0999999999990 $infiles $odir/$ofile 129 | fi 130 | 131 | # (3) Change name of variable in .nc file (defaults to band1) 132 | ncrename -v Band1,$var $odir/$ofile 133 | 134 | # (4) Change the long_name in .nc file (unfortunately doesn't look like spaces are allowed 135 | ncatted -O -a long_name,$var,o,c,"Liquid_Precipitation" $odir/$ofile 136 | 137 | # (5) Add units to new variable 138 | ncatted -a units,$var,c,c,"kgm-2" $odir/$ofile 139 | 140 | # else/if to move on to next variable 141 | elif [[ "$varn" == "1025" && "$pflg" == "01" ]]; 142 | 143 | # Solid Precipitation 144 | then 145 | 146 | Hdrfile=`echo ${infiles} | cut -c 6-48` 147 | echo "Now working on : ${Hdrfile}" 148 | var=`echo PRCPS` 149 | echo "And this variable: ${var}" 150 | date=`echo ${infiles} | cut -c 34-41` 151 | echo "For this day: ${date}" 152 | 153 | # (1) copy generic ENVI header file to filename 154 | # place in working directory for gdal_translate 155 | cp $genhdr $dirs/$Hdrfile.hdr 156 | echo "created generic .hdr: $hdr" 157 | 158 | # (2) Generate command to convert binary dat (infile) to netCDF (outfile) 159 | ofile=`echo ${var}_snodas_${date}.nc` 160 | echo "Will use this for the .nc filename: $ofile" 161 | # Check if masked or not 162 | if [ "$masked" = true ] ; then 163 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84' -a_nodata -9999 -A_ullr -124.7337 52.8754 -66.9421 24.9496 $infiles $odir/$ofile 164 | else 165 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' -a_nodata -9999 -a_ullr -130.516666666661 58.2333333333310 -62.2499999999975 24.0999999999990 $infiles $odir/$ofile 166 | fi 167 | 168 | # (3) Change name of variable in .nc file (defaults to band1) 169 | ncrename -v Band1,$var $odir/$ofile 170 | 171 | # (4) Change the long_name in .nc file (unfortunately doesn't look like spaces are allowed 172 | ncatted -O -a long_name,$var,o,c,"Solid_Precipitation" $odir/$ofile 173 | 174 | # (5) Add units to new variable 175 | ncatted -a units,$var,c,c,"kgm-2" $odir/$ofile 176 | 177 | elif [[ "$varn" == "1034" ]]; 178 | 179 | # Snow water equivalent 180 | then 181 | 182 | Hdrfile=`echo $infiles | cut -c 6-47` 183 | echo "Now working on : $Hdrfile" 184 | var=SWE 185 | echo "And this variable: $var" 186 | date=`echo $infiles | cut -c 33-40` 187 | echo "For this day: $date" 188 | 189 | # (1) copy generic ENVI header file to filename 190 | # place in working directory for gdal_translate 191 | cp $genhdr $dirs/$Hdrfile.hdr 192 | echo "created generic .hdr: $hdr" 193 | 194 | # (2) Generate command to convert binary dat (infile) to netCDF (outfile) 195 | ofile=`echo ${var}_snodas_${date}.nc` 196 | echo "Will use this for the .nc filename: $ofile" 197 | # Check if masked or not 198 | if [ "$masked" = true ] ; then 199 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84' -a_nodata -9999 -A_ullr -124.7337 52.8754 -66.9421 24.9496 $infiles $odir/$ofile 200 | else 201 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' -a_nodata -9999 -a_ullr -130.516666666661 58.2333333333310 -62.2499999999975 24.0999999999990 $infiles $odir/$ofile 202 | fi 203 | 204 | # (3) Change name of variable in .nc file (defaults to band1) 205 | ncrename -v Band1,$var $odir/$ofile 206 | 207 | # (4) Change the long_name in .nc file (unfortunately doesn't look like spaces are allowed 208 | ncatted -O -a long_name,$var,o,c,"Snow_Water_Equivalent" $odir/$ofile 209 | 210 | # (5) Add units to new variable 211 | ncatted -a units,${var},c,c,"mm" $odir/$ofile 212 | 213 | elif [[ "$varn" == "1036" ]]; 214 | 215 | # Snow depth 216 | then 217 | 218 | Hdrfile=`echo $infiles | cut -c 6-47` 219 | echo "Now working on : $Hdrfile" 220 | var=SNWZ 221 | echo "And this variable: $var" 222 | date=`echo $infiles | cut -c 33-40` 223 | echo "For this day: $date" 224 | 225 | # (1) copy generic ENVI header file to filename 226 | # place in working directory for gdal_translate 227 | cp $genhdr $dirs/$Hdrfile.hdr 228 | echo "created generic .hdr: $hdr" 229 | 230 | # (2) Generate command to convert binary dat (infile) to netCDF (outfile) 231 | ofile=`echo ${var}_snodas_${date}.nc` 232 | echo "Will use this for the .nc filename: $ofile" 233 | # Check if masked or not 234 | if [ "$masked" = true ] ; then 235 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84' -a_nodata -9999 -A_ullr -124.7337 52.8754 -66.9421 24.9496 $infiles $odir/$ofile 236 | else 237 | gdal_translate -of NetCDF -a_srs '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' -a_nodata -9999 -a_ullr -130.516666666661 58.2333333333310 -62.2499999999975 24.0999999999990 $infiles $odir/$ofile 238 | fi 239 | 240 | # (3) Change name of variable in .nc file (defaults to band1) 241 | ncrename -v Band1,$var $odir/$ofile 242 | 243 | # (4) Change the long_name in .nc file (unfortunately doesn't look like spaces are allowed 244 | ncatted -O -a long_name,$var,o,c,"Snow_Depth" $odir/$ofile 245 | 246 | # (5) Add units to new variable 247 | ncatted -a units,${var},c,c,"mm" $odir/$ofile 248 | else 249 | echo "None of your desired variables were found; check sub-directory abbreviations" 250 | fi 251 | 252 | # Add time dimension. Here we assume the output hour of the SNODAS files is at 06:00 UTC 253 | year=${date:0:4} 254 | month=${date:4:2} 255 | day=${date:6:2} 256 | date_format=${year}-${month}-${day} # make time format expected YYYY-MM-DD 257 | ncap2 -s "defdim(\"time\",-1);time[time]=0;time@long_name=\"Time\";time@timezone=\"UTC\";time@units=\"days since ${date_format} 06:00:00\"" -O $odir/$ofile $odir/$ofile'.temp' 258 | # Make time the record dimension and add to other variables 259 | ncwa -a time -O $odir/$ofile'.temp' $odir/$ofile 260 | ncecat -u time -O $odir/$ofile $odir/$ofile 261 | # Clean up 262 | rm -f $odir/$ofile'.temp' 263 | 264 | # Final option to subset netcdf file and remove original 265 | if [ $subset = true ]; then 266 | echo "Subsetting netcdf file to user defined region" 267 | sub_ofile="${ofile%.*}" 268 | ncea -O -d lat,$lat_min,$lat_max -d lon,$lon_min,$lon_max $odir/$ofile $odir/$sub_ofile"_sub.nc" 269 | 270 | # Remove full extent file 271 | echo "Removing full extent netcdf file" 272 | rm -f $odir/$ofile 273 | fi 274 | 275 | done # loop over files in subdir 276 | 277 | done # loop over subdirs 278 | 279 | 280 | --------------------------------------------------------------------------------