Skip to content

Commit

Permalink
Merge pull request #171 from JCSDA/feature/btj_extract_ACCoeff_NLTE_f…
Browse files Browse the repository at this point in the history
…rom_BIN_to_NC

Enhance SpcCoeff_BIN2NC to Support Generation of Associated ACCoeff and NLTECoeff NetCDF Files during Binary to NetCDF Conversion
  • Loading branch information
BenjaminTJohnson authored Aug 30, 2024
2 parents 9fcec64 + 0eab656 commit 2b17ae1
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 441 deletions.
34 changes: 22 additions & 12 deletions src/Coefficients/SpcCoeff/SpcCoeff_BIN2NC/SpcCoeff_BIN2NC.f90
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,37 @@ PROGRAM SpcCoeff_BIN2NC
INTEGER :: err_stat
CHARACTER(256) :: msg
CHARACTER(256) :: BIN_filename
CHARACTER(256) :: NC_filename
CHARACTER(256) :: answer
CHARACTER(256) :: NC_filename, tmp_filename
CHARACTER(256) :: version_str
INTEGER :: n_args

! Program header
CALL CRTM_Version(version_str)
CALL Program_Message( PROGRAM_NAME, &
'Program to convert a CRTM SpcCoeff data file from Binary to netCDF format.', &
'CRTM Version: '//TRIM(version_str) )

! Get the filenames
IF ( Big_Endian() ) THEN
WRITE(*,FMT='(/5x,"Enter the INPUT Binary [Big Endian] SpcCoeff filename : ")', ADVANCE='NO')
ELSE
WRITE(*,FMT='(/5x,"Enter the INPUT Binary [Little Endian] SpcCoeff filename : ")', ADVANCE='NO')
! Get the filename ! Check for command line argument
n_args = COMMAND_ARGUMENT_COUNT()
IF ( n_args > 0 ) THEN
CALL GET_COMMAND_ARGUMENT(1, BIN_filename)
tmp_filename = BIN_filename(1:LEN_TRIM(BIN_filename) - 4)//".nc"
NC_filename = TRIM(ADJUSTL(tmp_filename))
PRINT *, "NC_filename:", NC_filename
ELSE
! Get the filenames
IF ( Big_Endian() ) THEN
WRITE(*,FMT='(/5x,"Enter the INPUT Binary [Big Endian] SpcCoeff filename : ")', ADVANCE='NO')
ELSE
WRITE(*,FMT='(/5x,"Enter the INPUT Binary [Little Endian] SpcCoeff filename : ")', ADVANCE='NO')
END IF
READ(*,'(a)') BIN_filename
BIN_filename = ADJUSTL(BIN_filename)
WRITE(*,FMT='(/5x,"Enter the OUTPUT netCDF SpcCoeff filename: ")', ADVANCE='NO')
READ(*,'(a)') NC_filename
NC_filename = ADJUSTL(NC_filename)
END IF
READ(*,'(a)') BIN_filename
BIN_filename = ADJUSTL(BIN_filename)
WRITE(*,FMT='(/5x,"Enter the OUTPUT netCDF SpcCoeff filename: ")', ADVANCE='NO')
READ(*,'(a)') NC_filename
NC_filename = ADJUSTL(NC_filename)


! ...Sanity check that they're not the same
IF ( bin_filename == nc_filename ) THEN
Expand Down
4 changes: 2 additions & 2 deletions src/Coefficients/SpcCoeff/SpcCoeff_Binary_IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ FUNCTION SpcCoeff_Binary_WriteFile( &
CALL Write_Cleanup(); RETURN
END IF
! ...Write the channel data
IF(SpcCoeff%Version > 2) THEN
IF(SpcCoeff%Version > 3) THEN
WRITE( fid, IOSTAT=io_stat ) &
SpcCoeff%Sensor_Channel , &
SpcCoeff%Polarization , &
Expand All @@ -707,7 +707,7 @@ FUNCTION SpcCoeff_Binary_WriteFile( &
SpcCoeff%Band_C2 , &
SpcCoeff%Cosmic_Background_Radiance, &
SpcCoeff%Solar_Irradiance
ELSE IF(SpcCoeff%Version < 3) THEN
ELSE IF(SpcCoeff%Version < 4) THEN
WRITE( fid, IOSTAT=io_stat ) &
SpcCoeff%Sensor_Channel , &
SpcCoeff%Polarization , &
Expand Down
59 changes: 49 additions & 10 deletions src/Coefficients/SpcCoeff/SpcCoeff_IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ MODULE SpcCoeff_IO
USE SpcCoeff_netCDF_IO , ONLY: SpcCoeff_netCDF_InquireFile, &
SpcCoeff_netCDF_ReadFile , &
SpcCoeff_netCDF_WriteFile
USE ACCoeff_netCDF_IO , ONLY: ACCoeff_netCDF_ReadFile
USE NLTECoeff_netCDF_IO, ONLY: NLTECoeff_netCDF_ReadFile
USE ACCoeff_Define , ONLY: ACCoeff_Associated
USE NLTECoeff_Define , ONLY: NLTECoeff_Associated
USE ACCoeff_netCDF_IO , ONLY: ACCoeff_netCDF_ReadFile, ACCoeff_netCDF_WriteFile
USE NLTECoeff_netCDF_IO, ONLY: NLTECoeff_netCDF_ReadFile, NLTECoeff_netCDF_WriteFile

! Disable implicit typing
IMPLICIT NONE

Expand Down Expand Up @@ -820,26 +823,62 @@ FUNCTION SpcCoeff_Binary_to_netCDF( &
END IF
END IF

! Read the substructure netCDF filenames

! Read the substructure netCDF filenames
! ...ACCoeff
sub_filename = TRIM(spccoeff%Sensor_Id)//'.ACCoeff.nc'
IF ( File_Exists(sub_filename) ) THEN
err_stat = ACCoeff_netCDF_ReadFile( sub_filename, spccoeff%AC, Quiet = Quiet )
IF ( err_stat /= SUCCESS ) THEN
msg = 'Error reading netCDF file '//TRIM(sub_filename)
msg = 'Error reading existing ACCoeff netCDF file '//TRIM(sub_filename)
CALL Display_Message( ROUTINE_NAME, msg, err_stat )
RETURN
END IF
ELSE
IF ( ACCoeff_Associated(spccoeff%AC) ) THEN
!** there's no external file, but there's an ACCoeff structure
!** yoink the spccoeff%AC into sub_filename
err_stat = ACCoeff_netCDF_WriteFile( &
sub_filename , &
spccoeff%AC , &
Quiet = Quiet , &
Title = "Earth, Platform, Space antenna efficiencies for "//trim(adjustl(spccoeff%sensor_id))//" for each fov, channel.", &
History = "Extracted from "//trim(adjustl(BIN_Filename))//" using "//trim(adjustl(ROUTINE_NAME))//" [email protected]" , &
Comment = "" )
IF ( err_stat /= SUCCESS ) THEN
msg = 'Error creating ACCoeff netCDF file '//TRIM(sub_filename)
CALL Display_Message( ROUTINE_NAME, msg, err_stat )
RETURN
END IF
END IF
END IF

! ...NLTECoeff
sub_filename = TRIM(spccoeff%Sensor_Id)//'.NLTECoeff.nc'
IF ( File_Exists(sub_filename) ) THEN
err_stat = NLTECoeff_netCDF_ReadFile( sub_filename, spccoeff%NC, Quiet = Quiet )
IF ( err_stat /= SUCCESS ) THEN
msg = 'Error reading netCDF file '//TRIM(sub_filename)
CALL Display_Message( ROUTINE_NAME, msg, err_stat )
RETURN
END IF
err_stat = NLTECoeff_netCDF_ReadFile( sub_filename, spccoeff%NC, Quiet = Quiet )
IF ( err_stat /= SUCCESS ) THEN
msg = 'Error reading netCDF file '//TRIM(sub_filename)
CALL Display_Message( ROUTINE_NAME, msg, err_stat )
RETURN
END IF
ELSE
IF ( NLTECoeff_Associated(spccoeff%NC) ) THEN
!** there's no external file, but there's an NLTECoeff structure
!** yoink the spccoeff%NC into sub_filename
err_stat = NLTECoeff_netCDF_WriteFile( &
sub_filename , &
spccoeff%NC , &
Quiet = Quiet , &
Title = "NLTE Coefficients for "//TRIM(ADJUSTL(spccoeff%sensor_id))//"." , &
History = "Extracted from "//TRIM(ADJUSTL(BIN_Filename))//" using "//TRIM(ADJUSTL(ROUTINE_NAME))//" [email protected]" , &
Comment = "" )
IF ( err_stat /= SUCCESS ) THEN
msg = 'Error creating NLTECoeff netCDF file '//TRIM(sub_filename)
CALL Display_Message( ROUTINE_NAME, msg, err_stat )
RETURN
END IF
END IF
END IF


Expand Down
24 changes: 17 additions & 7 deletions src/Coefficients/SpcCoeff/SpcCoeff_Inspect/SpcCoeff_Inspect.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
!
! SpcCoeff_Inspect
!
! Program to inspect the contents of a CRTM Binary format SpcCoeff file.
! Program to inspect the contents of a CRTM SpcCoeff file (binary or netCDF)
!
!
! CREATION HISTORY:
Expand All @@ -19,7 +19,7 @@ PROGRAM SpcCoeff_Inspect
USE Message_Handler , ONLY: SUCCESS, FAILURE, Program_Message, Display_Message
USE SpcCoeff_Define , ONLY: SpcCoeff_type, SpcCoeff_Destroy, &
Inspect => SpcCoeff_Inspect
USE SpcCoeff_Binary_IO, ONLY: SpcCoeff_Binary_ReadFile
USE SpcCoeff_IO, ONLY: SpcCoeff_ReadFile
! Disable implicit typing
IMPLICIT NONE

Expand All @@ -36,6 +36,7 @@ PROGRAM SpcCoeff_Inspect
CHARACTER(256) :: filename, msg
INTEGER :: n_args
TYPE(SpcCoeff_type) :: sc
LOGICAL :: is_nc, is_bin

! Generate a string containing the SpcCoeff release for info
WRITE(msg,'(i10)') sc%Release
Expand All @@ -44,7 +45,7 @@ PROGRAM SpcCoeff_Inspect
! Output program header
CALL Program_Message( PROGRAM_NAME, &
'Program to display the contents of a CRTM '//&
'Binary format R'//TRIM(ADJUSTL(msg))//' SpcCoeff '//&
'Binary/netCDF format R'//TRIM(ADJUSTL(msg))//' SpcCoeff '//&
'file to stdout.', &
'$Revision$' )

Expand All @@ -53,7 +54,7 @@ PROGRAM SpcCoeff_Inspect
IF ( n_args > 0 ) THEN
CALL GET_COMMAND_ARGUMENT(1, filename)
ELSE
WRITE( *,FMT='(/5x,"Enter the Binary SpcCoeff filename: ")',ADVANCE='NO' )
WRITE( *,FMT='(/5x,"Enter the SpcCoeff filename: ")',ADVANCE='NO' )
READ( *,'(a)' ) filename
END IF
filename = ADJUSTL(filename)
Expand All @@ -62,10 +63,19 @@ PROGRAM SpcCoeff_Inspect
CALL Display_Message( PROGRAM_NAME, msg, FAILURE ); STOP
END IF

! Read the binary data file
err_stat = SpcCoeff_Binary_ReadFile( filename, sc )
! Check if filename ends in ".nc"
is_nc = (INDEX(TRIM(filename), '.nc') == LEN_TRIM(filename) - 2)

! Check if filename ends in ".bin"
is_bin = (INDEX(TRIM(filename), '.bin') == LEN_TRIM(filename) - 3)

err_stat = FAILURE
! Read the data file
If (is_bin) err_stat = SpcCoeff_ReadFile( filename, sc )
IF (is_nc) err_stat = SpcCoeff_ReadFile( filename, sc, netCDF=.TRUE. )

IF ( err_stat /= SUCCESS ) THEN
msg = 'Error reading Binary SpcCoeff file '//TRIM(filename)
msg = 'Error reading SpcCoeff file '//TRIM(filename)
CALL Display_Message( PROGRAM_NAME, msg, FAILURE ); STOP
END IF

Expand Down
39 changes: 24 additions & 15 deletions src/Coefficients/SpcCoeff/SpcCoeff_NC2BIN/SpcCoeff_NC2BIN.f90
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,37 @@ PROGRAM SpcCoeff_NC2BIN
INTEGER :: err_stat
CHARACTER(256) :: msg, version_str
CHARACTER(256) :: nc_filename
CHARACTER(256) :: bin_filename
CHARACTER(256) :: answer
INTEGER :: version
CHARACTER(256) :: bin_filename, tmp_filename
INTEGER :: n_args

! Program header
CALL CRTM_Version (version_str)
CALL Program_Message( PROGRAM_NAME, &
'Program to convert a CRTM SpcCoeff data file '//&
'from netCDF to Binary format.', &
'CRTM Version:'//trim(version_str) )

! Get the filenames
WRITE(*,FMT='(/5x,"Enter the INPUT netCDF SpcCoeff filename : ")', ADVANCE='NO')
READ(*,'(a)') nc_filename
nc_filename = ADJUSTL(nc_filename)
WRITE(*,FMT='(/5x,"Enter the OUTPUT Binary SpcCoeff filename: ")', ADVANCE='NO')
READ(*,'(a)') bin_filename
bin_filename = ADJUSTL(bin_filename)
! ...Sanity check that they're not the same
IF ( bin_filename == nc_filename ) THEN
msg = 'SpcCoeff netCDF and Binary filenames are the same!'
CALL Display_Message( PROGRAM_NAME, msg, FAILURE ); STOP

! Get the filename
n_args = COMMAND_ARGUMENT_COUNT()
IF ( n_args > 0 ) THEN
CALL GET_COMMAND_ARGUMENT(1, nc_filename)
!** automatically generate the binary filename based on the command line netcdf filename
tmp_filename = nc_filename(1:LEN_TRIM(nc_filename) - 3)//".bin"
bin_filename = TRIM(ADJUSTL(tmp_filename))
PRINT *, "Output filename:", bin_filename
ELSE
! Get the filenames
WRITE(*,FMT='(/5x,"Enter the INPUT netCDF SpcCoeff filename : ")', ADVANCE='NO')
READ(*,'(a)') nc_filename
nc_filename = ADJUSTL(nc_filename)
WRITE(*,FMT='(/5x,"Enter the OUTPUT Binary SpcCoeff filename: ")', ADVANCE='NO')
READ(*,'(a)') bin_filename
bin_filename = ADJUSTL(bin_filename)
! ...Sanity check that they're not the same
IF ( bin_filename == nc_filename ) THEN
msg = 'SpcCoeff netCDF and Binary filenames are the same!'
CALL Display_Message( PROGRAM_NAME, msg, FAILURE ); STOP
END IF
END IF

! Perform the conversion (no change to version)
Expand Down
45 changes: 25 additions & 20 deletions src/Coefficients/SpcCoeff/SpcCoeff_netCDF_IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ FUNCTION SpcCoeff_netCDF_WriteFile( &
CALL Write_Cleanup(); RETURN
END IF
! ...Polarization angle variable
IF( SpcCoeff%Version > 2 ) THEN
IF( SpcCoeff%Version > 3 ) THEN
NF90_Status = NF90_INQ_VARID( FileId,POLANGLE_VARNAME,VarId )
IF ( NF90_Status /= NF90_NOERR ) THEN
msg = 'Error inquiring '//TRIM(Filename)//' for '//POLANGLE_VARNAME//&
Expand Down Expand Up @@ -983,7 +983,7 @@ FUNCTION SpcCoeff_netCDF_ReadFile( &
CALL Read_Cleanup(); RETURN
END IF
! ...Polarization angle variable
IF ( SpcCoeff%Version > 2 ) THEN
IF ( SpcCoeff%Version > 3 ) THEN
nf90_status = NF90_INQ_VARID( fileid,POLANGLE_VARNAME,varid )
IF ( nf90_status /= NF90_NOERR ) THEN
msg = 'Error inquiring '//TRIM(Filename)//' for '//POLANGLE_VARNAME//&
Expand Down Expand Up @@ -1591,24 +1591,29 @@ FUNCTION CreateFile( &
msg = 'Error writing '//POLARIZATION_VARNAME//' variable attributes to '//TRIM(Filename)
CALL Create_Cleanup(); RETURN
END IF
! ...Polarization angle variable
nf90_status = NF90_DEF_VAR( FileID, &
POLANGLE_VARNAME, &
POLANGLE_TYPE, &
dimIDs=(/n_channels_dimid/), &
varID=variD )
IF ( nf90_status /= NF90_NOERR ) THEN
msg = 'Error defining '//POLANGLE_VARNAME//' variable in '//&
TRIM(Filename)//' - '//TRIM(NF90_STRERROR( nf90_status ))
CALL Create_Cleanup(); RETURN
END IF
put_status(1) = NF90_PUT_ATT( FileID,varid,LONGNAME_ATTNAME ,POLANGLE_LONGNAME )
put_status(2) = NF90_PUT_ATT( FileID,varid,DESCRIPTION_ATTNAME,POLANGLE_DESCRIPTION )
put_status(3) = NF90_PUT_ATT( FileID,varid,UNITS_ATTNAME ,POLANGLE_UNITS )
put_status(4) = NF90_PUT_ATT( FileID,varid,FILLVALUE_ATTNAME ,POLANGLE_FILLVALUE )
IF ( ANY(put_status /= NF90_NOERR) ) THEN
msg = 'Error writing '//POLANGLE_VARNAME//' variable attributes to '//TRIM(Filename)
CALL Create_Cleanup(); RETURN

IF ( PRESENT(VERSION) ) THEN
IF (VERSION > 3) THEN
! ...Polarization angle variable
nf90_status = NF90_DEF_VAR( FileID, &
POLANGLE_VARNAME, &
POLANGLE_TYPE, &
dimIDs=(/n_channels_dimid/), &
varID=variD )
IF ( nf90_status /= NF90_NOERR ) THEN
msg = 'Error defining '//POLANGLE_VARNAME//' variable in '//&
TRIM(Filename)//' - '//TRIM(NF90_STRERROR( nf90_status ))
CALL Create_Cleanup(); RETURN
END IF
put_status(1) = NF90_PUT_ATT( FileID,varid,LONGNAME_ATTNAME ,POLANGLE_LONGNAME )
put_status(2) = NF90_PUT_ATT( FileID,varid,DESCRIPTION_ATTNAME,POLANGLE_DESCRIPTION )
put_status(3) = NF90_PUT_ATT( FileID,varid,UNITS_ATTNAME ,POLANGLE_UNITS )
put_status(4) = NF90_PUT_ATT( FileID,varid,FILLVALUE_ATTNAME ,POLANGLE_FILLVALUE )
IF ( ANY(put_status /= NF90_NOERR) ) THEN
msg = 'Error writing '//POLANGLE_VARNAME//' variable attributes to '//TRIM(Filename)
CALL Create_Cleanup(); RETURN
END IF
END IF
END IF
! ...Channel_Flag variable
nf90_status = NF90_DEF_VAR( FileID, &
Expand Down
Loading

0 comments on commit 2b17ae1

Please sign in to comment.