-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProcess_SMOSxL1C.cpp
More file actions
1318 lines (1175 loc) · 53.8 KB
/
Copy pathProcess_SMOSxL1C.cpp
File metadata and controls
1318 lines (1175 loc) · 53.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// "Process_SMOSxL1C": mex function to process L1C SMOS data with Octave/Matlab
//
// Copyright (c) 2016 Pablo Saavedra Garfias
//
// this file is part of 'Process_SMOSxL1C'
//
// 'Process_SMOSxL1C' is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 'Process_SMOSxL1C' is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with 'Process_SMOSxL1C'. If not, see <http://www.gnu.org/licenses/>.
// ---------------------------------------------------------------------------
// Program to read SMOS SCL1C DBL data files (either dual or full pol)
// version 1.0
// COMPILATION:
// (from linux console):
// >[MATLAB_BIN_PATH]/mex CFLAGS='$CFLAGS -std=gnu++11' readSCL1C_smos_DBL.cpp -lgsl -lgslcblas
// (from MATLAB workspace):
// > mex Process_SMOSxL1C.cpp -lgsl -lgslcblas
//
// USAGE 1:
// > [TSF, SSI] = Process_SMOSxL1C;
// then, a file browser will be open and select a DBL file to open.
// USAGE 2:
// > [TSF, SSI] = Process_SMOSxL1C('fname.DBL',GEO_LIMS,outdir);
// WHERE:
// 'fname.DBL': full path string of DBL file to work with,
// GEOLIMS: a 4 element vector as [LAT1, LON1, LAT2, LON2],
// with elements indicating the latitude and longitude of bottom left
// of box and latitude and longitude of upper rigth of box.
// outdir: (optional) string with directory where filename.mat is stored.
// USAGE 3:
// > [TSF, SSI] = Process_SMOSxL1C('fname.DBL',GEO_LIMS);
// same as usage #2, but an output .mat file will not be generated.
// OUTPUTS:
// TSF: Structure with all variables for [Temp_Swath_Full] dataset.
// SSI: Structure with all variables for [Snapshot_Information]
// dataset (this output variable is optional).
// * Additionally, TSF includes the Brightness Temperatue in HV-pol after
// performing the Faraday rotation from the XY polarization plane.
// * The TSF memeber variables TB_Fixed_IncAngle and Fixed_IncAngle,
// are reprecenting the Brightness Temperatures at homogeneously spaced
// fixed incident angles (normally from 0 to 65 deg with 1 deg step).
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <time.h>
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_blas.h>
#ifdef MATLAB_MEX_FILE
// NOTE: The following three lines-block [#define char16_t] to [#undef char16_t]
// is a work-around for a problem during compilation error with c++11:
// redeclaration of C++ built-in type ‘char16_t’, typedef CHAR16_T char16_t;
///#define char16_t LIBRARY_char16_t
#include <mex.h>
///#undef char16_t // end of work-around char16_t.
#include "mat.h"
#define MYNAN mxGetNaN()
#else
#include <mex.h>
#include "matio.h"
#define MYNAN NAN
#endif
// -- Definition of global parameters --
#define PI 4.0*atan(1.0)
#define FAK90DEG 90.0/(UINT16_MAX+1) // Faktor: 90/2^-16, hint. UINT16_MAX=2^16-1
#define FAK360DEG 360.0/(UINT16_MAX+1) // Faktor: 360 [deg] for Faraday and Geometric angle
#define FAKRAD 2*PI/(UINT16_MAX+1) // Faktor: 2PI [rad] for Faraday and Geometric angle
#define TH_SIZE 80 // [km] Spatial resolution size threshold (SMOS specification defult 55 km)
#define TH_ELON 2.2 // Spatial resolution elongation threshold (specification default 1.5)
#define TBXX_MIN 50 // Minimum threshold for brigthness temperature [K]
#define TBXX_MAX 500 // Maximum threshold for brigthness temperature [K]
#define TH_RFI_ST4 50 // Threshould for 4-Stoke parameter Radio Frequency Interference
#define CA_TBS1 5 // parameter A for equation
#define CB_TBS1 4 // parameter B for equation ST4-<ST4> = A + B
#define INCA_INI 0 // initial incidence angle [deg]
#define INCA_NUM 61 // number of incidence angles [deg] (e.g. 66 SMOS)
#define INCA_DEL 1 // steps for incidence angle [deg]
// -- end of definition for global parameters
using namespace std;
namespace smos{
// ****** CLASS DEFINITION *********************
class Be_Discrete {
private:
int k, Ninc, Init, Step;
double *TB_i, *TBave;
int *N_i;
double Delta;
public:
double *theta;
// default creator for Be_Discrete class:
Be_Discrete () : Ninc(INCA_NUM),
Init(INCA_INI), Step(INCA_DEL) {
initialize_it();
}
// creator for Be_Discrete class with parameters:
Be_Discrete (int I, int S, int N) : Ninc(N),
Init(I), Step(S) {
initialize_it();
}
// destroyer for Be_Discrete:
~Be_Discrete () {
delete [] N_i; delete [] TBave; delete [] TB_i; delete [] theta;
}
// return the average Brightness temperature within the inc_angle interval:
double *deliver_it() {
for(k=0;k<Ninc;++k) TBave[k]=N_i[k]!=0?TB_i[k]/N_i[k]:MYNAN;
return(TBave);
}
// shows the results of the discretization:
void show_it(){
for(k=0;k<Ninc;++k)
cout<<theta[k]<<" "<<TBave[k]<<";"<<endl;
}
// take a incidence angle and its TB to include in the discretization:
void consider_it(double beta,double VARin){
for(k=0; k<Ninc; ++k)
if(beta>=(theta[k]-Delta) && beta<(theta[k]+Delta) && VARin==VARin){
// NOTE here that VARin==VARin is FALSE only when VARin=NaN
TB_i[k] += VARin;
N_i[k]++;
}
return;
}
protected:
void initialize_it(){
Delta = double(Step)/2;
N_i = new int[Ninc];
TBave = new double[Ninc];
TB_i = new double[Ninc];
theta = new double[Ninc];
for(k=0;k<Ninc;++k){
TBave[k]=0; TB_i[k]=0; N_i[k]=0;
theta[k]= (double) k*Step+Init;
};
}
};
// ****** SUBROUTINES DECLARATION **************
int GetInputDBL_BoxLIM(char *&, char *&, float []);
void MxTrans_XY2HV(double, double [], double [], int, bool);
void interp1(double [], double [], int, double [], double [], int);
float getHDR_fields(const char [], const char []);
int CreateMAToutput(mxArray *, mxArray *, char [], char []);
#ifndef MATLAB_MEX_FILE
matvar_t *CloneSTRUCTforMATIO(mxArray *, const char *);
#endif
int TimeEEC2date(int [], char []);
void TBHV_at_FixTheta(double [], double [], int, double []);
void ShowUsage(const char*);
void ShowGNUPL();
bool SSI_FLAG=true;
} // end of smos namespace
// ::::::::::: MAIN PROGRAM STARTS ::::::::::::::::::
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
// Defining the output structure variables:
mxArray *SnapShotInfo, *TempSwathFull;
// Following mex variables for the pixel variables:
const char *field_name[] = {"File_Name","GEOBoxLimits","Start_End_Time",
"GridPoint_ID","GridPoint_Latitude","GridPoint_Longitude",
"GridPoint_Altitude","GridPoint_Mask","BT_Data_Counter",
"flags","BTvalue_re","BTvalue_im","PixelRadiometry_acc",
"Incidence_angle","Azimuth_angle","FaradayRotation_angle",
"GeometricRotation_angle","Snapshot_ID_Pixels",
"Footprint_Axis1","Footprint_Axis2","TBxy","TBhv",
"RA","idx_SnapshotID","TB_Fixed_IncAngle","Fixed_IncAngle"};
const unsigned N_field_name = sizeof(field_name)/sizeof(field_name[0]);
// Following mex variables for the snapshot variables:
const char *snapsh_name[] = {"Snapshot_Time","Snapshot_ID","Snapshot_OBET",
"XYZ_Position","XYZ_Velocity","Vector_So","Qnn","TEC",
"Geomag_F_D_I","Sun_RA_DEC_BT",
"Accuracy","Radiometric_Accuracy","XBand","SIAC_Error_flag"};
const unsigned N_snapsh_name = sizeof(snapsh_name)/sizeof(snapsh_name[0]);
char *filen, *OUTDIR;
ifstream fp;
unsigned int i,j, inn, Snapshot_Counter;
unsigned int Grid_Point_Counter;
double RAD_ACC_SC, PIX_FOOTP_SC;
unsigned STOKES_VEC = 4; // default: 4 for Stokes vector (optional: 2 for dual-pol)
// List of Grid Point Data (TempSwathFull variables):
unsigned int Grid_Point_ID;
float Grid_Point_Latitude, Grid_Point_Longitude, Grid_Point_Altitude;
unsigned char Grid_Point_Mask;
unsigned short int BT_Data_Counter;
unsigned short int Flags, Pixel_Radiometric_Accuracy;
float BT_Value_Real, BT_Value_Imag;
unsigned short int Incidence_Angle, Azimuth_Angle;
unsigned short int Faraday_Rotation_Angle, Geometric_Rotation_Angle;
unsigned int Snapshot_ID_of_Pixel;
unsigned short int Footprint_Axis1, Footprint_Axis2;
// Others:
bool INMYBOX; //bool smos::SSI_FLAG=true;
float BOXLIM[4] = {-90,-180,90,180}; // Default Latitude-Longitude Box
char smos_date0[]="yyyymmddThh:mi:ss", smos_date1[]="yyyymmddThh:mi:ss";
// ---------------------------
// Checking output parameters:
if(nlhs<1 || nlhs>2) smos::ShowUsage("Need at least one output variable, try again ;)");
if(nlhs==1) smos::SSI_FLAG=false; else smos::SSI_FLAG=true;
// Checking the input parameters:
if (nrhs>=0 && nrhs<4){
int FileLength;
switch(nrhs){
case 3:
// third input: Output file path
if (mxIsChar(prhs[2])){
FileLength = mxGetN(prhs[2])+1;
OUTDIR = (char *) mxCalloc(FileLength, sizeof(char));
mxGetString(prhs[2], OUTDIR, FileLength);
cout<<"% Output dir: "<<OUTDIR<<endl;
}
else mexErrMsgTxt("Third input needs to be a string PATH.");
case 2:
// Second input: Box to extract [lat_min lon_min lat_max lon_max]
if (mxIsNumeric(prhs[1]) && mxGetNumberOfElements(prhs[1])==4) { // Getting the Lat, Lon boundaries:
// Latitude bottom left
BOXLIM[0] = (float) *(mxGetPr(prhs[1]));
// Longitude bottom left
BOXLIM[1] = (float) *(mxGetPr(prhs[1])+1);
// Latitude upper rigth
BOXLIM[2] = (float) *(mxGetPr(prhs[1])+2);
// Longitude upper rigth
BOXLIM[3] = (float) *(mxGetPr(prhs[1])+3);
}
else mexErrMsgTxt("Second input must be 4x1 numeric, try again ;)");
case 1:
// first input: file name:
if(mxIsChar(prhs[0])){
FileLength = mxGetN(prhs[0])+1;
filen = (char *) mxCalloc(FileLength, sizeof(char));
mxGetString(prhs[0], filen, FileLength);
mexPrintf("DBL file to open: %s\n",filen);
}
else mexErrMsgTxt("First input needs to be a string FILENAME.");
break;
case 0:
//open file brower
if(smos::GetInputDBL_BoxLIM(filen,OUTDIR,BOXLIM)!=0) mexErrMsgTxt("Wrong input DBL file or (Lat,Lon) min-max limits!");
break;
default:
mexErrMsgTxt("Ups! something is wrong with the input variables!");
} // end switch
}
else smos::ShowUsage("Wrong number of inputs, try again ;)");
cout<<"% Position min: ("<<BOXLIM[0]<<","<<BOXLIM[1]<<")"<<endl;
cout<<"% Position max: ("<<BOXLIM[2]<<","<<BOXLIM[3]<<")"<<endl;
cout<<__cplusplus<<endl;
// checking the polarization type from DBL file name:
string WhatPol = filen;
int idxPol = WhatPol.find("1C_");
if(WhatPol.at(idxPol-5)=='_' && WhatPol.at(idxPol-1)=='D') STOKES_VEC = 2; // dual-pol
else STOKES_VEC = 4; // 4-Stokes-pol
// Checking whether file name has extention ".DBL" as SMOS data block binary file:
if(WhatPol.compare(WhatPol.size()-4,4,".DBL")!=0) mexWarnMsgTxt("Are you sure this is a SMOS 1C DBL binary file?");
// Opening the BDL file:
fp.open(filen,ios::binary | ios::in);
if (!fp.is_open()){
cout<<filen;
mexErrMsgTxt(" :DBL file cannot be opened, it might be corrupted!");
return;
}
// Reading the necessary HDR fields:
RAD_ACC_SC = (double) smos::getHDR_fields(filen,"Radiometric_Accuracy_Scale");
if(RAD_ACC_SC!=MYNAN) RAD_ACC_SC = RAD_ACC_SC/(UINT16_MAX+1);
PIX_FOOTP_SC = (double) smos::getHDR_fields(filen,"Pixel_Footprint_Scale");
if(PIX_FOOTP_SC!=MYNAN) PIX_FOOTP_SC = PIX_FOOTP_SC/(UINT16_MAX+1);
float CREATOR_VER = smos::getHDR_fields(filen,"Creator_Version");
if(CREATOR_VER!=MYNAN) CREATOR_VER = 620; // default value
// Starting to read the data from DBL:
fp.read((char *) &Snapshot_Counter,sizeof(Snapshot_Counter));
int Snapshot_Time[Snapshot_Counter][3]; //= new int*[Snapshot_Counter];
int Snapshot_ID[Snapshot_Counter]; //= new int[Snapshot_Counter];
long int Snapshot_OBET; // = new long int[Snapshot_Counter];
double X_Position; // = new double[Snapshot_Counter];
double Y_Position; // = new double[Snapshot_Counter];
double Z_Position; // = new double[Snapshot_Counter];
double X_Velocity; // = new double[Snapshot_Counter];
double Y_Velocity; // = new double[Snapshot_Counter];
double Z_Velocity; // = new double[Snapshot_Counter];
double TEC; // = new double[Snapshot_Counter];
double Geomag_F; // = new double[Snapshot_Counter];
double Geomag_D; // = new double[Snapshot_Counter];
double Geomag_I; // = new double[Snapshot_Counter];
float Sun_RA; // = new float[Snapshot_Counter];
float Sun_DEC; // = new float[Snapshot_Counter];
float Sun_BT; // = new float[Snapshot_Counter];
float Accuracy[Snapshot_Counter]; // = new float[Snapshot_Counter];
float Radiometric_Accuracy[Snapshot_Counter][2]; // = new float*[Snapshot_Counter];
unsigned char Software_Error_flag;
unsigned char Instrument_Error_flag;
unsigned char ADF_Error_flag;
unsigned char Calibration_Error_flag[Snapshot_Counter];
unsigned char Vector_Source, X_Band;
double Qnn[4];
// creating the mex variables for snapshot variables:
mxArray *mex_snaptime=NULL, *mex_snapid=NULL, *mex_snapobet=NULL, *mex_xyzpo=NULL, *mex_xyzve=NULL;
mxArray *mex_vecsou=NULL, *mex_Qnn=NULL, *mex_tec=NULL, *mex_geomag=NULL, *mex_sunv=NULL;
mxArray *mex_accu=NULL, *mex_radaccu=NULL, *mex_xband=NULL, *mex_errflag=NULL;
if (smos::SSI_FLAG){
mex_snaptime = mxCreateDoubleMatrix(Snapshot_Counter,3,mxREAL);
mex_snapid = mxCreateDoubleMatrix(Snapshot_Counter,1,mxREAL);
mex_snapobet = mxCreateDoubleMatrix(Snapshot_Counter,1,mxREAL);
mex_xyzpo = mxCreateDoubleMatrix(Snapshot_Counter,3,mxREAL);
mex_xyzve = mxCreateDoubleMatrix(Snapshot_Counter,3,mxREAL);
mex_vecsou = mxCreateDoubleMatrix(Snapshot_Counter,1,mxREAL);
mex_Qnn = mxCreateDoubleMatrix(4,Snapshot_Counter,mxREAL);
mex_tec = mxCreateDoubleMatrix(Snapshot_Counter,1,mxREAL);
mex_geomag = mxCreateDoubleMatrix(Snapshot_Counter,3,mxREAL);
mex_sunv = mxCreateDoubleMatrix(Snapshot_Counter,3,mxREAL);
mex_accu = mxCreateDoubleMatrix(Snapshot_Counter,1,mxREAL);
mex_radaccu = mxCreateDoubleMatrix(Snapshot_Counter,2,mxREAL);
mex_xband = mxCreateDoubleMatrix(Snapshot_Counter,1,mxREAL);
mex_errflag = mxCreateDoubleMatrix(Snapshot_Counter,4,mxREAL);
}
for(i=0;i<Snapshot_Counter;++i){
//Snapshot_Time[i] = new int[3]; // (0:days, 1:secods, 2:microseconds)
// Radiometric_Accuracy[i] = new float[2];
fp.read((char *) &Snapshot_Time[i][0], 3*sizeof(int));
fp.read((char *) &Snapshot_ID[i], sizeof(int));
fp.read((char *) &Snapshot_OBET, sizeof(long int));
fp.read((char *) &X_Position, sizeof(double));
fp.read((char *) &Y_Position, sizeof(double));
fp.read((char *) &Z_Position, sizeof(double));
fp.read((char *) &X_Velocity, sizeof(double));
fp.read((char *) &Y_Velocity, sizeof(double));
fp.read((char *) &Z_Velocity, sizeof(double));
fp.read((char *) &Vector_Source,1); // sizeof(Vector_Source));
fp.read((char *) &Qnn[0], 4*sizeof(double));
fp.read((char *) &TEC, sizeof(double));
fp.read((char *) &Geomag_F, sizeof(Geomag_F));
fp.read((char *) &Geomag_D, sizeof(Geomag_D));
fp.read((char *) &Geomag_I, sizeof(Geomag_I));
fp.read((char *) &Sun_RA, sizeof(Sun_RA));
fp.read((char *) &Sun_DEC, sizeof(Sun_DEC));
fp.read((char *) &Sun_BT, sizeof(Sun_BT));
fp.read((char *) &Accuracy[i], sizeof(Accuracy[0]));
fp.read((char *) &Radiometric_Accuracy[i][0], 2*sizeof(float));
fp.read((char *) &X_Band, 1); //sizeof(X_Band));
fp.read((char *) &Software_Error_flag, sizeof(Software_Error_flag));
fp.read((char *) &Instrument_Error_flag, sizeof(Instrument_Error_flag));
fp.read((char *) &ADF_Error_flag, sizeof(ADF_Error_flag));
fp.read((char *) &Calibration_Error_flag[i], sizeof(Calibration_Error_flag[0]));
// filling SnapshotInfo variables to MeX:
if(smos::SSI_FLAG){
int k0 = i;
int k1 = i+Snapshot_Counter;
int k2 = i+2*Snapshot_Counter;
int k3 = i+3*Snapshot_Counter;
// Snapshot Time:
*(mxGetPr(mex_snaptime)+k0) = Snapshot_Time[i][0]; //days
*(mxGetPr(mex_snaptime)+k1) = Snapshot_Time[i][1]; //seconds
*(mxGetPr(mex_snaptime)+k2) = Snapshot_Time[i][2]; //microseconds
// Snapshot ID:
*(mxGetPr(mex_snapid)+i) = Snapshot_ID[i];
*(mxGetPr(mex_snapobet)+i) = Snapshot_OBET;
// X_Y_Z_Position:
*(mxGetPr(mex_xyzpo)+k0) = X_Position;
*(mxGetPr(mex_xyzpo)+k1) = Y_Position;
*(mxGetPr(mex_xyzpo)+k2) = Z_Position;
// X_Y_Z_Velocity:
*(mxGetPr(mex_xyzve)+k0) = X_Velocity;
*(mxGetPr(mex_xyzve)+k1) = Y_Velocity;
*(mxGetPr(mex_xyzve)+k2) = Z_Velocity;
// Vector_Source, Qnn():
*(mxGetPr(mex_vecsou)+i) = Vector_Source;
memcpy(mxGetPr(mex_Qnn)+i*4, Qnn, 4*sizeof(double));
*(mxGetPr(mex_tec)+i) = TEC;
// Geomag_F_D_I:
*(mxGetPr(mex_geomag)+k0) = Geomag_F;
*(mxGetPr(mex_geomag)+k1) = Geomag_D;
*(mxGetPr(mex_geomag)+k2) = Geomag_I;
// Sun_Right Ascention, Declination, Brightness Temperature:
*(mxGetPr(mex_sunv)+k0) = Sun_RA;
*(mxGetPr(mex_sunv)+k1) = Sun_DEC;
*(mxGetPr(mex_sunv)+k2) = Sun_BT;
// Accuracies:
*(mxGetPr(mex_accu)+i) = Accuracy[i];
*(mxGetPr(mex_radaccu)+k0) = Radiometric_Accuracy[i][0];
*(mxGetPr(mex_radaccu)+k1) = Radiometric_Accuracy[i][1];
// XBand:
*(mxGetPr(mex_xband)+i) = X_Band;
// Flags:
*(mxGetPr(mex_errflag)+k0) = Software_Error_flag;
*(mxGetPr(mex_errflag)+k1) = Instrument_Error_flag;
*(mxGetPr(mex_errflag)+k2) = ADF_Error_flag;
*(mxGetPr(mex_errflag)+k3) = Calibration_Error_flag[i];
} // end if SSI_FLAG
} // end loop over snapshots in orbit.
// Here start the reading of Snapshots:
fp.read((char *) &Grid_Point_Counter, sizeof(Grid_Point_Counter));
// Creating mex variables for TempSwathFull output variable:
mxArray *mex_gridid = mxCreateDoubleMatrix(Grid_Point_Counter,1,mxREAL);
mxArray *mex_lat = mxCreateDoubleMatrix(Grid_Point_Counter,1,mxREAL);
mxArray *mex_lon = mxCreateDoubleMatrix(Grid_Point_Counter,1,mxREAL);
mxArray *mex_alt = mxCreateDoubleMatrix(Grid_Point_Counter,1,mxREAL);
mxArray *mex_mask = mxCreateDoubleMatrix(Grid_Point_Counter,1,mxREAL);
mxArray *mex_BT_Counter = mxCreateDoubleMatrix(Grid_Point_Counter,1,mxREAL);
mxArray *mex_flag = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_BT_re = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_BT_im = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_pixrad = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_incang = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_aziang = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_fayang = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_geoang = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_idpix = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_foot1 = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_foot2 = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_BTxy = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_BThv = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_RA = mxCreateCellMatrix(Grid_Point_Counter,1);
mxArray *mex_snapID= mxCreateCellMatrix(Grid_Point_Counter,1);
double *mat_TBdscr= new double[Grid_Point_Counter*STOKES_VEC*INCA_NUM];
mxArray *mex_INCAdscr = mxCreateDoubleMatrix(INCA_NUM, 1, mxREAL);
inn = 0;
for(i=0;i<Grid_Point_Counter;++i){
INMYBOX = false; // it is set up to FALSE for every iteration
mxArray *vec_flag=NULL, *vec_BTre=NULL, *vec_BTim=NULL, *vec_pixrad=NULL, *vec_incang=NULL;
mxArray *vec_aziang=NULL, *vec_fayang=NULL, *vec_geoang=NULL, *vec_idpix=NULL, *vec_foot1=NULL, *vec_foot2=NULL;
mxArray *vec_BTxy=NULL, *vec_BThv=NULL, *vec_RA=NULL, *vec_snapID=NULL;
fp.read((char *) &Grid_Point_ID, sizeof(Grid_Point_ID));
fp.read((char *) &Grid_Point_Latitude, sizeof(float));
fp.read((char *) &Grid_Point_Longitude, sizeof(float));
fp.read((char *) &Grid_Point_Altitude , sizeof(float));
fp.read((char *) &Grid_Point_Mask, sizeof(unsigned char));
fp.read((char *) &BT_Data_Counter, sizeof(unsigned short int));
if (Grid_Point_Latitude>=BOXLIM[0] && Grid_Point_Latitude<=BOXLIM[2] &&
Grid_Point_Longitude>=BOXLIM[1] && Grid_Point_Longitude<=BOXLIM[3]){
INMYBOX = true;
vec_flag = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_RA = mxCreateDoubleMatrix(BT_Data_Counter,STOKES_VEC,mxREAL);
vec_BTxy = mxCreateDoubleMatrix(BT_Data_Counter,STOKES_VEC,mxREAL);
vec_BThv = mxCreateDoubleMatrix(BT_Data_Counter,STOKES_VEC,mxREAL);
vec_BTre = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_BTim = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_pixrad = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_incang = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_aziang = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_fayang = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_geoang = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_idpix = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_foot1 = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_foot2 = mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
vec_snapID= mxCreateDoubleMatrix(BT_Data_Counter,1,mxREAL);
} // enf of INMYBOX 0 block
// initializing variables for interpolation:
double Txx_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double Tyy_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double Txy_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTxx_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTyy_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTxyRE_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTxyIM_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double T_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTxx_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTyy_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTxyRE_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
double BTxyIM_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
double RAxx_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double RAyy_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double RAxy_in[BT_Data_Counter]; // = new double[BT_Data_Counter];
double RAxx_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
double RAyy_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
double RAxy_out[BT_Data_Counter]; // = new double[BT_Data_Counter];
int Nxx=0, Nyy=0, Nxy=0;
double theta_inc[BT_Data_Counter];
double A_vec[4], X_vec[STOKES_VEC]; // system: M*X = A --> X unknown.
double alpha[BT_Data_Counter];; // rotational angle
double filter1_ax1ax2[BT_Data_Counter], filter2_ax1ax2[BT_Data_Counter], filter3_tbxtby;
for(j=0;j<BT_Data_Counter;++j){
fp.read((char *) &Flags, sizeof(unsigned short int));
fp.read((char *) &BT_Value_Real, sizeof(float));
fp.read((char *) &BT_Value_Imag, sizeof(float));
fp.read((char *) &Pixel_Radiometric_Accuracy, sizeof(unsigned short int));
fp.read((char *) &Incidence_Angle, sizeof(unsigned short int));
fp.read((char *) &Azimuth_Angle, sizeof(unsigned short int));
fp.read((char *) &Faraday_Rotation_Angle, sizeof(unsigned short int));
fp.read((char *) &Geometric_Rotation_Angle, sizeof(unsigned short int));
fp.read((char *) &Snapshot_ID_of_Pixel, sizeof(unsigned int));
fp.read((char *) &Footprint_Axis1, sizeof(unsigned short int));
fp.read((char *) &Footprint_Axis2, sizeof(unsigned short int));
// Quality Control Checking:
if (Flags&0x80) continue; // SUN_POINT flag 8th bit=1, pixel degraded
// criteria to filter based on footprint spatial resolution requirements,
filter1_ax1ax2[j] = ((double) Footprint_Axis1)/((double) Footprint_Axis2);
filter2_ax1ax2[j] = 2*PIX_FOOTP_SC*sqrt((double) (Footprint_Axis1*Footprint_Axis2));
// RFI filter based on 4th Stoks vector ST4>TH_RFI_ST4 indicates RFI
bool RFI_ST4 = TH_RFI_ST4<abs(-2*BT_Value_Imag)? true : false;
if (INMYBOX){
int idx_snap = distance(Snapshot_ID,
find(Snapshot_ID,Snapshot_ID+Snapshot_Counter,
Snapshot_ID_of_Pixel));
// getting the time for the snapshot [sec]:
if(inn==0 && j==0) smos::TimeEEC2date(Snapshot_Time[idx_snap],smos_date0);
T_out[j] = (double) (Snapshot_Time[idx_snap][1]+
Snapshot_Time[idx_snap][2]*1E-6);
*(mxGetPr(vec_flag)+j) = Flags;
*(mxGetPr(vec_snapID)+j) = idx_snap+1;
// Flags' 15 and 16 bit = FALSE -> XX-pol:
if (((~Flags)&0x01) && ((~Flags)&0x02) && !(Flags&0x40) && !RFI_ST4){
Txx_in[Nxx] = T_out[j];
BTxx_in[Nxx] = (double) BT_Value_Real;
RAxx_in[Nxx] = (double) (Pixel_Radiometric_Accuracy*RAD_ACC_SC);
Nxx++;
}
// Flags' 15 bit = FALSE and 16 bit = TRUE -> YY-pol:
if (( Flags&0x01) && ((~Flags)&0x02) && !(Flags&0x4000) && !RFI_ST4){
Tyy_in[Nyy] = T_out[j];
BTyy_in[Nyy] = (double) BT_Value_Real;
RAyy_in[Nyy] = (double) (Pixel_Radiometric_Accuracy*RAD_ACC_SC);
Nyy++;
}
// Flags' 15 bit = TRUE -> XY-pol:
if (Flags&0x02 && !RFI_ST4){
Txy_in[Nxy] = T_out[j];
BTxyRE_in[Nxy] = (double) BT_Value_Real;
BTxyIM_in[Nxy] = (double) BT_Value_Imag;
RAxy_in[Nxy] = (double) (Pixel_Radiometric_Accuracy*RAD_ACC_SC);
Nxy++;
}
theta_inc[j] = Incidence_Angle*FAK90DEG;
*(mxGetPr(vec_BTre)+j) = BT_Value_Real;
*(mxGetPr(vec_BTim)+j) = BT_Value_Imag;
*(mxGetPr(vec_pixrad)+j) = Pixel_Radiometric_Accuracy*RAD_ACC_SC;
*(mxGetPr(vec_incang)+j) = theta_inc[j];
*(mxGetPr(vec_aziang)+j) = Azimuth_Angle*FAK360DEG;
*(mxGetPr(vec_fayang)+j) = Faraday_Rotation_Angle*FAK360DEG;
*(mxGetPr(vec_geoang)+j) = Geometric_Rotation_Angle*FAK360DEG;
*(mxGetPr(vec_idpix)+j) = Snapshot_ID_of_Pixel;
*(mxGetPr(vec_foot1)+j) = Footprint_Axis1*PIX_FOOTP_SC;
*(mxGetPr(vec_foot2)+j) = Footprint_Axis2*PIX_FOOTP_SC;
alpha[j] = (Faraday_Rotation_Angle + Geometric_Rotation_Angle)*FAKRAD;
if(CREATOR_VER<344) alpha[j] += -2*Faraday_Rotation_Angle*FAKRAD; //old version
} // end of INMYBOX block
} // end of BT_Data_Counter block (j index)
if (INMYBOX){
// Here local variables for TB with incident angle discrete:
// with c++11 TB_dscr may be initialized as {{init,step,Ndcsr},{},...};
smos::Be_Discrete TB_dscr[STOKES_VEC]; //={{0,1,46},{0,1,46},{0,1,46},{0,1,46}};
int Ndscr = INCA_NUM; // default SMOS spec. from 0 to 65 deg (66 angles).
memcpy(mxGetPr(mex_INCAdscr), TB_dscr[0].theta,Ndscr*sizeof(double));
// **** Interpolation:
switch (STOKES_VEC){
case 4:
smos::interp1(Txy_in,BTxyRE_in,Nxy,T_out,BTxyRE_out,BT_Data_Counter);
smos::interp1(Txy_in,BTxyIM_in,Nxy,T_out,BTxyIM_out,BT_Data_Counter);
smos::interp1(Txy_in,RAxy_in,Nxy,T_out,RAxy_out,BT_Data_Counter);
case 2:
smos::interp1(Txx_in,BTxx_in,Nxx,T_out,BTxx_out,BT_Data_Counter);
smos::interp1(Tyy_in,BTyy_in,Nyy,T_out,BTyy_out,BT_Data_Counter);
smos::interp1(Txx_in,RAxx_in,Nxx,T_out,RAxx_out,BT_Data_Counter);
smos::interp1(Tyy_in,RAyy_in,Nyy,T_out,RAyy_out,BT_Data_Counter);
break;
default:
mexErrMsgTxt("Problem with declaration of STOKES VECTOR: need to be 4 or 2!");
}
// ****
// calculating the average of the halved 1st Stokes parameter <ST1>:
float aveST1;
int k;
for(j=0, k=0, aveST1=0;j<BT_Data_Counter;++j){
if(std::isnan(BTxx_out[j]) || std::isnan(BTyy_out[j])) continue;
aveST1 += 0.5*(BTxx_out[j]+BTyy_out[j]);
k++;
}
aveST1 /= k;
// **** Rotation matrix:
for(j=0;j<BT_Data_Counter;++j){
// *** Here all filetering criteria ST4_RFI, ST1 and FootPrint:
bool AX1AX2 = false, TBXTBY = false, RFI_ST1 = false;
if(filter1_ax1ax2[j]>TH_ELON || filter2_ax1ax2[j]>TH_SIZE) AX1AX2 = true;
filter3_tbxtby = sqrt(BTxx_out[j]*BTxx_out[j]+BTyy_out[j]*BTyy_out[j]);
if(filter3_tbxtby<TBXX_MIN || filter3_tbxtby>TBXX_MAX) TBXTBY=true;
if((0.5*(BTxx_out[j]+BTyy_out[j])-aveST1)>(CA_TBS1+CB_TBS1*RAxx_out[j])) RFI_ST1=true;
// ***
if(!TBXTBY && !AX1AX2 && !RFI_ST1){
A_vec[0] = BTxx_out[j]; A_vec[1] = BTyy_out[j];
A_vec[2] = 2*BTxyRE_out[j]; A_vec[3] = -2*BTxyIM_out[j];
smos::MxTrans_XY2HV(alpha[j], A_vec, X_vec, STOKES_VEC, false); // rotation
}
else {
for(unsigned k1=0; k1<STOKES_VEC; ++k1) X_vec[k1] = MYNAN;
}
for(unsigned k1=0; k1<STOKES_VEC; ++k1){
// Feeding the "Be_Discrete" class type variables:
TB_dscr[k1].consider_it(theta_inc[j], X_vec[k1]);
// filling the HV output to mex matrix:
*(mxGetPr(vec_BThv)+j+k1*BT_Data_Counter) = X_vec[k1];
}
// TB_dscr[1].consider_it(theta_inc[j], X_vec[1]);
// TB_dscr[2].consider_it(theta_inc[j], X_vec[2]);
// TB_dscr[3].consider_it(theta_inc[j], X_vec[3]);
// // filling the HV output to mex matrix:
// *(mxGetPr(vec_BThv)+j) = X_vec[0];
// *(mxGetPr(vec_BThv)+j+BT_Data_Counter) = X_vec[1];
// *(mxGetPr(vec_BThv)+j+2*BT_Data_Counter) = X_vec[2];
// *(mxGetPr(vec_BThv)+j+3*BT_Data_Counter) = X_vec[3];
// -----
// for Radiometric_Accuracy RA:
if(!TBXTBY && !AX1AX2 && !RFI_ST1){
A_vec[0] = fabs(RAxx_out[j]); A_vec[1] = fabs(RAyy_out[j]);
A_vec[2] = 2*fabs(RAxy_out[j]); A_vec[3] = 2*fabs(RAxy_out[j]);
smos::MxTrans_XY2HV(alpha[j], A_vec, X_vec, STOKES_VEC, true);
}
else {
for(unsigned k1=0; k1<STOKES_VEC; ++k1) X_vec[k1] = MYNAN;
}
// filling the Radiometric accuracy HV to mex matrix:
for(unsigned k1=0; k1<STOKES_VEC; ++k1)
*(mxGetPr(vec_RA)+j+k1*BT_Data_Counter) = X_vec[k1];
// *(mxGetPr(vec_RA)+j+BT_Data_Counter) = X_vec[1];
// *(mxGetPr(vec_RA)+j+2*BT_Data_Counter) = X_vec[2];
// *(mxGetPr(vec_RA)+j+3*BT_Data_Counter) = X_vec[3];
} // end over BT_Data_Counter (number of incident angles)
// Feeding matrix with the TBs discrete incidence angle:
// inn: snapshot index, Ndscr: # of angles, STOKES_VEC: dimensions
unsigned k0 = inn*Ndscr*STOKES_VEC;
for(unsigned k1 = 0; k1<STOKES_VEC; k0 = ++k1*Ndscr+inn*Ndscr*STOKES_VEC)
memcpy((void *) (mat_TBdscr+k0), TB_dscr[k1].deliver_it(),Ndscr*sizeof(double));
// Populating the mex vector variables:
memcpy(mxGetPr(vec_BTxy), BTxx_out, BT_Data_Counter*sizeof(double));
memcpy(mxGetPr(vec_BTxy)+BT_Data_Counter, BTyy_out, BT_Data_Counter*sizeof(double));
if(STOKES_VEC==4){
memcpy(mxGetPr(vec_BTxy)+2*BT_Data_Counter, BTxyRE_out,
BT_Data_Counter*sizeof(double));
memcpy(mxGetPr(vec_BTxy)+3*BT_Data_Counter, BTxyIM_out,
BT_Data_Counter*sizeof(double));
}
*(mxGetPr(mex_gridid)+inn) = Grid_Point_ID;
*(mxGetPr(mex_lat)+inn) = Grid_Point_Latitude;
*(mxGetPr(mex_lon)+inn) = Grid_Point_Longitude;
*(mxGetPr(mex_alt)+inn) = Grid_Point_Altitude;
*(mxGetPr(mex_mask)+inn) = Grid_Point_Mask;
*(mxGetPr(mex_BT_Counter)+inn) = BT_Data_Counter;
// Populating the mex cell variables
mxSetCell(mex_flag, inn, vec_flag);
mxSetCell(mex_BT_re, inn, vec_BTre);
mxSetCell(mex_BT_im, inn, vec_BTim);
mxSetCell(mex_pixrad, inn, vec_pixrad);
mxSetCell(mex_incang, inn, vec_incang);
mxSetCell(mex_aziang, inn, vec_aziang);
mxSetCell(mex_fayang, inn, vec_fayang);
mxSetCell(mex_geoang, inn, vec_geoang);
mxSetCell(mex_idpix, inn, vec_idpix);
mxSetCell(mex_foot1, inn, vec_foot1);
mxSetCell(mex_foot2, inn, vec_foot2);
mxSetCell(mex_BTxy, inn, vec_BTxy);
mxSetCell(mex_BThv, inn, vec_BThv);
mxSetCell(mex_RA , inn, vec_RA);
mxSetCell(mex_snapID, inn, vec_snapID);
inn++;
} // end of INMYBOX block_2
}
fp.close();
// --- Assigning output variables:
// Setting MATLAB workspace variable mexFile_Name with info for DBL file name:
mxArray *mexFile_Name = mxCreateString(WhatPol.substr(WhatPol.find_last_of("/")+1).c_str());
mxArray *mex_boxlim = mxCreateDoubleMatrix(2,2,mxREAL);
for(i=0;i<4;++i) *(mxGetPr(mex_boxlim)+i) = BOXLIM[i];
int idx_snap = distance(Snapshot_ID,
find(Snapshot_ID,Snapshot_ID+Snapshot_Counter,
Snapshot_ID_of_Pixel));
smos::TimeEEC2date(Snapshot_Time[idx_snap],smos_date1); // time for last point in BOXLIM
char *str[] = {smos_date0, smos_date1}; // {Start_Time, End_Time}
mxArray *mex_startdate = mxCreateCharMatrixFromStrings(2, (const char **)str);
// --- Setting the actual size of variables according to index: inn
mxSetM(mex_gridid,inn);
mxSetM(mex_lat,inn);
mxSetM(mex_lon,inn);
mxSetM(mex_alt,inn);
mxSetM(mex_mask,inn);
mxSetM(mex_BT_Counter,inn);
mxSetM(mex_flag,inn);
mxSetM(mex_BT_re,inn);
mxSetM(mex_BT_im,inn);
mxSetM(mex_pixrad,inn);
mxSetM(mex_incang,inn);
mxSetM(mex_aziang,inn);
mxSetM(mex_fayang,inn);
mxSetM(mex_geoang,inn);
mxSetM(mex_idpix,inn);
mxSetM(mex_foot1,inn);
mxSetM(mex_foot2,inn);
mxSetM(mex_BTxy,inn);
mxSetM(mex_BThv,inn);
mxSetM(mex_RA ,inn);
mxSetM(mex_snapID,inn);
// *NOTE: The following lines get the output as Array [INC_ANGLE,POLARIZATION,NUM_DATA]
// mwSize dims3d[3]={INCA_NUM,STOKES_VEC,(int) inn};
// mxArray *mex_TBdscr = mxCreateNumericArray(3,dims3d,mxDOUBLE_CLASS,mxREAL);
// memcpy(mxGetPr(mex_TBdscr), (const void *) mat_TBdscr,INCA_NUM*inn*STOKES_VEC*sizeof(double));
// *NOTE: The following lines get the output as Array [NUM_DATA,INC_ANGLE,POLARIZATION]
// this dimension arrange is more convinient for MATLAB Workspace data treatment.
mwSize dims3d[3] = {(mwSize) inn, (mwSize) INCA_NUM, (mwSize) STOKES_VEC};
mxArray *mex_TBdscr = mxCreateNumericArray(3,dims3d,mxDOUBLE_CLASS,mxREAL);
// Arranging mat_TBdscr array to dimensions {NUM_DATA, INC_ANGLE, POLARIZATION}
for(unsigned k1=0,idat=0;k1<STOKES_VEC;++k1)
for(unsigned k2=0;k2<INCA_NUM; ++k2)
for(unsigned k3=0;k3<inn;k3++, idat++)
*(mxGetPr(mex_TBdscr)+idat) = mat_TBdscr[k1*INCA_NUM+k2+k3*(STOKES_VEC*INCA_NUM)];
// deleting the temporal variable mat_TBdscr:
delete[] mat_TBdscr;
// ---
// Create the output structure:
mwSize dims[2]={1,1};
TempSwathFull = mxCreateStructArray(2, dims, N_field_name, field_name);
// Setting the variables into the Output Structure:
mxSetField(TempSwathFull,0,"File_Name", mexFile_Name );
mxSetField(TempSwathFull,0,"GEOBoxLimits", mex_boxlim);
mxSetField(TempSwathFull,0,"Start_End_Time", mex_startdate);
mxSetField(TempSwathFull,0,"GridPoint_ID", mex_gridid);
mxSetField(TempSwathFull,0,"GridPoint_Latitude",mex_lat);
mxSetField(TempSwathFull,0,"GridPoint_Longitude",mex_lon);
mxSetField(TempSwathFull,0,"GridPoint_Altitude",mex_alt);
mxSetField(TempSwathFull,0,"GridPoint_Mask", mex_mask);
mxSetField(TempSwathFull,0,"BT_Data_Counter",mex_BT_Counter);
mxSetField(TempSwathFull,0,"flags", mex_flag);
mxSetField(TempSwathFull,0,"BTvalue_re",mex_BT_re);
mxSetField(TempSwathFull,0,"BTvalue_im",mex_BT_im);
mxSetField(TempSwathFull,0,"PixelRadiometry_acc", mex_pixrad);
mxSetField(TempSwathFull,0,"Incidence_angle",mex_incang);
mxSetField(TempSwathFull,0,"Azimuth_angle",mex_aziang);
mxSetField(TempSwathFull,0,"FaradayRotation_angle",mex_fayang);
mxSetField(TempSwathFull,0,"GeometricRotation_angle",mex_geoang);
mxSetField(TempSwathFull,0,"Snapshot_ID_Pixels", mex_idpix);
mxSetField(TempSwathFull,0,"Footprint_Axis1",mex_foot1);
mxSetField(TempSwathFull,0,"Footprint_Axis2",mex_foot2);
mxSetField(TempSwathFull,0,"TBxy",mex_BTxy);
mxSetField(TempSwathFull,0,"TBhv",mex_BThv);
mxSetField(TempSwathFull,0,"RA",mex_RA);
mxSetField(TempSwathFull,0,"idx_SnapshotID",mex_snapID);
mxSetField(TempSwathFull,0,"TB_Fixed_IncAngle",mex_TBdscr);
mxSetField(TempSwathFull,0,"Fixed_IncAngle", mex_INCAdscr);
SnapShotInfo = mxCreateStructArray(2, dims, N_snapsh_name, snapsh_name);
if (smos::SSI_FLAG){
mxSetField(SnapShotInfo,0,"Snapshot_Time",mex_snaptime);
mxSetField(SnapShotInfo,0,"Snapshot_ID",mex_snapid);
mxSetField(SnapShotInfo,0,"Snapshot_OBET",mex_snapobet);
mxSetField(SnapShotInfo,0,"XYZ_Position",mex_xyzpo);
mxSetField(SnapShotInfo,0,"XYZ_Velocity",mex_xyzve);
mxSetField(SnapShotInfo,0,"Vector_So",mex_vecsou);
mxSetField(SnapShotInfo,0,"Qnn",mex_Qnn);
mxSetField(SnapShotInfo,0,"TEC",mex_tec);
mxSetField(SnapShotInfo,0,"Geomag_F_D_I",mex_geomag);
mxSetField(SnapShotInfo,0,"Sun_RA_DEC_BT",mex_sunv);
mxSetField(SnapShotInfo,0,"Accuracy",mex_accu);
mxSetField(SnapShotInfo,0,"Radiometric_Accuracy",mex_radaccu);
mxSetField(SnapShotInfo,0,"XBand",mex_xband);
mxSetField(SnapShotInfo,0,"SIAC_Error_flag",mex_errflag);
}
// ***********************************************************
// IF output directory included, creating MAT file:
if(nrhs==3 && inn!=0){
if(smos::CreateMAToutput(TempSwathFull,SnapShotInfo, OUTDIR, filen)==0)
mexErrMsgTxt("Output MAT file could not be created!.");
} // end block creating MAT file
// ***********************************************
// Returning Variables to MATLAB workspace
switch(nlhs){
case 2:
plhs[1] = SnapShotInfo;
case 1:
plhs[0] = TempSwathFull;
}
return;
}
// ******** END OF MAIN PROGRAM ***************
// --------------------------------------------
// ******** AUXILIARY ROUTINES ***************
// *******************************************
//
namespace smos{
// ----------------------------------------------------------
// Routine to:
// 1) open a GUI file browser to select a DBL input file
// to work with.
// 2) open a GUI input dialog to write the minimum and maximum
// Latitude-Longitude pairs to select from the DBL input file.
// ARGUMENTS:
// * filen: input DBL file name (output arg).
// * OUTDIR: directory where filen is located (output arg).
// * BOXLIM: 4-element vector (lat_min,lon_min,lat_max,lon_max) (output atg).
// RETURN: status=0 -> OK, status!=0 -> wrong procedure.
int GetInputDBL_BoxLIM(char *& filen, char *& OUTDIR, float BOXLIM[4]){
int strLength, status;
mxArray *INVAR, *OUTVAR[2];
smos::ShowGNUPL(); // displaying License, it is free!.
INVAR = mxCreateString("*.DBL");
status = mexCallMATLAB(2,OUTVAR,1,&INVAR,"uigetfile");
if (status!=0) mexErrMsgTxt("File selection not possible!");
// passing the input and output path
strLength = mxGetN(OUTVAR[0])+mxGetN(OUTVAR[1])+1;
if (strLength<4) mexErrMsgTxt("File selection empty or canceled!");
filen = (char *) mxCalloc(strLength, sizeof(char));
mxGetString(OUTVAR[1],filen,strLength);
// passing the file name
strLength = mxGetN(OUTVAR[0])+1;
mxGetString(OUTVAR[0],filen+mxGetN(OUTVAR[1]),strLength);
strLength = mxGetN(OUTVAR[1])+1;
OUTDIR = (char *) mxCalloc(strLength,sizeof(char));
mxGetString(OUTVAR[1], OUTDIR, strLength);
mexPrintf("DBL file chosen: %s\n",filen);
// Getting the Latitude and Longitude Box limits:
mxArray *OUTbox=mxCreateCellMatrix(1,4);
mxArray *prompt = mxCreateCellMatrix(1,4);
const mxArray *name = mxCreateString("Lat-Lon BOX Limits");
const mxArray *numlines = mxCreateDoubleMatrix(1,1,mxREAL);
mxArray *defaultanswer = mxCreateCellMatrix(1,4);
#ifdef MATLAB_MEX_FILE
const int Narg = 5;
const mxArray *option = mxCreateString("on");
const mxArray *INOPTS[Narg] = {prompt,name,numlines,defaultanswer,option};
#else
const int Narg = 4;
const mxArray *INOPTS[Narg] = {prompt,name,numlines,defaultanswer};
#endif
char temp_str[4];
*mxGetPr(numlines) = 1;
mxSetCell(prompt,0,mxCreateString("Latitude bottom left [-90:+90]"));
mxSetCell(prompt,1,mxCreateString("Longitude bottom left [-180:+180]"));
mxSetCell(prompt,2,mxCreateString("Latitude upper rigth [-90:+90]"));
mxSetCell(prompt,3,mxCreateString("Longitude upper rigth [-180:+180]"));
mxSetCell(defaultanswer,0,mxCreateString("-90"));
mxSetCell(defaultanswer,1,mxCreateString("-180"));
mxSetCell(defaultanswer,2,mxCreateString("+90"));
mxSetCell(defaultanswer,3,mxCreateString("+180"));
status = mexCallMATLAB(1,&OUTbox,(int) Narg,(mxArray **) INOPTS,"inputdlg");
if (status!=0) mexErrMsgTxt("(Lat,Lon) Limits selection wrong!");
if (mxIsEmpty(OUTbox)) mexErrMsgTxt("(Lat,Lon) Limits selection empty!");
for(int i=0;i<4;++i){
strLength = mxGetN(mxGetCell(OUTbox,i))+1;
mxGetString(mxGetCell(OUTbox,i),temp_str,strLength);
BOXLIM[i]=atof(temp_str);
}
// #else
// cout<<"Please introduce Lat-Lon BOX Limits"<<endl;
// cout<<"Latitude bottom left [-90:+90] : "; cin>>BOXLIM[0];
// cout<<"Longitude bottom left [-180:+180]: "; cin>>BOXLIM[1];
// cout<<"Latitude upper rigth [-90:+90] : "; cin>>BOXLIM[2];
// cout<<"Longitude upper rigth [-180:+180]: "; cin>>BOXLIM[3];
// #endif
if(BOXLIM[0]>BOXLIM[2] || BOXLIM[1]>BOXLIM[3] ||
(BOXLIM[0]<-90 || BOXLIM[0]>90) ||
(BOXLIM[2]<-90 || BOXLIM[2]>90) ||
(BOXLIM[1]<-180 || BOXLIM[1]>180) ||
(BOXLIM[3]<-180 || BOXLIM[3]>180)){
mexPrintf("Wrong set of min-max parameters (%5.1f,%5.1f) - (%5.1f,%5.1f)\n",
BOXLIM[0],BOXLIM[1],BOXLIM[2],BOXLIM[3]);
return(1);}
return(0);
}
// --------------------------------------------------------------
// Linear Interpolation subroutine:
// Values at (Xo,Yo) are interpolated based on the (Xi,Yi) pairs.
// Ni: number of input data for (Xi[j],Yi[j]), with j=0,...,Ni-1
// No: number of output data for (Xo[j],Yo[j]), with j=0,...,No-1
// NOTE 1: In case Xo[j] is out of the Xi lower/upper limits,
// then Yo[j] is assigned with a non-interpolated value of MYNAN;
// NOTE 2: In case Ni is less than 6 points, Yo is returned MYNAN
// (no extrapolation allowed).
void interp1(double Xi[], double Yi[], int Ni, double Xo[], double Yo[], int No){
int i,k;
// Interpolation attempt only when input has more than 6 points!
if(Ni<7) for(i=0,k=0;i<No;++i) Yo[i] = Xi[k]==Xo[i] ? Yi[k++] : MYNAN;
else{
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_interp *inter = gsl_interp_alloc (gsl_interp_linear, Ni); //cspline, Ni);
gsl_interp_init(inter, Xi, Yi, Ni);
for(i=0;i<No;++i){
if(Xo[i]>=Xi[0] && Xo[i]<=Xi[Ni-1])
Yo[i] = gsl_interp_eval(inter,Xi,Yi, Xo[i], acc);
else Yo[i]=MYNAN;
}
gsl_interp_free(inter);
gsl_interp_accel_free(acc);
}
return;
}
// Matrix solver for the system M*X = B, with X the unknown,
// and M a square matrix of order N. X and B Nx1 arrays.
// The solver uses LU decompositions and it is applied directly
// to solve in case of the TBxy. For case of Radiometri Accuracies (RA)