undef("curvilinear_to_SCRIP") procedure curvilinear_to_SCRIP(scrip_fname[1]:string,lat2d[*][*]:numeric,\ lon2d[*][*]:numeric,cts_opt[1]:logical) begin print("INSIDE modified curvilinear_to_SCRIP") start_time = get_start_time() DEBUG = True ;---Check if the file already exists print("Checking for existence of " + scrip_fname) check_for_file(scrip_fname,cts_opt) ;---Do we need to create a large file? if (.not.isatt_logical_false(cts_opt,"LargeFile")) then setfileoption("nc","Format","LargeFile") end if ;---Create a netcdf-4 file setfileoption("nc","Format","netcdf4") nc_file_type = "netcdf4" if ( any(dimsizes(lat2d).ne.dimsizes(lon2d)) ) then print("curvilinear_to_SCRIP: latitude and longitude must have the same number of elements.") exit end if print("Getting dimension sizes for grid") latlon_dims = dimsizes(lat2d) nlat = latlon_dims(0) nlon = latlon_dims(1) file_title = "curvilinear_to_SCRIP (" + nlat + "," + nlon + ")" ;---Was a mask provided? print("Checking for a mask array") grid_mask_name = get_mask_name(cts_opt) if(grid_mask_name.ne."") then if(.not.all(dimsizes(cts_opt@$grid_mask_name$).eq.latlon_dims)) then print("curvilinear_to_SCRIP: cts_opt@" + grid_mask_name + \ " is not the correct dimensionality") exit else grid_mask = cts_opt@$grid_mask_name$ end if else ;---No masking grid_mask = new(latlon_dims, "integer","No_FillValue") grid_mask = 1 end if ;---Open the file in create mode print("Open file in create mode") fid = addfile(scrip_fname,"c") setfileoption(fid,"DefineMode",True) ;---Define the file attributes print("Define global attributes") global_file_atts = True global_file_atts@title = file_title global_file_atts@Conventions = "SCRIP" global_file_atts@Createdby = "ESMF_regridding.ncl" global_file_atts@date_created = systemfunc("date") fileattdef(fid,global_file_atts) ;---Define the SCRIP dimensions print("Define dimension sizes on SCRIP file") grid_size = nlat*nlon ; This is number of data points (grid nodes) grid_corners = 4 grid_rank = 2 file_dim_names = (/ "grid_size","grid_corners","grid_rank" /) file_dim_sizes = (/ grid_size,grid_corners,grid_rank /) file_dim_unlim = (/ False,False,False /) filedimdef(fid,file_dim_names,file_dim_sizes,file_dim_unlim) ;---Define Variables print("Define variables on SCRIP file") filevardef(fid,"grid_dims","integer","grid_rank") filevardef(fid,"grid_center_lat","double","grid_size") filevardef(fid,"grid_center_lon","double","grid_size") filevardef(fid,"grid_imask","integer","grid_size") filevardef(fid,"grid_corner_lat","double",(/ "grid_size", "grid_corners" /) ) filevardef(fid,"grid_corner_lon","double",(/ "grid_size", "grid_corners" /) ) ;---Define the variables unit attribute print("Define variable attributes on SCRIP file") DummyAtt1 = 0 DummyAtt2 = 0 DummyAtt1@units = tochar("degrees") ; NetCDF4 doesn't support strings DummyAtt2@units = tochar("unitless") filevarattdef(fid,"grid_center_lat",DummyAtt1) filevarattdef(fid,"grid_center_lon",DummyAtt1) filevarattdef(fid,"grid_imask",DummyAtt2) filevarattdef(fid,"grid_corner_lat",DummyAtt1) filevarattdef(fid,"grid_corner_lon",DummyAtt1) delete(DummyAtt1) delete(DummyAtt2) ;---Prepare the file to store the values setfileoption(fid,"DefineMode",False) ;---Storing Grid Dims print("Start writing values to SCRIP file") fid->grid_dims = (/ nlon, nlat /) ; SCRIP is FORTRAN-based. ; (nlat,nlon) in NCL is equivalent to ; (nlon,nlat) in FORTRAN ;---Store Cell Center Lat/Lon print("Write lat/lon values to SCRIP file") fid->grid_center_lat = (/ndtooned(lat2d)/) fid->grid_center_lon = (/ndtooned(lon2d)/) ;---Store Cell Masks print("Write mask (if any) to SCRIP file") if (grid_size.ne.dimsizes(ndtooned(grid_mask))) then print("curvilinear_to_SCRIP: Mask array is not the appropriate size.") exit else fid->grid_imask=(/ tointeger(ndtooned(grid_mask)) /) end if print("Calculate lat/lon corners") if(DEBUG) then print("curvilinear_to_SCRIP: calculating grid corners...") end if lat_type = typeof(lat2d) lon_type = typeof(lon2d) if(.not.any(lat_type.eq.(/"float","double"/))) then lat_type = "float" end if if(.not.any(lon_type.eq.(/"float","double"/))) then lon_type = "float" end if grid_corner_lat = new( (/ grid_size, grid_corners /), lat_type) grid_corner_lon = new( (/ grid_size, grid_corners /), lon_type) if(any(fabs(lat2d).eq.90)) then if(DEBUG) then print("curvilinear_to_SCRIP: one or more lat values are at the") print(" poles, so calculating grid corners using") print(" calc_SCRIP_corners_boundaries...") end if calc_SCRIP_corners_boundaries(lat2d,lon2d,grid_corner_lat,grid_corner_lon,cts_opt) else if(DEBUG) then print("curvilinear_to_SCRIP: no lat values are at the poles, so") print(" calculating grid corners using") print(" calc_SCRIP_corners_noboundaries...") end if calc_SCRIP_corners_noboundaries(lat2d,lon2d,grid_corner_lat,grid_corner_lon,cts_opt) if(any(fabs(grid_corner_lat).gt.90)) then if(DEBUG) then print("curvilinear_to_SCRIP: calc_SCRIP_corners_noboundaries") print(" produced out-of-range latitude values.") print(" Trying calc_SCRIP_corners_boundaries...") end if calc_SCRIP_corners_boundaries(lat2d,lon2d,grid_corner_lat,grid_corner_lon,cts_opt) end if end if ;---Store the cell corners print("Write lat/lon corners to SCRIP file") if (isatt(grid_corner_lat,"_FillValue")) then delete(grid_corner_lat@_FillValue) end if if (isatt(grid_corner_lon,"_FillValue")) then delete(grid_corner_lon@_FillValue) end if fid->grid_corner_lat = (/ todouble(grid_corner_lat) /) fid->grid_corner_lon = (/ todouble(grid_corner_lon) /) print_elapsed_esmf_time(start_time,"curvilinear_to_SCRIP") end ; of curvilinear_to_SCRIP(...)